Skip to content

field_note · · 8 min

Two bytes to the right: decoding an attendance terminal on site, with read requests only

A terminal that answered hello and its record count, then refused every download with code 3. How three read-only requests, varied one argument at a time from a laptop on the office LAN, found a field layout two bytes off, a four-byte block ending, 20-byte records, and a year packed as year minus 1900, confirmed by one test punch.

This is part 4 of the fifth-protocol series. The terminal answered the office PC on TCP port 5005, said hello, said how many records it held, and refused every download with "code 3". The connector's error said to set the terminal's communication password to 0. The terminal had no password. This post is how the real cause was found, from a laptop on the same LAN, without sending the terminal a single request that could change it.

The protocol, as far as it was public

The terminal belongs to a family built on a widely licensed attendance SDK. Its TCP protocol is not documented by the vendor, but it has been described in public by someone who captured it between the vendor's software and a terminal. The connector's reader for it was written from that write-up and one live reply:

  • A request is 16 bytes. 55 AA, the device number, a command byte, ten argument bytes, and a two-byte packet number.
  • A reply starts with 10 bytes. AA 55, the device number, 01 for yes or 00 for no, a four-byte value, and two more bytes.
  • A read that returns data follows the head with 55 AA, the data, and a closing 00 00.
  • Four commands matter: hello (80), record count (B4), a block of the log (A4), and a name for a staff number (C7).

The reader only has constants for those four. Anything else is refused before it reaches the wire. That is the first safety rule, and it is in the code, not in my head.

The first theory was wrong, and harmless

The logs showed one successful early read: hello and count on one connection, both answered. Every later read opened with the count and was refused. So the first theory was that this terminal wants a hello at the start of every connection. The fix was twelve lines and a test machine that refuses until it is greeted. It passed. It did not fix the terminal. Part 5 is about why the test passed.

Probing, read-only

My laptop was on the office LAN, so I could talk to the terminal directly. The probe was a throwaway Go program with one rule at its top:

terminal
func req(c net.Conn, cmd byte, a [10]byte) (ok bool, val uint32, err error) {
	if cmd != 0x80 && cmd != 0xB4 && cmd != 0xA4 {
		panic("read requests only")
	}
	// 55 AA | device 1 | cmd | 10 args | packet no.
	...
}

Then I varied one thing at a time, on a fresh connection each try, and wrote down the reply head:

figure 2 · the conversation, step by step

PC → terminal · 16 bytes

55 AA 01 80 00 00 00 00 00 00 00 00 00 00 00 00

terminal → PC

AA 55 01 01 00 00 00 00 00 00

Byte 3 of the reply is 01: yes. The terminal speaks this protocol and has no communication password.

Every request here is a read. The terminal accepted the first two and refused the third, which is where every read in the field had died. Shrinking one value made it say yes and send nothing; moving the fields two bytes right made it send the log.

What the probing showed:

Block request argumentsReply
our layout, size 1,024no, value 3
our layout, size 500 or moreno, value 3
our layout, size 256 or 300yes, value 0, then silence
all zeroyes, value 0, then silence
a 1 or 12 or 100 in any single argument positionyes, value 0

"Yes, zero bytes" for small values and "no" for large ones is the shape of a range check on the wrong field. The terminal was reading our block size as a block number. A block number past its last block is code 3; a small one is fine but asks for zero bytes, because the size it was really looking for was zero.

Two bytes to the right

The write-up shows the download request as a whole packet in hex:

terminal
55aa01a400000000xxxx000000040100

Read against the 16-byte layout, the record count (xxxx) is at packet bytes 8–9, the block number at 10–11, and the size, 0004 (1,024, little-endian), at 12–13. In the reader's argument array, which starts at packet byte 4, that is 4–5, 6–7 and 8–9. The reader had them at 2–3, 4–5 and 6–7. Two bytes early, because the first reading of that hex counted from the wrong place.

figure 3 · the bytes, field by field
0
1
2
3
4
5
6
7
8
9
  • 0–1 (zero)
  • 2–3 record count
  • 4–5 block no.
  • 6–7 block size
  • 8–9 (zero)

Our size field landed where the terminal looks for a block number, and the terminal found no size at all. So it answered: yes, zero bytes; or, for a 'block number' past its last block, code 3.

The request and reply are the protocol's public shape. The block request and the record are where our reader was wrong. Flip the switches to see the first guess against what the terminal actually wanted and stored.

With the fields moved, block 0 came back: 1,040 bytes.

The four-byte ending

The write-up says a data reply ends with 00 00. 1,040 bytes is 10 of head, 2 of 55 AA, 1,024 of block, and four, not two. The extra two vary from block to block; they are a checksum of some kind. One sample is not enough to say which, so the reader does not enforce it. What it must not do is leave two bytes unread: the next reply would start two bytes early and look like garbage.

The reader now waits half a second after the first block for two more bytes. If they come, every later block reads four. If not, two, as on the terminals the write-up described. The choice is made once per connection.

20 bytes a record, not 12

With the whole log on the laptop, the records did not line up at 12 bytes. They lined up at 20, with the staff number first, a seconds byte at 7, and the packed minute at bytes 8–11. Byte 15's low half is 1 or 2: in or out.

The reader does not assume either size. It decodes block 0 both ways and keeps the size that gives more real punches: a plausible staff number, month, day, hour and minute. A terminal of the 12-byte kind still reads exactly as before.

A year that goes backwards

The packed minute is one 32-bit word: minute in bits 26–31, hour 21–25, day 16–20, month 12–15, and, per the write-up, year in bits 0–11. Read that way, every month, day, hour and minute was right, and every year was about 505.

That alone says "the year field is something else". The log said what. Grouped by that 12-bit value, the log fell into three long runs:

12-bit valueRecordsMonths
502about 2,000the last two months of one yearoldest
506about 14,000January to late Novembermiddle
505a few hundredfrom one afternoon in Novembernewest

The value went down by one, in the middle of an afternoon, while months and days carried on in order. A year does not do that. Two low bits flipping does. Drop the two low bits and the runs read 125, 126 and 126: years 2025, 2026 and 2026, counted from 1900. The two bits are something else. What they mean is still open; the reader ignores them.

The test that settled it was a punch. A person punched once on the terminal while I watched its screen. The next read decoded the newest record as that staff number, that date, and that minute and second, and the screen said 2026.

The five minutes that hid the fix

The fixed build went onto the office PC. The terminal stayed "not reading" for five minutes. It was not the fix. After 33 failures in a row the connector tries a machine every five minutes instead of every minute, so a dead machine does not hold the PC up; the last failure had been a minute before the update.

The rule is right for a dead machine and wrong for a machine someone just fixed. The connector now forgets the run of failures when it is updated, and when the machine's address, port, device number or key changes. The next read happens within a minute.

What the error message said

"The machine refused the request; if a communication password is set on it, set it to 0". It was true of the code the connector had seen before and false here. Once the terminal has answered a hello, a password is not the problem, so the message now names the step that was refused: the count, the punches, or a name.

What changed in the reader

BeforeAfter
Block requestcount, block, size at 2, 4, 6at 4, 6, 8
Block ending2 bytes2 or 4, decided from the first block
Record size1212 or 20, decided from block 0
Yearbits 0–1112-byte: bits 0–11; 20-byte: bits 2–11 + 1900
Empty terminalasked for blocksasks for none
Device numberalways 1from the platform, 1 if unset
Refusal after hello"set the password to 0"which step was refused

Each row has a test, and each test was checked by taking its fix out and watching it fail. The test for the record format decodes three records copied byte for byte from the terminal, with the staff numbers changed.

The protocol's public description was right about almost everything. The two places it differed from this terminal, the ending and the record, are exactly the places a single sample cannot show you. That is why the next post is about the simulator.

Working through something like this? I help teams ship AI and cloud systems that hold up, and cost what they should.