The following describes how to construct an HTTP packet,
The IP layer and TCP layer use the python Impacket library, and fill in the http content.
Copy codeThe Code is 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 "))
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 ()
Packet capture after running is as follows: