免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5561 | 回复: 1
打印 上一主题 下一主题

winpcap在python3中的使用,求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-13 11:19 |只看该作者 |倒序浏览
1、如题,下了个能在python3中使用的包包“winpcapy”,但网上有关这包包的使用教程太少,自带的列子看了,不知道怎么设置包过滤,pcap_compile(),pcap_setfilter(),如果只要UDP包,用哪个函数?

2、pcap_loop这个函数是设置嗅探的次数,能不能做到按键停止

大大们给点指点

英文水平不高,100个单词能猜出30个,读书时三级未过,后来学PYTHON硬着头皮搞了下单词,常用的可以认出来了。

其他编程软件不会,只是与朋友玩游戏,从别人的外挂源码直接开始学的,不懂得就查百度、谷歌。

以前在论坛中发的问题,已经解决了,在工作中将那些片段组合起来了,做到了截图、数据库访问、IE类浏览器窗口的遍历,现在想玩玩嗅探了!!!

论坛徽章:
0
2 [报告]
发表于 2011-03-14 20:05 |只看该作者
回复 1# 我不是老手
pcap_compile (pcap_t *p, struct bpf_program *fp, char *str, int optimize, bpf_u_int32 netmask)
pcap_setfilter (pcap_t *p, struct bpf_program *fp)
这上面两个中的struct bpf_program *fp怎么定义啊???有人懂吗
pcap_t *p是用例子中的adhandle,我的理解对不对???

#!/usr/bin/env python
#-------------------------------------------------------------------------------
# Name:        basic_dump.py
#
# Author:      Massimo Ciani
#
# Created:     01/09/2009
# Copyright:   (c) Massimo Ciani 2009
#
#-------------------------------------------------------------------------------


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自带的例子。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP