1. 程式人生 > 其它 >鍵盤流量分析UsbKeyboardDataHacker指令碼

鍵盤流量分析UsbKeyboardDataHacker指令碼

這是python3的指令碼
 1 #!/usr/bin/env python
 2 
 3 import sys
 4 import os
 5 
 6 DataFileName = "usb.dat"
 7 
 8 presses = []
 9 
10 normalKeys = {"04":"a", "05":"b", "06":"c", "07":"d", "08":"e", "09":"f", "0a":"g", "0b":"h", "0c":"i", "0d":"j", "0e":"k", "0f":"l", "10":"m", "11":"n", "12":"o", "13":"p", "14":"q", "15
":"r", "16":"s", "17":"t", "18":"u", "19":"v", "1a":"w", "1b":"x", "1c":"y", "1d":"z","1e":"1", "1f":"2", "20":"3", "21":"4", "22":"5", "23":"6","24":"7","25":"8","26":"9","27":"0","28":"<RET>","29":"<ESC>","2a":"<DEL>", "2b":"\t","2c":"<SPACE>","2d":"-","2e":"=","2f":"[","30":"
]","31":"\\","32":"<NON>","33":";","34":"'","35":"<GA>","36":",","37":".","38":"/","39":"<CAP>","3a":"<F1>","3b":"<F2>", "3c":"<F3>","3d":"<F4>","3e":"<F5>","3f":"<F6>","40":"<F7>","41":"<F8>","42":"<F9>","43":"<F10>","44":"<F11>
","45":"<F12>"} 11 12 shiftKeys = {"04":"A", "05":"B", "06":"C", "07":"D", "08":"E", "09":"F", "0a":"G", "0b":"H", "0c":"I", "0d":"J", "0e":"K", "0f":"L", "10":"M", "11":"N", "12":"O", "13":"P", "14":"Q", "15":"R", "16":"S", "17":"T", "18":"U", "19":"V", "1a":"W", "1b":"X", "1c":"Y", "1d":"Z","1e":"!", "1f":"@", "20":"#", "21":"$", "22":"%", "23":"^","24":"&","25":"*","26":"(","27":")","28":"<RET>","29":"<ESC>","2a":"<DEL>", "2b":"\t","2c":"<SPACE>","2d":"_","2e":"+","2f":"{","30":"}","31":"|","32":"<NON>","33":"\"","34":":","35":"<GA>","36":"<","37":">","38":"?","39":"<CAP>","3a":"<F1>","3b":"<F2>", "3c":"<F3>","3d":"<F4>","3e":"<F5>","3f":"<F6>","40":"<F7>","41":"<F8>","42":"<F9>","43":"<F10>","44":"<F11>","45":"<F12>"} 13 14 def main(): 15 # check argv 16 if len(sys.argv) != 2: 17 print("Usage : ") 18 print(" python UsbKeyboardHacker.py data.pcap") 19 print("Tips : ") 20 print(" To use this python script , you must install the tshark first.") 21 print(" You can use `sudo apt-get install tshark` to install it") 22 print("Author : ") 23 print(" WangYihang <[email protected]>") 24 print(" If you have any questions , please contact me by email.") 25 print(" Thank you for using.") 26 exit(1) 27 28 # get argv 29 pcapFilePath = sys.argv[1] 30 31 # get data of pcap 32 os.system("tshark -r %s -T fields -e usb.capdata 'usb.data_len == 8' > %s" % (pcapFilePath, DataFileName)) 33 34 # read data 35 with open(DataFileName, "r") as f: 36 for line in f: 37 presses.append(line[0:-1]) 38 # handle 39 result = "" 40 for press in presses: 41 if press == '': 42 continue 43 if ':' in press: 44 Bytes = press.split(":") 45 else: 46 Bytes = [press[i:i+2] for i in range(0, len(press), 2)] 47 if Bytes[0] == "00": 48 if Bytes[2] != "00" and normalKeys.get(Bytes[2]): 49 result += normalKeys[Bytes[2]] 50 elif int(Bytes[0],16) & 0b10 or int(Bytes[0],16) & 0b100000: # shift key is pressed. 51 if Bytes[2] != "00" and normalKeys.get(Bytes[2]): 52 result += shiftKeys[Bytes[2]] 53 else: 54 print("[-] Unknow Key : %s" % (Bytes[0])) 55 print("[+] Found : %s" % (result)) 56 57 # clean the temp data 58 os.system("rm ./%s" % (DataFileName)) 59 60 61 if __name__ == "__main__": 62 main()

執行這個指令碼需要下載 numpy 和 matplotlib

執行格式

python3 UsbKeyboardDataHacker.py  xxx.pcapng