/writeups

Reversing a Razer keyboard's command protocol

10 min read#reverse-engineering#keyboards#usb

I own a Razer Huntsman V3 Pro Mini (USB 1532:02B0) and wanted to know what the vendor software actually says to it. This is the command protocol as recovered from the Windows middleware and the firmware image inside the official updater, cross-checked in an emulator and against the board itself.

The firmware image I disassembled is 2.01.00; during this pass my keyboard was still running 2.00. Because I was mapping an undocumented protocol across two revisions before touching the firmware or bootloader, I kept every live probe in this phase strictly read-only and verified write paths offline in Unicorn instead.

The frame

Commands travel as HID feature reports on interface 3. Windows describes the collection as usage page 0x0C, usage 1, with no input or output reports and a 91-byte feature report. That’s one report-ID byte (zero) and then a 90-byte message. The host sends a request with HidD_SetFeature and picks up the reply with HidD_GetFeature, so even a “read” means sending a report out.

This is the 90-byte layout my parser enforces:

offset size field notes
0 1 status 0 in a request; 1 = busy, 2 = success in a reply
1 1 transaction id 1F
2 3 must be zero I check them, I have not named them
5 1 size argument bytes, at most 80
6 1 class
7 1 command
8 80 arguments
88 1 checksum XOR of bytes 2 to 87
89 1 reserved zero

The checksum skips both the status and the transaction id:

def checksum(raw):
    value = 0
    for byte in raw[2:88]:
        value ^= byte
    return value

Interface 3 and transaction id 1F come from earlier work on this keyboard. I didn’t rederive them here. The rest of the layout is checked against the firmware dispatcher.

A firmware version query is class 00, command 81, size 2:

00 1F 00 00 00 02 00 81  00 ... 00  83 00
                                    ^^ 02 ^ 00 ^ 81

On my keyboard (firmware 2.00) the reply carried arguments 02 00.

In the setter/getter pairs I traced, the getter is the setter’s command id with the top bit set: 02:1B/02:9B, 02:2A/02:AA, 05:08/05:88.

The dispatcher

The updater carries the main MCU image as Intel HEX records. My extractor validates every record checksum, rejects overlaps and holes, and gives me a 131,072-byte image based at 0x20000000. There’s also a 37,408-byte secondary payload, which stays opaque.

Incoming reports land in a control buffer at 0x04001CF8. The class dispatcher at 0x2001284C uses a byte-offset jump table with sixteen entries. Nine of them share one target. Classes 00, 02, 03, 05, 06, 0A and 0F get their own. In this image, class 0A goes straight to the response epilogue.

The class 02 handler at 0x2000FAC8 subtracts 2 from the command and indexes a table of 16-bit offsets, with a bound that makes AA the highest command. I wasn’t going to read 169 table slots by eye, so I pin the instruction bytes and walk the table:

check(0xFAE8, bytes.fromhex("df e8 10 f0"))  # TBH, command - 2
check(0xFADE, bytes.fromhex("a8 28"))        # max index A8 => cmd AA
for c in range(2, 0xAB):
    relative = struct.unpack_from("<H", raw, 0xFAEC + 2 * (c - 2))[0]
    target = BASE + 0xFAEC + 2 * relative
    if target != 0x2000FCEE:                 # shared default target
        commands[f"02:{c:02X}"] = hex(target)

That gives 35 class 02 entries. The script refuses any image whose SHA-256 differs from the one I analysed, because every offset is revision-specific. Notes from the full-size keyboard don’t quite fit: the Mini adds 02:1B, 02:9B, 02:2A, and 02:AA, and uses a different profile record size. 02:2A stores one byte and 02:AA reads it back, though I haven’t pinned down what it controls.

Mod Tap: following one setting to the wire

Mod Tap gives four keys an arrow key as their tap output. The managed configurator builds one record per key, each with a 250 ms hold time:

firmware key id key tap HID code tap output
3E Right Alt 50 left arrow
40 Right Ctrl 4F right arrow
81 Menu (non-Japanese layouts) 51 down arrow
39 Right Shift 52 up arrow

Japanese layouts swap key 3B in for 81. The records get serialised to JSON and handed to the 32-bit native middleware. There, an adapter splits them into four parallel arrays (key id, enable, key code, hold time) and calls a virtual method on its storage protocol object. That method writes command word 0x021B, computes the argument length as 1 + 4*n, interleaves the arrays, and rejects n > 19.

class 02, command 1B
profile {keyId enable tapHID holdTime} * n      n <= 19

profile 0, four arrow taps enabled:
00  3E 01 50 FA  40 01 4F FA  81 01 51 FA  39 01 52 FA

To check I’d read that right, I ran the method itself. The script maps the DLL image into Unicorn as plain memory, builds a fake object and vtable, stubs memset and the stack-cookie check, and puts a sentinel address in the vtable slot the builder uses for transport:

elif address == 0x40000010:                  # fake transport slot
    pointer = get(stack + 4)
    count = u.mem_read(pointer + 2, 1)[0]
    captured.append(bytes(u.mem_read(pointer, 3 + count)))
    u.reg_write(UC_X86_REG_EAX, 0)
    ret(12)

Windows never loads the DLL and nothing reaches USB. I ran four cases: enabled, disabled, profile 6, and the 19-entry boundary. Each one produces a single transport call whose bytes equal a packet I’d assembled independently. For the example above the captured descriptor is 1B 02 11 followed by the seventeen argument bytes. That descriptor is internal to the middleware. It isn’t a USB report: a lower layer adds the 90-byte frame, transaction id and checksum.

The firmware agrees with that layout. 02:1B writes four-byte entries into a 0x120-byte table at offset 0x116B of the profile record, preserving the stored third byte when the incoming one is zero and defaulting a zero hold time to FA (250 ms). 02:9B reads those four-byte entries back.

On hardware (firmware 2.00), sending 02:9B with size 17, profile 0, and the four key ids returned:

00 3E 01 50 FA 40 01 4F FA 81 01 51 FA 39 01 52 FA

That matches the middleware builder’s defaults byte for byte. I didn’t send the 02:1B setter during this read-only pass, so live Mod Tap writes remain for later.

The lighting matrix is 5 x 15

Custom RGB is class 0F, command 03, which calls a frame writer at 0x20019FB4. Three instructions in it fix the geometry:

0x20019FB4  0e 2b   last column <= 14
0x20019FBE  04 29   row <= 4
0x20019FD6  2d 20   row stride 45 bytes (15 RGB triples)

Five rows of fifteen. That’s not the full-size board’s 6 x 22 matrix, and it’s not this keyboard’s own 5 x 14 sensor matrix either.

A second source agrees. The vendor software ships per-locale LED layout files for this PID. Each starts with the magic LEDD and a 28-byte header, then 14-byte records of {kind: u16, rawId: u32, row: u32, column: u32}. All 17 layouts declare 5 x 15, with 58 to 60 records depending on locale.

The 5 x 15 grid is settled; the remaining piece is mapping each raw LED id to its physical key, which takes a per-key colour sweep on hardware (and leaves 20 trailing bytes in each LEDD file unparsed).

Profiles: position is not id

Class 05 is the profile system, with fourteen dispatch entries. These five getters are both emulated on 2.01.00 and read on my 2.00 keyboard:

command returns
05:8A capacity (6)
05:80 number of occupied records
05:81 count, then the stored ids
05:84 active stored id
05:88 metadata, by offset

The firmware keeps six stored records at a stride of 0x12D3 (4,819) bytes. Each starts with a little-endian 16-bit id, a presence byte, three bytes I haven’t decoded, a 16-bit metadata length and the metadata blob. Key mappings, actuation and Rapid Trigger rows come further in.

Here’s the trap. A record’s position in the table and its stored id are different numbers, and the commands don’t agree on which one they take. 05:88 searches occupied records for an id. 02:9E and 02:99, the mapping and actuation getters, address positions directly.

To test this I run the firmware’s own getters in an ARM emulator against synthetic storage with holes: ids 1, 4 and 6 at positions 1, 3 and 6, with position 3 active. 05:81 returns 03 01 04 06 and 05:84 returns 04, not 3. The harness writes a 90-byte report into the control buffer, starts at the class dispatcher, refuses any command outside a getter allowlist, and fails if the synthetic storage changes.

Reading metadata back

05:88 takes an id and a big-endian 16-bit offset. The request’s size field reserves 5 + n bytes for n bytes of data. Here’s the emulated firmware being asked for the last 21 bytes of a synthetic 96-byte blob for id 4:

request   size 1A, class 05, cmd 88, args 04 00 4B          checksum D8
reply     04  00 4B  00 60  4B 4C 4D ... 5F
          id  offset total  data

05:85 looks like a shortcut. It isn’t: in emulation it declares 96 bytes and returns 75 with a success status.

The native serialiser lays the blob out like this (confirmed by replaying the builder in Unicorn):

blob offset size field
0 16 profile UUID, .NET Guid byte order
16 100 name, UTF-8
116 128 owner, UTF-8
244 6 unknown

On hardware (2.00), reading every enumerated id twice returned identical 250-byte copies for ids 1 to 6, with the inventory unchanged before and after. Ids 3 to 6 had byte-identical metadata, so a UUID can’t uniquely key an onboard record.

One detail from the disassembly shaped the reader: the vendor’s high-level enumeration isn’t a pure read. For any record other than id 1 with an all-zero UUID, a branch can emit 05:03, which triggers a firmware helper that rebuilds a preset. Issuing 05:88 directly sidesteps that completely.

Keeping the live probes safe

The offline test suite never opens a HID device—it runs against synthetic fixtures and recorded packet vectors. The live probe tool is separate and locked down for read-only discovery:

  • A fixed allowlist: the five inventory getters plus bounded 05:88 reads for the ids the inventory just returned. No arbitrary class, command, or payload.
  • Every reply is validated on report id, length, transaction id, class, command, zero bytes, size bound, checksum, and status. Any mismatch aborts the session.
  • At most four polls on busy status, never a resend.
  • Every read runs twice and must match, with a timeout that discards partial output.
  • The tool refuses to run while Razer’s software or services are active, and rechecks before every command.
  • Reset, erase, calibration, factory mode, and bootloader commands stay out of the probe tool altogether.

The single-owner rule came from getting it wrong: an early probe on 2.00 got a 02:9C reply back to its 00:81 query because another tool of mine was polling key depth at the same time.

Hardware also surfaced two Windows quirks the fixtures couldn’t catch. First, opening the HID collection with an exclusive handle fails with a sharing violation before a single report goes out; tracing the middleware showed it opens the device with desired access 0 and shared read/write. Second, one session validated 55 metadata chunks and then failed a checksum/size check. In the disassembly, the middleware’s protocol object sleeps 10 ms before each send and each receive; adding that pacing got the next session cleanly through all 60 metadata queries.

What’s left open

A few pieces of the protocol are still unmapped:

  • Whether firmware 2.00 and 2.01.00 lay out every profile record identically.
  • The LED id namespace, the six-byte metadata tail, and the bytes in stored profile rows that standard getters omit.
  • Live write verification for the setters traced here, including Mod Tap (02:1B).

With the framing, class-02 jump table, and profile layout mapped out, the next step was bringing the board up to 2.01.00 and moving from reading the firmware to modifying it.