python3 pcap_winpcap在python3中的使用,求助

匿名用户

1级

2018-08-27 回答

from ctypes import *

from winpcapy import *

import time

import sys

import string

import platform

if platform.python_version()[0] == "3":

raw_input=input

#/* prototype of the packet handler */

#void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);

PHAND=CFUNCTYPE(None,POINTER(c_ubyte),POINTER(pcap_pkthdr),POINTER(c_ubyte))

## Callback function invoked by libpcap for every incoming packet

def _packet_handler(param,header,pkt_data):

## convert the timestamp to readable format

local_tv_sec = header.contents.ts.tv_sec

ltime=time.localtime(local_tv_sec);

timestr=time.strftime("%H:%M:%S", ltime)

print

print("%s,%.6d len:%d" % (timestr, header.contents.ts.tv_usec, header.contents.len))

packet_handler=PHAND(_packet_handler)

alldevs=POINTER(pcap_if_t)()

errbuf= create_string_buffer(PCAP_ERRBUF_SIZE)

## Retrieve the device list

if (pcap_findalldevs(byref(alldevs), errbuf) == -1):

print ("Error in pcap_findalldevs: %s\n" % errbuf.value)

sys.exit(1)

## Print the list

i=0

try:

d=alldevs.contents

except:

print ("Error in pcap_findalldevs: %s" % errbuf.value)

print ("Maybe you need admin privilege?\n")

sys.exit(1)

while d:

i=i+1

print("%d. %s" % (i, d.name))

if (d.description):

print (" (%s)\n" % (d.description))

else:

print (" (No description available)\n")

if d.next:

d=d.next.contents

else:

d=False

if (i==0):

print ("\nNo interfaces found! Make sure WinPcap is installed.\n")

sys.exit(-1)

print ("Enter the interface number (1-%d):" % (i))

inum= raw_input('--> ')

if inum in string.digits:

inum=int(inum)

else:

inum=0

if ((inum < 1) | (inum > i)):

print ("\nInterface number out of range.\n")

## Free the device list

pcap_freealldevs(alldevs)

sys.exit(-1)

## Jump to the selected adapter

d=alldevs

for i in range(0,inum-1):

d=d.contents.next

## Open the device

## Open the adapter

d=d.contents

adhandle = pcap_open_live(d.name,65536,1,1000,errbuf)

if (adhandle == None):

print("\nUnable to open the adapter. %s is not supported by Pcap-WinPcap\n" % d.contents.name)

## Free the device list

pcap_freealldevs(alldevs)

sys.exit(-1)

print("\nlistening on %s...\n" % (d.description))

## At this point, we don't need any more the device list. Free it

pcap_freealldevs(alldevs)

## start the capture (we take only 15 packets)

pcap_loop(adhandle, 15, packet_handler, None)

pcap_close(adhandle)

这是WINPCAPY自带的例子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于WinPcap的网络抓包Python程序的示例代码: ``` import socket import struct import sys import threading from ctypes import * from winpcapy import * # define constants PCAP_ERRBUF_SIZE = 256 # define structure for IP header 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, data=None): return self.from_buffer_copy(data) def __init__(self, data=None): # map protocol constants to their names 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) # define callback function for packet capture def packet_handler(header, data): # parse IP header ip_header = IP(data) # print out packet information print("Protocol: %s, Source: %s, Destination: %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)) def main(): # open network adapter for capturing errbuf = create_string_buffer(PCAP_ERRBUF_SIZE) adapter = pcap_open_live("eth0", 65536, 1, 1000, errbuf) if not adapter: print("Unable to open adapter: %s" % errbuf.value.decode("utf-8")) sys.exit(1) # start packet capture loop try: pcap_loop(adapter, -1, packet_handler, None) except KeyboardInterrupt: pass # close the adapter pcap_close(adapter) if __name__ == "__main__": main() ``` 这个程序使用WinPcap库来捕获网络数据包,并使用Python的ctypes库来定义IP头的结构体。程序打开一个名为“eth0”的网络适配器,然后进入无限循环以捕获数据包。当用户按下Ctrl + C时,程序将退出循环并关闭适配器。程序还包括一个包处理程序回调函数,它将在每个捕获的数据包上运行,并打印有关该包的一些信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值