island server bug fixes

This commit is contained in:
Barak Argaman 2017-08-29 14:12:07 +03:00
parent 2a96d23a4a
commit 3d0d0f71a2
6 changed files with 69 additions and 51 deletions

View File

@ -8,7 +8,7 @@ from cc.database import mongo
from cc.resources.monkey import Monkey from cc.resources.monkey import Monkey
from cc.resources.local_run import LocalRun from cc.resources.local_run import LocalRun
from cc.resources.telemetry import Telemetry from cc.resources.telemetry import Telemetry
from cc.resources.new_config import NewConfig from cc.resources.monkey_configuration import MonkeyConfiguration
from cc.resources.monkey_download import MonkeyDownload from cc.resources.monkey_download import MonkeyDownload
from cc.resources.netmap import NetMap from cc.resources.netmap import NetMap
from cc.resources.edge import Edge from cc.resources.edge import Edge
@ -17,12 +17,13 @@ from cc.resources.root import Root
__author__ = 'Barak' __author__ = 'Barak'
def send_admin(path): def serve_static_file(path):
return send_from_directory('admin/ui', path) print 'requested', path
return send_from_directory('ui/dist', path)
def send_to_default(): def serve_home():
return redirect('/admin/index.html') return serve_static_file('index.html')
def normalize_obj(obj): def normalize_obj(obj):
@ -44,30 +45,22 @@ def normalize_obj(obj):
return obj return obj
def output_json(obj, code, headers=None):
obj = normalize_obj(obj)
resp = make_response(bson.json_util.dumps(obj), code)
resp.headers.extend(headers or {})
return resp
def init_app(mongo_url): def init_app(mongo_url):
app = Flask(__name__) app = Flask(__name__)
api = flask_restful.Api(app) api = flask_restful.Api(app)
api.representations = {'application/json': output_json}
app.config['MONGO_URI'] = mongo_url app.config['MONGO_URI'] = mongo_url
mongo.init_app(app) mongo.init_app(app)
app.add_url_rule('/', 'send_to_default', send_to_default) app.add_url_rule('/', 'serve_home', serve_home)
app.add_url_rule('/admin/<path:path>', 'send_admin', send_admin) app.add_url_rule('/<path:path>', 'serve_static_file', serve_static_file)
api.add_resource(Root, '/api') api.add_resource(Root, '/api')
api.add_resource(Monkey, '/api/monkey', '/api/monkey/', '/api/monkey/<string:guid>') api.add_resource(Monkey, '/api/monkey', '/api/monkey/', '/api/monkey/<string:guid>')
api.add_resource(LocalRun, '/api/island', '/api/island/') api.add_resource(LocalRun, '/api/island', '/api/island/')
api.add_resource(Telemetry, '/api/telemetry', '/api/telemetry/', '/api/telemetry/<string:monkey_guid>') api.add_resource(Telemetry, '/api/telemetry', '/api/telemetry/', '/api/telemetry/<string:monkey_guid>')
api.add_resource(NewConfig, '/api/config/new', '/api/config/new/') api.add_resource(MonkeyConfiguration, '/api/configuration', '/api/configuration/')
api.add_resource(MonkeyDownload, '/api/monkey/download', '/api/monkey/download/', api.add_resource(MonkeyDownload, '/api/monkey/download', '/api/monkey/download/',
'/api/monkey/download/<string:path>') '/api/monkey/download/<string:path>')
api.add_resource(NetMap, '/api/netmap', '/api/netmap/') api.add_resource(NetMap, '/api/netmap', '/api/netmap/')

View File

@ -8,7 +8,7 @@ if BASE_PATH not in sys.path:
sys.path.insert(0, BASE_PATH) sys.path.insert(0, BASE_PATH)
from cc.app import init_app from cc.app import init_app
from cc.utils import init_collections from cc.utils import init_collections, local_ip_addresses
ISLAND_PORT = 5000 ISLAND_PORT = 5000
DEFAULT_MONGO_URL = "mongodb://localhost:27017/monkeyisland" DEFAULT_MONGO_URL = "mongodb://localhost:27017/monkeyisland"
@ -29,5 +29,6 @@ if __name__ == '__main__':
ssl_options={'certfile': os.environ.get('SERVER_CRT', 'server.crt'), ssl_options={'certfile': os.environ.get('SERVER_CRT', 'server.crt'),
'keyfile': os.environ.get('SERVER_KEY', 'server.key')}) 'keyfile': os.environ.get('SERVER_KEY', 'server.key')})
http_server.listen(ISLAND_PORT) http_server.listen(ISLAND_PORT)
print('Monkey Island C&C Server is running on https://{}:{}'.format(local_ip_addresses()[0], ISLAND_PORT))
IOLoop.instance().start() IOLoop.instance().start()
# 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'))

View File

@ -0,0 +1,40 @@
import json
from flask import request, jsonify
import flask_restful
from cc.database import mongo
__author__ = 'Barak'
SCHEMA = {
'type': 'object',
'title': 'Monkey',
'properties': {
'alive': {
'title': 'Alive',
'type': 'boolean'
}
},
'options': {
'collapsed': True
}
}
class MonkeyConfiguration(flask_restful.Resource):
def get(self):
return jsonify(schema=SCHEMA, configuration=self._get_configuration())
def post(self):
config_json = json.loads(request.data)
mongo.db.config.update({'name': 'newconfig'}, {"$set": config_json}, upsert=True)
return jsonify(schema=SCHEMA, configuration=self._get_configuration())
@classmethod
def _get_configuration(cls):
config = mongo.db.config.find_one({'name': 'newconfig'}) or {}
for field in ('name', '_id'):
config.pop(field, None)
return config

View File

@ -1,20 +0,0 @@
import json
from flask import request
import flask_restful
from cc.database import mongo
__author__ = 'Barak'
class NewConfig(flask_restful.Resource):
def get(self):
config = mongo.db.config.find_one({'name': 'newconfig'}) or {}
if 'name' in config:
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)

View File

@ -1,11 +1,11 @@
from datetime import datetime from datetime import datetime
from flask import request from flask import request, make_response, jsonify
import flask_restful import flask_restful
from cc.database import mongo from cc.database import mongo
from cc.utils import init_collections from cc.utils import init_collections, local_ip_addresses
__author__ = 'Barak' __author__ = 'Barak'
@ -14,11 +14,10 @@ class Root(flask_restful.Resource):
def get(self, action=None): def get(self, action=None):
if not action: if not action:
action = request.args.get('action') action = request.args.get('action')
if not action: if not action:
return { return jsonify(ip=local_ip_addresses()[0], mongo=str(mongo.db))
'status': 'OK',
'mongo': str(mongo.db),
}
elif action == "reset": elif action == "reset":
mongo.db.config.drop() mongo.db.config.drop()
mongo.db.monkey.drop() mongo.db.monkey.drop()
@ -28,15 +27,10 @@ class Root(flask_restful.Resource):
mongo.db.node.drop() mongo.db.node.drop()
mongo.db.edge.drop() mongo.db.edge.drop()
init_collections() init_collections()
return { return jsonify(status='OK')
'status': 'OK',
}
elif action == "killall": elif action == "killall":
mongo.db.monkey.update({}, {'$set': {'config.alive': False, 'modifytime': datetime.now()}}, upsert=False, mongo.db.monkey.update({}, {'$set': {'config.alive': False, 'modifytime': datetime.now()}}, upsert=False,
multi=True) multi=True)
return { return 200
'status': 'OK',
}
else: else:
return {'status': 'BAD', return make_response(400, {'error': 'unknown action'})
'reason': 'unknown action'}

View File

@ -4,6 +4,8 @@ import sys
import array import array
import struct import struct
from netifaces import interfaces, ifaddresses, AF_INET
from cc.database import mongo from cc.database import mongo
__author__ = 'Barak' __author__ = 'Barak'
@ -75,3 +77,11 @@ else:
finally: finally:
return result return result
# End of local ips function # End of local ips function
def local_ip_addresses():
ip_list = []
for interface in interfaces():
addresses = ifaddresses(interface).get(AF_INET, [])
ip_list.extend([link['addr'] for link in addresses if link['addr'] != '127.0.0.1'])
return ip_list