コード例 #1
0
def test_read_valid_file_should_return_non_empty_dictionary():
    # arrange
    sut = PcapReader()

    # act
    dir_path = os.path.dirname(os.path.realpath(__file__))
    res = sut.read(dir_path + "/files_fingerprint_recognizer/valid_input.pcap")

    # assert
    assert bool(res)
コード例 #2
0
def test_read_valid_file_should_return_dictionary_of_flows():
    # arrange
    sut = PcapReader()

    # act
    dir_path = os.path.dirname(os.path.realpath(__file__))
    res = sut.read(dir_path + "/files_fingerprint_recognizer/valid_input.pcap")

    # assert
    assert all(isinstance(x, Flow) for x in res.values())
コード例 #3
0
def test_read_invalid_path_should_throw_exception():
    # arrange
    sut = PcapReader()
    # act
    try:
        sut.read("")
        assert False
    except Exception as e:
        print(e)
        assert True
コード例 #4
0
def test_read_should_not_throw_os_exception():
    # arrange
    sut = PcapReader()
    dir_path = os.path.dirname(os.path.realpath(__file__))

    # act
    try:
        res = sut.read(dir_path +
                       "/files_fingerprint_recognizer/valid_input.pcap")
    except OSError as ex:
        assert False
    except:
        assert True
コード例 #5
0
def test_tshark_should_be_installed():
    # arrange
    sut = PcapReader()
    dir_path = os.path.dirname(os.path.realpath(__file__))

    # act
    try:
        res = sut.read(dir_path +
                       "/files_fingerprint_recognizer/valid_input.pcap")
    except ModuleNotFoundError as ex:
        assert False
    except:
        assert True
コード例 #6
0
def run(pcaps_paths=[], flows_paths=[], write_flows_dir='', symbols_path='', out_dir='', train_size=0.0, recognizer=None):
    # Phase 1: Read flows from pcap file or binary file
    flows_per_app = dict()
    if flows_paths:
        for pcaps_path in flows_paths:
            appName = Path(pcaps_path).parts[-2]
            executionName = Path(pcaps_path).stem
            flows_per_app[appName] = flows_per_app.get(appName, {'all': {}})
            print(f"reading binaries from {pcaps_path}")
            flows_per_app[appName]['all'][executionName] = BinaryReader.read(pcaps_path)

            if write_flows_dir:
                write_path = write_flows_dir + '/' + appName + '/' + executionName + '.p'
                print(f"writing binaries into {write_path}")
                BinaryReader.write(flows_per_app[appName]['all'][executionName], write_path)

    elif pcaps_paths:
        pcap_reader = PcapReader()
        for pcaps_path in pcaps_paths:
            appName = Path(pcaps_path).parts[-2]
            executionName = Path(pcaps_path).stem
            flows_per_app[appName] = flows_per_app.get(appName, {'all': {}})
            print(f"reading pcaps from {pcaps_path}")

            flows_per_app[appName]['all'][executionName] = pcap_reader.read(pcaps_path)

            if write_flows_dir:
                write_path = write_flows_dir + '/' + appName + '/' + executionName + '.p'
                print(f"writing binaries into {write_path}")
                BinaryReader.write(flows_per_app[appName]['all'][executionName], write_path)
                
    else:
        raise Exception("There is no input file specified")

    # Phase 2: Save flows as binary if output path is speficied


    # Phase 3: Load previously saved symbols dictionary
    symbols = {}
    if symbols_path:
        symbols = BinaryReader.read(symbols_path, {})


    # Phase 4: Split test/train data if needed
    reshape_flows_per_app(flows_per_app, train_size)


    # Phase 5: Generate sequence of symbols based on symbols set and flows

    fingerprint_recognizer = build_recognizer(symbols, recognizer)

    for appName, value in flows_per_app.items():
        result = {}
        for label, v in value.items():
            result[label] = []
            for executionName, flows in v.items():
                # IT IS THE MAIN PART OF APPLICATION
                r = fingerprint_recognizer.recognize(flows)
                result[label].append(r)

        # Phase 6: Save generated sequence of symbols
        if out_dir:
            save_app_flows(appName, result, out_dir);        
        
        # Phase 7: Save set of unique symbols to use in the future
        if symbols_path:
            symbols = fingerprint_recognizer.get_symbols()
            BinaryReader.write(symbols, symbols_path)