-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport-api.py
More file actions
116 lines (98 loc) · 4.04 KB
/
Copy pathport-api.py
File metadata and controls
116 lines (98 loc) · 4.04 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sys
import time
import json
import requests
from flask import Flask, jsonify, request
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
data_file = "data.json" # json file storing the ports
def process_ports():
try:
with open(data_file, "r") as file: # reading the json file
data = json.load(file) # store as data
ports = data.get("ports", []) # ports is the array of ports in data
guess = predict_ports(ports) # call ai method to guess function
data["predicted_use_case"] = guess
with open(data_file, "w") as file: # open json file and write the ai prediction to it
json.dump(data, file, indent=2)
print("Prediction saved")
except Exception as e:
print(f"Error processing ports: {e}")
training = [ # X AXIS - is the category of service
# Y AXIS - is the port (listed in columns)
# 1 = this service is associated with this port
# 0 = not
[1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, "Web browsing/hosting"],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "SSH remote access"],
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, "Web server with database"],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "Database access"],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "Remote desktop (RDP)"],
[0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, "Email server (SMTP, IMAP, POP3)"],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, "Secure email (SMTPS, IMAPS, POP3S)"],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "Local development server"],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, "Enterprise hosting with full admin access"],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "DNS server"],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "FTP file transfer"],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "VPN access (OpenVPN)"],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "Minecraft server hosting"],
]
columns = [
"Port 22", "Port 80", "Port 443", "Port 3306", "Port 3389",
"Port 3000", "Port 25", "Port 110", "Port 143", "Port 465", "Port 993", "Use Case"
]
df = pd.DataFrame(training, columns=columns) # makes data set
# gives every row a unique numerical value
label_mapping = {label: idx for idx, label in enumerate(df["Use Case"].unique())}
reverse_label_mapping = {v: k for k, v in label_mapping.items()}
df["Use Case"] = df["Use Case"].map(label_mapping)
X = df.drop(columns=["Use Case"])
y = df["Use Case"]
clf = DecisionTreeClassifier()
clf.fit(X, y) # actually makes the decision tree and stores the x and y coordinates
# function for the ai prediction
def predict_ports(ports):
port_mapping = {
22: "Port 22",
80: "Port 80",
443: "Port 443",
3306: "Port 3306",
3389: "Port 3389",
3000: "Port 3000",
25: "Port 25",
110: "Port 110",
143: "Port 143",
465: "Port 465",
993: "Port 993",
}
# this converts it to a dataset instead of a dictionary, containing 1s and 0s
input_vector = {col: 0 for col in X.columns}
for port in ports:
if port in port_mapping:
input_vector[port_mapping[port]] = 1
input_df = pd.DataFrame([input_vector])
prediction = clf.predict(input_df)[0]
return reverse_label_mapping.get(prediction, "Unknown") # unknown for some adverse scenario
@app.route("/save-ports", methods=["POST"])
def save_ports():
try:
data = request.json
with open(data_file, "w") as file:
json.dump(data, file, indent=2)
process_ports() # Generate prediction immediately after saving
return jsonify({"message": "Ports saved successfully"}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/get-ports", methods=["GET"])
def get_ports():
try:
with open(data_file, "r") as file:
data = json.load(file)
return jsonify(data), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(debug=True, port=5012)