-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifyFlows.py
More file actions
74 lines (54 loc) · 1.87 KB
/
classifyFlows.py
File metadata and controls
74 lines (54 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
Classifies flows from a pcap trace
'''
import sys
import os
from sklearn import tree
from sklearn import ensemble
import pickle
import flow
import capture_packets
import classifier # helper functions to create feature vectors from flow strings
from scapy.all import * # import scapy stuff to read in the pcap file
import tempfile as tp
def main():
argv = sys.argv
argc = len(argv)
if argc > 1:
pcap_path = argv[1]
else:
print("Need to specify pcap trace!")
sys.exit(1)
model_path = "classifier.randomforest" # set the default path to the random forest model
clf = load_model(model_path)
flowstring = capture_packets.read_from_file(pcap_path)
# use a temp file because we already have a method that will work with a file rather than a large string
tempfile = tp.TemporaryFile(mode='w+')
tempfile.write(flowstring)
tempfile.seek(0) # go back to start of file
fstring = classifier.find_next_flow(tempfile)
features = []
out_strings = []
while not (fstring==-1):
out_strings.append(fstring)
feature = classifier.extract_features(classifier.parse_flow(fstring))
features.append(feature)
fstring = classifier.find_next_flow(tempfile)
preds = clf.predict(features)
# predictions = clf.predict(test_set)
pred_strings = classifier.map_predictions_to_strings(preds)
for index in range(len(pred_strings)):
sl = len(out_strings[index])-1
out_strings[index] = out_strings[index][:sl] + (" <"+pred_strings[index]+">\n")
for string in out_strings:
print(string)
# close
tempfile.close() # close/get rid of temp file
'''
Loads a model from a given filepath. returns the clf
'''
def load_model(filepath):
with open(filepath, "rb") as file:
return pickle.loads(file.read()) # read the file and convert to clf
if __name__=="__main__":
main()