Skip to content

TJCTF 2026

TJCTF 2026

Writeup for the rotated challenge from TJCTF 2026.

TJCTF 2026

rev/ Rotated

Description

This file isn't making any sense to me. can you discover what it means?

First Look

We were handed a single file called rot.

$ file rot
rot: data

So whatever this is, it has no recognizable magic bytes. xxd showed something more interesting though:

00000000: 9c62 6963 1f1e 1e1d 1d1d 1d1d 1d1d 1d1d  .bic............
00000010: 201d 5b1d 1e1d 1d1d e575 1d1d 1d1d 1d1d   .[......u......
00000020: 5d1d 1d1d 1d1d 1d1d 1d1d 1d1d 1d1d 1d1d  ]...............
00000030: 1d1d 1d1d 5d1d 551d 201d 1d1d 1d1d 1d1d  ....].U. .......

Notice anything? The byte 0x1d is everywhere.

Figuring out the encoding

The challenge name is rot, maybe “rotation” of some kind. The first guess was bit rotation.

I figured out it wasn’t bit rotation pretty quickly. Bit rotation preserves the number of set bits in a byte. The first byte of the file is 0x9c (four bits set). If the original file were an ELF or PNG or any common binary, the first byte should be something like 0x7f (six bits) or 0x89 (three bits).

So the transform has to actually change bit counts.

Now the 0x1d clue becomes useful. Real binaries are full of zero bytes. ELF headers have a 9 byte padding region that is always zero, So whichever byte value dominates an encoded binary is almost certainly the encoding of 0x00.

That gives us a candidate key. If the encoding is “add 0x1d to every byte”, then subtracting 0x1d from the first four bytes should give a recognizable file magic:

0x9c - 0x1d = 0x7F
0x62 - 0x1d = 0x45   'E'
0x69 - 0x1d = 0x4C   'L'
0x63 - 0x1d = 0x46   'F'

7F 45 4C 46 is the ELF magic number. so the encoding is definitely just:

encoded[i] = (original[i] + 0x1d) mod 256

A small Python one liner reverses it:

Bash
python3 -c "d=open('rot','rb').read(); open('rot.elf','wb').write(bytes((b-0x1d)%256 for b in d))"
chmod +x rot.elf
$ file rot.elf
rot.elf: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), statically linked, no section header

A static x86_64 PIE binary. Progress.

The binary is packed

strings rot.elf was strange. There were a few obvious symbols (perror, strlen, open, write), but also fragments that looked like shell code mixed with random gibberish, plus this:

$Info: This file is packed with the UPX executable packer http://upx.sf.net $
$Id: UPX 4.24 Copyright (C) 1996-2024 the UPX Team. All Rights Reserved. $

UPX is a runtime executable compressor. It takes a normal ELF, compresses the original code and data into a blob, and prepends a tiny stub. When you run the packed file, the stub decompresses the original into memory and jumps to it. The packed file still works, but its real contents are hidden behind compression, which is why strings was almost useless.

The fix is just to unpack it.

Bash
sudo apt install -y upx-ucl
upx -d rot.elf -o rot.unpacked

Now strings rot.unpacked is much more revealing. Symbols like open, write, perror, no other libc calls, plus a script.sh string and a chunk of obfuscated bash. So this binary almost certainly just writes a script to disk (or to a pipe) and executes it.

The obfuscated script

The embedded script looks like this:

Bash
script.sh
#!/bin/bash
   ${*,} ${@//nj2p#@$\!/^Bis\X}  e\val   "$(      ${*#+*=\gr4#}  ${*~~}   'p'r\i"n"tf 'H4sIAEDAzmkC/0tNzshXUPLJz8/OzEtXSMsvUkhUSMtJTLdXUlBWSHEvyEpxjzKPzAo0THSzzPY18jL0y7Es8XMJNfY19rJ0Tre1BQCGqZA9QQAAAA=='  ${!*} | ${*%b:d\)} b""'a'''s"e"6"${@^^}"4  -d   ${*}  |  ${*//\FsXY^F} ${*#0ms7JMci}  \gu${*//.Km\`1B/vmdvatBX}n$'\172''i'p  -c  ${*,,}      )"  ${*%TB.h}

This is a classic bash obfuscation trick.

If you mentally delete every ${*...} and ${@...} and unquote the chopped up letters, what is left is:

eval "$(printf 'H4sIAEDAzmkC...QAAAA==' | base64 -d | gunzip -c)"

So the binary’s real job is: print a base64 string, decode it, gunzip it, and eval the result.

A few concepts worth naming here:

The string starts with H4sIA, which is 1f 8b 08 00 in base64. 1f 8b is the gzip magic number, so we know without decoding that the inner blob is gzip compressed.

eval in bash takes a string and runs it as if you had typed it. It is the standard way to execute “code as data”, and a standard CTF and malware indicator.

$'\172' is the bash way of writing the byte 0x7a, which is the letter z.

Extracting the payload without running it

You should never eval an unknown blob. Just decode the same pipeline manually:

Bash
printf 'H4sIAEDAzmkC/0tNzshXUPLJz8/OzEtXSMsvUkhUSMtJTLdXUlBWSHEvyEpxjzKPzAo0THSzzPY18jL0y7Es8XMJNfY19rJ0Tre1BQCGqZA9QQAAAA==' \
  | base64 -d \
  | gunzip -c
  
echo "Looking for a flag?" # dGpjdGZ7YjQ1aF9kM2J1Nl9tNDU3M3J9Cg== 

That prints the final command, which is a plain echo of the flag string.

Bash
echo 'dGpjdGZ7YjQ1aF9kM2J1Nl9tNDU3M3J9Cg==' | base64 -d

tjctf{b45h_d3bu6_m4573r}

Flag

tjctf{b45h_d3bu6_m4573r}

Discussion

Loading discussion…

Related solves

Keep reading

Complete archive