usb-rea เป็นเครื่องมือโอเพนซอร์สภายใต้สัญญาอนุญาต MIT สำหรับคนที่มีอุปกรณ์ USB ซึ่งมีโปรแกรมสำหรับ Windows ให้มาเพียงอย่างเดียว โดยตัวเครื่องมือจะดักจับแพ็กเก็ตระหว่างที่คุณใช้งานแอปบน Windows แล้วสร้างไดรเวอร์ pyusb สำหรับ Linux ให้อัตโนมัติ
ย้อนไปหลายปีก่อน เราเคยต้องทำ Reverse Engineering กับ USB Video Capture Card เนื่องจากผู้ผลิตไม่ต้องการทำงานร่วมกับบริษัทที่เชี่ยวชาญด้านการผลิตจำนวนน้อย จึงต้องใช้ Wireshark ดักจับแพ็กเก็ต และเขียนโค้ดเพื่อส่ง URB (USB Request Block) ผ่าน libusb ด้วยตนเอง รวมถึงต้องควบคุมจังหวะเวลาอย่างระมัดระวัง ซึ่งเครื่องมืออย่าง usb-rea น่าจะช่วยลดความยุ่งยากได้มาก
การทำงานของ usb-rea คือดักจับข้อมูล USB ผ่าน USBPcap หรือ usbmon ขณะที่ผู้ใช้คลิกใช้งานโปรแกรมของผู้ผลิต พร้อมระบุว่าแต่ละการกระทำคืออะไร จากนั้นโปรแกรม Open Source จะวิเคราะห์โครงสร้างคำสั่ง เช่น ค่าคงที่ ตัวนับ ฟิลด์ข้อมูล ตาราง Lookup และ CRC พร้อมสร้างรายงานโปรโตคอลในรูปแบบ Markdown และ Linux Driver ที่พร้อมใช้งาน
Emir Mrkaljevic ผู้พัฒนา ได้ทดสอบเครื่องมือนี้กับชุดระบายความร้อนด้วยน้ำ NZXT Kraken X AIO และเมาส์ SteelSeries Rival 100 ซึ่งมีเครื่องมือ Linux รองรับอยู่แล้ว ได้แก่ liquidctl และ rivalcfg ทำให้สามารถตรวจสอบผลลัพธ์ได้ง่าย โดย usb-rea สามารถกู้คืนคำสั่งควบคุมพัดลมที่ liquidctl ใช้งานได้ตรงกันทุกไบต์ และไดรเวอร์ที่สร้างขึ้นยังสามารถส่งแพ็กเก็ตสำหรับค่าที่ไม่เคยพบระหว่างการดักจับได้อย่างถูกต้อง นอกจากนี้ยังรองรับการทำงานเป็น MCP Server เพื่อให้ AI Coding Assistant ช่วยวิเคราะห์ได้
ซอร์สโค้ดและเอกสารประกอบสามารถดูได้บน GitHub ใช้ pip ติดตั้งเวอร์ชันล่าสุด โดยผู้พัฒนาได้ทดลองติดตั้งบน Ubuntu 26.04:
|
1 2 3 |
python3 -m venv .venv source .venv/bin/activate pip install usb-rea |
ตัวเลือกการใช้งานที่มีให้มีดังนี้:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
usb-rea usage: usb-rea [-h] [--version] {capture,mark,transfers,infer,report,scaffold,dissector,hid,demo,testcard,replay,lookup,mcp} ... Reverse Engineer any USB gadget positional arguments: {capture,mark,transfers,infer,report,scaffold,dissector,hid,demo,testcard,replay,lookup,mcp} capture start/stop a usbmon/USBPcap capture mark mark the action you are about to do in the vendor app, or what it shows right now ('seen rpm 1180 temp 34', 'seen mode turbo') transfers list parsed transfers infer infer protocol (JSON) report write evidence report (markdown) scaffold write pyusb driver dissector write a Wireshark Lua dissector hid decode a HID report descriptor (from a capture or hex) demo generate a synthetic capture and run the whole pipeline testcard write the test card picture; show it on the screen while capturing so the report can tell RGB from BGR replay send one packet to a real device (requires --yes) lookup before capturing: is this VID:PID already documented by an open-source project? mcp run the MCP server (stdio) options: -h, --help show this help message and exit --version show program's version number and exit |
ถ้าต้องการสร้าง Linux Driver จากโปรแกรม Windows จะต้องดักจับแพ็กเก็ตด้วย Wireshark หรือ USBPcap บน Windows ก่อน จากนั้นจึงนำข้อมูลมาวิเคราะห์ด้วย usb-rea ซึ่งรองรับทั้ง Windows, Linux และ macOS โดยหน้า Capture Windows มีคำแนะนำการใช้งานทีละขั้นตอน
สำหรับผู้ที่ต้องการทดลองใช้งานอย่างรวดเร็ว สามารถเรียกใช้ Demo ผ่านอุปกรณ์พัดลมจำลอง หรืออุปกรณ์จำลองประเภทอื่นได้:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$ usb-rea demo --device fan captured 11 labelled actions, device 1234:abcd mode 100% 0x5a | counter (+1 per message) | 0x30 | lookup 1→0x11, 2→0x22, 3→0x47 | 0x00 | 0x00 | u16le crc16_modbus(bytes[0:6]) fan 100% 0x5a | counter (+1 per message) | 0x10 | u16be round(value * 1 + 0) | 0x00 | u16le crc16_modbus(bytes[0:6]) color 100% 0x5a | counter (+1 per message) | 0x20 | round(value * 1 + 0) | round(value[1] * 1 + 0) | round(value[2] * 1 + 0) | u16le crc16_modbus(bytes[0:6]) color only sent while mode 3 (3x; other commands ran in mode 1, 2): the driver sets it first handshake (3 messages sent once when the app opens the device): 1. 5a 00 01 00 00 00 0d 1d -> a5 01 ?? ?? ?? ?? ??, sent again 2x later (session reopened) 2. 5a 00 02 38 b4 e6 52 1e 1e -> a5 02 ?? ??, bytes 3-6 = token from step 1, sent again 2x later (session reopened) reply byte 2 = 01 (2x, most common): the steps below reply byte 2 = 02 (1x): 5a 00 04 01 00 00 5c 11 -> a5 04 01 ??, 5a 00 03 01 00 00 5d 65 -> a5 03 01 ?? 3. 5a 00 03 01 00 00 5d 65 -> a5 03 01 ??, sent again 1x later (session reopened) device replies: mode 6/6 0xa5 | = query byte 2 (0x30) | = query byte 1 | = query byte 3 | 0x00 | sum8(bytes[0:5]) fan 6/6 0xa5 | = query byte 2 (0x10) | = query byte 1 | u16le round(value * 1 + 0) | sum8(bytes[0:5]) color 6/6 0xa5 | = query byte 2 (0x20) | = query byte 1 | 0x00 | 0x00 | sum8(bytes[0:5]) poll 1 7/7 0xa5 | = query byte 2 (0x40) | u16le rpm = raw, seen 800..4011 | u8 temp = raw, seen 35..38 | mode: 0x11 = silent, 0x22 = balanced, 0x47 = turbo | sum8(bytes[0:6]) 24 of 24 'seen' observations matched a reply byte (mode, rpm, temp) wrote usb-rea-demo/report.md, usb-rea-demo/fan_driver.py and the Wireshark dissector usb-rea-demo/fan.lua |
ผลลัพธ์ที่ได้จะเป็น 3 ไฟล์:
- report.md – รายงานโปรโตคอลของ usb-rea ที่สร้างจากไฟล์ Demo PCAP
- fan_driver.py – ไดรเวอร์ Python ที่พัฒนาด้วย pyusb
- fan.lua – Wireshark Dissector สำหรับวิเคราะห์แพ็กเก็ต
อีกหนึ่งฟีเจอร์ที่น่าสนใจคือ usb-rea สามารถตรวจสอบ VID:PID ของอุปกรณ์ และแจ้งให้ทราบว่ามี Linux Driver รองรับอยู่แล้วหรือไม่:
|
1 2 3 4 5 |
$ usb-rea lookup 0416:7395 0416:7395 Lian Li GA II LCD (Winbond Electronics Corp.) protocol: documented - liquidctl: https://github.com/liquidctl/liquidctl/blob/main/liquidctl/driver/ga2_lcd.py note: A matching USB ID alone does not prove your unit speaks the same protocol; check the project's notes. |
VID 0x0416 และ PID 0x7395 เป็นของหน้าจอ USB LCD IPS ขนาด 2.88 นิ้ว ซึ่งรองรับโดย Liquidctl อยู่แล้ว จึงไม่จำเป็นต้องใช้ usb-rea ทำ Reverse Engineering เพิ่มเติม
แปลจากบทความ : usb-rea generates a Linux driver for your Windows-only USB gadget

บรรณาธิการข่าวและบทความภาษาไทย CNX Software ได้มีความสนใจในด้านเทคโนโลยี โดยเฉพาะ Smart Home และ IoT

