Learn · Tier 2 · Module 4 of 10

Encodings: base64, hex, URL and XOR

Recognize the four encodings malware uses most, undo them, and undo them in layers.

The idea

Encoding changes how bytes are written down, not who can read them. Anyone can undo it without a key. Malware uses it to get binary data through text-only channels (HTTP, JSON, a script) and to hide strings from tools that search for URLs.

Base64

Writes any bytes with 64 safe characters: A-Z a-z 0-9 + /, and = as padding at the end. Every 3 bytes become 4 characters.

  • necropsy → bmVjcm9wc3k=
  • Recognize it: only those characters, length a multiple of 4, often ending in = or ==.
  • Variants: URL-safe base64 uses - and _ instead of + and /. PowerShell's -enc is base64 of UTF-16LE text, so every other byte of the decoded text is 00.

Hex

Each byte as two characters 0-9 a-f. c2 → 6332.

  • Recognize it: only hex digits, even length. Sometimes with \x before each byte (\x63\x32) or spaces between.

URL encoding (percent-encoding)

A byte that is not allowed in a URL is written as % and two hex digits: space is %20, / is %2F, ? is %3F. In form bodies a space can also be +.

  • Recognize it: % followed by two hex digits.

XOR

Each byte is combined with a key byte: where the bits differ, the result has a 1.

  0100 0001   0x41 'A'
^ 0010 0000   0x20 key
= 0110 0001   0x61 'a'

XOR twice with the same key gives back the original: p ^ k ^ k == p. So encrypting and decrypting are the same operation.

  • Single-byte key: every byte XORed with the same value. Only 256 keys exist: try them all and keep the readable result.
  • Repeating key: byte i XORed with key[i % len(key)]. If the plaintext has a known part (like http://), XOR it with the ciphertext there to get the key.
  • Recognize it: unreadable bytes the same length as what they hide, often found next to the key in the code.

Layers

Malware stacks them: XOR, then hex, then base64. Undo them in reverse order, one at a time, and look at the result after each step:

After a step you seeNext
A-Za-z0-9+/=base64
%xxURL decode
0-9a-f, even lengthhex
1f 8b or 78 9c at the startgzip or zlib: inflate it
Unreadable, same lengthXOR, look for a key
Readable textdone

Walkthrough: in the browser

Showing the browser path.

The Decoder tab is CyberChef, running in your browser.

  1. Open the practice puzzle's workspace. In Files, press Send to the decoder next to dropper.py.txt. The whole file lands in CyberChef's Input.
  2. Delete everything in the input except the base64 string: join the lines of CFG into one, without the quotes.
  3. In Operations search for From Base64 and drag it into Recipe. The Output shows the result: %-escapes.
  4. Add URL Decode. The output is now hex digits.
  5. Add From Hex. Now it is unreadable bytes: XOR.
  6. Add XOR, set the key to 37 and its type to Hex (the code says K = 0x37). The output is the configuration.
  7. CyberChef keeps the recipe in its URL, so a recipe can be saved and shared. Hints in these puzzles can open the decoder with a recipe filled in: look for Open in the decoder.

The Terminal line under the decoder shows the shell equivalent of each step.

If you get stuck on a layer, Magic (search for it in Operations) tries common decodings and shows which give readable output.

Practice

More at this level

Worked examples

Past dailies that use this module, with their explainers once solved.

Checkpoint

Answers from either path count.