sourceFiles[currentType] = [currentPath] if len(sourceFiles) == 0: print("No files were added. Exiting.") sys.exit(1) nn = NN(Payload.vectorSize(), OutputMapper.vectorSize) if os.path.isfile(weightsPath): nn.load(weightsPath) samples = TrainingSample.TrainingSampleBatch() for trafficType in sourceFiles.keys(): for filePath in sourceFiles[trafficType]: cap = pyshark.FileCapture(filePath) for packet in cap: try: sample = TrainingSample.TrainingSample( Payload.fromPySharkCapture(packet), trafficType) samples.add(sample) except AttributeError as ae: print(ae) samples.shuffle() vectors = samples.toVectors() nn.train(vectors[0], vectors[1]) nn.save(weightsPath) sys.exit(0)
iterations = 10 interface = 'enp3s0' filter = 'tcp port %d' % port cap = pyshark.LiveCapture(interface = interface, bpf_filter = filter) nn = NN(Payload.vectorSize(), OutputMapper.vectorSize) nn.load(weightsPath) for i in range(iterations): try: cap.sniff(packet_count = 1) packet = cap.next() payload = Payload.fromPySharkCapture(packet) res = nn.predict(payload.toVector()) output = OutputMapper.fromVector(res) for protocolRes in output: print("%s: %f, " % protocolRes, end = '') print() except KeyboardInterrupt: break except Exception(ex): print("Error has ocurred: " + ex) sys.stdout.flush() continue i += 1 sys.stdout.flush()