python绑定到windivert驱动程序

pydivert的Python项目详细描述


Appveyor Build StatusCoverage StatusLatest VersionSupported Python versions

python绑定WinDivert,一个windows驱动程序,允许用户模式应用程序 捕获/修改/丢弃发送到/来自Windows网络堆栈的网络数据包。

要求

  • python 2.7或python 3.4+(32或64位)
  • Windows Vista/7/8/10或Windows Server 2008(32或64位)
  • 管理员权限

安装

您可以通过运行来安装pydivert

$ pip install pydivert

从pydivert 1.0.2开始,WinDivert与 pydivert和不需要单独安装。

windiver版本兼容性

PyDivertWinDivert
0.0.71.0.x or 1.1.x
1.0.x (API-compatible with 0.0.7)1.1.8 (bundled)
2.0.x1.1.8 (bundled)
2.1.x1.3 (bundled)

开始

pydivert包含两个主要类:pydivert.WinDivertpydivert.Packet。 首先,通常需要创建一个WinDivert对象来开始捕获网络流量,然后 调用.recv()接收捕获的第一个Packet。通过接收数据包,它们被捕获 不在Windows网络堆栈中,除非您采取操作,否则不会发送出去。 您可以通过调用.send(packet)重新注入数据包。 下面的例子打开一个windivert句柄,接收一个包,打印它,重新注入它, 然后退出:

importpydivert# Capture only TCP packets to port 80, i.e. HTTP requests.w=pydivert.WinDivert("tcp.DstPort == 80 and tcp.PayloadLength > 0")w.open()# packets will be captured from now onpacket=w.recv()# read a single packetprint(packet)w.send(packet)# re-inject the packet into the network stackw.close()# stop capturing packets

windivert不会处理与"tcp.DstPort == 80 and tcp.PayloadLength > 0"筛选器不匹配的数据包 像往常一样继续。过滤语言的语法在WinDivert documentation中描述。

python习惯用法

pydivert.WinDivertinstances可以用作context managers来捕获流量,并用作(无限的)iteratorsover 小包。以下代码与上面的示例等效:

importpydivertwithpydivert.WinDivert("tcp.DstPort == 80 and tcp.PayloadLength > 0")asw:forpacketinw:print(packet)w.send(packet)break

数据包修改

pydivert.Packet提供了多种属性,可用于访问和修改 数据包的头或有效负载。例如,您可以使用pydivert在端口1234上浏览网页:

importpydivertwithpydivert.WinDivert("tcp.DstPort == 1234 or tcp.SrcPort == 80")asw:forpacketinw:ifpacket.dst_port==1234:print(">")# packet to the serverpacket.dst_port=80ifpacket.src_port==80:print("<")# reply from the serverpacket.src_port=1234w.send(packet)

尝试在浏览器中打开http://example.com:1234/

windivert支持对各种tcp/udp/icmp属性的即时访问和修改。

>>>print(packet)Packet({'direction':<Direction.OUTBOUND:0>,'dst_addr':'93.184.216.34','dst_port':443,'icmpv4':None,'icmpv6':None,'interface':(23,0),'ipv4':{'src_addr':'192.168.86.169','dst_addr':'93.184.216.34','packet_len':81},'ipv6':None,'is_inbound':False,'is_loopback':False,'is_outbound':True,'payload':'\x17\x03\x03\x00$\x00\x00\x00\x00\x00\x00\x02\x05\x19q\xbd\xcfD\x8a\xe3...','raw':<memoryat0x028924E0>,'src_addr':'192.168.86.169','src_port':52387,'tcp':{'src_port':52387,'dst_port':443,'syn':False,'ack':True,'fin':False,'rst':False,'psh':True,'urg':False,'header_len':20,'payload':'\x17\x03\x03\x00$\x00\x00\x00\x00\x00\x00\x02\x05\x19q\xbd\xcfD\x8a\xe3...'},'udp':None})

卸载pydivert

您可以通过运行来卸载pydivert

$ pip uninstall pydivert

如果windivert驱动程序此时仍在运行,它将在下次重新启动时自行删除。

API参考文件

pydivert的api参考文档可以在here中找到。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用Eclipse Helios for Web项目   java指定行。作为编译器设置的分隔符?   osm映射中带有osmdroid的java标记   java Android ClassNotFoundException在路径:DexPathList上未找到类   java从j2me调用GWT方法   更新到java 8后的aspectj问题   如何仅从根元素java,com提取文本内容。滴虫软件。是的。html   安卓公司。谷歌。火基。数据库DatabaseException:未能转换java类型的值。将字符串改为长字符串   从java中的方法返回arraylist变量   java如果将引用重新分配给同步块内的锁对象,会是什么?   java下载。使用spring mvc的资源文件中的xlsx文件   java Eclipse提供了什么工具来运行Eclipse中未包含的程序?   spring boot Java泛型如何读取传递给泛型方法的类型上的字段或调用方法   Javacard中的javaecdsa签名