In the experiment, you need to construct a separate HTTP data packet, and use SOCK_STREAM to send data packets requires complete TCP interaction. Therefore, we want to use the original socket for programming, construct data packets directly, and send data at the IP layer, that is, use SOCK_RAW for data transmission. The advantage of SOCK_RAW is that it can completely modify the data packets, process all data packets on the IP layer, and modify fields without the limitations of UDP and TCP. 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.
The 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:
#-------------------------------------------------------------------------------
Import sys
Import socket
From 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 = 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: