scapy examples

The following lists scapy examples based on experiments performed on the below topology. The topology contains two PC’s , Ubuntu and Windows 7. Scapy is setup on Ubuntu system.

Example 1 – How to send a ping packet from Scapy on Ubuntu to Windows 7.

ip=IP() # Creates an IP header
ip.src=’192.168.1.25′ # Source address in the IP header is configured with IP address of ubuntu.
ip.dst =’ 192.168.1.100′ # Destination address in the IP header is configured with the IP address of Windows 7.
icmp=ICMP() # Creates an ICMP header
icmp.type=8 # Type value inserted in ICMP header as 8 for ping crafting
icmp.code=0 # Code value inserted in ICMP header as 0 for ping crafting.
send(ip/icmp) # Sending ping packet.

Take our Python Network programming Course for $12.99 on udemy

Example 2 – How to send a ping packet from Scapy on Ubuntu to Windows 7 with Random Source address.

ip=IP() # Creates an IP header
ip.src=RandIP() # Configures the source address in the IP header with a random IP address.
ip.dst =’ 192.168.1.100′ # Destination address in the IP header is configured with the IP address of Windows 7.
icmp=ICMP() # Creates an ICMP header
icmp.type=8 # Type value inserted in ICMP header as 8 for ping crafting
icmp.code=0 # Code value inserted in ICMP header as 0 for ping crafting.
send(ip/icmp) # Sending ping packet.

Take our Python Network programming Course for $12.99 on udemy

——————————————————————————————————————-
Building Network troubleshooting tools with Python and Scapy

——————————————————————————————————————-

Example 3 – How to create a TCP SYN to port 80 on Windows 7 from Scapy on Ubuntu with Random Source address.

tcp=TCP() # Creates a TCP header
tcp.dport=80 # Configures the destination port in the TCP header with port 80.
tcp.flags=’S’ # Configure the flag in the TCP header with the SYN bit.
ip=IP() # Creates an IP header
ip.src=’192.168.1.25′ # Source address in the IP header is configured with IP address of ubuntu.
ip.dst =’ 192.168.1.100′ # Destination address in the IP header is configured with the IP address of Windows 7.
send(ip/tcp) # Sending tcp packet.

——————————————————————————————————————-
Take our Python Network programming Course for $12.99 on udemy
——————————————————————————————————————-

Example 4 – How to print the TTL value of a received ping reply packet using Scapy

ip=IP() # Creates an IP header
ip.src=’192.168.1.25′ # Source address in the IP header is configured with IP address of ubuntu.
ip.dst =’ 192.168.1.100′ # Destination address in the IP header is configured with the IP address of Windows 7.
icmp=ICMP() # Creates an ICMP header
icmp.type=8 # Type value inserted in ICMP header as 8 for ping crafting
icmp.code=0 # Code value inserted in ICMP header as 0 for ping crafting.
p=sr1(ip/icmp) # Sends and receives the packet in the variable p
p.ttl # Displays the ttl value in the received IP header.

Example 4 – How to create an ARP request packet with Scapy on Ubuntu to Windows 7

ether=Ether() # Creates an ethernet header
ether.src=’00:e0:1c:3c:22:b4′ # Configures the source mac address in the ethernet header with the mac-address of ubuntu system
ether.dst=’FF:FF:FF:FF:FF:FF’ Configures the destination mac address in the ethernet header with broadcast for ARP request packet
arp=ARP() # Creates an ARP header
arp.op=1 # Configures the ARP type as 1
arp.hwsrc=’00:e0:1c:3c:22:b4′ # Configures the sender mac address in the ethernet header with the mac-address of ubuntu system
arp.psrc=’192.168.1.25′ # Configures the sender IP address in the ethernet header with the IP address of ubuntu system
arp.pdst=’192.168.1.100′ # Configures the target IP address in the ethernet header with the IP address of ubuntu system
arp.hwdst=’00:00:00:00:00:00′ # Configures the target mac address in the ethernet header as NULL
p=srp1(ether/arp) # Sends the packet at layer 2 using the command srp1, appending the ether and arp headers.

Take our Python Network programming Course for $12.99 on udemy
—————————————————————————————————————–
Building Network troubleshooting tools with Python and Scapy

——————————————————————————————————————-