| Follow @anir0y | |
|---|---|
| Scripting |
Scripting is a medium TryHackMe room made of three small programming challenges that get harder as you go: peel 50 layers of Base64 off a file, chase a web server that hops to a new port every four seconds, and talk AES-GCM to a UDP server until it hands over the right flag. There is nothing to exploit; the whole room is about writing the glue code you end up needing in real engagements. If you are working through the Python side of THM, it pairs well with my Python for Pentesters walkthrough and the Custom Tooling with Python room. I solved everything from my Mac over the THM VPN, and all three scripts are in the repo.
Task 1: Decode a file that was Base64 encoded 50 times
The task file (b64_1550406728131.txt) is a 32 MB blob of Base64. Encoding grows data by a third every round, so 50 rounds turn a 13 character string into 32 MB. The fix is a loop. The room asks for both Bash and Python, so I wrote both.
Python:
import base64
import sys
data = open(sys.argv[1], "rb").read()
for _ in range(50):
data = base64.b64decode(data)
print(data.decode())
Bash, keeping the intermediate value in a variable and piping it through base64 -d each round:
data=$(cat "$1")
for i in $(seq 1 50); do
data=$(printf '%s' "$data" | base64 -d)
done
echo "$data"
Both agree:

The final string is HackBack2019=. The trailing = looks like Base64 padding, but it is part of the answer: the mask is 13 characters, and decoding it once more gives garbage, so stop at exactly 50 rounds.
Task 2: Chase a port-hopping web server
Port 3010 explains the game. The server listens on one port at a time, and each live port answers with <operation> <number> <next port>, for example add 900 23456. You start from 0, apply every operation in order, follow the next port, and stop when a port answers STOP (port 9765). Each port is only live for about four seconds, and the chain starts at port 1337, so you may have to wait for the cycle to come round.
The 3010 page also shows the current port (“Its currenly on port 7832”), which is a nice sanity check but not something to start from: you have to begin at 1337 or the running total is wrong.
The core of my solver is a raw-socket HTTP GET that returns None while a port is closed, plus a tight loop that keeps knocking on the next port until it answers:
OPS = {
"add": lambda a, b: a + b,
"minus": lambda a, b: a - b,
"multiply": lambda a, b: a * b,
"divide": lambda a, b: a / b,
}
def get(port_number):
try:
with socket.create_connection((HOST, port_number), timeout=2) as s:
s.sendall(f"GET / HTTP/1.1\r\nHost: {HOST}:{port_number}\r\n\r\n".encode())
data = b""
while chunk := s.recv(4096):
data += chunk
except OSError:
return None
return data.split(b"\r\n\r\n", 1)[-1].decode(errors="replace").strip()
number, port, seen = 0.0, 1337, []
while True:
body = get(port)
if body is None or body in seen[-1:]:
time.sleep(0.1)
continue
seen.append(body)
if body.split()[0] == "STOP" or port == 9765:
break
op, value, next_port = body.split()
number = OPS[op](number, float(value))
port = int(next_port)
print(f"{number:.2f}")
Two details matter. Polling every 100 ms means you catch each port within its four-second window. Keeping the running total as a float and rounding only at the end follows the question’s instruction to round “at the end of your calculation”; rounding at every hop drifts after the divide steps (I replayed the log that way and got 344769.09, which the mask would happily accept and the grader would reject).
The run took 34 hops, including some nasty ones (multiply by -2, multiply by -12, divide by 3):

The final number is 344769.12.
Task 3: Decrypt AES-GCM flags from a UDP server
The last VM runs a UDP server on port 4000. It expects a small conversation, and each reply tells you the next word to send:
# send: hello
You've connected to the super secret server, send a packet with the payload ready to receive more information
# send: ready
key:thisisaverysecretkeyl337 iv:secureivl337 to decrypt and find the flag that has a SHA256 checksum of <32 raw bytes> send final in the next payload to receive all the encrypted flags
So the server gives you the AES key (24 bytes, AES-192), the 12-byte GCM nonce, and the raw SHA256 digest of the real flag. Each final returns the ciphertext, and a second final returns the 16-byte GCM tag. Every round is a different candidate flag, and only one hashes to the checksum. The two traps the room warns about are real:
- Everything is bytes. The checksum is a raw 32-byte digest embedded in the message, not hex, so compare it with
hashlib.sha256(...).digest(), nothexdigest(). - PyCA’s
AESGCM.decryptwants the ciphertext and tag concatenated (ct + tag). The server sends them as two separate packets, so glue them back together.
aes = AESGCM(key)
for attempt in range(1, 501):
ciphertext = ask(b"final")
tag = ask(b"final")
plaintext = aes.decrypt(iv, ciphertext + tag, None)
if hashlib.sha256(plaintext).digest() == checksum:
print(f"[+] attempt {attempt}: {plaintext.decode()}")
break
The decoys all look like THM{XXXXXXXX} with random uppercase characters, so you cannot pick the real flag by eye from a single round. Mine matched on the sixth attempt:

The flag is THM{eW-sCrIpTiNg-AnD-cRyPtO}.
One practical note: Tasks 2 and 3 each have their own lab machine, and starting the Task 2 machine terminates the Task 3 one. I solved Task 3 first, captured the evidence, submitted, and only then switched machines.
Takeaways
- Let the server tell you the protocol, then script the loop. Every challenge here is “observe one exchange by hand, then automate it”: one decode becomes 50, one GET becomes a port-chasing loop, one
finalbecomes a decrypt-and-verify loop. The same pattern drives real work, from unwrapping nested encodings in malware to replaying multi-step API flows. - Respect byte-level details in crypto glue code. Raw digest versus hex, ciphertext and tag as separate packets versus PyCA’s concatenated input, the nonce length: getting any one wrong throws an
InvalidTagor a silent mismatch, not a helpful error. Print lengths and types first, and keep a known-good check (here, the SHA256) to tell you when you are right.
Room solved 100%: 3 tasks, 3 answers.