Added resources explicitly for testing, improved analyzer, added config parser class

This commit is contained in:
VakarisZ 2019-09-05 15:03:30 +03:00
parent a0d932908f
commit 772880f952
6 changed files with 53 additions and 17 deletions

View File

@ -1,16 +1,22 @@
import json
class CommunicationAnalyzer(object): class CommunicationAnalyzer(object):
def __init__(self, island_client, machines): def __init__(self, island_client, machine_ips):
self.island_client = island_client self.island_client = island_client
self.machines = machines self.machine_ips = machine_ips
def analyze_test_results(self): def analyze_test_results(self):
for machine in self.machines: for machine_ip in self.machine_ips:
if self.did_monkey_communicate_back(machine): if not self.did_monkey_communicate_back(machine_ip):
print("Monkey from {} communicated back".format(machine)) return False
print("Monkey from {} communicated back".format(machine_ip))
return True
def did_monkey_communicate_back(self, monkey_ip): def did_monkey_communicate_back(self, machine_ip):
request = self.island_client.send_get_request("api/telemetry", {'telem_category': 'state'}) query = json.dumps({'ip_addresses': {'$elemMatch': {'$eq': machine_ip}}})
response = self.island_client.send_get_request("api/test/monkey", {'find_query': query})
return len(json.loads(response.content)['results']) > 0

View File

@ -0,0 +1,16 @@
import json
import os
class IslandConfigParser(object):
def __init__(self, config_filename):
self.config_raw = open(IslandConfigParser.get_conf_file_path(config_filename), 'r').read()
self.config_json = json.loads(self.config_raw)
def get_ips_of_targets(self):
return self.config_json['basic_network']['general']['subnet_scan_list']
@staticmethod
def get_conf_file_path(conf_file_name):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name)

View File

@ -1,16 +1,15 @@
import os
import unittest import unittest
import pytest import pytest
from envs.monkey_zoo.blackbox.monkey_island_client import MonkeyIslandClient from envs.monkey_zoo.blackbox.monkey_island_client import MonkeyIslandClient
from envs.monkey_zoo.blackbox.analyzers.communication_analyzer import CommunicationAnalyzer from envs.monkey_zoo.blackbox.analyzers.communication_analyzer import CommunicationAnalyzer
from envs.monkey_zoo.blackbox.island_config_parser import IslandConfigParser
def generic_blackbox_test_case(client, config_file_path, analyzers): def generic_blackbox_test_case(client, raw_config, analyzers):
with open(config_file_path, "r") as config_file: client.import_config(raw_config)
client.import_config(config_file.read()) # client.run_monkey_local()
client.run_monkey_local()
for analyzer in analyzers: for analyzer in analyzers:
assert analyzer.analyze_test_results() assert analyzer.analyze_test_results()
@ -33,11 +32,10 @@ class TestMonkeyBlackbox(unittest.TestCase):
assert client.get_api_status() is not None assert client.get_api_status() is not None
def test_ssh_exec(self): def test_ssh_exec(self):
conf_file_name = 'SSH.conf'
client = MonkeyIslandClient(self.island) client = MonkeyIslandClient(self.island)
conf_file_name = "SSH.conf" config_parser = IslandConfigParser(conf_file_name)
generic_blackbox_test_case(client, get_conf_file_path(conf_file_name), analyzer = CommunicationAnalyzer(client, config_parser.get_ips_of_targets())
[CommunicationAnalyzer(client, ["10.2.2.41", "10.2.2.42"])]) generic_blackbox_test_case(client, config_parser.config_raw, [analyzer])
def get_conf_file_path(conf_file_name):
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "island_configs", conf_file_name)

View File

@ -35,6 +35,7 @@ from monkey_island.cc.services.remote_run_aws import RemoteRunAwsService
from monkey_island.cc.resources.pba_file_upload import FileUpload from monkey_island.cc.resources.pba_file_upload import FileUpload
from monkey_island.cc.resources.attack.attack_config import AttackConfiguration from monkey_island.cc.resources.attack.attack_config import AttackConfiguration
from monkey_island.cc.resources.attack.attack_report import AttackReport from monkey_island.cc.resources.attack.attack_report import AttackReport
from monkey_island.cc.resources.test.monkey_test import MonkeyTest
__author__ = 'Barak' __author__ = 'Barak'
@ -134,6 +135,7 @@ def init_api_resources(api):
api.add_resource(AttackConfiguration, '/api/attack') api.add_resource(AttackConfiguration, '/api/attack')
api.add_resource(AttackReport, '/api/attack/report') api.add_resource(AttackReport, '/api/attack/report')
api.add_resource(VersionUpdate, '/api/version-update', '/api/version-update/') api.add_resource(VersionUpdate, '/api/version-update', '/api/version-update/')
api.add_resource(MonkeyTest, '/api/test/monkey')
def init_app(mongo_url): def init_app(mongo_url):

View File

@ -0,0 +1,14 @@
import json
import flask_restful
from flask import request
from monkey_island.cc.auth import jwt_required
from monkey_island.cc.database import mongo
class MonkeyTest(flask_restful.Resource):
@jwt_required()
def get(self, **kw):
find_query = json.loads(request.args.get('find_query'))
return {'results': list(mongo.db.monkey.find(find_query))}