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
|
||||
|
|
|
@ -23,12 +23,15 @@ class ControlClient(object):
|
|||
|
||||
@staticmethod
|
||||
def wakeup(parent=None, default_tunnel=None):
|
||||
for server in WormConfiguration.command_servers:
|
||||
try:
|
||||
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:
|
||||
WormConfiguration.current_server = server
|
||||
|
||||
monkey = {'guid': GUID,
|
||||
|
@ -41,6 +44,10 @@ 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,7 +56,7 @@ 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:
|
||||
|
@ -57,9 +64,10 @@ class ControlClient(object):
|
|||
LOG.info("Starting tunnel lookup...")
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -42,7 +41,7 @@ MONKEY_DOWNLOADS = [
|
|||
|
||||
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,7 +64,7 @@ class Monkey(restful.Resource):
|
|||
return result
|
||||
|
||||
def patch(self, guid):
|
||||
monkey_json = json.loads(request.data);
|
||||
monkey_json = json.loads(request.data)
|
||||
update = {"$set": {'modifytime': datetime.now()}}
|
||||
|
||||
if monkey_json.has_key('keepalive'):
|
||||
|
@ -106,8 +105,9 @@ 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')
|
||||
|
||||
|
@ -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)]
|
||||
|
@ -146,7 +146,7 @@ class Telemetry(restful.Resource):
|
|||
{'$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']
|
||||
|
|
Loading…
Reference in New Issue