Network Scanning using scapy module - Python

Network Scanning using scapy module - Python

The scapy module is a powerful tool in Python for packet manipulation and network scanning. Using scapy, you can craft, send, and capture network packets, making it a useful tool for network analysis, testing, and security assessments.

Here's a basic example of how you can use scapy to perform a simple ICMP ping (echo request) scan on a subnet to see which hosts are up:

  1. Installation:

    First, you need to install scapy:

    pip install scapy 
  2. ICMP Ping Scan:

    from scapy.all import ICMP, IP, sr1, srp, conf import time # To avoid verbosity in scapy output conf.verb = 0 # Target subnet subnet = "192.168.1.0/24" # Create the packet packet = IP(dst=subnet) / ICMP() # Send the packet and capture the response alive, _ = srp(packet, timeout=2, retry=1) # Print live hosts print("Live hosts:") for _, rcv in alive: print(rcv.src) 
  3. ARP Scan:

    An ARP scan can also be performed using scapy to determine which hosts are active within a local network:

    from scapy.all import ARP, Ether, srp, conf conf.verb = 0 # Target subnet subnet = "192.168.1.0/24" # Create ARP request packet to get the MAC address arp = ARP(pdst=subnet) ether = Ether(dst="ff:ff:ff:ff:ff:ff") packet = ether/arp # Send the packet and capture the response result, _ = srp(packet, timeout=2, retry=1) # Print live hosts print("Live hosts:") for sent, received in result: print(received.psrc) 

These scripts send packets to each IP address in the specified subnet and then display the IP addresses that responded, indicating that they are up.

Important: Be sure you have the appropriate permissions to scan the network you're targeting. Unauthorized scanning can be illegal and disruptive. Always obtain explicit permission before conducting any network scanning activities.


More Tags

mobile-webkit formbuilder event-handling white-box xcode4.5 classpath wc spring-boot-2 jquery-ui-draggable pygame2

More Programming Guides

Other Guides

More Programming Examples