$ cat writeup.md…
$ cat writeup.md…
fortid2026
Task: stripped x86-64 ELF replays custom .pmaf bucket-fill operation files onto a start PNG to paint each flag part. Solution: reverse the PMAF format and splitmix64 hash-chain keystream in Ghidra, reimplement the decoder plus flood fill in C for speed, render parts at a partial record count, and concatenate the painted text of parts 01..13.
It was an honor to paint this flag, I hope you'll enjoy it.
The task description links to the Microsoft Paint Wikipedia article — a strong
hint at the paint-bucket / flood-fill tool. The handout is a handout.zip
(≈121 MB) containing:
flag_part_01..13.pmaf — 13 custom binary "Paint Me A Flag" operation filesflag_part_01..13.png — 13 start imagespaint_me_a_flag — a stripped x86-64 ELFThe shipped tool is invoked as:
paint_me_a_flag START.png OPERATIONS.pmaf OUTPUT.png
It replays the bucket-fill operations encoded in the .pmaf file onto the start
PNG and writes an output PNG. The goal is to recover the flag, which is painted
as text across the 13 parts and concatenates in order.
"It was an honor to paint this flag" + the Microsoft Paint link ⇒ the .pmaf
stream is a sequence of flood-fill / bucket operations, executed exactly the
way the Paint bucket tool spreads a colour over a connected region.
Static analysis in Ghidra (paint_me_a_flag is stripped) plus struct
cross-references produced the .pmaf layout:
| Offset | Size | Field |
|---|---|---|
| 0x00 | 8 | magic "PMAF\x03\r\n\x1a" |
| 0x08 | 4 | u32 width |
| 0x0c | 4 | u32 height |
| 0x10 | 8 | u64 record_count (== width * height) |
| 0x18 | 8 | u64 seed |
| 0x20 | 4 | u32 fill_count (0 for part 01; 64 for parts 02..13) |
| 0x24 | 4 | u32 reserved (must be 0) |
| 0x28 | 8×N | record_count × 8-byte records |
Total header = 40 bytes, then record_count * 8 bytes of records.
All mixing is splitmix64-style with these constants:
C1 = 0xbf58476d1ce4e5b9
C2 = 0x94d049bb133111eb
C3 = 0xd6e8feb86659fd93
GOLD = 0x9e3779b97f4a7c15
PHI = 0x243f6a8885a308d3
mix1(x) = ((x >> 30) ^ x) * C1
mix2(x) = (x ^ (x >> 27)) * C2
mixfull(x) = (x >> 31) ^ x
State initialisation. A single u8 keystream state is derived from the
header. Note that seed participates, and it is coupled to record_count, so
you cannot truncate the file to build a faster oracle — the keystream would
change.
s = mix1(record_count); s = mix2(s)
u8 = (s >> 31) ^ seed ^ height ^ ((u64)width << 32)
^ ((u64)fill_count * C3) ^ s ^ 0x6f3d9b21a4c875e1
u8 = mixfull( mix2( mix1(u8) ) )
Record decoding. A counter local_398 starts at 0 and is incremented by
GOLD after every record. Each 8-byte record decrypts to a (pos, color) pair:
base = local_398 ^ u8
t = mix2( mix1(base) )
value = (t >> 31) ^ t ^ raw64
pos = value & 0xffffff # 24-bit pixel index
color = (value >> 24) & 0xffffff # 24-bit 0xRRGGBB
A 16-bit consistency check on the high bits guards the stream; on mismatch the
binary raises the string "operation stream lost synchronization".
Flood fill. fill(pos, color) performs a 4-connected flood fill of the
connected component whose current colour equals the colour at pos,
recolouring the entire region. It returns three values that drive the state
chain:
L — the region length (pixel count)H — an XOR hash over the region's pixel indices: H ^= mix2(mix1(idx ^ PHI)) ^ that>>31orig — the region's original colourAfter every record the outer state is re-mixed from (L, H, orig, color); the
exact formulas are in the decompilation (ghidra/decomp_c.txt).
The fill_count inner loop. For parts 02..13, fill_count = 64. Each of the
64 inner iterations fills the same position with a pseudo-random odd 24-bit
colour and then immediately fills it back to the original colour. Since the
random fill is undone, the canvas is (almost always) unchanged — but the return
values still feed the hash chain, so the state must be advanced every time. The
random colour is rejected and re-derived if it equals 0xf2eadf.
Output. The final canvas holds 0x00RRGGBB per pixel and is written as an
RGB PNG.
The shipped binary is roughly O(n^2.3) because of the repeated full-region
flood fills, taking tens of minutes per part even under emulation (Docker with
Rosetta). Reimplementing the algorithm natively is the practical route.
handout.zip to get the parts, start PNGs and the ELF.pmaf.c) for speed. Key optimisations:
L/H once per record and
perform only cheap state advances for the remaining inner iterations
(instead of 128 full flood fills per record).uint32 RGB buffer and back
(pngraw.py).FortID{...} format. The painted phrase is a leetspeak meme matching the
"Paint me like one of your French flags" flavour, split across the parts.Final result (format only): FortID{...} — HTB{REDACTED}-style placeholder,
not stored here.
The C reimplementation used as the solver:
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> #define C1 0xbf58476d1ce4e5b9ULL #define C2 0x94d049bb133111ebULL #define C3 0xd6e8feb86659fd93ULL #define GOLD 0x9e3779b97f4a7c15ULL #define PHI 0x243f6a8885a308d3ULL #ifndef PW #define PW 1600 #endif static uint32_t W,H; static uint64_t CNT, SEED; static uint32_t FILLC; static uint32_t *canvas; static uint32_t *stackbuf; static uint32_t *regionbuf; static uint64_t g_visits=0; static inline uint64_t mix1(uint64_t x){ return ((x>>30) ^ x) * C1; } static inline uint64_t mix2(uint64_t x){ x=(x^(x>>27))*C2; return x; } static inline uint64_t mixfull(uint64_t x){ return (x>>31) ^ x; } /* flood fill: returns length L, hash Hh, orig color */ static void fill(uint32_t pos, uint32_t newcolor, uint64_t *Lp, uint64_t *Hp, uint32_t *origp){ uint32_t orig = canvas[pos]; uint32_t newv = (orig != newcolor) ? newcolor : (orig | 0x1000000u); uint64_t Hh = 0; uint64_t L = 0; size_t sp = 0, rp = 0; stackbuf[sp++] = pos; canvas[pos] = newv; while(sp){ uint32_t idx = stackbuf[--sp]; regionbuf[rp++] = idx; uint64_t x = ((uint64_t)idx ^ PHI); uint64_t h = mix1(x); h = mix2(h); Hh ^= h ^ (h>>31); L++; uint32_t col = idx % PW; if(col != 0){ uint32_t p=idx-1; if(canvas[p]==orig){canvas[p]=newv; stackbuf[sp++]=p;} } if(col+1 != PW){ uint32_t p=idx+1; if(canvas[p]==orig){canvas[p]=newv; stackbuf[sp++]=p;} } if(idx >= PW){ uint32_t p=idx-PW; if(canvas[p]==orig){canvas[p]=newv; stackbuf[sp++]=p;} } if(idx/PW + 1 != H){ uint32_t p=idx+PW; if(canvas[p]==orig){canvas[p]=newv; stackbuf[sp++]=p;} } } if(orig == newcolor){ for(size_t j=0;j<rp;j++) canvas[regionbuf[j]] = orig; } g_visits += rp; *Lp=L; *Hp=Hh; *origp=orig; } static uint8_t *readfile(const char*f, size_t *sz){ FILE*fp=fopen(f,"rb"); if(!fp){fprintf(stderr,"open %s\n",f);exit(1);} fseek(fp,0,SEEK_END); long n=ftell(fp); fseek(fp,0,SEEK_SET); uint8_t*b=malloc(n); if(fread(b,1,n,fp)!=(size_t)n){fprintf(stderr,"read\n");exit(1);} fclose(fp); *sz=n; return b; } int main(int argc,char**argv){ if(argc<6){fprintf(stderr,"usage: %s raw_canvas.bin W H ops.pmaf out_raw.bin\n",argv[0]);return 1;} const char*rawin=argv[1]; W=atoi(argv[2]); H=atoi(argv[3]); const char*pmaf=argv[4]; const char*rawout=argv[5]; size_t psz; uint8_t*pd=readfile(pmaf,&psz); /* parse header: magic8, w4,h4, cnt8, seed8, fill4, pad4 -> 40 bytes */ if(memcmp(pd,"PMAF\x03\r\n\x1a",8)!=0){fprintf(stderr,"bad magic\n");return 1;} uint32_t fw, fh; memcpy(&fw,pd+8,4); memcpy(&fh,pd+12,4); memcpy(&CNT,pd+16,8); memcpy(&SEED,pd+24,8); memcpy(&FILLC,pd+32,4); if(fw!=W||fh!=H){fprintf(stderr,"dim mismatch file %u x %u vs %u x %u\n",fw,fh,W,H);return 1;} /* load canvas raw */ size_t rsz; uint8_t*rd=readfile(rawin,&rsz); if(rsz < (size_t)W*H*4){fprintf(stderr,"raw too small\n");return 1;} canvas=malloc((size_t)W*H*4); memcpy(canvas,rd,(size_t)W*H*4); stackbuf=malloc((size_t)W*H*8u); regionbuf=malloc((size_t)W*H*4u); /* init state */ uint64_t u8; { uint64_t s=CNT; u8=mix1(s); u8=mix2(u8); u8 = (u8>>31) ^ SEED ^ (uint64_t)H ^ ((uint64_t)W<<32) ^ ((uint64_t)FILLC*C3) ^ u8 ^ 0x6f3d9b21a4c875e1ULL; u8=mix1(u8); u8=mix2(u8); u8=mixfull(u8); } uint64_t local_398=0; const uint8_t *ops = pd+40; /* sanity: file size */ if(psz < 40 + CNT*8){fprintf(stderr,"file too small for cnt\n");return 1;} uint64_t totalvisits=0; const char* maxrec_s=getenv("MAXREC"); uint64_t maxrec = maxrec_s?strtoull(maxrec_s,0,10):CNT; if(maxrec>CNT) maxrec=CNT; for(uint64_t i=0;i<maxrec;i++){ if((i & 0xfffff)==0) fprintf(stderr,"i=%llu visits=%llu\n",(unsigned long long)i,(unsigned long long)g_visits); uint64_t raw; memcpy(&raw, ops+i*8, 8); uint64_t base = local_398 ^ u8; uint64_t t = mix1(base); t=mix2(t); uint64_t value = (t>>31) ^ t ^ raw; uint32_t pos = (uint32_t)(value & 0xffffff); uint32_t color = (uint32_t)((value>>24)&0xffffff); if(pos >= (uint32_t)((uint64_t)W*H)){fprintf(stderr,"pos oob %u\n",pos);return 1;} uint64_t L0,H0; uint32_t orig0; fill(pos,color,&L0,&H0,&orig0); /* state update outer */ { uint64_t uv14 = mix1(H0); uint64_t uv16 = mix2(uv14); uint64_t tt = ((uint64_t)orig0)<<24; uint64_t uv14b = ((tt>>30) ^ ((uint64_t)color | tt)) * C1; uv14b = mix2(uv14b); u8 = (uv14b>>31) ^ (L0*C3) ^ uv16 ^ (uv16>>31) ^ u8 ^ local_398 ^ uv14b; u8=mix1(u8); u8=mix2(u8); u8=mixfull(u8); } if(FILLC!=0){ uint64_t uv18=C3; uint64_t L1=0,H1=0; int have1=0; for(uint32_t k=0;k<FILLC;k++){ /* compute c */ uint64_t x14 = local_398 ^ u8 ^ uv18; uint64_t x13 = mix1(x14); uint64_t x16 = x13>>27; uint32_t c; while(1){ x16 = (x16 ^ x13)*C2; c = (uint32_t)(((uint32_t)(x16>>31) ^ (uint32_t)x16) & 0xfffffeu) | 1u; if(c != 0xf2eadf) break; uint64_t y = x14 + GOLD; x14 = ((y>>30) ^ y)*C1; x14 = mix2(x14); x14 = mixfull(x14); x16 = mix1(x14); x13 = x16>>27; } uint64_t ULe1,UHe1,ULe2,UHe2; uint32_t oo1,oo2; int exact = (getenv("EXACT")!=0); if(exact){ fill(pos,c,&ULe1,&UHe1,&oo1); fill(pos,color,&ULe2,&UHe2,&oo2); if(!have1){L1=ULe1;H1=UHe1;have1=1;} } else { if(!have1){ fill(pos,c,&ULe1,&UHe1,&oo1); fill(pos,color,&ULe2,&UHe2,&oo2); L1=ULe1; H1=UHe1; have1=1; } ULe1=L1; UHe1=H1; ULe2=L1; UHe2=H1; } /* state update first inner fill: orig=color, newcolor=c */ { uint64_t v16 = mix2(mix1(UHe1)); uint64_t tt = ((uint64_t)color)<<24; uint64_t v14 = ((tt>>30) ^ ((uint64_t)c | tt)) * C1; v14 = mix2(v14); u8 = (v14>>31) ^ (ULe1*C3) ^ v16 ^ (v16>>31) ^ local_398 ^ u8 ^ v14; u8 = mix1(u8); v16 = mix2(u8); /* store uVar16 carry */ /* state update second inner fill: orig=c, newcolor=color */ uint64_t w8 = mix1(UHe2); uint64_t w14 = mix2(w8); uint64_t tt2 = ((uint64_t)c)<<24; uint64_t w8c = ((tt2>>30) ^ ((uint64_t)color | tt2)) * C1; w8c = mix2(w8c); u8 = (w8c>>31) ^ (ULe2*C3) ^ w14 ^ (w14>>31) ^ local_398 ^ w8c ^ v16 ^ (v16>>31); u8 = mix1(u8); u8 = mix2(u8); u8 = mixfull(u8); } uv18 += C3; } } local_398 += GOLD; } fprintf(stderr,"processed %llu records, visits=%llu\n",(unsigned long long)maxrec,(unsigned long long)g_visits); /* write raw out */ FILE*of=fopen(rawout,"wb"); for(uint64_t i=0;i<(uint64_t)W*H;i++){ uint32_t px=canvas[i]; uint8_t rgb[3]; rgb[0]=(px>>16)&0xff; rgb[1]=(px>>8)&0xff; rgb[2]=px&0xff; fwrite(rgb,1,3,of); } fclose(of); fprintf(stderr,"done %s\n", rawout); return 0; }
Build and run:
gcc -O2 -DPW=1600 -o pmaf pmaf.c
python3 pngraw.py png2raw flag_part_01.png start.raw
MAXREC=300000 ./pmaf start.raw 1600 1200 flag_part_01.pmaf out.raw
python3 pngraw.py raw2png out.raw part01_partial.png 1600 1200
Use this technique when:
record_count == width*height and a seed, signalling a
per-pixel operation stream generated by a hand-rolled PRNG hash chain.0xbf58476d1ce4e5b9,
0x9e3779b97f4a7c15, 0x94d049bb133111eb) — the keystream is trivially
reimplementable from the decompilation.O(n): recognise it is a no-op on the canvas and collapse it to cheap state
advances instead of replaying every fill.MAXREC) to read the flag quickly and
validate against a full run.$ cat /etc/motd
Liked this one?
Pro unlocks every complete writeup and expanded API access. $9/mo.
$ cat pricing.md$ grep --similar