forked from Veil-Framework/Veil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompleter.py
633 lines (489 loc) · 20 KB
/
completer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
"""
Contains any classes used for tab completion.
Reference - http://stackoverflow.com/questions/5637124/tab-completion-in-pythons-raw-input
"""
# Import Modules
import readline
import subprocess
import re
import os
import sys
# try to find and import the settings.py config file
if os.path.exists("/etc/veil/settings.py"):
try:
sys.path.append("/etc/veil/")
import settings
except:
print("Error importing Veil Settings!")
sys.exit(1)
class none(object):
def complete(self, args):
return [None]
class MainMenuCompleter(object):
"""
Class used for tab completion of the main Controller menu
Takes a list of available commands, and loaded payload modules.
"""
def __init__(self, cmds, payloads):
self.commands = [cmd for cmd, desc in cmds.items()]
self.payloads = payloads
def complete_use(self, args):
"""Complete payload/module"""
res = []
payloads = []
for name, payload in self.payloads.items():
payloads.append(name)
# return all payloads if we just have "use"
if len(args[0].split("/")) == 1:
res = [m for m in payloads if m.startswith(args[0])] + [None]
else:
# get the language
lang = args[0].split("/")[0]
# get the rest of the paths
rest = "/".join(args[0].split("/")[1:])
payloads = []
for name, payload in self.payloads.items():
parts = name.split("/")
# iterate down the split parts so we can handle the nested payload structure
for x in range(0, len(parts)):
# if the first part of the iterated payload matches the language, append it
if parts[x] == lang:
payloads.append("/".join(parts[x + 1:]))
res = [lang + '/' + m + ' ' for m in payloads if m.startswith(rest)] + [None]
return res
def complete_info(self, args):
"""Complete payload/module"""
res = []
payloads = []
for (name, payload) in self.payloads:
payloads.append(name)
# return all payloads if we just have "use"
if len(args[0].split("/")) == 1:
res = [ m for m in payloads if m.startswith(args[0])] + [None]
else:
# get the language
lang = args[0].split("/")[0]
# get the rest of the paths
rest = "/".join(args[0].split("/")[1:])
payloads = []
for (name, payload) in self.payloads:
parts = name.split("/")
# iterate down the split parts so we can handle the nested payload structure
for x in xrange(len(parts)):
# if the first part of the iterated payload matches the language, append it
if parts[x] == lang:
payloads.append("/".join(parts[ x+ 1:]))
res = [lang + '/' + m + ' ' for m in payloads if m.startswith(rest)] + [None]
return res
def complete(self, text, state):
"Generic readline completion entry point."
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in self.commands][state]
# account for last argument ending in a space
RE_SPACE = re.compile('.*\s+$', re.M)
if RE_SPACE.match(buffer):
line.append('')
# resolve command to the implementation functions (above)
cmd = line[0].strip()
if cmd in self.commands:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in self.commands if c.startswith(cmd)] + [None]
return results[state]
class PayloadCompleter(object):
def __init__(self, cmds, payload):
self.commands = [cmd for cmd, desc in cmds.items()]
self.payload = payload
def _listdir(self, root):
"""
Complete a directory path.
"""
res = []
for name in os.listdir(root):
path = os.path.join(root, name)
if os.path.isdir(path):
name += os.sep
res.append(name)
return res
def _complete_path(self, path=None):
"""
Complete a file path.
"""
if not path:
return self._listdir('.')
dirname, rest = os.path.split(path)
tmp = dirname if dirname else '.'
res = [os.path.join(dirname, p)
for p in self._listdir(tmp) if p.startswith(rest)]
# more than one match, or single match which does not exist (typo)
if len(res) > 1 or not os.path.exists(path):
return res
# resolved to a single directory, so return list of files below it
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
# exact file match terminates this completion
return [path + ' ']
def complete_path(self, args):
"""
Entry point for path completion.
"""
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete_set(self, args):
"""
Complete all options for the 'set' command.
"""
res = []
if hasattr(self.payload, 'required_options'):
options = [k for k in sorted(self.payload.required_options.keys())]
if args[0] != "":
if args[0].strip() == "LHOST":
# autocomplete the IP for LHOST
if settings.DISTRO == 'Debian':
ip_output = subprocess.getoutput("ip a").split("\n")[8][9:].split('/')[0]
ip_output = subprocess.getoutput("/sbin/ifconfig eth0").split("\n")[1].split()[1]
if 'addr' in ip_output:
ip_output = ip_output[5:]
res = [ip_output] + [None]
elif args[0].strip() == "LPORT":
# autocomplete the common MSF port of 4444 for LPORT
res = ["4444"] + [None]
elif args[0].strip() == "original_exe":
# tab-complete a file path for an exe
res = self.complete_path(args)
elif args[0].strip().endswith("_source"):
# tab-complete a file path for an exe
res = self.complete_path(args)
# elif args[0].strip() == "other path-needing option":
# # tab-complete a file path
# res = self.complete_path(args)
else:
# complete the command in the list ONLY if it's partially completed
res = [o + ' ' for o in options if (o.startswith(args[0]) and o != args[0])] + [None]
else:
# return all required_options available to 'set'
res = [o + ' ' for o in options] + [None]
return res
def complete(self, text, state):
"""
Generic readline completion entry point.
"""
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in self.commands][state]
# account for last argument ending in a space
RE_SPACE = re.compile('.*\s+$', re.M)
if RE_SPACE.match(buffer):
line.append('')
# resolve command to the implementation functions (above)
cmd = line[0].strip()
if cmd in self.commands:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in self.commands if c.startswith(cmd)] + [None]
return results[state]
class MSFCompleter(object):
"""
Class used for tab completion of metasploit payload selection.
Used in ./modules/common/shellcode.py
Takes a payloadTree next dictionary as an instantiation argument, of the form
payloadTree = {"windows" : {"meterpreter", "shell",...}, "linux" : {...}}
"""
def __init__(self, payloadTree):
self.payloadTree = payloadTree
def complete(self, text, state):
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# extract available platforms from the payload tree
platforms = [k for k, v in self.payloadTree.items()]
# show all platforms
if not line:
return [p + '/' for p in platforms][state]
# account for last argument ending in a space
RE_SPACE = re.compile('.*\s+$', re.M)
if RE_SPACE.match(buffer):
line.append('')
i = line[0].strip()
# complete the platform
if len(i.split("/")) == 1:
results = [p + '/' for p in platforms if p.startswith(i)] + [None]
return results[state]
# complete the stage, including singles (no /)
elif len(i.split("/")) == 2:
platform = i.split("/")[0]
stage = i.split("/")[1]
stages = [k for k, v in self.payloadTree[platform].items()]
results = [platform + "/" + s + '/' for s in stages if s.startswith(stage) and type(self.payloadTree[platform][s]) is dict]
singles = [platform + "/" + s + ' ' for s in stages if s.startswith(stage) and type(self.payloadTree[platform][s]) is not dict]
results += singles + [None]
return results[state]
# complete the stage (for x64) or stager (for x86)
elif len(i.split("/")) == 3:
platform = i.split("/")[0]
stage = i.split("/")[1]
stager = i.split("/")[2]
stagers = [k for k, v in self.payloadTree[platform][stage].items()]
results = [platform + "/" + stage + '/' + s + '/' for s in stagers if s.startswith(stager) and type(self.payloadTree[platform][stage][s]) is dict]
singles = [platform + "/" + stage + '/' + s + ' ' for s in stagers if s.startswith(stager) and type(self.payloadTree[platform][stage][s]) is not dict]
results += singles + [None]
return results[state]
# complete the stager for x64 (i.e. reverse_tcp)
elif len(i.split("/")) == 4:
platform = i.split("/")[0]
arch = i.split("/")[1]
stage = i.split("/")[2]
stager = i.split("/")[3]
stagers = [k for k, v in self.payloadTree[platform][arch][stage].items()]
results = [platform + "/" + arch + "/" + stage + '/' + s for s in stagers if s.startswith(stager)] + [None]
return results[state]
else:
return ""
class IPCompleter(object):
"""
Class used for tab completion of local IP for LHOST.
"""
def __init__(self):
pass
"""
If blank line, fill in the local IP
"""
def complete(self, text, state):
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
if not line:
if settings.DISTRO == 'Debian':
ip[state] = subprocess.getoutput("ip a").split("\n")[8][9:].split('/')[0]
else:
ip = [subprocess.getoutput("/sbin/ifconfig eth0").split("\n")[1].split()[1]] + [None]
if 'addr' in ip[state]:
ip[state] = ip[state][5:]
return ip[state]
else:
return text[state]
class MSFPortCompleter(object):
"""
Class used for tab completion of the default port (4444) for MSF payloads
"""
def __init__(self):
pass
"""
If blank line, fill in 4444
"""
def complete(self, text, state):
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
if not line:
port = ["4444"] + [None]
return port[state]
else:
return text[state]
class PathCompleter(object):
"""
Class used for tab completion of files on the local path.
"""
def __init__(self):
pass
def _listdir(self, root):
res = []
for name in os.listdir(root):
path = os.path.join(root, name)
if os.path.isdir(path):
name += os.sep
res.append(name)
return res
def _complete_path(self, path=None):
if not path:
return self._listdir('.')
dirname, rest = os.path.split(path)
tmp = dirname if dirname else '.'
res = [os.path.join(dirname, p)
for p in self._listdir(tmp) if p.startswith(rest)]
# more than one match, or single match which does not exist (typo)
if len(res) > 1 or not os.path.exists(path):
return res
# resolved to a single directory, so return list of files below it
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
# exact file match terminates this completion
return [path + ' ']
def complete_path(self, args):
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete(self, text, state):
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
return (self.complete_path(line) + [None])[state]
class OrdnanceCompleter(object):
def __init__(self, cmds, payload):
self.commands = [cmd for cmd, desc in cmds.items()]
self.payload = payload
def _listdir(self, root):
"""
Complete a directory path.
"""
res = []
for name in os.listdir(root):
path = os.path.join(root, name)
if os.path.isdir(path):
name += os.sep
res.append(name)
return res
def _complete_path(self, path=None):
"""
Complete a file path.
"""
if not path:
return self._listdir('.')
dirname, rest = os.path.split(path)
tmp = dirname if dirname else '.'
res = [os.path.join(dirname, p)
for p in self._listdir(tmp) if p.startswith(rest)]
# more than one match, or single match which does not exist (typo)
if len(res) > 1 or not os.path.exists(path):
return res
# resolved to a single directory, so return list of files below it
if os.path.isdir(path):
return [os.path.join(path, p) for p in self._listdir(path)]
# exact file match terminates this completion
return [path + ' ']
def complete_path(self, args):
"""
Entry point for path completion.
"""
if not args:
return self._complete_path('.')
# treat the last arg as a path and complete it
return self._complete_path(args[-1])
def complete_set(self, args):
"""
Complete all options for the 'set' command.
"""
res = []
if hasattr(self.payload, 'required_options'):
options = [k for k in sorted(self.payload.required_options.keys())]
if args[0] != "":
if args[0].strip() == "LHOST":
if settings.DISTRO == 'Debian':
ip_output = subprocess.getoutput("ip a").split("\n")[8][9:].split('/')[0]
# autocomplete the IP for LHOST
else:
ip_output = subprocess.getoutput("/sbin/ifconfig eth0").split("\n")[1].split()[1]
if 'addr' in ip_output:
ip_output = ip_output[5:]
res = [ip_output] + [None]
elif args[0].strip() == "LPORT":
# autocomplete the common MSF port of 4444 for LPORT
res = ["8675"] + [None]
elif args[0].strip() == "original_exe":
# tab-complete a file path for an exe
res = self.complete_path(args)
elif args[0].strip().endswith("_source"):
# tab-complete a file path for an exe
res = self.complete_path(args)
# elif args[0].strip() == "other path-needing option":
# # tab-complete a file path
# res = self.complete_path(args)
else:
# complete the command in the list ONLY if it's partially completed
res = [o + ' ' for o in options if (o.startswith(args[0]) and o != args[0])] + [None]
else:
# return all required_options available to 'set'
res = [o + ' ' for o in options] + [None]
return res
def complete(self, text, state):
"""
Generic readline completion entry point.
"""
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in self.commands][state]
# account for last argument ending in a space
RE_SPACE = re.compile('.*\s+$', re.M)
if RE_SPACE.match(buffer):
line.append('')
# resolve command to the implementation functions (above)
cmd = line[0].strip()
if cmd in self.commands:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in self.commands if c.startswith(cmd)] + [None]
return results[state]
class VeilMainMenuCompleter(object):
"""
Class used for tab completion of the main Controller menu
Takes a list of available commands, and loaded payload modules.
"""
def __init__(self, cmds, tools):
self.commands = [cmd for cmd, desc in cmds.items()]
self.tools = [obj.cli_name for name, obj in tools.items()]
def complete_use(self, args):
"""Complete payload/module"""
res = []
tools = []
for name in self.tools:
tools.append(name)
# return all payloads if we just have "use"
if len(args[0]) == 0:
res = [m for m in tools] + [None]
else:
tools = []
for name in self.tools:
if name.lower().startswith(args[0].lower()):
return [name + ' '] + [None]
return res
def complete_info(self, args):
"""Complete payload/module"""
res = []
tools = []
for name in self.tools:
tools.append(name)
# return all payloads if we just have "use"
if len(args[0]) == 0:
res = [m for m in tools] + [None]
else:
tools = []
for name in self.tools:
if name.lower().startswith(args[0].lower()):
return [name + ' '] + [None]
return res
def complete(self, text, state):
"Generic readline completion entry point."
buffer = readline.get_line_buffer()
line = readline.get_line_buffer().split()
# show all commands
if not line:
return [c + ' ' for c in self.commands][state]
# account for last argument ending in a space
RE_SPACE = re.compile('.*\s+$', re.M)
if RE_SPACE.match(buffer):
line.append('')
# resolve command to the implementation functions (above)
cmd = line[0].strip()
if cmd in self.commands:
impl = getattr(self, 'complete_%s' % cmd)
args = line[1:]
if args:
return (impl(args) + [None])[state]
return [cmd + ' '][state]
results = [c + ' ' for c in self.commands if c.startswith(cmd)] + [None]
return results[state]