-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (63 loc) · 2.75 KB
/
main.py
File metadata and controls
76 lines (63 loc) · 2.75 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
import time
import threading
from packet_sniffer import PacketSniffer
from feature_extractor import FeatureExtractor
from ids_engine import IDSEngine
def main():
"""
Main function to initialize and run the AI-powered Intrusion Detection System.
"""
print("Starting AI-Powered Intrusion Detection System...")
# --- Configuration ---
# For a real scenario, use the actual network interface.
# On Linux/macOS, it might be 'eth0', 'en0', etc.
# On Windows, you can find it with 'ipconfig' or 'getmac'.
# For this example, we might not be able to sniff without root/admin privileges,
# so the sniffer might be limited.
network_interface = "default" # Let scapy try to find a default interface
# --- Initialization ---
try:
engine = IDSEngine()
print("IDS Engine initialized.")
# Train the model with some initial data. In a real-world scenario,
# you would load a pre-trained model.
print("Training initial detection model...")
engine.train_model()
print("Model training complete.")
sniffer = PacketSniffer(interface=network_interface)
print(f"Packet Sniffer started on interface: {sniffer.get_interface_name()}")
except Exception as e:
print(f"Error during initialization: {e}")
print("Please ensure you have the necessary permissions to run the sniffer and that all dependencies are installed.")
return
# --- Main Loop ---
try:
print("\n--- Network Monitoring Started ---")
while True:
# 1. Sniff a batch of packets
packets = sniffer.sniff_packets(count=10)
if not packets:
time.sleep(1)
continue
# 2. Extract features from packets
features = FeatureExtractor.extract_features(packets)
if not features:
continue
# 3. Predict threats
predictions = engine.predict(features)
# 4. Report threats
for i, prediction in enumerate(predictions):
if prediction == 1: # '1' typically represents an anomaly/attack
print(f"ALERT: Potential threat detected in packet batch!")
# In a real system, you would log packet details here.
# Example: print(packets[i].summary())
time.sleep(2) # Pause between batches
except KeyboardInterrupt:
print("\n--- Stopping Intrusion Detection System ---")
sniffer.stop_sniffing()
print("System stopped.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
sniffer.stop_sniffing()
if __name__ == "__main__":
main()