Home >Backend Development >Python Tutorial >Python raw socket programming

Python raw socket programming

高洛峰
高洛峰Original
2016-10-18 10:01:461479browse

In the experiment, you need to construct a separate HTTP data message yourself, and using SOCK_STREAM to send data packets requires complete TCP interaction.

So I want to use raw sockets for programming, directly construct data packets, and send them at the IP layer, that is, use SOCK_RAW to send data.

The advantage of using SOCK_RAW is that the data packet can be completely modified, all data packets on the IP layer can be processed, and each field can be modified without being restricted by UDP and TCP.

Let’s start constructing the HTTP data packet.

The IP layer and TCP layer use python’s Impacket library, and the http content is filled in by yourself.

#!/usr/bin/env python
  
#-------------------------------------------------------------------------------
# Name:     raw_http.py
# Purpose:       construct a raw http get packet
#
# Licence:   PythonTab.com    
#-------------------------------------------------------------------------------
  
import sys
import socket
from impacket import ImpactDecoder, ImpactPacket
  
def main():
  
    if len(sys.argv) = 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()


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn