RC4 and XOR are the workhorses of malware config encryption: simple to implement, good enough to hide from static view, and everywhere in crimeware and targeted implants alike. The analyst's job is stereotyped: find the blob, find the key, reimplement the cipher, parse the plaintext. This sub-skill covers each step with the recognition patterns that tell RC4 from XOR from something custom. Scope: extract configs only from samples you are authorized to handle, in an isolated lab, during authorized DFIR or threat intel work.
Quick Workflow
- Locate the encrypted blob (entropy map) and the crypto code near its cross-references.
- Classify: RC4 (256-byte state, KSA loop), single/multi-byte XOR (tight XOR loop), or custom.
- Recover the key: hardcoded bytes, ASCII string nearby, brute force, or runtime memory.
- Implement the decoder in Python and decrypt the blob.
- Parse the plaintext config into fields.
- Validate against sandbox network behavior.
1. Recognizing RC4 in disassembly
KSA shape (key scheduling):
for i in 0..255: S[i] = i
j = 0
for i in 0..255:
j = (j + S[i] + key[i % keylen]) % 256
swap(S[i], S[j])
PRGA shape (keystream):
i = (i+1) % 256; j = (j+S[i]) % 256
swap(S[i], S[j])
K = S[(S[i]+S[j]) % 256]
out = in ^ K
In disassembly: a 256-iteration init loop writing sequential bytes to a 256-byte buffer, then a second loop with two indices, swaps, and a final XOR. capa often flags "encrypt data using RC4" directly; use that to jump to the function. Modified RC4 (extra swaps, skipped bytes) still shows the 256-byte state; note the modification before reimplementing.
2. Recognizing XOR variants
Single-byte: xor byte ptr [reg], 0x4A (immediate constant)
Multi-byte: key in a register or buffer, index % keylen
Rolling: key updated per iteration (add, ror)
Single-byte XOR is brute-forced in milliseconds (see the string-decoding sub-skill). Multi-byte XOR needs the key: check for a hardcoded byte array near the loop, or an ASCII string used as key. Rolling XOR requires reading the update step; reimplement exactly, since one wrong rotation breaks everything.
3. Key recovery strategies
In order of effort:
- Hardcoded: byte array or ASCII string in the data section near the decode function. Check cross-references to the decode call.
- Nearby strings: FLOSS output around the crypto function often contains the key as a readable string.
- Brute force: single-byte XOR (256 tries); short multi-byte keys when plaintext is predictable (starts with "http").
- Runtime memory: break after the decode in the debugger and read the key schedule or plaintext directly; works when the key is derived at runtime.
- Known-plaintext: if you know the plaintext starts with "http://", derive keystream bytes and work backward to the key.
# Known-plaintext keystream recovery for stream ciphers
cipher = blob[:16]
known = b"http://beacon.exa"[:16]
keystream = bytes(c ^ k for c, k in zip(cipher, known))
print("keystream:", keystream.hex())
# For RC4, this keystream constrains the key; for XOR it IS the key
4. Correct RC4 implementation
def rc4(key: bytes, data: bytes) -> bytes:
if not key:
raise ValueError("empty key")
s = list(range(256))
j = 0
for i in range(256):
j = (j + s[i] + key[i % len(key)]) % 256
s[i], s[j] = s[j], s[i]
i = j = 0
out = bytearray()
for byte in data:
i = (i + 1) % 256
j = (j + s[i]) % 256
s[i], s[j] = s[j], s[i]
out.append(byte ^ s[(s[i] + s[j]) % 256])
return bytes(out)
Test the implementation against a known vector before touching the sample (RC4("Key", "Plaintext") is the standard check). Then try each candidate key; the right one yields structured plaintext. If none works, suspect modified RC4: compare the disassembly's KSA/PRGA against this reference line by line.
5. Parsing the decrypted config
# Common layout: null-delimited fields
pt = rc4(key, blob)
fields = [f for f in pt.split(b"\x00") if f]
for f in fields:
print(classify(f), f[:80])
def classify(f: bytes) -> str:
s = f.decode(errors="replace")
if s.startswith(("http://", "https://")): return "C2_URL "
if re.fullmatch(r"\d{1,3}(\.\d{1,3}){3}", s): return "IP "
if f.isdigit() or (len(f) <= 4 and f.isdigit()): return "NUMBER "
return "STRING "
Example output shape:
C2_URL b'http://beacon.example-cdn[.]net/api/v2/beacon'
C2_URL b'http://backup.example-cdn[.]net/api/v2/beacon'
NUMBER b'60'
STRING b'CAMP-2026-118'
STRING b'Global\\NimbusSyncMutex_7f3a'
Primary plus backup C2, 60-second beacon, campaign ID, mutex: the full operational picture from one blob. Validate the interval against the sandbox pcap before publishing.
Edge cases
- RC4 with the keystream offset (first N bytes discarded): common hardening; try discards of 256, 512, 768, 1536 bytes.
- Key longer than 256 bytes: still valid RC4; do not truncate.
- Blob with a header (magic, length, version) before the ciphertext: parse the header first, decrypt only the payload bytes.
- Double encryption (XOR then RC4): decode outside-in; the outer layer's plaintext is the inner layer's ciphertext.
- Wrong blob: the binary may contain several encrypted regions (strings vs config); the config is usually the largest.
Escalation
If the cipher is custom (not RC4/XOR/AES) after disassembly review, escalate to a reverse engineer for a dedicated crypto analysis.
If key recovery needs runtime derivation from victim-specific values, document the derivation algorithm; the IOC becomes the algorithm plus observed instances.
If the decrypted config references a second-stage download, extract and analyze it as a new sample; do not stop at the config.
If key material is split between the binary and a C2 response, capture both in the same detonation or the decode cannot be validated.
Detection and defender view
RC4/XOR configs are why network IOCs must be extracted, not just observed: the backup C2 in the config may never have been contacted during detonation, but it is live infrastructure. Publish primary and backup addresses with a note on which were observed vs extracted. Keep the decoder script: when the family rotates keys next month, only the key changes and re-extraction takes minutes. Track key reuse across samples; a shared key across families signals a shared builder.
Key references
MITRE ATT&CK: T1573 (Encrypted Channel), T1027 (Obfuscated Files or Information)
Practical Malware Analysis (Sikorski and Honig), Chapter 13 (data encoding)
capa crypto capability rules: https://github.com/mandiant/capa-rules
RC4 reference (for implementation validation): https://en.wikipedia.org/wiki/RC4
Malpedia (family config references): https://malpedia.caad.fkie.fraunhofer.de/
