forked from wisepythagoras/website-fingerprinting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
executable file
·63 lines (45 loc) · 1.41 KB
/
predict.py
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
#!/usr/bin/env python
import os.path
import sys
import json
import utils
from joblib import load
if len(sys.argv) == 1:
print("A pcap file is needed")
sys.exit(1)
elif os.path.exists('config.json') == False:
print("No configuration found")
sys.exit(1)
elif os.path.exists('./classifier-nb.dmp') == False:
print("No classifier dump found; train first")
sys.exit(1)
elif os.path.exists(sys.argv[1]) == False:
print("The input file was not found")
sys.exit(1)
# Read the configuration and start training.
with open('config.json') as fp:
print("* Parsing configuration")
# Load the configuration from the file.
config = json.load(fp)
# This is where all the labels are going to live.
base_labels = [None] * len(config['pcaps'])
# The base label starts from 1 and increments after that.
current_label = 1
for domain in config['pcaps']:
# Set the base label.
base_labels[current_label - 1] = domain
# Increment the label
current_label += 1
print("Loading the classifier...")
# Try to read the classifier.
classifier = load("./classifier-nb.dmp")
i = 0
right = 0
wrong = 0
# Read the pcap file.
stream = utils.read_pcap_file(sys.argv[1])
# Run the prediction.
prediction = classifier.predict([stream])
print(classifier.predict_proba([stream]))
# Print the results.
print(f"[{prediction[0]}] Prediction: {base_labels[prediction[0] - 1]}")