2019-11-11 10:06:39 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-06-04 05:42:26 +00:00
|
|
|
|
|
|
|
from gevent.pool import Pool
|
2017-03-27 21:09:36 +00:00
|
|
|
from gevent import sleep
|
2016-06-04 05:42:26 +00:00
|
|
|
from cme.logger import setup_logger, setup_debug_logger, CMEAdapter
|
2016-12-20 07:23:40 +00:00
|
|
|
from cme.helpers.logger import highlight
|
2017-10-25 02:08:19 +00:00
|
|
|
from cme.helpers.misc import identify_target_file
|
2017-10-21 23:24:09 +00:00
|
|
|
from cme.parsers.ip import parse_targets
|
2017-10-25 02:08:19 +00:00
|
|
|
from cme.parsers.nmap import parse_nmap_xml
|
|
|
|
from cme.parsers.nessus import parse_nessus_file
|
2016-12-15 07:28:00 +00:00
|
|
|
from cme.cli import gen_cli_args
|
|
|
|
from cme.loaders.protocol_loader import protocol_loader
|
|
|
|
from cme.loaders.module_loader import module_loader
|
|
|
|
from cme.servers.http import CMEServer
|
2016-06-04 05:42:26 +00:00
|
|
|
from cme.first_run import first_run_setup
|
2016-12-15 07:28:00 +00:00
|
|
|
from cme.context import Context
|
2016-09-12 06:52:50 +00:00
|
|
|
from pprint import pformat
|
2019-11-10 21:42:04 +00:00
|
|
|
import configparser
|
2017-06-26 09:49:04 +00:00
|
|
|
import cme.helpers.powershell as powershell
|
2016-12-15 07:28:00 +00:00
|
|
|
import cme
|
2017-06-26 09:49:04 +00:00
|
|
|
import shutil
|
2016-12-15 07:28:00 +00:00
|
|
|
import webbrowser
|
2016-06-04 05:42:26 +00:00
|
|
|
import sqlite3
|
2016-12-15 07:28:00 +00:00
|
|
|
import random
|
2016-06-04 05:42:26 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2016-08-12 06:36:38 +00:00
|
|
|
import logging
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2017-10-25 02:08:19 +00:00
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
def main():
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
setup_logger()
|
|
|
|
logger = CMEAdapter()
|
2016-06-04 05:42:26 +00:00
|
|
|
first_run_setup(logger)
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
args = gen_cli_args()
|
|
|
|
|
|
|
|
if args.darrell:
|
2017-03-27 21:09:36 +00:00
|
|
|
links = open(os.path.join(os.path.dirname(cme.__file__), 'data', 'videos_for_darrell.harambe')).read().splitlines()
|
2016-12-15 07:28:00 +00:00
|
|
|
try:
|
|
|
|
webbrowser.open(random.choice(links))
|
|
|
|
except:
|
2017-03-27 21:09:36 +00:00
|
|
|
sys.exit(1)
|
2016-12-15 07:28:00 +00:00
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
cme_path = os.path.expanduser('~/.cme')
|
|
|
|
|
2019-11-10 21:42:04 +00:00
|
|
|
config = configparser.ConfigParser()
|
2016-12-15 07:28:00 +00:00
|
|
|
config.read(os.path.join(cme_path, 'cme.conf'))
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2018-03-01 19:36:17 +00:00
|
|
|
module = None
|
2017-03-27 21:09:36 +00:00
|
|
|
module_server = None
|
|
|
|
targets = []
|
|
|
|
jitter = None
|
2016-12-15 07:28:00 +00:00
|
|
|
server_port_dict = {'http': 80, 'https': 443, 'smb': 445}
|
|
|
|
current_workspace = config.get('CME', 'workspace')
|
2016-06-04 05:42:26 +00:00
|
|
|
|
|
|
|
if args.verbose:
|
|
|
|
setup_debug_logger()
|
|
|
|
|
2016-09-12 06:52:50 +00:00
|
|
|
logging.debug('Passed args:\n' + pformat(vars(args)))
|
2016-08-12 06:36:38 +00:00
|
|
|
|
2017-03-27 21:09:36 +00:00
|
|
|
if args.jitter:
|
|
|
|
if '-' in args.jitter:
|
|
|
|
start, end = args.jitter.split('-')
|
|
|
|
jitter = (int(start), int(end))
|
|
|
|
else:
|
|
|
|
jitter = (0, int(args.jitter))
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
if hasattr(args, 'username') and args.username:
|
2016-06-04 05:42:26 +00:00
|
|
|
for user in args.username:
|
|
|
|
if os.path.exists(user):
|
|
|
|
args.username.remove(user)
|
|
|
|
args.username.append(open(user, 'r'))
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
if hasattr(args, 'password') and args.password:
|
2016-08-02 04:06:17 +00:00
|
|
|
for passw in args.password:
|
|
|
|
if os.path.exists(passw):
|
|
|
|
args.password.remove(passw)
|
|
|
|
args.password.append(open(passw, 'r'))
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
elif hasattr(args, 'hash') and args.hash:
|
2016-08-02 04:06:17 +00:00
|
|
|
for ntlm_hash in args.hash:
|
|
|
|
if os.path.exists(ntlm_hash):
|
|
|
|
args.hash.remove(ntlm_hash)
|
|
|
|
args.hash.append(open(ntlm_hash, 'r'))
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
if hasattr(args, 'cred_id') and args.cred_id:
|
2016-09-12 06:52:50 +00:00
|
|
|
for cred_id in args.cred_id:
|
|
|
|
if '-' in str(cred_id):
|
|
|
|
start_id, end_id = cred_id.split('-')
|
2016-09-21 19:40:59 +00:00
|
|
|
try:
|
|
|
|
for n in range(int(start_id), int(end_id) + 1):
|
|
|
|
args.cred_id.append(n)
|
|
|
|
args.cred_id.remove(cred_id)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error('Error parsing database credential id: {}'.format(e))
|
|
|
|
sys.exit(1)
|
2016-09-12 06:52:50 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
if hasattr(args, 'target') and args.target:
|
|
|
|
for target in args.target:
|
|
|
|
if os.path.exists(target):
|
2017-10-25 02:08:19 +00:00
|
|
|
target_file_type = identify_target_file(target)
|
|
|
|
if target_file_type == 'nmap':
|
|
|
|
targets.extend(parse_nmap_xml(target, args.protocol))
|
|
|
|
elif target_file_type == 'nessus':
|
|
|
|
targets.extend(parse_nessus_file(target, args.protocol))
|
|
|
|
else:
|
|
|
|
with open(target, 'r') as target_file:
|
|
|
|
for target_entry in target_file:
|
2020-04-28 12:42:25 +00:00
|
|
|
targets.extend(parse_targets(target_entry.strip()))
|
2016-12-15 07:28:00 +00:00
|
|
|
else:
|
|
|
|
targets.extend(parse_targets(target))
|
|
|
|
|
2017-06-26 09:49:04 +00:00
|
|
|
# The following is a quick hack for the powershell obfuscation functionality, I know this is yucky
|
|
|
|
if hasattr(args, 'clear_obfscripts') and args.clear_obfscripts:
|
|
|
|
shutil.rmtree(os.path.expanduser('~/.cme/obfuscated_scripts/'))
|
|
|
|
os.mkdir(os.path.expanduser('~/.cme/obfuscated_scripts/'))
|
|
|
|
logger.success('Cleared cached obfuscated PowerShell scripts')
|
|
|
|
|
|
|
|
if hasattr(args, 'obfs') and args.obfs:
|
|
|
|
powershell.obfuscate_ps_scripts = True
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
p_loader = protocol_loader()
|
|
|
|
protocol_path = p_loader.get_protocols()[args.protocol]['path']
|
|
|
|
protocol_db_path = p_loader.get_protocols()[args.protocol]['dbpath']
|
|
|
|
|
|
|
|
protocol_object = getattr(p_loader.load_protocol(protocol_path), args.protocol)
|
|
|
|
protocol_db_object = getattr(p_loader.load_protocol(protocol_db_path), 'database')
|
|
|
|
|
|
|
|
db_path = os.path.join(cme_path, 'workspaces', current_workspace, args.protocol + '.db')
|
|
|
|
# set the database connection to autocommit w/ isolation level
|
|
|
|
db_connection = sqlite3.connect(db_path, check_same_thread=False)
|
|
|
|
db_connection.text_factory = str
|
|
|
|
db_connection.isolation_level = None
|
|
|
|
db = protocol_db_object(db_connection)
|
2016-06-05 20:43:51 +00:00
|
|
|
|
2018-02-23 10:13:46 +00:00
|
|
|
setattr(protocol_object, 'config', config)
|
|
|
|
|
2017-03-27 21:09:36 +00:00
|
|
|
if hasattr(args, 'module'):
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
loader = module_loader(args, db, logger)
|
2016-09-12 06:52:50 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
if args.list_modules:
|
|
|
|
modules = loader.get_modules()
|
2016-09-21 19:40:59 +00:00
|
|
|
|
2017-05-08 05:19:04 +00:00
|
|
|
for name, props in sorted(modules.items()):
|
|
|
|
logger.info('{:<25} {}'.format(name, props['description']))
|
2017-03-27 21:09:36 +00:00
|
|
|
sys.exit(0)
|
2016-09-21 19:40:59 +00:00
|
|
|
|
2016-12-20 07:23:40 +00:00
|
|
|
elif args.module and args.show_module_options:
|
2016-09-21 19:40:59 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
modules = loader.get_modules()
|
2017-05-08 05:19:04 +00:00
|
|
|
for name, props in modules.items():
|
|
|
|
if args.module.lower() == name.lower():
|
|
|
|
logger.info('{} module options:\n{}'.format(name, props['options']))
|
2017-03-27 21:09:36 +00:00
|
|
|
sys.exit(0)
|
2016-09-12 06:52:50 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
elif args.module:
|
|
|
|
modules = loader.get_modules()
|
2017-05-08 05:19:04 +00:00
|
|
|
for name, props in modules.items():
|
|
|
|
if args.module.lower() == name.lower():
|
|
|
|
module = loader.init_module(props['path'])
|
2016-12-15 07:28:00 +00:00
|
|
|
setattr(protocol_object, 'module', module)
|
|
|
|
break
|
|
|
|
|
2017-04-07 04:34:30 +00:00
|
|
|
if not module:
|
|
|
|
logger.error('Module not found')
|
|
|
|
exit(1)
|
|
|
|
|
2016-12-20 07:23:40 +00:00
|
|
|
if getattr(module, 'opsec_safe') is False:
|
2020-01-18 12:20:10 +00:00
|
|
|
ans = input(highlight('[!] Module is not opsec safe, are you sure you want to run this? [Y/n] ', 'red'))
|
2017-03-27 21:09:36 +00:00
|
|
|
if ans.lower() not in ['y', 'yes', '']:
|
|
|
|
sys.exit(1)
|
2016-12-20 07:23:40 +00:00
|
|
|
|
|
|
|
if getattr(module, 'multiple_hosts') is False and len(targets) > 1:
|
2020-01-18 12:20:10 +00:00
|
|
|
ans = input(highlight("[!] Running this module on multiple hosts doesn't really make any sense, are you sure you want to continue? [Y/n] ", 'red'))
|
2017-03-27 21:09:36 +00:00
|
|
|
if ans.lower() not in ['y', 'yes', '']:
|
|
|
|
sys.exit(1)
|
2016-12-20 07:23:40 +00:00
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
if hasattr(module, 'on_request') or hasattr(module, 'has_response'):
|
|
|
|
|
|
|
|
if hasattr(module, 'required_server'):
|
|
|
|
args.server = getattr(module, 'required_server')
|
|
|
|
|
|
|
|
if not args.server_port:
|
|
|
|
args.server_port = server_port_dict[args.server]
|
|
|
|
|
|
|
|
context = Context(db, logger, args)
|
2017-03-27 21:09:36 +00:00
|
|
|
module_server = CMEServer(module, context, logger, args.server_host, args.server_port, args.server)
|
|
|
|
module_server.start()
|
|
|
|
setattr(protocol_object, 'server', module_server.server)
|
2016-09-21 19:40:59 +00:00
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
try:
|
|
|
|
'''
|
2016-12-15 07:28:00 +00:00
|
|
|
Open all the greenlet (as supposed to redlet??) threads
|
2016-06-04 05:42:26 +00:00
|
|
|
Whoever came up with that name has a fetish for traffic lights
|
|
|
|
'''
|
|
|
|
pool = Pool(args.threads)
|
2017-03-27 21:09:36 +00:00
|
|
|
jobs = []
|
|
|
|
for target in targets:
|
|
|
|
jobs.append(pool.spawn(protocol_object, args, db, str(target)))
|
|
|
|
|
|
|
|
if jitter:
|
|
|
|
value = random.choice(range(jitter[0], jitter[1]))
|
2017-04-05 15:07:00 +00:00
|
|
|
logging.debug("Doin' the Jitterbug for {} seconds".format(value))
|
2017-03-27 21:09:36 +00:00
|
|
|
sleep(value)
|
|
|
|
|
2016-12-15 07:28:00 +00:00
|
|
|
for job in jobs:
|
|
|
|
job.join(timeout=args.timeout)
|
2016-06-04 05:42:26 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
2017-10-25 02:08:19 +00:00
|
|
|
if module_server:
|
|
|
|
module_server.shutdown()
|