蓝牙键盘流量包分析和还原

蓝牙键盘的按键还原与普通USB键盘有些许区别,但是在网上只找到了一个很好用的USB键盘流量包解析脚本,没有找到蓝牙键盘的。随后稍微研究了一下,加入蓝牙键盘解析的支持。

脚本已放到Github:https://github.com/BaiHLiu/Bluetooth-UsbKeyboardDataHacker

工具说明

  • 抓包工具:Wireshark 4.2.0
  • 系统环境:Windows 11
  • 键盘型号:罗技 K585
  • 脚本运行:Ubuntu18.04

流量分析

普通USB键盘数据包的数据长度为 8 个字节,击键信息集中在第 3 个字节。但对蓝牙ATT流量进行分析后发现,击键信息应该是在第 2 个字节。
iShot_2023-11-21_10.58.41.png
iShot_2023-11-21_10.58.49.png

脚本改造

主要需要修改tshark过滤字段,以及下面解析字节时的位置。
image-1.png

测试后发现所有键盘、Shift键等均可正确解析。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
import os

DataFileName = "usb.dat"

presses = []

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>"}

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>"}

def main():
# check argv
if len(sys.argv) != 2:
print("Usage : ")
print(" python UsbKeyboardHacker.py data.pcap")
print("Tips : ")
print(" To use this python script , you must install the tshark first.")
print(" You can use `sudo apt-get install tshark` to install it")
print("Author : ")
print(" WangYihang <wangyihanger@gmail.com>")
print(" If you have any questions , please contact me by email.")
print(" Thank you for using.")
exit(1)

# get argv
pcapFilePath = sys.argv[1]

# get data of pcap
os.system("tshark -r %s -Y 'bthci_acl && btatt' -T fields -e btatt.value > %s" % (pcapFilePath, DataFileName))

# read data
with open(DataFileName, "r") as f:
for line in f:
presses.append(line[0:-1])
# handle
result = ""
for press in presses:
if press == '':
continue
if ':' in press:
Bytes = press.split(":")
else:
Bytes = [press[i:i+2] for i in range(0, len(press), 2)]
if Bytes[0] == "00":
if Bytes[1] != "00" and normalKeys.get(Bytes[1]):
result += normalKeys[Bytes[1]]
elif int(Bytes[0],16) & 0b10 or int(Bytes[0],16) & 0b100000: # shift key is pressed.
if Bytes[1] != "00" and normalKeys.get(Bytes[1]):
result += shiftKeys[Bytes[1]]
else:
print("[-] Unknow Key : %s" % (Bytes[0]))
print("[+] Found : %s" % (result))

# clean the temp data
os.system("rm ./%s" % (DataFileName))


if __name__ == "__main__":
main()
作者

Catop

发布于

2023-11-21

更新于

2024-11-10

许可协议

评论