2017-02-19 208 views
1

我目前正在嘗試使用USBPcap + Python/dpkt分析傳入USB流量,並以光學USB鼠標作爲示例輸入設備。如何使用dpkt.pcap.Reader從標準輸入讀取?

推出包含命令批處理文件後

USBPcapCMD.exe -d \\.\USBPcap7 -o - | pipetest.py 

下面的代碼工作完美:

# pipetest.py 
# sniffing for USB-mouse activities 
import sys 
import dpkt, struct 

try: 
    f = open('c:\\users\\user\\downloads\\test.pcap','wb') 
    while True: 
     inpt = sys.stdin.read(34)  # package length 
     f.write(inpt) 
except KeyboardInterrupt: 
    f.close() 

f = open('c:\\users\\user\\downloads\\test.pcap','rb') 
pcap = dpkt.pcap.Reader(f) 
print 

for ts, buf in pcap: 
    data = struct.unpack("b"*7, buf[-7:]) # 7-byte leftover with mouse info 
    print data 

f.close() 

輸出是:

34 
34 
34 
34 
34 
34 
34 
34 
34 
34 
34 
34 
^C 
(3, 4, 0, 0, 0, 0, 0) <---| 
(3, 0, 0, 0, 0, 0, 0)  | 
(3, 4, 0, 0, 0, 0, 0) <---| 
(3, 0, 0, 0, 0, 0, 0)  |------ Four clicks with mouse wheel 
(3, 4, 0, 0, 0, 0, 0) <---| 
(3, 0, 0, 0, 0, 0, 0)  | 
(3, 4, 0, 0, 0, 0, 0) <---| 
(0, 0, 0, 9, 0, 1, 7) 

不幸的是,我有一個問題捕獲數據的實時分析。如何讓dpkt.pcap.Reader()與sys.stdin而不是打開('foo.pcap')一起使用?

P.S.我一定能行

USBPcapCMD.exe -d \\.\USBPcap2 -o - | "C:\Program Files\Wireshark\Wireshark.exe" -k -i - 

official mini-tutorial顯示,但我想用USB嗅探器+ Python來進行實時USB流量。

P.P.S. Python/PyUSB + libusb-win32完美工作,但我確實需要USBPcap! :)

回答

0