一、附上源碼python
import socket import os import struct from ctypes import * # 本地監聽 host = "127.0.0.1" #IP頭定義 class IP(Structure): _fields_ = [ ("ihl", c_ubyte, 4), ("version", c_ubyte, 4), ("tos", c_ubyte), ("len", c_ushort), ("id", c_ushort), ("offset", c_ushort), ("ttl", c_ubyte), ("protocol_num", c_ubyte), ("sum", c_ushort), ("src", c_ulong), ("dst", c_ulong) ] def __new__(self, socket_buffer=None): return self.from_buffer_copy(socket_buffer) def __init__(self, socket_buffer=None): # 協議字段與協議名稱對應 self.protocol_map = {1:"ICMP", 6:"TCP", 17:"UDP"} # human readable IP addresses self.src_address = socket.inet_ntoa(struct.pack("<L",self.src)) self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst)) # human readable protocol try: self.protocol = self.protocol_map[self.protocol_num] except: self.protocol = str(self.protocol_num) # Windows下嗅探全部數據包,Linux下嗅探ICMP數據包 if os.name == "nt": socket_protocol = socket.IPPROTO_IP else: socket_protocol = socket.IPPROTO_ICMP sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol) sniffer.bind((host, 0)) # we want the IP headers included in the capture sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) # Windows下要打開混雜模式 if os.name == "nt": sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) try: while True: # 讀取數據包 raw_buffer = sniffer.recvfrom(65565)[0] # 讀取前20字節 ip_header = IP(raw_buffer[0:20]) #輸出協議和雙方通訊的IP地址 print "Protocol: %s %s -> %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address) except KeyboardInterrupt: if os.name == "nt": sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
二、源碼解讀網絡
ctype庫socket
python的底層大部分都是C/C++實現,python賦予了黑客相似於c語言同樣的底層操做能力,且不失動態語言的便捷;函數
python訪問C/C++的方式不少(ctypes,pybind11,cffi,swig),但ctypes是最方便的;.net
ctype庫提供了和C語言兼容的數據類型和函數來加載dll文件,所以在調用時不需對源文件作任何的修改。code
用ctype庫加載dll文件orm
import platform from ctypes import * if platform.system() == 'Windows': libc = cdll.LoadLibrary('msvcrt.dll') elif platform.system() == 'Linux': libc = cdll.LoadLibrary('libc.so.6') libc.printf('%s\n', 'here!') libc.printf('%S\n', u'there!')
struct模塊blog
能夠按照指定格式將Python數據和字節流進行轉換;能夠處理c語言中的結構體。ip
struct
的pack
函數把任意數據類型變成bytes:
內存
pack(fmt,v1,v2…) | 按照給定的格式(fmt),把數據轉換成字符串(字節流),並將該字符串返回. |
struct.pack("<L",self.src) <表示字節順序是little-endian,即低位字節排放在內存的低地址端,高位字節排放在內存的高地址端。 L表示4字節無符號長整型
fmt格式——對齊方式
fmt格式——格式符
IP結構
Socket模塊中一些IP轉換函數
socket.ntohl(x) // 相似於C語言的ntohl(x) 把32位正整數從網絡序轉換成主機字節序。 socket.ntohs(x) // 相似於C語言的ntohs(x) 把16位正整數從網絡序轉換成主機字節序。 socket.htonl(x) // 相似於C語言的htonl(x) 把32位正整數從主機字節序轉換成網絡序。 socket.htons(x) // 相似於C語言的htons(x) 把16位正整數從主機字節序轉換成網絡序。 socket.inet_aton(ip_string) // 依賴於inet_aton的C實現 轉換IPV4地址字符串(192.168.10.8)成爲32位打包的二進制格式(長度爲4個字節的二進制字符串),它不支持IPV6。inet_pton()支持IPV4/IPV6地址格式。 socket.inet_ntoa(packed_ip) 轉換32位打包的IPV4地址爲IP地址的標準點號分隔字符串表示。 socket.inet_pton(address_family,ip_string) 轉換IP地址字符串爲打包二進制格式。地址家族爲AF_INET和AF_INET6,它們分別表示IPV4和IPV6。 socket.inet_ntop(address_family,packed_ip) 轉換一個打包IP地址爲標準字符串表達式,例如:「5aef:2b::8」或「127.0.0.1」。 以上來自於https://blog.csdn.net/fan_hai_ping/article/details/8435140