Examples of using raw sockets for network programming in Python

Source: Internet
Author: User
In the experiment, it is necessary to construct a separate HTTP data packet, and to send the packet using SOCK_STREAM, the full TCP interaction is required.

So you want to use the original socket programming, directly construct the packet, and send at the IP layer, that is, using SOCK_RAW for data transmission.

The advantage of using SOCK_RAW is that the packet can be fully modified to handle all packets on the IP layer and modify the fields without the limitation of UDP and TCP.

The following begins the construction of the HTTP packet,

The IP layer and TCP layer use Python's impacket Library, and the HTTP content is self-populated.

#!/usr/bin/env python #-------------------------------------------------------------------------------# name:raw_ http.py# purpose:construct a raw http GET packet## author:yangjun## created:08/02/2014# Copyright: (c) Yangjun 2014# Licence:
 
  #-------------------------------------------------------------------------------Import Sysimport Socketfrom Impacket Import Impactdecoder, Impactpacket def main (): If Len (SYS.ARGV) < 3:print "Use:%s
   
   
    "% sys.argv[0] print" use:%s
     
      
      
        "% sys.argv[0] sys.exit (1) elif len (sys.argv) = = 3:SRC = sys.argv[1] DST = sys.argv[2] cnt = 1 elif len (sys.argv) ==4: src = sys.argv[1] dst = sys.argv[2] cnt = sys.argv[3] else:print "Input error!" sys.exit (1) #print src, DST IP = Impactpac Ket. IP () ip.set_ip_src (SRC) ip.set_ip_dst (DST) # Create a new ICMP packet of type ECHO. ICMP = impactpacket.icmp () TCP = Impactpacket.tcp () tcp.set_th_sport (55968) tcp.set_th_dport (1) Tcp.set_th_ack (1) tcp.set_th_flags (0x18) Tcp.set_th_win (tcp.contains) impactpacket.data ("get/att/diylife/41264 /528 http/1.1\r\nhost:192.168.111.1\r\naccept-encoding:identity\r\n\r\n ")) Ip.contains (TCP) # Open a raw socket. Special permissions is usually required. s = socket.socket (socket.af_inet, socket. SOCK_RAW, Socket. IPPROTO_TCP) s.setsockopt (socket. IPPROTO_IP, Socket. IP_HDRINCL, 1) seq_id = 0 while CNT >= 1: # Calculate its checksum. seq_id = seq_id + 1 tcp.set_th_seq (seq_id) tcp.calculate_checksum () # Send it to the target host. S.SendTo (Ip.get_packet (), (dst,80)) cnt= cnt-1 if __name__ = = ' __main__ ': Main () 
      
     
    
   
  
 
  • Related Article

    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.