def runIDS(verbose=False): print("Starting IDS...") try: # Create log file if it does not exist. if not os.path.exists(r'logs\idslogs\ids.log'): file = open(os.path.join(r'logs\idslogs', 'ids.log'), 'w') file.close() logging.basicConfig(filename=os.path.join(r'logs\idslogs', 'ids.log'), level=logging.INFO) csvloader = CSVFlowLoader(os.path.join(CSVFILEPATH, csvfilename)) mlengine = MLEngine(MODEL_FILEPATH, DATACLEAN_PIPELINE_FILEPATH, DATAPREP_PIPELINE_FILEPATH) while True: for flowline in csvloader.tailFile(): csValsArray = [list(flowline.split(","))] csValsDF = pd.DataFrame(csValsArray, columns=columns) # Actual detection and printing results out in stdout. if mlengine.predict(csValsDF)[0] == PredictLabel.ANOMALY.value: print("ANOMALY: %s" % (parsePredictionDF(csValsDF))) logging.info("ANOMALY: %s" % (parsePredictionDF(csValsDF))) else: if verbose: print("BENIGN: %s" % (parsePredictionDF(csValsDF))) except KeyboardInterrupt: print("Exiting...") csvloader.destroy()
def test_predict(self): columns = constants.COLUMNS mlengine = MLEngine() # resultSet = [-1,1] # inputData = [[49188,4,2,0,12,0,6,6,6,0,0,0,0,0,14000,3500,4,0,4,4,4,4,0,4,4,0,0,0,0,0,0,0,0,0,40,0,500000,0,6,6,6,0,0,0,0,0,0,1,1,0,0,0,9,6,0,40,0,0,0,0,0,0,2,12,0,0,329,-1,1,20,0,0,0,0,0,0,0,0]] inputData = [[ '192.168.91.225-13.107.4.52-65026-80-6', '192.168.91.225', 65026, '13.107.4.52', 80, 6, '30/09/2019 07:34:49 PM', 19002, 2, 5, 111.0, 1009.0, 111.0, 0.0, 55.5, 78.48885271170677, 1009.0, 0.0, 201.8, 451.2385178594576, 58941.164087990735, 368.3822755499421, 3167.0, 3013.9242857112386, 7587.0, 135.0, 543.0, 543.0, 0.0, 543.0, 543.0, 19002.0, 4750.5, 2930.5069527301926, 8265.0, 1358.0, 0, 0, 0, 0, 40, 124, 105.25207872855488, 263.1301968213872, 0.0, 1009.0, 140.0, 353.2708390375375, 124800.28571428571, 0, 1, 0, 0, 0, 0, 0, 0, 2.0, 160.0, 55.5, 201.8, 0, 0, 0, 0, 0, 0, 2, 111, 5, 1009, -1, 115, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'No Label' ]] prep_inputData = pd.DataFrame(inputData, columns=columns) prediction = mlengine.predict(prep_inputData) # print(prediction) self.assertEquals(prediction, [-1]) # if __name__ == '__main__': # unittest.main()