-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_baby.py
97 lines (79 loc) · 3.13 KB
/
test_baby.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
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
"""
Test script for baby DPI package.
"""
import time
import logging
from baby import PacketInspector, IPSEngine
from baby.ips import IPSRule
# Configure basic logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
def test_packet_inspector():
"""Test basic packet inspection functionality."""
print("\n=== Testing Packet Inspector ===")
# Choose a network interface (e.g., en0, eth0, wlan0, etc.)
# You can find your interface with:
# - On macOS/Linux: ifconfig or ip addr
# - On Windows: ipconfig
interface = "en0" # Change this to your network interface
# Create a packet inspector
inspector = PacketInspector(interface=interface)
# Add some protocol detection rules
inspector.add_rule("http", lambda pkt: "HTTP" in str(pkt) or "GET" in str(pkt) or "POST" in str(pkt))
inspector.add_rule("dns", lambda pkt: "DNS" in str(pkt) or "port 53" in str(pkt).lower())
inspector.add_rule("ssh", lambda pkt: "SSH" in str(pkt) or "port 22" in str(pkt).lower())
inspector.add_rule("tls", lambda pkt: "TLS" in str(pkt) or "port 443" in str(pkt).lower())
# Capture packets (30 packets)
print(f"Capturing 30 packets from {interface}...")
print("(Generate some network traffic now - try browsing a website)")
results = inspector.start(count=30)
# Print results
print("\nResults:")
for protocol, count in results.items():
print(f" {protocol}: {count} packets")
stats = inspector.get_statistics()
print(f"\nTotal packets analyzed: {stats['total_packets']}")
def test_ips_engine():
"""Test IPS engine functionality."""
print("\n=== Testing IPS Engine ===")
# Choose a network interface
interface = "en0" # Change this to your network interface
# Create a packet inspector
inspector = PacketInspector(interface=interface)
# Create an IPS engine
ips = IPSEngine(inspector)
# Add some IPS rules
ips.add_rule(IPSRule(
name="dns_traffic",
detection_func=lambda pkt: "DNS" in str(pkt),
action="alert",
severity=1,
description="DNS traffic detected (this is just a test rule)"
))
ips.add_rule(IPSRule(
name="http_traffic",
detection_func=lambda pkt: "HTTP" in str(pkt) or "GET" in str(pkt),
action="alert",
severity=2,
description="HTTP traffic detected (this is just a test rule)"
))
# Capture packets (15 packets)
print(f"Running IPS engine on {interface} for 15 packets...")
print("(Generate some network traffic now - try browsing a website)")
inspector.start(count=15)
# Print alerts
alerts = ips.get_alerts()
print(f"\nDetected {len(alerts)} alerts:")
for alert in alerts:
print(f" {alert['rule']} - Severity: {alert['severity']} - {alert['src_ip']} -> {alert['dst_ip']}")
if __name__ == "__main__":
try:
test_packet_inspector()
test_ips_engine()
print("\nAll tests completed successfully!")
except KeyboardInterrupt:
print("\nTests stopped by user")
except Exception as e:
print(f"\nError during tests: {e}")