2017-02-19 1 views
1

J'essaie actuellement d'analyser le trafic USB entrant en utilisant USBPcap + Python/dpkt, avec une souris USB optique comme périphérique d'entrée.Comment lire depuis stdin en utilisant dpkt.pcap.Reader?

Après le lancement de fichier batch contenant la commande

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

le code suivant fonctionne parfaitement:

# 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() 

sortie est:

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) 

Malheureusement, j'ai un problème avec Analyse LIVE des données capturées. Comment puis-je obtenir dpkt.pcap.Reader() pour travailler avec sys.stdin au lieu d'ouvrir ('foo.pcap')?

P.S. Je peux sûrement faire

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

comme le montre official mini-tutorial mais je voudrais effectuer le trafic USB en temps réel à l'aide sniffeur USB + Python.

P.P.S. Python/PyUSB + libusb-win32 fonctionne parfaitement mais j'ai besoin d'USBPcap! :)

Répondre

0

vous pouvez utiliser canal nommé(tampon FIFO) pour cette (https://en.wikipedia.org/wiki/Named_pipe#In_Windows) voir createNamedPipe in python et Windows named pipes in practice

vous créez le canal nommé, ouvert USBpcap à partir de votre programme python et redirigez la sortie le canal nommé vous venez de créer (How to Run an .exe File in Python, how to run an exe file with the arguments using python), alors vous lu à partir du tube nommé avec dpkt.pcap.Reader()

https://msdn.microsoft.com/de-de/library/windows/desktop/aa365590%28v=vs.85%29.aspx

https://msdn.microsoft.com/de-de/library/windows/desktop/aa365592%28v=vs.85%29.aspx