-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathMetasploitProAPI.py
executable file
·692 lines (545 loc) · 26.1 KB
/
MetasploitProAPI.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#!/usr/bin/env python
#coding:utf-8
"""
##--------------------------------------#
## Kvasir
##
## (c) 2010-2014 Cisco Systems, Inc.
## (c) 2015 Kurt Grutzmacher
##
## MetasploitPro API Integration Library
##
## Author: Kurt Grutzmacher <[email protected]>
##--------------------------------------#
"""
import os
import sys
import logging
import httplib
try:
import msgpack
except ImportError, e:
raise Exception("Install 'msgpack' library: 'pip install msgpack-python'")
logger = logging.getLogger("web2py.app.kvasir")
########################################################################
class MSFProAPIError(Exception):
pass
########################################################################
class MetasploitProAPI:
"""
Connects to the Metasploit Pro library via msgpack
"""
def __init__(self, host="localhost:3790", ssl=True, apikey=None, username=None, password=None):
self.log = logging.getLogger(self.__class__.__name__)
self.username = username
self.password = password
self.apikey = apikey
self.ssl = ssl
if host.startswith('http'):
from urlparse import urlsplit
(self.host, self.port) = urlsplit(host)[1].split(':')
else:
(self.host, self.port) = host.split(':')
self.port = int(self.port)
self.debug = False
self.libraryversion = "1.0"
self.apiurl = "/api/%s" % (self.libraryversion)
self.connected = False
#-----------------------------------------------------------------#
def build_command(self, command=[], *opts):
"""Builds an API command w/ msgpack, appends the API key and
any options"""
if type(command) is not type(list()):
command = [command]
if self.apikey:
command.append(self.apikey)
for k in opts:
command.append(k)
return msgpack.packb(command)
#-----------------------------------------------------------------#
def send(self, message=None):
"""Sends a message to Metasploit API and unpacks the response"""
headers = {"Content-type" : "binary/message-pack"}
if self.ssl:
httpclient = httplib.HTTPSConnection(self.host, self.port)
else:
httpclient = httplib.HTTPConnection(self.host, self.port)
try:
httpclient.request("POST", self.apiurl, message, headers)
except Exception, e:
raise MSFProAPIError("HTTP Client error:", e)
response = httpclient.getresponse()
if response.status == 200:
try:
res = msgpack.unpackb(response.read())
except Exception, e:
raise MSFProAPIError("Unable to process response:", e)
if res.get('error') is True:
raise MSFProAPIError("API Error:", res['error_string'])
else:
raise MSFProAPIError("HTTP Error from MSF:", response.status)
return res
#-----------------------------------------------------------------#
def login(self, host="localhost:3790", ssl=True, apikey=None, username=None, password=None):
"""Perform session setup, sending username/password if defined or
validating API key. Sets self.connected to True when successfull"""
self.connected = False
if self.username:
self.log.debug("Authenticating with username/password")
self.apikey = None
message = self.build_command(['auth.login', self.user, self.password])
res = self.send(message)
if res['result'] == 'success':
self.apikey = res['token']
self.connected = True
else:
self.log.error("Authentication failed!")
self.connected = False
elif self.apikey:
# checking to be sure API key is valid
self.log.debug("Obtaining Metasploit version and statistics")
resp = self.version()
if resp.has_key('version'):
self.log.info("Server v%s, API %s, Ruby v%s" % (resp['version'], resp['api'], resp['ruby']))
self.connected = True
else:
self.connected = False
else:
self.connected = False
return self.connected
#-----------------------------------------------------------------#
def close(self):
"""Closes the connection"""
self.connected = False
###################################################################
# Metasploit core commands
#-----------------------------------------------------------------#
def version(self):
"""Obtain the Metasploit Framework version"""
self.log.debug("Sending version request")
message = self.build_command(['core.version'])
return self.send(message)
#-----------------------------------------------------------------#
def add_module_path(self, mod_path=""):
"""Add a local file path to module search list"""
self.log.debug("Adding %s to module path" % mod_path)
message = self.build_command(['core.add_module_path'], mod_path)
return self.send(message)
#-----------------------------------------------------------------#
def save(self):
"""Saves the global datastore configuration to disk"""
self.log.debug("Saving the global datastore to disk")
message = self.build_command(['core.save'])
return self.send(message)
###################################################################
# Metasploit Module commands
#-----------------------------------------------------------------#
def module_stats(self):
"""Obtain the module counts"""
self.log.debug("Sending stats request")
message = self.build_command(['core.module_stats'])
return self.send(message)
#-----------------------------------------------------------------#
def reload_modules(self):
"""Reloads modules"""
self.log.debug("Reloading modules")
message = self.build_command(['core.reload_modules'])
return self.send(message)
#-----------------------------------------------------------------#
def module_list(self, modtype="exploits"):
"""Obtain a list of Metasploit Framework modules
Modtype can be: exploits, auxiliary, payloads, encoders, nops
"""
self.log.debug("Sending module.%s request" % (modtype))
message = self.build_command(["module.%s" % (modtype)])
return self.send(message)
#-----------------------------------------------------------------#
def module_info(self, modtype="", module=""):
"""Obtain info on a specific module"""
if modtype.lower() not in ["exploit", "auxiliary", "post", "payload", "encoder", "nop"]:
self.log.debug("Invalid module type")
return
message = self.build_command(["module.info"], modtype, module)
return self.send(message)
#-----------------------------------------------------------------#
def module_options(self, modtype="", module=""):
"""Obtain options on a specific module"""
if modtype.lower() not in ["exploit", "auxiliary", "post", "payload", "encoder", "nop"]:
self.log.debug("Invalid module type")
return
message = self.build_command(["module.options"], modtype, module)
return self.send(message)
#-----------------------------------------------------------------#
def compatible_payloads(self, module=""):
"""Obtain compatible payloads for a specific module"""
message = self.build_command(["module.compatible_payloads"], module)
return self.send(message)
#-----------------------------------------------------------------#
def target_compatible_payloads(self, module="", target=1):
"""Obtain compatible payloads for a specific module and target"""
message = self.build_command(["module.target.compatible_payloads"], module, target)
return self.send(message)
#-----------------------------------------------------------------#
def compatible_sessions(self, module=""):
"""Obtain compatible sessions for a specific module"""
message = self.build_command(["module.compatible_sessions"], module)
return self.send(message)
#-----------------------------------------------------------------#
def module_execute(self, modtype="", module="", options={}):
"""Execute a specific module"""
if module == "":
self.log.debug("[module_execute] No module specified")
return
if modtype.lower() not in ["exploit", "auxiliary", "post"]:
self.log.debug("[module_execute] Invalid module type specified")
return
message = self.build_command(["module.execute"], modtype, module, options)
return self.send(message)
###################################################################
# Metasploit Jobs, Sessions and Meterpreter
#-----------------------------------------------------------------#
def job_list(self):
"""Returns a list of running jobs and job names"""
message = self.build_command(["job.list"])
return self.send(message)
#-----------------------------------------------------------------#
def job_stop(self, jobid=""):
"""Kill a specific job by ID"""
message = self.build_command(["job.kill"], jobid)
return self.send(message)
#-----------------------------------------------------------------#
def session_list(self):
"""Returns a list of running jobs and job names"""
message = self.build_command(["session.list"])
return self.send(message)
#-----------------------------------------------------------------#
def session_stop(self, jobid=""):
"""Kill a specific job by ID"""
message = self.build_command(["session.kill"], jobid)
return self.send(message)
#-----------------------------------------------------------------#
def shell_read(self, sessionid="", readptr=0):
"""Read any pending output from a 'shell' session"""
message = self.build_command(["session.shell_read"], sessionid, readptr)
return self.send(message)
#-----------------------------------------------------------------#
def shell_write(self, sessionid="", data=None):
"""Write data to a 'shell' session"""
message = self.build_command(["session.shell_write"], data)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_read(self, sessionid=""):
"""Read any pending output from a 'Meterpreter' session"""
message = self.build_command(["session.meterpreter_read"], sessionid)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_write(self, sessionid="", data=None):
"""Write data to a 'Meterpreter' session"""
message = self.build_command(["session.meterpreter_write"], data)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_run_single(self, sessionid="", data=None):
"""Run a command in a 'Meterpreter' session, no matter who is interacting with it"""
message = self.build_command(["session.meterpreter_run_single"], sessionid, data)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_script(self, sessionid="", data=None):
"""Run a script on a 'Meterpreter' session"""
message = self.build_command(["session.meterpreter_script"], data)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_session_detach(self, sessionid=""):
"""Detatch a 'Meterpreter' session"""
message = self.build_command(["session.meterpreter_session_detach"], sessionid)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_session_kill(self, sessionid=""):
"""Terminates a 'Meterpreter' session"""
message = self.build_command(["session.meterpreter_session_kill"], sessionid)
return self.send(message)
#-----------------------------------------------------------------#
def meterpreter_tabs(self, sessionid="", data=None):
"""Emulates pressing tab from a 'Meterpreter' session"""
message = self.build_command(["session.meterpreter_tabs"], data)
return self.send(message)
#-----------------------------------------------------------------#
def compatible_modules(self, sessionid=""):
"""Lists compatbile post modules for a session"""
message = self.build_command(["session.compatible_modules"], sessionid)
return self.send(message)
#-----------------------------------------------------------------#
def shell_upgrade(self, sessionid="", host=None, port=4444):
"""Attempts to upgrade a shell to Meterpreter, expects multi/handler
to be running on {host} and {port}"""
message = self.build_command(["session.shell_upgrade"], sessionid, host, port)
return self.send(message)
#-----------------------------------------------------------------#
def ring_clear(self, sessionid=""):
"""Wipes ring buffer from sessionid"""
message = self.build_command(["session.ring_clear"], sessionid)
return self.send(message)
#-----------------------------------------------------------------#
def ring_last(self, sessionid=""):
"""Returns last issued read pointer for a session"""
message = self.build_command(["session.ring_last"], sessionid)
return self.send(message)
#-----------------------------------------------------------------#
def ring_put(self, sessionid="", data=""):
"""Same as shell_write"""
message = self.build_command(["session.ring_put"], sessionid, data)
return self.send(message)
#-----------------------------------------------------------------#
def ring_read(self, sessionid="", readptr=0):
"""Same as shell_read"""
message = self.build_command(["session.ring_read"], sessionid, readptr)
return self.send(message)
###################################################################
# Metasploit Pro Basic features
#-----------------------------------------------------------------#
def pro_about(self):
"""Whatdisabout?"""
message = self.build_command(["pro.about"])
return self.send(message)
#-----------------------------------------------------------------#
def pro_workspaces(self):
"""List active workspaces"""
message = self.build_command(["pro.workspaces"])
return self.send(message)
#-----------------------------------------------------------------#
def pro_workspace_add(self, options={}):
"""Adds a workspace"""
message = self.build_command(["pro.workspaces_add"], options)
return self.send(message)
#-----------------------------------------------------------------#
def pro_workspace_del(self, workspace=None):
"""Deletes a workspace"""
message = self.build_command(["pro.workspaces_del"], workspace)
return self.send(message)
#-----------------------------------------------------------------#
def pro_users(self):
"""List users"""
message = self.build_command(["pro.users"])
return self.send(message)
#-----------------------------------------------------------------#
# Metasploit Pro Import features
#-----------------------------------------------------------------#
def pro_import_data(self, workspace=None, data=None, options={}):
"""Import raw data into MSF"""
# data should be a string, join it!
if isinstance(data, list):
data = ''.join(data)
message = self.build_command(["pro.import_data"], workspace, data, options)
return self.send(message)
#-----------------------------------------------------------------#
def pro_import_file(self, workspace=None, filename=None, options={}):
"""Import local file into MSF (local to MSF)"""
message = self.build_command(["pro.import_file"], workspace, filename, options)
return self.send(message)
#-----------------------------------------------------------------#
def pro_start_import(self, options={}):
"""Starts the Import action within Metasploit Pro"""
message = self.build_command(["pro.start_import"], options)
return self.send(message)
#-----------------------------------------------------------------#
def pro_validate_import_file(self, filename=None):
"""Validates a local file may be imported to MSF"""
message = self.build_command(["pro.validate_import_file("], filename)
return self.send(message)
#-----------------------------------------------------------------#
def pro_start_import_creds(self, options={}):
"""Import credentials such as users, passwords, hashes, and keys"""
message = self.build_command(["pro.start_import_creds"], options)
return self.send(message)
###################################################################
# Metasploit Pro Report features
#-----------------------------------------------------------------#
def start_report(self, options={}):
"""Generates report/exportfunctons"""
message = self.build_command(["pro.start_report"], options)
return self.send(message)
#-----------------------------------------------------------------#
def report_list(self, workspace=None):
"""Lists reports"""
message = self.build_command(["pro.report_list"], workspace)
return self.send(message)
#-----------------------------------------------------------------#
def report_download(self, rptid=0):
"""Downloads a specific report"""
message = self.build_command(["pro.report_download"], rptid)
return self.send(message)
#-----------------------------------------------------------------#
def report_download_by_task(self, taskid=0):
"""Downloads a report based upon a task ID"""
message = self.build_command(["pro.report_download_by_task"], taskid)
return self.send(message)
###################################################################
# Metasploit Pro Task features
#-----------------------------------------------------------------#
def task_list(self):
"""Lists active tasks"""
message = self.build_command(["pro.task_list"])
return self.send(message)
#-----------------------------------------------------------------#
def task_status(self, task=None):
"""Current status of a task"""
message = self.build_command(["pro.task_status"], task)
return self.send(message)
#-----------------------------------------------------------------#
def task_stop(self, task=None):
"""Stops a task"""
message = self.build_command(["pro.task_stop"], task)
return self.send(message)
#-----------------------------------------------------------------#
def task_log(self, task=None):
"""Returns status and log data for a task"""
message = self.build_command(["pro.task_log"], task)
return self.send(message)
#-----------------------------------------------------------------#
def task_delete_log(self, task=None):
"""Deletes a log data for a task"""
message = self.build_command(["pro.task_delete_log"], task)
return self.send(message)
###################################################################
# Metasploit Pro Exploit features
#-----------------------------------------------------------------#
def start_import_creds(self, options={}):
"""Imports credentials from a file"""
message = self.build_command(["pro.start_import_creds"], options)
return self.send(message)
#-----------------------------------------------------------------#
def start_bruteforce(self, options={}):
"""Starts a bruteforce task"""
message = self.build_command(["pro.start_bruteforce"], options)
return self.send(message)
#-----------------------------------------------------------------#
def start_exploit(self, options={}):
"""Starts an exploit task"""
message = self.build_command(["pro.start_exploit"], options)
return self.send(message)
###################################################################
# Metasploit Pro Loot features
#-----------------------------------------------------------------#
def loot_list(self, workspace=None):
"""Lists the loot for a workspace"""
message = self.build_command(["pro.loot_list"], workspace)
return self.send(message)
#-----------------------------------------------------------------#
def loot_download(self, lootid=0):
"""Downloads a loot from an identifier"""
message = self.build_command(["pro.loot_download"], lootid)
return self.send(message)
#-----------------------------------------------------------------#
def debugmsg(self, message):
"""Simple debug message output"""
from pprint import pprint
if self.debug:
pprint(message)
#----------------------------------------------------------------------
def listallmodules(msf):
"""Function to list all the modules"""
from pprint import pprint
payloads = msf.payloads()
print "All Paylods:"
for m in payloads:
pprint("%s: %s" % (m, payloads[m]))
exploits = msf.module_list("exploits")
for f in exploits:
print "Exploit name: %s" % (f)
modinfo = msf.module_info("exploits", f)
print "Module Info:"
for m in modinfo:
print("%s: %s" % (m, modinfo[m]))
modopts = msf.module_options("exploits", f)
print "Module Options:"
for m in modopts:
print("%s: %s" % (m, modopts[m]))
auxiliary = msf.module_list("auxiliary")
for f in auxiliary:
print "Auxiliary name: %s" % (f)
modinfo = msf.module_info("auxiliary", f)
for m in modinfo:
print("%s: %s" % (m, modinfo[m]))
modopts = msf.module_options("exploits", f)
print "Module Options:"
for m in modopts:
print("%s: %s" % (m, modinfo[m]))
#----------------------------------------------------------------------
def import_xml_report(filename=None):
"""
Process a MSF Pro XML report
"""
return
#----------------------------------------------------------------------
def login(options):
""""
Login!
"""
msf = MetasploitProAPI( options.username, options.password, options.server, options.ssl )
print "-" * 75
print "Metasploit Pro API v%s" % (msf.libraryversion)
if options.ssl:
msf.ssl = True
else:
msf.ssl = False
if not msf.connect():
print "[main] Unable to continue, not connected!"
sys.exit(1)
print "Connected to Metasploit Pro version %s" % (msf.version())
print "-" * 75
return msf
#----------------------------------------------------------------------
if __name__=='__main__':
from optparse import OptionParser
# set up commandline arguments
Progname=os.path.basename(sys.argv[0])
Usage="%prog usage: XXX:[command_line_args]\n" \
"%prog usage: -h\n" \
"%prog usage: -V"
optparser = OptionParser(usage=Usage, version="%prog: $Id:$" )
optparser.add_option("-d", "--debug", dest = "debug", action="store_true", help="Debugging messages")
optparser.add_option("-v", "--verbose", dest = "verbose", action="store_true", help="Verbose messages")
optparser.add_option("-u", "--username", dest = "username", action="store", default=None, help="Username")
optparser.add_option("-p", "--password", dest = "password", action="store", default=None, help="Password")
optparser.add_option("-k", "--apikey", dest = "apikey", action="store", default=None, help="API Key")
optparser.add_option("-s", "--server", dest = "server", action="store", default="127.0.0.1:3790", help="Server address:port")
optparser.add_option("-S", "--SSL", dest = "ssl", action="store_true", default=True, help="Use SSL encryption")
optparser.add_option("-i", "--interactive", dest = "interactive", action="store_true", default=False, help="Go Interactive")
optparser.add_option("-b", "--bpython", dest = "bpython", action="store_true", default=False, help="Use Bpython")
(options, params) = optparser.parse_args()
root_log = logging.getLogger()
if options.debug:
root_log.setLevel(logging.DEBUG)
elif options.verbose:
root_log.setLevel(logging.INFO)
else:
root_log.setLevel(logging.WARN)
handler = logging.StreamHandler()
logformat = "%(name)s: %(levelname)s: %(message)s"
handler.setFormatter(logging.Formatter(logformat))
root_log.addHandler(handler)
log = logging.getLogger(Progname)
msf = MetasploitProAPI(host=options.server, ssl=options.ssl, apikey=options.apikey, username=options.username, password=options.password)
msf.login()
if options.interactive or options.bpython:
log.info("Attempting to go interactive...")
if options.bpython:
try:
import bpython
bpython.embed(locals_={'msf':msf}, banner="\nWelcome to MetasploitProAPI, use the variable 'msf'\n")
except:
log.warning('import bpython error; trying ipython...')
else:
try:
import IPython
if IPython.__version__ >= '0.11':
from IPython.frontend.terminal.embed import InteractiveShellEmbed
shell = InteractiveShellEmbed(user_ns={'msf':msf})
shell()
else:
shell = IPython.Shell.IPShell(argv=[],user_ns={'msf':msf})
shell.mainloop()
except:
log.warning("Unable to go interactive")
sys.exit(0)
if msf:
listallmodules(msf)