PALIMPSEST

The print shop has closed, but the optical machine still retains the traces of the final draft. Fresh ink covers the old; the lettering dissolves into overlapping layers. Find the ticket that makes the machine reproduce the original print.

Recon

A palimpsest is a manuscript page that was scraped clean and written over, the older text still faintly underneath.

The handout carries a single stripped Linux ELF named palimpsest, and the name describes the mechanics: the program takes a 32-byte “ticket” on stdin, pushes it through a layered transformation, and compares the final bytes against a fixed table in .rodata. The ticket that passes every comparison is the “original print”, and it doubles as the flag.

Running it before reading it

We ran the binary before opening it in anything. Every run below is the same binary with different stdin:

$ printf '' | ./palimpsest; echo "exit=$?"
PALIMPSEST / optical press 08
One 32-byte ticket on stdin; finish input with EOF.
Exposure rejected.
exit=1

Even empty input gets the banner and the instructions, then an immediate rejection. Two more probes mapped the input contract. A short line fails the same way, and so does a well-formed 32-byte candidate:

$ printf 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' | ./palimpsest; echo "exit=$?"
PALIMPSEST / optical press 08
One 32-byte ticket on stdin; finish input with EOF.
Exposure rejected.
exit=1

So the check is about content, not just length. A strings pass before any disassembly found three flag-shaped strings, one of them claiming “eight layers of static”:

$ strings -n 4 palimpsest | grep -E 'PALIMPSEST|Exposure|NVR|ticket|impression'
PALIMPSEST / optical press 08
Exposure rejected.
NVR{the_printer_remembers}
NVR{carbon_copy_archived}
NVR{eight_layers_of_static}
One 32-byte ticket on stdin; finish input with EOF.
Original impression recovered.

We tried the first one as a ticket, padded to 32 bytes (NVR{the_printer_remembers} is 26 chars, so we appended six x), and the binary rejected it too. None of those strings is the answer as-is. The last line, Original impression recovered., is the success output we are hunting.

What the binary is

$ file palimpsest
palimpsest: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, stripped

It is 14,416 bytes, stripped and PIE (position-independent, so code addresses are image-relative). The imports are only getc, puts and stdin plus the usual process startup helpers, so no crypto library does the work: everything is inline in two functions, main at 0x10a0 and the validator at 0x12f0.

main reads bytes with getc into a 256-byte buffer. A newline flushes the line (a trailing \r is stripped) and the next read must then be EOF; on EOF the counter must be exactly 32. Every byte must land in 0x21 to 0x7e, printable ASCII without space. Anything else prints Exposure rejected. and exits 1. A ticket that survives goes to the validator, and a zero return value still means rejection.

One oddity in main is a gate at 0x10d8 that exits silently with code 2 if any of three .data qwords (0x4010, 0x4018, 0x4020) is zero. Dumping .data shows they hold pointers to the three decoy strings, so they are never zero and the gate always passes. It is styled like an anti-tamper check and does nothing.

The tables the validator checks against

The secret material sits in one compact .rodata block:

0x20df  "INK8v1"                       magic blob header
0x20e7  20 83 1d 0d 07 00 00 00        parameter echo: 32, 131, 29, 13, 7
0x20ef  99 b3 a4 29 7f 1d 7c 97 ...    32-byte table T
0x210f  50 84 97 7c e0 35 81 f5 ...    32 expected bytes E

The 32-byte blob at 0x20ef is overloaded; the validator’s own addressing splits it: phase 1 indexes a key table at base 0x20df + 0x10 (that is 0x20ff), phase 4 indexes an add table at 0x20df + 0x28 (0x2107), and phase 3 reads matrix rows from 0x20ef. Three slices, three roles:

Slice Bytes Role
M0 = T[0:16] 99 b3 a4 29 7f 1d 7c 97 4f c4 9f 6e 15 06 25 5d 4x4 GF(2^8) matrix
K1 = T[16:24] b5 87 51 f9 7c 39 8c 7a per-layer XOR key table
T3 = T[24:32] 1b 3f 74 99 48 a9 11 5d rotate-output add table

E at 0x210f is the “final impression”, the 32 bytes the transformed ticket must reproduce.

Exploitation

Four iterations, four phases

The validator’s outer loop keeps a counter starting at 7 and adding 104 per iteration until it reaches 423, so it runs exactly four times. The “eight layers” decoy does not match the structure: it is four iterations of eight bytes each, covering the 32-byte ticket. Each iteration fills three 8-byte buffers A, B and X in four phases.

Phase 1 mixes ticket bytes into A with a rolling key (code at 0x1388 to 0x13b9):

A[k] = K1[(L+k) & 7] ^ P[(T + 13k) & 31] ^ ((17L + 29k) & 0xff)

with L the iteration number and T = 7 + 104L. The odd stride matters: 13k mod 32 for k = 0..7 visits eight distinct positions {0,13,26,7,20,1,14,27}, and the four layer bases 7, 15, 23, 31 offset those sets so that, across the whole run, every ticket byte is read exactly once. That partition is what later guarantees a unique answer.

Phase 2 transposes the 8 bytes of A as an 8x8 bit matrix, B[i] bit (7-j) = A[j] bit i.

Phase 3 multiplies each 4-byte half of B by a 4x4 matrix over GF(2^8), byte arithmetic where addition is XOR and multiplication wraps via a fixed reduction polynomial, here 0x11d (an xtime-style doubling with ^0x11d on carry, the same construction AES uses but with a different polynomial than its 0x11b).

Phase 4 is a chained rotate-add compared against E (code at 0x14d3 to 0x157f). A chaining byte K starts at 0x83 and persists across all four iterations:

out_i = (ROL8(X[i] ^ K_i, ((L+i) mod 7) + 1) + T3[(3L+i) & 7]) & 0xff
K_{i+1} = (K_i + L + 3i + out_i) & 0xff

Every out_i must equal E[8L+i]; the validator ORs all 32 mismatch bytes together and succeeds only if the accumulator is zero.

Emulating the validator to settle the ambiguities

Two details resisted a confident static read: which slice of table T multiplies which half of B, and which operand the K chain adds. Guessing either one wrong would poison the inversion, so we built ground truth instead of trusting our reading. A Unicorn harness (Unicorn embeds a CPU emulator in Python) mapped the ELF’s load segments, set the FS base for the stack canary, pointed rdi at a 32-byte ticket buffer, and hooked the four phase boundaries to dump A, B, X and K after each phase:

$ python3 emu.py 'ABCDEFGHIJKLMNOPQRSTUVWX'
[L0 after-A] A=fdcf29e108e174f2 B=0000000000000000 X=0000000000000000 K=83 acc=07
[L0 after-T] A=fdcf29e108e174f2 B=f441c2e883b7d7d5 X=0000000000000000 K=83 acc=00
[L0 after-M] A=fdcf29e108e174f2 B=f441c2e883b7d7d5 X=6b1bcf29e3a90959 K=f2 acc=04
[L0 after-C] A=fdcf29e108e174f2 B=f441c2e883b7d7d5 X=6b1bcf29e3a90959 K=24 acc=ff

(The same four dumps follow for L1 to L3; the acc column is only meaningful at after-C, where ff just says this garbage ticket mismatches.)

We then wrote a Python forward model of the four phases and iterated until its per-phase dumps matched the emulator byte-for-byte on every value, all four layers, with the K chain walking 83 -> 24 -> 8e -> 53 -> 83 in both. The comparison settled both ambiguities. The phase-3 key pointer resets to the table base for each 4-byte block, so both halves are multiplied by the same M0, not by two different matrices. And in phase 4 the instruction after the rotate and add is lea r9d, [rax + rdi] with rdi holding the just-computed output byte, so the chain adds the output byte, not the pre-rotation value.

Inverting the pipeline layer by layer

Every phase is a bijection and E is literally the required output, so the pipeline inverts layer by layer with no guessing:

  1. Undo phase 4: out_i is known, it is E[8L+i]. Subtract the T3 term, rotate right by ((L+i) mod 7) + 1, and XOR the current chain byte to get X[i]. The chain replays without knowing the ticket, because each K update consumes only out_i, which comes from E.
  2. Undo phase 3: multiply each 4-byte half of X by the Gauss-Jordan inverse of M0 over GF(2^8).
  3. Undo phase 2: apply the true inverse transpose, A[j] bit i = B[i] bit (7-j).
  4. Undo phase 1: P[(T + 13k) & 31] = A[k] ^ K1[(L+k) & 7] ^ ((17L + 29k) & 0xff).

Step 3 is the one phase whose inverse we initially got wrong. A transpose looks self-inverse, so the first round-trip script (random ticket, forward model, invert, compare) used the transpose as its own inverse and failed every trial:

$ python3 roundtrip.py
trial 0 round-trip: FAIL
 orig : 3757702c54792e6d6222645b5b2332224d21657d686b765d3424793a65367523
 rec  : 5138aa769bc7443694ea480a5176e29337d58cc0086d9e65cef09a31f721a88f
trial 1 round-trip: FAIL

A per-stage check isolated the failure:

$ python3 stages.py
Mat*M0i == I : True
transpose involution: False
L0: X-inv OK  B-inv OK  A-inv FAIL  P-idx FAIL

Phases 4 and 3 were already inverted correctly in layer 0, and phase 2 alone was broken. The reason is that this transpose is not an involution: applied twice it returns the input with the byte order mirrored and each byte bit-reversed (byte j becomes the bit-reversal of A[7-j]). The proper inverse maps A[j] bit i = B[i] bit (7-j). With that fixed, the full solver reads its tables straight out of the binary and peels all four layers:

raw = open('palimpsest', 'rb').read()
rod = raw[0x2000:0x2130]               # .rodata (vma == file offset here)
M  = rod[0x0ef:0x10f]                  # the overloaded 32-byte table T
E  = rod[0x10f:0x12f]                  # expected output bytes
K1, T3 = M[16:24], M[24:32]
M0 = [M[4*i:4*i+4] for i in range(4)]  # 4x4 GF(2^8) matrix

def gmul(a, b):                        # GF(2^8) multiply, reduction poly 0x11d
    r = 0
    while b:
        if b & 1: r ^= a
        a <<= 1
        if a & 0x100: a ^= 0x11d
        b >>= 1
    return r & 0xff

def mat_inv(rows):                      # Gauss-Jordan over GF(2^8)
    n = 4
    a = [list(rows[i]) + [1 if i == j else 0 for j in range(n)] for i in range(n)]
    for c in range(n):
        piv = next(r for r in range(c, n) if a[r][c])
        a[c], a[piv] = a[piv], a[c]
        inv = next(x for x in range(1, 256) if gmul(a[c][c], x) == 1)
        a[c] = [gmul(v, inv) for v in a[c]]
        for r in range(n):
            if r != c and a[r][c]:
                a[r] = [a[r][j] ^ gmul(a[r][c], a[c][j]) for j in range(2*n)]
    return [row[n:] for row in a]

def itranspose(bits):                   # true inverse: A[j] bit i = B[i] bit (7-j)
    return [sum(((bits[i] >> (7 - j)) & 1) << i for i in range(8)) for j in range(8)]

def transpose(bits):                    # the binary's phase 2, for the forward check
    return [sum(((bits[j] >> i) & 1) << (7 - j) for j in range(8)) for i in range(8)]

def rol8(v, n): n %= 8; return ((v << n) | (v >> (8 - n))) & 0xff

def ror8(v, n): n %= 8; return ((v >> n) | (v << (8 - n))) & 0xff

M0i = mat_inv(M0)
P = [None]*32
K = 0x83                                # chain byte persists across layers
for L in range(4):
    T = (7 + 104*L) & 31
    X = []
    for i in range(8):                  # undo the rotate/add chain
        out = E[8*L+i]                  # required output byte, known
        rot = (out - T3[(3*L+i) & 7]) & 0xff
        X.append(ror8(rot, ((L+i) % 7) + 1) ^ K)
        K = (K + L + 3*i + out) & 0xff
    B = [0]*8                           # B_half = M0^-1 . X_half
    for half, xr in ((0, 0), (1, 4)):
        for r in range(4):
            v = 0
            for c in range(4):
                v ^= gmul(M0i[r][c], X[xr+c])
            B[4*half+r] = v
    A = itranspose(B)                   # undo the bit transpose
    for k in range(8):                  # undo the keyed XOR
        P[(T + 13*k) & 31] = A[k] ^ K1[(L+k) & 7] ^ ((17*L + 29*k) & 0xff)

ticket = bytes(P)
print('ticket bytes :', ticket.hex(' '))

def forward(p):                        # re-run what the binary actually checks
    acc = 0
    K = 0x83
    for L in range(4):
        T = 7 + 104*L
        A = [K1[(L+k) & 7] ^ p[(T + 13*k) & 31] ^ ((17*L + 29*k) & 0xff) for k in range(8)]
        B = transpose(A)
        X = [0]*8
        for c in range(8):
            for m in range(4):
                X[c] ^= gmul(B[4*(c//4) + m], M[4*(c % 4) + m])
        for i in range(8):
            out = (rol8(X[i] ^ K, ((L+i) % 7) + 1) + T3[(3*L+i) & 7]) & 0xff
            acc |= out ^ E[8*L+i]
            K = (K + L + 3*i + out) & 0xff
    return acc

print('ticket ascii :', ticket.decode('latin1'))
print('printable    :', all(0x21 <= b <= 0x7e for b in ticket))
print('forward check (0 == success):', forward(list(ticket)))
$ python3 solve.py
ticket bytes : 4e 56 52 7b 31 6e 6b 5f 70 6c 34 6e 33 35 5f 39 ...
printable    : True
forward check (0 == success): 0

The solver prints the ticket twice, as hex and as ASCII, and the ASCII line is the flag itself, so we hold it for the end. The forward check is the model re-running all four phases over the recovered bytes: 0 means every one of the 32 comparisons passes.

Confirming on the real binary

The model already matched the emulator byte-for-byte, but the final proof is the real program. We piped the solver’s ASCII line straight into the binary so the ticket never had to be transcribed by hand:

$ T=$(python3 solve.py | sed -n 's/^ticket ascii : //p')
$ printf '%s' "$T" | ./palimpsest; echo "exit=$?"
PALIMPSEST / optical press 08
One 32-byte ticket on stdin; finish input with EOF.
Original impression recovered.
exit=0

Flag capture

The accepted ticket is the flag:

$ python3 solve.py | sed -n 's/^ticket ascii : //p'
NVR{1nk_pl4n35_91a4b0752e311274}