标准8字节键盘数据格式
|
|
|
---|---|---|
字节1 |
|
|
字节2 |
|
0x00 ,部分设备可能用于扩展功能(如多媒体键)。 |
字节3-8 |
|
|
字节1:Modifier Keys(功能键位掩码)
|
|
|
|
---|---|---|---|
|
|
|
1 。 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
字节3-8:普通按键码(HID Usage ID)
|
|
|
---|---|---|
0x00 |
|
|
0x04 |
|
|
0x05 |
|
|
0x06 |
|
|
0x07 |
|
|
0x08 |
|
|
0x09 |
|
|
0x0A |
|
|
0x0B |
|
|
0x0C |
|
|
0x0D |
|
|
0x0E |
|
|
0x0F |
|
|
0x10 |
|
|
0x11 |
|
|
0x12 |
|
|
0x13 |
|
|
0x14 |
|
|
0x15 |
|
|
0x16 |
|
|
0x17 |
|
|
0x18 |
|
|
0x19 |
|
|
0x1A |
|
|
0x1B |
|
|
0x1C |
|
|
0x1D |
|
|
0x1E |
|
|
0x1F |
|
|
0x20 |
|
|
0x21 |
|
|
0x22 |
|
|
0x23 |
|
|
0x24 |
|
|
0x25 |
|
|
0x26 |
|
|
0x27 |
|
|
0x28 |
|
|
0x29 |
|
|
0x2A |
|
|
0x2B |
|
|
0x2C |
|
|
0x2D |
|
|
0x2E |
|
|
0x2F |
|
|
0x30 |
|
|
0x31 |
|
|
0x33 |
|
|
0x34 |
|
|
0x35 |
|
|
0x36 |
|
|
0x37 |
|
|
0x38 |
|
|
0x39 |
|
|
0x3A |
|
|
0x3B |
|
|
|
|
|
0x52 |
|
|
0x51 |
|
|
0x50 |
|
|
0x4F |
|
|
# 导入所需库 import os import subprocess import json # 定义tshark命令:从usb.pcapng文件中提取特定USB设备(2.2.1)的HID数据,输出为JSON格式 command = 'tshark -r usb.pcapng -Y usb.addr=="2.2.1" -T json -e usbhid.data > 1.json' # 使用subprocess执行命令 proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 等待命令执行完成 proc.communicate() # 加载生成的JSON文件 with open("1.json","r") as f: data=json.load(f) # data现在包含所有匹配的USB数据包 a2=[] for i in data: try: # 提取每个数据包的usbhid.data字段 a1=i['_source']['layers']['usbhid.data'][0] # [0]取第一个出现的数据 a2.append(a1) except: continue # 跳过没有usbhid.data字段的数据包 # 定义键盘映射表 normalKeys = { "04":"a", "05":"b", "06":"c", "07":"d", "08":"e", # ... 其他常规按键映射 ... "39":"<CAP>" # 大写锁定键 } shiftKeys = { "04":"A", "05":"B", "06":"C", "07":"D", "08":"E", # ... 其他Shift组合键映射 ... "39":"<CAP>" # 大写锁定键(实际应保持相同) } nums = [] for line in a2: if len(line)!=16: continue # 过滤长度不符合要求的HID数据 # 组合前两位(修饰键)和5-6位(实际按键代码) nums.append(line[0:2]+line[4:6]) output = [] for n in nums: if n[2:4] == "00" : # 00表示没有按键动作 continue if n[2:4] in normalKeys: if n[0:2]=="02": # 02表示Shift键按下 output.append(shiftKeys[n[2:4]]) else : output.append(normalKeys[n[2:4]]) else: output += '[unknown]' # 未识别的键值 print(output) # 输出原始解析结果 flag = 0 # 大写锁定状态标志 # 退格键处理 for i in range(len(output)): try: a = output.index('<DEL>') # 查找退格键位置 del output[a] # 删除DEL本身 del output[a - 1] # 删除前一个字符 except: pass # 没有DEL时跳过 # 大写锁定处理 for i in range(len(output)): try: if output[i] == "<CAP>": flag += 1 output.pop(i) # 移除CAP标记 if flag == 2: # 两次CAP恢复小写 flag = 0 if flag != 0: output[i] = output[i].upper() # 转换为大写 except: pass # 处理索引越界等情况 # 最终输出 print("".join(output))
moectf{Learned_a6ou7_USB_tr@ffic}
原文始发于微信公众号(信安一把索):流量分析 - USB流量(手搓+一把梭)
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论