Python uses scapy for frame injection

Source: Internet
Author: User

1. Description

The use of scapy for the injection of Ethernet frames is relatively simple compared to raw_socket. Before you tell packet injection, learn about the Scapy forged Ethernet frames. Package format that corresponds to the Ethernet frame format and scapy.
  

2. Example 2.1 using Scapy to construct IP packets

To enter the Scapy environment, enter the following code:
  

>>>
>>> PKT = IP ()/icmp ()/"Hello"
>>>
>>>
>>> Pkt.show ()
###[IP]###
Version= 4
Ihl= None
tos= 0x0
len= None
Id= 1
flags=
frag= 0
Ttl= 64
proto= ICMP
chksum= None
src= 127.0.0.1
dst= 127.0.0.1
\options\
###[ICMP]###
Type= Echo-request
Code= 0
chksum= None
Id= 0x0
seq= 0x0
###[Raw]###
load= ' Hello '
  
  

The above code means to construct a PKT package that encapsulates the ICMP packet in the IP layer, and the payload's data bit "Hello". pkt.show()Displays the details of the package through a function.

2.2 Send Packet
    • sendp--sends packets on the second level. The correct NIC interface needs to be given.
    • send--sends packets on the third level. Routing is routed based on the local routing table.
      • Loop to send the same package.
      • The packet is sent a few seconds apart.
2.2.1 Send package on third level (Layer 3)

First, we send packet on the third level to do the testing. Before this, start another terminal, enter the following command to do a bit of monitoring traffic.
  tcpdump -i eth0 -XX -vvv icmp
Then run send(pkt) the command to send the packet, observing the result:

Monitoring results:
  
From the results, we sent an ICMP request packet, received an ICMP reply packet from Baidu, and can see the data we sent.

# # # #2.2.2 Third-level send package (Layer 3)
Use the refactoring packet and send it with the SENDP () function.
  sendp(Ether()/IP(dst= "www.baidu.com")/ICMP()/"XXX",iface="eth0")
Ether () constructs the Ethernet frame, and the third level is different, the second level is sent to specify the NIC interface, here I use eth0. Executes the code and successfully sends a packet. If you want to send loops, you can use the Loop option.
  sendp(Ether()/IP(dst= "www.baidu.com")/ICMP()/"XXX",iface="eth0",loop=1)
After the code executes, it will quickly loop through the packet and end with CTRL + C.
  
Use the Inter option if you want to have a certain time interval between sending packets two times.
  sendp(Ether()/IP(dst= "www.baidu.com")/ICMP()/"XXX",iface="eth0",loop=1,inter=1)
Indicates that a packet is sent every 1 seconds.

2.3 Sending and receiving packets on level 2 and Level 3
    • Level 3
      • --SR (). Returns the reply and no reply packages.
      • --SR1 (). Returns only the packets that were replied to or sent.
    • Level 2
      • --SRP ()
      • --SRP1 ()

Execute sr(IP(dst="www.baidu.com")/ICMP()/"XXX") , observe the results of the execution:

A packet was successfully sent, and 2 packets were received, one of which was a reply packet. This is where I want to focus my attention. Assign the received data to a custom variable and view it.
  
  
Using the SR1 () function, the effect:
  
  

Python uses scapy for frame injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.