|
| 1 | +import dpkt |
| 2 | +import socket |
| 3 | +import threading |
| 4 | +import time |
| 5 | +import discord |
| 6 | + |
| 7 | +# Set the IP address and port number to listen on |
| 8 | +HOST = '0.0.0.0' |
| 9 | +PORT = 8080 |
| 10 | + |
| 11 | +# Set the threshold for detecting a DDoS attack |
| 12 | +THRESHOLD = 1000 |
| 13 | + |
| 14 | +# Set the Discord webhook URL for sending notifications |
| 15 | +DISCORD_WEBHOOK_URL = 'https://discord.com/api/webhooks/your-webhook-url-here' |
| 16 | + |
| 17 | +# Create a UDP socket and bind it to the specified address and port |
| 18 | +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 19 | +sock.bind((HOST, PORT)) |
| 20 | + |
| 21 | +# Define a function to parse incoming network traffic |
| 22 | +def parse_packet(packet): |
| 23 | + try: |
| 24 | + eth = dpkt.ethernet.Ethernet(packet) |
| 25 | + ip = eth.data |
| 26 | + src_ip = socket.inet_ntoa(ip.src) |
| 27 | + dst_ip = socket.inet_ntoa(ip.dst) |
| 28 | + if isinstance(ip.data, dpkt.tcp.TCP): |
| 29 | + src_port = ip.data.sport |
| 30 | + dst_port = ip.data.dport |
| 31 | + if dst_port == PORT: |
| 32 | + return src_ip |
| 33 | + except Exception as e: |
| 34 | + print(e) |
| 35 | + return None |
| 36 | + |
| 37 | +# Define a function to send notifications to Discord |
| 38 | +def send_discord_notification(msg): |
| 39 | + client = discord.Webhook.from_url(DISCORD_WEBHOOK_URL, adapter=discord.RequestsWebhookAdapter()) |
| 40 | + client.send(msg) |
| 41 | + |
| 42 | +# Define a function to monitor incoming network traffic for DDoS attacks |
| 43 | +def monitor_traffic(): |
| 44 | + traffic = {} |
| 45 | + while True: |
| 46 | + packet, addr = sock.recvfrom(65535) |
| 47 | + src_ip = parse_packet(packet) |
| 48 | + if src_ip is not None: |
| 49 | + if src_ip in traffic: |
| 50 | + traffic[src_ip] += 1 |
| 51 | + else: |
| 52 | + traffic[src_ip] = 1 |
| 53 | + if traffic[src_ip] >= THRESHOLD: |
| 54 | + msg = f"DDoS attack detected from {src_ip}!" |
| 55 | + send_discord_notification(msg) |
| 56 | + print(msg) |
| 57 | + traffic = {} |
| 58 | + |
| 59 | +# Start the monitoring thread |
| 60 | +monitor_thread = threading.Thread(target=monitor_traffic) |
| 61 | +monitor_thread.start() |
| 62 | + |
| 63 | +# Main loop |
| 64 | +while True: |
| 65 | + time.sleep(1) |
0 commit comments