DoS & DDoS Attack with Python

Introduction:

In this tutorial, we will learn about DoS and DDoS attacks with Python. Denial of Service (DoS) is a cyber-attack on a personal computer or website that aims to prevent service to the target user. Its purpose is to disrupt the organization's network operations by blocking user access. Denial of service is typically accomplished by redirecting requests to the target computer or resource in an attempt to overload the system and block some or all of the successfully legitimized requests.

On the other hand, a DDoS attack, which stands for "Distributed Denial of Service (DDoS) attack," is a type of cybercrime in which attackers flood a network with users to prevent users from accessing online services and websites. The motivations for performing DDoS and the types of individuals and organizations that carry out such cyber-attacks vary. Some attacks are carried out by individuals and hackers who want to shut down the company's servers to make a statement, use the network for entertainment, or express their disapproval.

With the explosion of e-commerce, web servers are vulnerable, and they can become a target for hackers. Hackers typically try two types of attacks, which are -

  1. DoS attack or Denial-of-Service attack
  2. DDoS attack or Distributed-Denial-of-Service attack

What is the DoS attack or Denial-of-Service attack?

A Denial-of-Service attack (DoS) is a hacker's attempt to make network resources unusable. It usually interrupts the host computer's Internet connection temporarily or permanently. These attacks are mainly aimed at services such as banks and credit card payment gateways, which are the main targets on the web.

Symptoms of the Denial-of-Service attack:

The symptoms of the Denial-of-Service attack are given below -

  1. It unusually provided slow network performance.
  2. A particular website is unavailable.
  3. Cannot access a website.
  4. The number of spam emails received has increased significantly.
  5. Prolonged denial of access to the web or any service on the network.
  6. Some websites cannot be accessed.

Types of the Denial-of-Service attack in Python:

DoS attacks can be made at the data link layer, network layer, or application layer. Now let us understand the implementations of different types of DoS attacks in Python -

1. Single IP single port DOS attack:

Send multiple packets to a web server using an IP and a port number. This is a low-level attack used to analyze the behavior of a web server. Its implementation in Python can be done with the help of Scapy. The Python script below will help implement a single IP single port DoS attack -

Program Code:

Here, we will give a program code of a single IP single port DOS attack in Python. The code is given below -

When finished, the above script will ask the following three things:

  1. Source IP address and target IP address.
  2. The number of source ports.
  3. It then sends several packets to the server to check its behavior.

2. Single IP Multiple port DOS attack:

Send multiple packets to the web using a single IP and multiple ports. Its implementation in Python can be done with the help of Scapy. The following Python script will help in using a single IP multi-port DoS attack -

Program Code:

Here, we will give a program code of a single IP multiple port DOS attack in Python. The code is given below -

3. Multiple IP Single port DOS attack:

Send large packets to the web using multiple IP addresses and single port numbers. With the help of Scapy, the implementation of a Multiple IP Single port DOS attack was done in Python. The following Python script uses the single IP multiple port DoS attack -

Program Code:

Here we will give a program code of multiple IP single port DOS attack in Python. The code is given below -

4. Multiple IP Multiple port DOS attack:

Use multiple IPs and multiple ports to send multiple packets to the web server. With the help of Scapy, the implementation of a Multiple IP Multiple port DOS attack was done in Python. The following Python script helps perform a multiple IP, multiple port DoS attack -

Program Code:

Here we will give a program code of multiple IP multiple port DOS attack in Python. The code is given below -

What is a DDoS attack or Distributed Denial of Service attack?

The Distributed Denial of Service or DDoS attack is an attempt to render an online service or website unusable by overloading it with traffic from multiple sources. While a Denial of Service (DoS) attack uses a single computer and Internet connection to send multiple packets to the source, a DDoS attack uses multiple computers and multiple servers, often distributed around the world, to create a botnet. A major DDoS attack can generate tens (or hundreds) of gigabits per second of traffic.

Steps of the Distributed Denial of Service (DDoS) attack in Python:

In reality, DDoS attacks are difficult to detect because you don't know whether the host sending the traffic is fake or real. The Python script given below will help in catching the Distributed Denial of Service or DDoS attacks.

Step 1:

Firstly, we need to import all the necessary libraries in Python, which are given below -

Step 2:

Now, we will create the same socket that we created in the previous section, which is given below -

Step 3:

Now we will use the empty dictionary, which is given below -

Step 4:

The below given line will open a text file containing the details of the DDoS attack in append mode -

Step 5:

After that, with the help of the following lines of code, the current time is written every time the program is run.

Step 6:

After that, with the help of the following lines of code, the current time is written every time the program is run.

Step 7:

Now, the following line will check if the IP exists in the dictionary. If the IP exists, then add 1.

Step 8:

The next line of code eliminates the redundancy.

After running the above script, we will get the results in the text. According to the script, if the IP has more than 20 hits, then the DDoS attack will be issued, as shown for the IP address.

Conclusion:

In this tutorial, we will learn about DoS and DDoS attacks with Python. A Denial of Service (DoS) attack is a hacker's attempt to make network resources unusable. On the other hand, a Distributed Denial of Service attack (DDoS) is an attempt to render an online service or website unusable by overloading it with traffic from multiple sources. The motivations for performing DDoS and the types of individuals and organizations that carry out such cyber-attacks vary. Here, we will learn some program code for the DoS attack and the steps of the DDoS attack in Python.