Python parsing packets sent to this computer (parsing packets) _python

Source: Internet
Author: User
Tags bind sleep unpack

tcp.py

Copy Code code as follows:

#-*-coding:cp936-*-
Import socket
from struct import *
From time import Ctime,sleep
From OS import system

System (' title TCP Sniffer ')
System (' Color 05 ')

# The Public network interface
HOST = Socket.gethostbyname (Socket.gethostname ())

# Create a raw socket and bind it to the public interface
s = socket.socket (socket.af_inet, socket. SOCK_RAW, Socket. IPPROTO_IP)
S.bind ((HOST, 0))

# Include IP Headers
S.setsockopt (socket. IPPROTO_IP, Socket. IP_HDRINCL, 1)

# Receive All Packages
#s. IOCTL (socket. Sio_rcvall, Socket. RCVALL_ON)

# Receive a Package
While 1==1:
Packet = S.recvfrom (65565)
Packet = packet[0]

Ip_header = packet[0:20]
Iph = Unpack ('! Bbhhhbbh4s4s ', Ip_header)
Version = Iph[0] >> 4 #Version
IHL = iph[0] * 0xF #IHL
Iph_length = IHL * 4 #Total length
TTL = iph[5]
protocol = iph[6]
S_ADDR = Socket.inet_ntoa (Iph[8])
D_ADDR = Socket.inet_ntoa (iph[9])
Print CTime ()
print ' version: ' + str (Version) + ' IHL: ' + str (IHL) + ' total Length: ' +str (iph_length) + ' TTL: ' +str (TTL) + ' Pro Tocol: ' + str (protocol) + ' Source address: ' + str (S_ADDR) + ' Destination address: ' + str (D_ADDR)

    If protocol = 6:
        tcp_header = packet[20:40]
& nbsp;       tcph = Unpack ('! Hhllbbhhh ', Tcp_header)
        source_port = tcph[0]
         dest_port = tcph[1]
        sequence = tcph[2]
        acknowledgement = tcph[3]
         doff_reserved = tcph[4]
        tcph_length = doff_reserved >> 4
        print ' Source port: ' + str (source_port) + ' Dest port: ' + str (dest_port + ' Sequence number: ' + str (Sequence) + ' acknowledgement: ' + str (acknowledgement) + ' TCP header length: ' + str (t Cph_length)

data = Packet[40:len (packet)]
print ' data: ' + data


# Disabled Promiscuous mode
S.IOCTL (socket. Sio_rcvall, Socket. Rcvall_off)

udp.py

Copy Code code as follows:

#-*-coding:cp936-*-
Import socket
from struct import *
From time import Ctime,sleep
From OS import system

System (' title UDP sniffer ')
System (' Color 05 ')
# The Public network interface
HOST = Socket.gethostbyname (Socket.gethostname ())

# Create a raw socket and bind it to the public interface
s = socket.socket (socket.af_inet, socket. SOCK_RAW, Socket. IPPROTO_IP)
S.bind ((HOST, 0))

# Include IP Headers
S.setsockopt (socket. IPPROTO_IP, Socket. IP_HDRINCL, 1)

# Receive All Packages
#s. IOCTL (socket. Sio_rcvall, Socket. RCVALL_ON)

# Receive a Package
While 1==1:
Packet = S.recvfrom (65565)
Packet = packet[0]

Ip_header = packet[0:20]
Iph = Unpack ('! Bbhhhbbh4s4s ', Ip_header)
Version = Iph[0] >> 4 #Version
IHL = iph[0] * 0xF #IHL
Iph_length = IHL * 4 #Total length
TTL = iph[5]
protocol = iph[6]
S_ADDR = Socket.inet_ntoa (Iph[8])
D_ADDR = Socket.inet_ntoa (iph[9])

If protocol = 17:
Udp_header = packet[20:28]
UDPH = Unpack ('! HHHH ', Udp_header)
Source_port = udph[0]
Dest_port = udph[1]
Length = udph[2]
Checksum = Udph[3]
data = Packet[28:len (packet)]

Print CTime ()
print ' version: ' + str (Version) + ' IHL: ' + str (IHL) + ' total Length: ' +str (iph_length) + ' TTL: ' +str (TTL) + ' Pro Tocol: ' + str (protocol) + ' Source address: ' + str (S_ADDR) + ' Destination address: ' + str (D_ADDR)
print ' Source port: ' + str (source_port) + ' Dest Port: ' + str (dest_port) + ' length: ' + str (Length) + ' Checksum: ' + STR (CHECKSUM)
print ' data: ' + data

# Disabled Promiscuous mode
S.IOCTL (socket. Sio_rcvall, Socket. Rcvall_off)

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.