-
Notifications
You must be signed in to change notification settings - Fork 8
/
02_dos_blocker.py
41 lines (31 loc) · 1.1 KB
/
02_dos_blocker.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
import os
import sys
import time
from collections import defaultdict
from scapy.all import sniff, IP
THRESHOLD = 40
print(f"THRESHOLD: {THRESHOLD}")
def packet_callback(packet):
src_ip = packet[IP].src
packet_count[src_ip] += 1
current_time = time.time()
time_interval = current_time - start_time[0]
if time_interval >= 1:
for ip, count in packet_count.items():
packet_rate = count / time_interval
#print(f"IP: {ip}, Packet rate: {packet_rate}")
if packet_rate > THRESHOLD and ip not in blocked_ips:
print(f"Blocking IP: {ip}, packet rate: {packet_rate}")
os.system(f"iptables -A INPUT -s {ip} -j DROP")
blocked_ips.add(ip)
packet_count.clear()
start_time[0] = current_time
if __name__ == "__main__":
if os.geteuid() != 0:
print("This script requires root privileges.")
sys.exit(1)
packet_count = defaultdict(int)
start_time = [time.time()]
blocked_ips = set()
print("Monitoring network traffic...")
sniff(filter="ip", prn=packet_callback)