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)