

用Python获取本机网卡IP数据包
source link: https://www.cnblogs.com/MikeZhang/archive/2012/08/30/pythonSniffer20120829.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

用Python获取本机网卡IP数据包
这几天用到了raw socket,用python写了些demo程序,这里记录下,也方便我以后查阅。
首先我们看一个简单的sniffer程序:
#! /usr/bin/python # code for linux import socket #s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP) s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP) while True: print s.recvfrom(65535)
这里直接用raw socket接收数据,直接print操作。这个就几行代码,也没什么好解释的了,不懂的google下。
得到IP数据包后,接下来的工作就是对IP头进行解析,在这之前,我们先看看RFC中是怎么定义的(RFC791 : http://www.ietf.org/rfc/rfc791.txt ):
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 即对应的图:

从RFC和上图中可以看到IP数据包头各个字段所占的位数,我们可以根据这些定义去解析IP数据包头,然后根据相应的策略处理数据。 这里给出一段用python实现的解析IP头的代码(呵呵,是demo中的代码,只解析了前20个字节,这里贴出来,欢迎拍砖……):
def decodeIpHeader(packet): mapRet = {} mapRet["version"] = (int(ord(packet[0])) & 0xF0)>>4 mapRet["headerLen"] = (int(ord(packet[0])) & 0x0F)<<2 mapRet["serviceType"] = hex(int(ord(packet[1]))) mapRet["totalLen"] = (int(ord(packet[2])<<8))+(int(ord(packet[3]))) mapRet["identification"] = (int( ord(packet[4])>>8 )) + (int( ord(packet[5]))) mapRet["id"] = int(ord(packet[6]) & 0xE0)>>5 mapRet["fragOff"] = int(ord(packet[6]) & 0x1F)<<8 + int(ord(packet[7])) mapRet["ttl"] = int(ord(packet[8])) mapRet["protocol"] = int(ord(packet[9])) mapRet["checkSum"] = int(ord(packet[10])<<8)+int(ord(packet[11])) mapRet["srcaddr"] = "%d.%d.%d.%d" % (int(ord(packet[12])),int(ord(packet[13])),int(ord(packet[14])), int(ord(packet[15]))) mapRet["dstaddr"] = "%d.%d.%d.%d" % (int(ord(packet[16])),int(ord(packet[17])),int(ord(packet[18])), int(ord(packet[19]))) return mapRet
调用代码:
proto = socket.getprotobyname('tcp') # only tcp sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, proto) while True: packet = sock.recvfrom(65535)[0] if len(packet) == 0: sck.close() else: #print str(packet) mapIpTmp = decodeIpHeader(packet) for k,v in mapIpTmp.items(): print k,"\t:\t",v print ""
Windows版本参考这里,有相应的demo,自己根据情况改写下啦。
好,就这些了,希望对你有帮助。
Recommend
-
8
编程获取本机IPv4及IPv6地址 本文来自依云's...
-
8
V2EX › Android GitMind 怎么获取的本机号码? Aquamarine · 17 小时 52 分钟前 · 664...
-
11
Java 获取本机的外网 IP发布于 45 分钟前通过 HTTP 访问第三方获取 IP 的服务接口获取本机的外网 IP,例如:考虑到这些第三方接口不一定 100% 稳定,例如可...
-
15
vue项目获取本机局域网IP地址 2021-08-30 19:35:26 有时会有在局域网下通过 IP 地址访问 vue 项目的需求,记录下获取本机 IP 的方法。
-
16
V2EX › 程序员 求推荐几个获取本机 IPv6 地址的 API 接口 LxnChan ·
-
2
Windows 7有线网卡自动...
-
5
Windows XP系统有线网卡...
-
5
Windows 8系统有线网卡自动获取...
-
3
LM Hash和NTML HashWindows操作系统中的密码由两部分加密组成,即LM Hash和NTML Hash。 LM Hash(LAN Manager Hash),本质为DES加密,密码不足14位用0补全。 自Server 2003之后,...
-
7
【笔记】Python3 获取网卡 MAC 地址 捕捉一只爱折腾的绯鞠 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK