一、解题思路总览
capture.pcapng是 USBPcap 格式(pcapng linktype = 249),里面同时存在两个中断端点:0x81:手柄,20 字节 HID 报文;0x82:键盘接口,9 字节 HID 报文。
手柄流量先发生:用报文的游程还原每一次按键(按住时会有多个重复报文,不能当多次动作),再按截图里的 4×10 屏幕键盘重放,得到
profile.zip的口令。用口令解开 AES-256 加密的
profile.zip,得到layout.json。它不是标准 HID 键位表,而是一张自定义布局。用这张自定义布局重放
0x82上的键盘事件(只在按下沿产生字符,Shift 使字母大写),即可读出 Flag。
二、解析 USBPcap 流量
pcapng 中只有 SHB、IDB 和 Enhanced Packet Block(type = 6)。每个包的数据是 USBPcap 头 + HID payload。关键字段偏移:
只取 info == 1 的完成包,并按 pkt[headerLen:] 取数据。
控制传输(8 个包)里能看到设备/配置描述符和 HID Report Descriptor。0x82 的报告描述符为:
Report ID 1 | modifier(8bit) | reserved(8bit) | key1..key6也就是常见的 9 字节键盘报告:[report_id, modifier, reserved, key1, ..., key6]。
统计结果:
0x81 completion with data : 604 个(20 字节)
0x82 completion with data : 161 个(9 字节)三、从手柄输入还原 profile.zip 口令
3.1 手柄报文格式
0x81 的报文形如:
00 14 04 00 00 00 ...byte2:十字键位标志0x01= 上,0x02= 下,0x04= 左,0x08= 右
byte3:0x10= A(选择字符),0x20= B(删除最后一个字符)
注意:按下一个键时,设备会连续上报 5 个完全相同的报文,然后 3 个空闲报文,再进入下一次动作。截图也提示:
One action per press; release before next action.
因此必须按游程压缩:每个非空闲游程只算一次动作。共得到 68 次动作(14 次 A、2 次 B、52 次方向键)。若直接把 604 个报文当动作,或简单合并相邻相同状态,都会得到错误字符串。
3.2 屏幕键盘
截图中的键盘为 4 行 × 10 列,光标初始在 1,边缘环绕:
1 2 3 4 5 6 7 8 9 0
q w e r t y u i o p
a s d f g h j k l -
z x c v b n m _ { }按动作序列重放(A 取当前字符,B 删除末字符,方向键移动光标),关键选择轨迹为:
p 4 d x(B删除) _ r o u t x(B删除) e _ 7 3最终输入缓冲区:
p4d_route_73这就是 profile.zip 的口令。
四、解开加密的维护配置
profile.zip 内两个文件均为 WinZip AES 加密。本地头的 AES 扩展字段为:
01 99 07 00 02 00 41 45 03 08 00
↑ AE-2 ↑ "AE" ↑ strength=3 (AES-256) ↑ 实际压缩方法=8 (Deflate)本环境的 Python zipfile 不带 AES 支持,正确口令时会抛出NotImplementedError: That compression method is not supported(而不是密码错误),
因此改用 pyzipper:
import pyzipper
with pyzipper.AESZipFile("profile.zip") as z:
z.setpassword(b"p4d_route_73")
print(z.read("service-note.txt").decode())
layout = z.read("layout.json")解出的 service-note.txt:
The controller unlocked the service profile.
The second interface uses a custom host-side layout.
Use layout.json to replay its keyboard events.layout.json 给出自定义映射(节选):
{
"profile": "service-keyboard",
"interface": 1,
"endpoint": "0x82",
"report_id": 1,
"report_format": "[report_id, modifier, reserved, key1, ...]",
"usage_to_character": {
"0x11": "p", "0x28": "c", "0x1e": "t", "0x1a": "f",
"0x0b": "{", "0x1b": "}", "0x2a": "_",
"0x16": "g", "0x1c": "4", "0x14": "m", "0x10": "3", ...
},
"shift_rule": "Left Shift (0x02) or Right Shift (0x20) uppercases ASCII letters.",
"event_rule": "Emit on key-down transitions. Repeated held reports are not new characters."
}关键点:这不是标准 USB HID 键位表。例如 0x11 在标准布局是 n,这里却是 p;0x2a 不是退格而是 _;0x0b / 0x1b 分别是 { / }。
五、重放键盘接口,读出 Flag
0x82 上每个按键同样保持 3 个报告再松开 2 个报告。按 event_rule 只在按下沿取一次:
events = []
for endpoint, report in packets:
if endpoint != 0x82 or report[3] == 0:
continue
state = (report[1], report[3]) # (modifier, keycode)
if not events or events[-1] != state:
events.append(state)
flag = ""
for modifier, code in events:
ch = usage[code]
if modifier & 0x22 and ch.isalpha(): # 左/右 Shift
ch = ch.upper()
flag += ch前 5 个事件是 (0x02, 0x11) (0x02, 0x28) (0x02, 0x1e) (0x02, 0x1a) (0x02, 0x0b),
映射并大写后正好是 PCTF{,验证了思路。全部 32 个事件拼出:
PCTF{g4m3p4d_h1d_pr0f1le_sw1tch}六、完整脚本
# -*- coding: utf-8 -*-
"""Gamepad_Service_v2 完整解题脚本。
用法:把 capture.pcapng、onscreen_keyboard.png、profile.zip 放在本脚本同目录,
然后执行 `python solve_full.py`。
依赖:pyzipper(用于读取 WinZip AES-256 加密的 profile.zip)。
"""
import json
import struct
from pathlib import Path
import pyzipper
HERE = Path(__file__).resolve().parent
PCAP = HERE / "capture.pcapng"
PROFILE = HERE / "profile.zip"
# 屏幕键盘:4 行 x 10 列,光标初始在 '1',边缘环绕
GRID = [
"1234567890",
"qwertyuiop",
"asdfghjkl-",
"zxcvbnm_{}",
]
FLAT = "".join(GRID)
def read_usbpcap(path):
"""读取 pcapng(USBPcap, linktype=249),返回完成态的 HID 报文列表。"""
data = path.read_bytes()
pos, packets = 0, []
while pos + 12 <= len(data):
block_type, block_len = struct.unpack_from("<II", data, pos)
if block_len < 12 or pos + block_len > len(data):
break
body = data[pos + 8 : pos + block_len - 4]
if block_type == 6: # Enhanced Packet Block
caplen = struct.unpack_from("<I", body, 12)[0]
pkt = body[20 : 20 + caplen]
header_len = struct.unpack_from("<H", pkt, 0)[0]
info = pkt[16] # 0=submission, 1=completion
endpoint = pkt[21]
payload = pkt[header_len:]
if info == 1 and payload:
packets.append((endpoint, payload))
pos += block_len
return packets
def gamepad_actions(packets):
"""把 0x81 上的手柄报文按游程压缩为一次一次的按键动作。"""
states = [p for ep, p in packets if ep == 0x81]
runs = []
for st in states:
state = (st[2], st[3])
if not runs or runs[-1] != state:
runs.append(state)
return [st for st in runs if st != (0, 0)]
def replay_onscreen_keyboard(actions):
"""重放屏幕键盘输入,得到 profile.zip 的口令。"""
# 0x81 报文的 byte2 为十字键位标志,byte3 的 0x10/0x20 为 A/B
dpad = {0x01: "U", 0x02: "D", 0x04: "L", 0x08: "R"}
idx, out = 0, ""
for b2, b3 in actions:
if b3 == 0x10: # A:选择当前字符
out += FLAT[idx]
elif b3 == 0x20: # B:删除末尾字符
out = out[:-1]
elif b2: # 十字键:移动光标(环绕)
r, c = divmod(idx, 10)
direction = dpad[b2]
if direction == "U":
r = (r - 1) % 4
elif direction == "D":
r = (r + 1) % 4
elif direction == "L":
c = (c - 1) % 10
else:
c = (c + 1) % 10
idx = r * 10 + c
return out
def open_profile(password):
"""用 pyzipper 解开 AES-256 加密的 profile.zip。"""
with pyzipper.AESZipFile(PROFILE) as zf:
zf.setpassword(password.encode())
return {name: zf.read(name) for name in zf.namelist()}
def replay_keyboard_interface(packets, layout):
"""用 layout.json 的自定义映射重放 0x82 键盘接口事件。"""
usage = {int(k, 16): v for k, v in layout["usage_to_character"].items()}
events = []
for ep, p in packets:
if ep != 0x82:
continue
code = p[3]
if code == 0:
continue
state = (p[1], code) # (modifier, keycode)
if not events or events[-1] != state: # 只在按下沿产生字符
events.append(state)
out = ""
for modifier, code in events:
ch = usage.get(code, "?")
if modifier & 0x22 and ch.isalpha(): # 左右 Shift
ch = ch.upper()
out += ch
return out
def main():
packets = read_usbpcap(PCAP)
actions = gamepad_actions(packets)
password = replay_onscreen_keyboard(actions)
print(f"[+] 手柄动作数:{len(actions)}")
print(f"[+] profile.zip 口令:{password}")
files = open_profile(password)
print("[+] service-note.txt:")
print(files["service-note.txt"].decode())
layout = json.loads(files["layout.json"].decode())
flag = replay_keyboard_interface(packets, layout)
print(f"[+] FLAG: {flag}")
if __name__ == "__main__":
main()