Python raw socket programming sample sharing _python

Source: Internet
Author: User
Tags socket

The following starts to construct the HTTP packet,

The IP layer and the TCP layer use the Python impacket Library, and the HTTP content is filled out by itself.

Copy Code code as follows:

#!/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: <your licence>
#-------------------------------------------------------------------------------

Import Sys
Import socket
From Impacket import Impactdecoder, Impactpacket

def main ():

If Len (SYS.ARGV) < 3:
print ' use:%s <src ip> <dst ip> '% sys.argv[0]
print ' use:%s <src ip> <dst ip> <cnt> '% 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 = Impactpacket.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 (80)
TCP.SET_TH_SEQ (1)
Tcp.set_th_ack (1)
Tcp.set_th_flags (0x18)
Tcp.set_th_win (64)

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 are 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 ()


after running, grab the bag as follows:

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.