Challenge Description

Category: Reverse Engineering

Difficulty: 🔴 Hard

You received an ancient HMI that you believe was compromised. Can you figure out what it does and identify what the “drop too much” is?

We get one file: SETPT16.COM.

No manual, no capture, no second-stage payload. Just a 3 KiB DOS program pretending to be an old SPC-88 serial setpoint terminal. It asks for a 16-bit unsigned value, formats it as a big-endian control word, and sends it through COM1.

The title is the only real hint. If the largest legal value is 65535, then one drop too much should be 65536. That is a good guess, but a guess is not a reverse. The interesting part is proving why only that exact value reaches code that every other overflow misses.


TL;DR

  • SETPT16.COM is a flat 16-bit DOS .COM image loaded at offset 0x0100
  • Its input routine stores a decimal value as a manually maintained 32-bit integer: high word in DX, low word in AX
  • Ordinary setpoints require DX == 0 and are really sent to COM1 as high byte, then low byte
  • Most values above 0xFFFF reach a generic overflow screen
  • A hidden branch requires exactly DX == 1 && AX == 0
  • That register pair is 0x00010000, decimal 65536
  • The branch decrypts 33 bytes from runtime address 0x0D1C with single-byte XOR key 0x5A
  • The resulting flag is FLAG{W0RK5_0N_MY_M4CH1N3_DOS_3.3}

Initial Triage

I started with the boring facts. They matter more than usual here because a DOS .COM file does not have a loader-friendly header telling a disassembler what it is.

Get-Item .\SETPT16.COM | Select-Object Name,Length
Get-FileHash .\SETPT16.COM -Algorithm SHA256
Name   : SETPT16.COM
Length : 3178

SHA256 : D6D1DFDC86BBB8545BE6C688408FB9DEF4C47ABBDA97AEAE656DCA993CDF8937

A quick hex dump rules out an MZ executable immediately:

xxd -g1 -l 32 SETPT16.COM
00000000: b8 03 00 cd 10 b8 00 06 b7 1e 31 c9 ba 4f 18 cd
00000010: 10 e8 ce 00 be c0 02 e8 d1 00 b4 02 30 ff ba 06

Those bytes are executable 16-bit instructions from byte zero. B8 03 00 is mov ax,3; CD 10 is a BIOS video interrupt. No header, no relocation table, no section metadata. Classic .COM.

DOS loads a .COM image at offset 0x0100 in a single segment and starts execution there. That gives us the address rule I kept beside the disassembly for the rest of the challenge:

runtime address = file offset + 0x100
file offset     = runtime address - 0x100

I used ndisasm because a flat 3 KiB real-mode image does not need a heavyweight loader:

ndisasm.exe -b 16 -o 0x100 SETPT16.COM > setpt16.asm

The two important options are -b 16 and -o 0x100. Forget the origin and all absolute data references will be displaced by exactly 256 bytes. The code still looks plausible, which makes this a particularly annoying mistake.

If you prefer IDA or Ghidra, import it as a raw binary, select 16-bit x86 real mode, and set the load address to 0x100.

A strings pass gives away the industrial cover story, but not the flag:

strings.exe -n 5 .\SETPT16.COM
INDUSTRIATECH AUTOMATION SYSTEMS LTD
MODEL: SPC-88    FIRMWARE REV: 3.3.1    [16-BIT]
SERIAL SETPOINT WRITE TERMINAL
CHANNEL  :  COM1      BAUD: 9600    FORMAT: 8N1
DATA     :  16-BIT UNSIGNED INTEGER (BIG-ENDIAN)
FATAL EXCEPTION 0x0006 : INTEGER OVERFLOW
SPC-88 REGISTER FAULT  : SETPOINT > 0xFFFF
THE 16-BIT CONTROL WORD HAS WRAPPED TO ZERO.

My first thought was that the flag might simply sit after the dramatic error text. It does not. There is no FLAG{ in the file, and the final bytes look like noise rather than another DOS string.

The useful clue in the strings is the wording: wrapped to zero. That is more precise than “out of range.” It points at 0x10000, where a 16-bit word becomes 0x0000 and produces a carry into the next word.

Initial triage of the raw DOS COM image

Mapping the Tiny Binary

Once the data region is separated from the first few hundred bytes of code, the program is small enough to map by hand:

0x0100  entry point / main UI flow
0x01E2  move cursor to row 0, column 0
0x01EB  print NUL-terminated string through INT 10h
0x01F9  print BX as four hexadecimal digits
0x021B  read and parse decimal input
0x02C0  UI strings and static data

The entry point sets 80×25 text mode, clears the screen, prints the terminal banner, positions the cursor at the input field, and calls the parser at 0x021B.

The ordinary string printer is a useful landmark:

01EB  lodsb                ; AL = *SI++
01EC  or   al,al
01EE  jz   0x01F8          ; stop on NUL
01F0  mov  ah,0x0e         ; BIOS teletype output
01F2  xor  bh,bh
01F4  int  0x10
01F6  jmp  0x01EB
01F8  ret

That tells us most UI strings are NUL-terminated and printed through BIOS video services. Later, the hidden diagnostic uses a completely different DOS printing API and a different terminator. That difference ends up explaining one otherwise odd write in the decoder.

The function that matters first is the decimal parser.


Reconstructing the Decimal Parser

The parser starts at runtime 0x021B. It clears four variables:

021B  mov word [0x0d63],0  ; low word
0221  mov word [0x0d65],0  ; high word
0227  mov byte [0x0d69],0  ; number of typed digits

It then reads keys with INT 16h, accepts only ASCII 0 through 9, supports backspace, and caps the field at ten digits.

The interesting detail is that this supposedly 16-bit terminal does not parse into one word. It keeps a full 32-bit value in two words:

0x0D63  accumulator low word
0x0D65  accumulator high word
0x0D67  carry from low-word multiplication
0x0D69  digit count

For each decimal digit, it implements value = value * 10 + digit manually:

0253  sub  al,0x30         ; ASCII digit -> integer
0255  movzx bx,al          ; BX = digit

0258  mov  ax,[0x0d63]     ; low word
025B  mov  cx,10
025E  mul  cx              ; DX:AX = low * 10
0260  mov  [0x0d63],ax
0263  mov  [0x0d67],dx     ; carry into high word

0267  mov  ax,[0x0d65]     ; high word
026A  mul  cx              ; DX:AX = high * 10
026C  add  ax,[0x0d67]     ; add carry from low product
0270  mov  [0x0d65],ax

0273  mov  ax,[0x0d63]
0276  add  ax,bx            ; add new digit
0278  mov  [0x0d63],ax
027B  jnc  next_key
027D  inc  word [0x0d65]   ; propagate carry

In pseudocode:

uint16_t lo = 0;
uint16_t hi = 0;

for each digit {
    uint32_t low_product = lo * 10;
    lo = low_product & 0xffff;

    hi = (hi * 10 + (low_product >> 16)) & 0xffff;

    uint16_t old_lo = lo;
    lo += digit;
    if (lo < old_lo)
        hi++;
}

The backspace path confirms this is intentional 32-bit arithmetic, not an accidental carry that happens to survive. It divides the high word by ten, carries the remainder into the low-word division, and stores the 32-bit quotient back into the same pair:

029E  mov  ax,[0x0d65]
02A1  xor  dx,dx
02A3  mov  cx,10
02A6  div  cx              ; high / 10
02A8  mov  [0x0d65],ax
02AB  push dx              ; remainder of high division

02AC  mov  ax,[0x0d63]
02AF  pop  dx              ; remainder becomes upper dividend
02B0  div  cx              ; combined low division
02B2  mov  [0x0d63],ax

Finally, pressing Enter returns the accumulator in the conventional register pair:

02B8  mov ax,[0x0d63]      ; low 16 bits
02BB  mov dx,[0x0d65]      ; high 16 bits
02BF  ret

So after the call, the input value is DX:AX.

At this point I expected a normal unsigned check like test dx,dx / jnz overflow. The binary does start that way. Then it adds one very specific exception.


The Gate at 0x0126

Immediately after the parser returns, the entry function examines DX:AX:

0123  call 0x021b          ; read decimal, return DX:AX

0126  test dx,dx
0128  jz   normal_16_bit_value

012A  cmp  dx,1
012D  ja   generic_overflow

0131  test ax,ax
0133  jnz  generic_overflow

0137  jmp  hidden_diagnostic

Translated literally:

uint32_t value = read_decimal();

if ((value >> 16) == 0) {
    transmit((uint16_t)value);
} else if ((value >> 16) == 1 && (value & 0xffff) == 0) {
    reveal_hidden_diagnostic();
} else {
    show_generic_overflow();
}

The special case is not “any value above 65535.” It is one exact register state:

DX:AX = 0001:0000
       = 0x00010000
       = 65536

That is the title: the first illegal setpoint, one drop above 0xFFFF.

The exact DX AX gate that distinguishes 65536 from every other overflow

The dead end that looks reasonable

Before reading this block carefully, I tried thinking like the interface: “anything larger than 65535 is an overflow, so any six-digit number should work.” It does not.

The boundary behavior is deliberately narrow:

Input DX:AX Result
65535 0000:FFFF Normal serial write of FF FF
65536 0001:0000 Hidden diagnostic and flag
65537 0001:0001 Generic overflow, no flag
70000 0001:1170 Generic overflow, no flag
131072 0002:0000 Generic overflow, no flag

cmp dx,1 / ja eliminates high words above one. test ax,ax / jnz eliminates every 0x0001xxxx value except 0x00010000.

⚠️ Gotcha: this is an equality puzzle disguised as a range check. Random fuzzing above 0xFFFF mostly lands in the generic error path and makes the obvious hypothesis look wrong.

The modulo-2^32 aliases

The parser accepts at most ten digits, and its high-word multiplication discards overflow beyond 32 bits. That means arithmetic is effectively modulo 2^32.

As a result, two other ten-digit strings reduce to the same DX:AX pair:

4295032832 = 65536 + 2^32
8590000128 = 65536 + 2×2^32

Both eventually become 0001:0000 and should reach the same hidden branch. They are fun parser aliases, not the intended answer. The clean solution is still 65536, and it is the only value that matches the title and the control-word story directly.


The Normal Path Is Not a Decoy

I spent some time checking the normal branch because industrial-looking strings in CTF binaries are often stage dressing. Here the serial terminal is real enough to be convincing.

For an in-range value, the program stores AX at 0x0D61, renders the word as four hexadecimal digits, initializes COM1, and transmits two bytes:

0139  mov  [0x0d61],ax
...
0157  mov  bx,[0x0d61]
015B  call 0x01f9          ; print BX as four hex digits

0164  xor  dx,dx           ; DX=0 -> COM1
0166  mov  ax,0x00e3       ; AH=0 init, AL=E3 configuration
0169  int  0x14

016B  mov  ah,1            ; transmit character
016D  mov  al,[0x0d62]     ; high byte of stored word
0170  xor  dx,dx
0172  int  0x14

0174  mov  ah,1
0176  mov  al,[0x0d61]     ; low byte
0179  xor  dx,dx
017B  int  0x14

The BIOS serial configuration byte 0xE3 decodes to 9600 baud, no parity, one stop bit, eight data bits: exactly the 9600 8N1 shown in the UI.

The byte order is also exactly as advertised. x86 stores the word at 0x0D61 little-endian, so the high byte lives at 0x0D62. The code deliberately sends [0x0D62] first and [0x0D61] second, producing a big-endian two-byte frame on the wire.

That detail is not needed to decrypt the flag, but it is why the binary works as a challenge. The overflow path is hidden beside a coherent application, not pasted into a dummy shell.


Following the Hidden Diagnostic

The exact 0001:0000 case jumps to runtime 0x0185. The program clears the screen in red, prints the fatal overflow panel, and then reaches a short copy loop:

019A  mov  si,0x0d1c       ; encrypted bytes
019D  mov  di,0x0d3d       ; decoded output buffer
01A0  mov  cx,0x21         ; 33 bytes

01A3  lodsb
01A4  xor  al,0x5a
01A6  stosb
01A7  loop 0x01a3

01A9  mov  byte [di],0x24  ; '$'

01B2  mov  dx,0x0d3d
01B5  mov  ah,9
01B7  int  0x21            ; print '$'-terminated DOS string

There are a few nice old-DOS details packed into those ten instructions.

LODSB reads from DS:SI; STOSB writes to ES:DI. A .COM process starts with its code and data in the same segment, so the loop can copy directly from the embedded blob into scratch space without setting up a separate data segment.

The UI printer at 0x01EB stopped on a NUL byte and used BIOS INT 10h. The decoded flag is instead printed with DOS INT 21h, function 09h, which stops on $. That is why the loop writes 0x24 after the plaintext. It is a terminator, not part of the flag and not another layer of encoding.

The 33-byte XOR decoder and DOS dollar-string print

The second address gotcha

The encrypted source is runtime address 0x0D1C. It is tempting to seek to file offset 0x0D1C, especially after staring at raw disassembly for a while.

That is wrong for a .COM image.

runtime address  0x0D1C
- load origin    0x0100
-----------------------
file offset      0x0C1C

Seeking to 0x0D1C goes past the end of this 3178-byte file. The correct 33-byte encrypted block at offset 0x0C1C is:

1c 16 1b 1d 21 0d 6a 08 11 6f 05 6a 14 05 17 03
05 17 6e 19 12 6b 14 69 05 1e 15 09 05 69 74 69
27

XORing the first byte is enough for a quick sanity check:

0x1c ^ 0x5a = 0x46 = 'F'
0x16 ^ 0x5a = 0x4c = 'L'
0x1b ^ 0x5a = 0x41 = 'A'
0x1d ^ 0x5a = 0x47 = 'G'

We have FLAG immediately. At that point the remaining work is mechanical.


Extracting the Flag Statically

I did not need to emulate the serial hardware or patch the program. Once the origin, source address, length, and XOR key are known, a standalone extractor is cleaner and reproducible:

from pathlib import Path

ROM_ORIGIN = 0x100
ENC_RUNTIME = 0x0D1C
ENC_LENGTH = 0x21
XOR_KEY = 0x5A

data = Path("SETPT16.COM").read_bytes()

enc_offset = ENC_RUNTIME - ROM_ORIGIN
enc = data[enc_offset:enc_offset + ENC_LENGTH]

assert len(enc) == ENC_LENGTH

plain = bytes(byte ^ XOR_KEY for byte in enc)

assert plain.startswith(b"FLAG{")
assert plain.endswith(b"}")

print(plain.decode())

Running it:

FLAG{W0RK5_0N_MY_M4CH1N3_DOS_3.3}
Static extractor recovering the XOR-decoded flag

If you do want the full dynamic route, run the program in DOSBox, type 65536, and press Enter. No serial device is needed to reach the special branch because it jumps around the COM1 transmit path entirely.


Flag

FLAG{W0RK5_0N_MY_M4CH1N3_DOS_3.3}

Reflection

This challenge is a good example of how little code a convincing reverse can need.

There is no anti-debugging, no virtual machine, no custom packer, and barely a cipher. The only encoding is a fixed XOR over 33 bytes. The difficulty comes from understanding the execution environment and refusing to flatten a precise comparison into a vague idea like “overflow.”

The binary layers its misdirection cleanly:

  • The UI says the device accepts a 16-bit unsigned integer, which is true.
  • The serial path really initializes COM1 as 9600 8N1 and sends a big-endian word.
  • The parser quietly preserves an extra 16 bits, which the normal device does not need.
  • The error screen talks about a value greater than 0xFFFF, encouraging broad overflow tests.
  • The hidden branch accepts only the first value over the boundary, where the low control word becomes zero.
  • The encrypted bytes sit at a runtime address that is easy to mistake for a file offset.

The two facts that matter most are both old-school: .COM files begin at 0x100, and DOS function 09h prints until $. Once those are in place, the rest reduces to one register pair and one byte of XOR.

What I like most is that the challenge’s title is not merely a hint toward integer overflow. It describes the exact comparison. 65535 is the last legal drop. 65536 is one too many, the 16-bit word falls back to zero, and the program finally shows what it was hiding.