-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.py
25 lines (22 loc) · 882 Bytes
/
tcp.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
from scapy.all import sniff
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import joblib
MODEL_PATH = "rf_model.pkl"
model = joblib.load(MODEL_PATH)
def process_packet(packet):
if packet.haslayer("IP"):
features = {
'src_ip': packet['IP'].src,
'dst_ip': packet['IP'].dst,
'protocol': packet['IP'].proto,
'length': len(packet),
}
df = pd.DataFrame([features])
df = pd.get_dummies(df)
if not set(df.columns).issubset(model.feature_names_in_):
print("Unexpected features in live data. Model may not work correctly.")
prediction = model.predict(df)[0]
print(f"Packet: {features}, Prediction: {'Intrusion' if prediction == 1 else 'Normal'}")
print("Starting live packet capture...")
sniff(prn=process_packet, filter="ip", store=False)