Exemplo n.º 1
0
def service_unit(sqlalchemy_session, service_annotated_unit):
    analyzer = mock.Mock()
    analyzer.analyze.return_value = {}
    return UnitService(
        sqlalchemy_session, service_annotated_unit,
        FileStorage(storage_folder="{}/storage/units".format(APP_DIR),
                    compression=Compression(),
                    subdirectories=False), TraceAnalyzer())
Exemplo n.º 2
0
    def configure(self, binder):
        """
        Configure application, setup binder

        :param binder:
        """

        from traces_api.modules.unit.service import UnitService
        from traces_api.modules.annotated_unit.service import AnnotatedUnitService
        from traces_api.modules.mix.service import MixService
        from traces_api.storage import FileStorage

        annotated_unit_storage = FileStorage(self._abs_storage_path(
            self._config.get("storage", "ann_units_dir")),
                                             compression=Compression())
        annotated_unit_service = AnnotatedUnitService(self._session_maker,
                                                      annotated_unit_storage,
                                                      TraceAnalyzer(),
                                                      TraceNormalizer())

        unit_storage = FileStorage(self._abs_storage_path(
            self._config.get("storage", "units_dir")),
                                   compression=Compression(),
                                   subdirectories=False)
        unit_service = UnitService(self._session_maker, annotated_unit_service,
                                   unit_storage, TraceAnalyzer())

        mix_storage = FileStorage(self._abs_storage_path(
            self._config.get("storage", "mixes_dir")),
                                  compression=Compression())
        mix_service = MixService(self._session_maker, self._engine,
                                 annotated_unit_service, mix_storage,
                                 TraceNormalizer(), TraceMixing())

        binder.bind(UnitService, to=unit_service)
        binder.bind(AnnotatedUnitService, to=annotated_unit_service)
        binder.bind(MixService, to=mix_service)
Exemplo n.º 3
0
def test_mix_hydra_and_medusa(client):
    medusa_id_ann_unit = create_medusa(client)
    hydra_id_ann_unit = create_hydra(client)

    data = dict(name="Hydra and medusa",
                description="Mix hydra and medusa",
                labels=[],
                annotated_units=[
                    dict(id_annotated_unit=hydra_id_ann_unit,
                         ip_mapping=[{
                             "original": "240.0.0.2",
                             "replacement": "172.18.1.1"
                         }, {
                             "original": "240.170.0.2",
                             "replacement": "172.18.1.2"
                         }],
                         mac_mapping=[],
                         port_mapping=[],
                         timestamp=0),
                    dict(id_annotated_unit=medusa_id_ann_unit,
                         ip_mapping=[{
                             "original": "240.0.0.2",
                             "replacement": "172.17.1.1"
                         }, {
                             "original": "240.170.0.2",
                             "replacement": "172.17.1.2"
                         }],
                         mac_mapping=[],
                         port_mapping=[],
                         timestamp=0),
                ])
    r = client.post("/mix/create", json=data, content_type="application/json")
    assert r.json["id_mix"]

    r2 = client.post("/mix/%s/generate" % r.json["id_mix"],
                     json={},
                     content_type="application/json")
    assert r2.status_code == 200

    wait_for_generation(client, r.json["id_mix"])

    TraceAnalyzer()

    r2 = client.get("/mix/%s/download" % r.json["id_mix"])
    assert r2.status_code == 200

    with tempfile.NamedTemporaryFile(mode="wb") as f:
        f.write(r2.data)
        f.flush()
        f.file.close()

        analyzed = TraceAnalyzer().analyze(f.name)

    assert compare_list_dict(
        analyzed['pairs_mac_ip'],
        [
            {
                'MAC': '08:00:27:00:00:00',
                'IP': '172.18.1.1'
            },
            {
                'MAC': '08:00:27:00:00:00',
                'IP': '172.18.1.1'
            },
            {
                'MAC': '08:00:27:aa:00:00',
                'IP': '172.18.1.2'
            },
            {
                'MAC': '08:00:27:aa:00:00',
                'IP': '172.18.1.2'
            },
            {
                'MAC': '08:00:27:00:00:00',
                'IP': '172.17.1.1'
            },
            {
                'MAC': '08:00:27:00:00:00',
                'IP': '172.17.1.1'
            },
            {
                'MAC': '08:00:27:aa:00:00',
                'IP': '172.17.1.2'
            },
            {
                'MAC': '08:00:27:aa:00:00',
                'IP': '172.17.1.2'
            },

            # Base pcap
            {
                'IP': '192.168.10.9',
                'MAC': 'b8:ac:6f:1d:1f:6c'
            },
            {
                'IP': '52.3.119.216',
                'MAC': '00:c1:b1:14:eb:31'
            },
            {
                'IP': '192.168.10.9',
                'MAC': 'b8:ac:6f:1d:1f:6c'
            },
            {
                'IP': '52.3.119.216',
                'MAC': '00:c1:b1:14:eb:31'
            },
        ])
Exemplo n.º 4
0
def analyzer():
    return TraceAnalyzer()
Exemplo n.º 5
0
def service_annotated_unit(sqlalchemy_session):
    return AnnotatedUnitService(sqlalchemy_session, FileStorage(storage_folder="{}/storage/ann_units".format(APP_DIR), compression=Compression()), TraceAnalyzer(), TraceNormalizer())