-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (63 loc) · 2.37 KB
/
app.py
File metadata and controls
74 lines (63 loc) · 2.37 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
import sys
import json
from db import DataStorage
from helpers import create_dataset
from model.model import run_model
def main():
# Init model name
model_name = "gnn_model.pt"
# Determine whether to train or evaluate model (train on default)
is_train = True
use_real_data = False
log_gen_pkgs = False
i = 1
while i < len(sys.argv):
arg = sys.argv[i]
if arg == "--train" or arg == "-t":
is_train = True
elif arg == "--eval" or arg == "-e":
is_train = False
elif arg == "--use-real-data":
use_real_data = True
elif arg == "--log-generated-packages":
log_gen_pkgs = True
else:
print(f"Incorrect arguments passed: {arg}\nUsage: python app.py --samples <file.txt>")
sys.exit(1)
i += 1
# Load sample data
benign_json_data = None
malicious_json_data = None
if is_train:
with open("./samples/train_benign.json") as f:
benign_json_data = json.load(f)
with open("./samples/train_malicious.json") as f:
malicious_json_data = json.load(f)
else:
with open("./samples/test_benign.json") as f:
benign_json_data = json.load(f)
with open("./samples/test_malicious.json") as f:
malicious_json_data = json.load(f)
if use_real_data:
# Load data from Database
print("Connecting to database...")
ds = DataStorage()
if not ds.verify_connection():
print("Could not connect to the database")
exit(1)
# Get real world benign data
print("Fetching data from the db")
all_packages = ds.get_all_packages()
print(f"Retrieved {len(all_packages)} packages")
benign_json_data = all_packages
# Get synthetic data based on real world benign data
malicious_json_data = create_dataset(benign_json_data, len(benign_json_data))
if log_gen_pkgs:
with open("./samples/b_data.json", "w") as f:
json.dump(benign_json_data, f, indent=4)
with open("./samples/m_data.json", "w") as f:
json.dump(malicious_json_data, f, indent=4)
# Run model
run_model(is_train, model_name, benign_json_data, malicious_json_data)
if __name__ == "__main__":
main()