Labs / Cryptography at Work / Hashes, passwords and obfuscation

Lab 5 of 5 · Hashes, passwords and obfuscation

Obfuscated scripts: peeling an encoded dropper back to what it does

Peel a wall of base64, then hex, then a one-byte XOR, with standard tools and no key, until the payload proves obfuscation isn't encryption.

Start this lab 5 steps

The idea

Ishaan is handling an incident for Elmgrove Dental, a small clinic. On Monday morning the receptionist's PC flagged a file in its temp folder. Opened in an editor, the file is a solid wall of base64, made unreadable on purpose. The clinic wants to know what it does and whether any other machine talked to it.

The wrapping is obfuscation: base64, then hex, then a short XOR. Base64 and hex need no key at all, and the XOR's key is tiny. That is encoding, not encryption. It hides the content from a glance and from a scan for a known string, but not from someone who sits down to peel it. You'll take the layers off one at a time, down to the marker and the beacon host the script carried.

You work the sample statically: decode it, never run it. The payload here is an inert stand-in that says so in its own text. A real dropped script only ever runs in a sandbox built for it, never on your own machine.

The alphabet names the wrapper
The alphabet names the wrapper
One layer off, another under it
One layer off, another under it
Two hex characters make one byte
Two hex characters make one byte
256 keys, and only one reads
256 keys, and only one reads
Three wrappers, and not one a lock
Three wrappers, and not one a lock

Steps

  1. The file from the reception PC

    You're on your analyst machine, workstation, with the sample saved at /evidence/stage.txt. Read it before you touch it. The characters a file uses tell you which encoding wraps it, and so which tool to pick next.

    Look for a single long line made of letters, digits, + and /, ending in =. That alphabet, with the = padding at the end, is the mark of base64: a way to write any bytes using only 64 safe characters.

    workstation:~# cat /evidence/stage.txt
  2. Peel the base64: hex underneath

    Take the outer layer off. base64 -d reads the file and prints what is underneath. It needs no key and no password, which is why base64 protects nothing.

    Before you call it done, read what comes out. A payload would read as text. A long run of only 0–9 and a–f is something else: hex, which writes each byte as two of those characters.

    workstation:~# base64 -d /evidence/stage.txt
  3. Decode the hex: scrambled text is a XOR tell

    Take the hex layer off too. xxd -r -p reads a plain hex string and writes the bytes it stands for. The pipe, |, hands the output of base64 -d straight to it, so you peel both layers in one line.

    This time the output is almost text: letters and punctuation, but no words. Bytes that are all printable yet spell nothing are the usual sign of a single-byte XOR. XOR flips bits in each byte against a key, and here every byte was flipped against the same one-byte key.

    workstation:~# base64 -d /evidence/stage.txt | xxd -r -p
  4. Brute the one-byte XOR: the payload appears

    XOR with a key is a cipher, not an encoding. But one byte holds only 256 values, so a one-byte key leaves only 256 keys to try. A computer tries all of them in well under a second.

    The short Python script below does that. It XORs the bytes against each key from 0 to 255, and prints the result only when every character is printable and there are real spaces in it: the shape of a sentence. Pipe the decoded bytes into it.

    workstation:~# base64 -d /evidence/stage.txt | xxd -r -p | python3 /evidence/unxor.py
  5. What the dropper was, and the indicators to block

    The recovered line is what Ishaan came for. Each string in it is an indicator of compromise: something concrete to block and search for. The marker names the tool. The beacon host is where the script would have called home, so it goes on the firewall's block list and into a search of the clinic's proxy logs, to find any other machine that already talked to it.

    Save the recovered line next to the sample, so the case file holds the result as well as the evidence, and read it back.

    workstation:~# base64 -d /evidence/stage.txt | xxd -r -p | python3 /evidence/unxor.py > /evidence/payload.txt
    workstation:~# cat /evidence/payload.txt

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab