Medium
ec2-dropper — Writeup
A vault that locked itself with the cloud
Type: malware code review / cloud
Scenario
A cloud server at Tanvoro — an Amazon EC2 instance, which is just a computer you rent in Amazon's data center — was caught running a script it had no business running. It showed up right after someone logged in from an address nobody recognized. The script keeps its settings in a locked "vault" file, and it does something clever: it doesn't store the key anywhere. It rebuilds the key on the fly from the server's own identity, betting that you can only open the vault on that exact server.
We grabbed the script, the vault, and the server's logs. Let's call that bet.
The incident note
The bundle ships with an incident.txt, so that's the first thing I open.

INCIDENT NOTE — CERT
---------------------
An EC2 instance (web tier) was found running an unauthorised scheduled script,
/opt/agent/vault.py, installed after an SSH login from an unfamiliar address.
The script keeps its configuration in an encrypted vault (vault.bin) and the key
is NOT stored on disk — it is derived at runtime from the instance's own
identity. We pulled the script, the vault, and the host logs below.
Your task: review the script, work out how the vault is keyed, and recover the
operator's configuration note from vault.bin.The note hands me two useful facts. The key is "derived at runtime from the instance's own identity," so it's built from something about the machine rather than written down where I could grep for it. And the intrusion came in over SSH "from an unfamiliar address," which means the host logs will carry a timeline, and very likely the machine's identity as well.
The rest of the bundle:

I'll review vault.py first, since that's where the lock is, then go looking for the
key in the logs.
Source code analysis
The script is short (about 30 lines), which is good news: the entire puzzle is in understanding it, not running it. Let's read it top to bottom and narrate what each piece is telling us.
SALT = "s4lt_"A hardcoded SALT. File that away — it's one of the two ingredients that build the
encryption key.
def instance_id() -> str:
cache = Path(__file__).with_name("instance-id.cache")
if cache.exists():
return cache.read_text().strip()
import urllib.request
url = "http://169.254.169.254/latest/meta-data/instance-id"
return urllib.request.urlopen(url, timeout=1).read().decode()This is the important function. The script wants the machine's instance-id, and it
has two ways to get it: a cached copy in a file called instance-id.cache next to the
script, or a live call to 169.254.169.254. That cache file wasn't pulled with the
evidence, so it's not an option here. The address is the EC2 Instance Metadata
Service (IMDS) — a link-local endpoint that only answers when you're actually running
on the instance.
That detail quietly rules out the lazy approach. If I try to run vault.py on my own
machine, the IMDS call goes nowhere, times out after a second, and the script dies.
That's exactly what the note was pointing at: the key is tied to the instance's
identity, and whoever built this assumed the identity couldn't be recovered off the
box.
def derive_key(iid: str) -> bytes:
return hashlib.sha256((SALT + iid).encode()).digest()The key is SHA-256(SALT + instance_id). SHA-256 always returns 32 bytes, which is an
AES-256 key. The part that matters: SHA-256 is deterministic. The same input always
produces the same output, so once I know the instance-id I can reproduce this key
exactly. Nothing to crack; I just need one value.
def open_vault(path: str) -> dict:
blob = Path(path).read_bytes()
iv, ciphertext = blob[:16], blob[16:] # IV is prepended
key = derive_key(instance_id())
plain = AES.new(key, AES.MODE_CBC, iv).decrypt(ciphertext)
plain = plain[:-plain[-1]] # strip PKCS#7 padding
return json.loads(plain)And this lays out vault.bin for me. It's AES in CBC mode. The first 16 bytes of the
file are the IV — the random starter value CBC needs, which isn't secret and so gets
prepended to the ciphertext. Everything after those 16 bytes is the ciphertext. After
decryption the code strips PKCS#7 padding (plain[:-plain[-1]] just means "the final
byte says how many filler bytes to remove") and parses the result as JSON.
So the review leaves me with a complete recipe:
- Key =
SHA-256("s4lt_" + instance-id) - File =
IV (16 bytes) + AES-CBC ciphertext, PKCS#7 padded, JSON inside
The only blank left is the instance-id, and the code already told me where that normally comes from. Over to the logs.
The logs
cloud-init-output.log — the machine's identity
cloud-init configures an EC2 instance on boot, and along the way it records what the
machine is:
instance-id: i-0a1b2c3d4e5f67890
instance-type: t3.small
region: ap-south-1
ami-id: ami-0f3c9e4b21d7a5c88
local-hostname: ip-10-0-3-47.ap-south-1.compute.internalThere's the missing ingredient:
instance-id: i-0a1b2c3d4e5f67890The one value the attacker assumed nobody could pull off the box, sitting in a boot log
the responders collected. That's everything derive_key needs.
syslog.txt — the intrusion
Oct 13 02:14:07 ip-10-0-3-47 sshd[2211]: Accepted publickey for ubuntu from 203.0.113.44 port 51022
Oct 13 02:15:51 ip-10-0-3-47 sudo: ubuntu : ... USER=root ; COMMAND=/bin/mkdir -p /opt/agent
Oct 13 02:16:03 ip-10-0-3-47 python3[2290]: agent: loaded config vault (version 2)
Oct 13 02:16:03 ip-10-0-3-47 CRON[2301]: (ubuntu) CMD (/usr/bin/python3 /opt/agent/vault.py)Read top to bottom, this is the whole break-in. At 02:14:07 someone logs in as
ubuntu from 203.0.113.44 — the unfamiliar address from the note. At 02:15:51
they sudo mkdir /opt/agent, building a home for the dropper. At 02:16:03 the agent
runs for the first time, reports loaded config vault (version 2), and cron fires it
off.
One thing to hold onto: the agent logged config vault (version 2). If the config I
decrypt later also says version 2, that lines up.
crontab.txt — persistence
*/5 * * * * /usr/bin/python3 /opt/agent/vault.py >/dev/null 2>&1Here's how it stays alive: */5 * * * * runs it every five minutes, and
>/dev/null 2>&1 throws away any output so it never makes noise. Cheap, quiet, and the
kind of entry that survives for months because nobody looks.
Between the three logs, the story is complete before I've decrypted a byte: SSH in from
203.0.113.44, create /opt/agent, drop vault.py, and wire it into cron to run
silently every five minutes — with the instance-id the encryption depends on recorded
right there in the boot log.
A look at vault.bin
Before writing any decryption, a two-second look at the file:
$ xxd agent/vault.bin | head -2
00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff .."3DUfw........
00000010: b5ad 2183 00a3 03a0 87ba 4f0c 989a c245 ..!.......O....E
$ wc -c < agent/vault.bin
128128 bytes. The first sixteen (0011 2233 ... eeff) are the IV that open_vault sliced
with blob[:16], and the remaining 112 are ciphertext — seven AES blocks of 16. It
matches the layout the code described, which is reassuring before I commit to it.
Opening the vault
With the instance-id in hand, I just follow the recipe from the review.
import hashlib, json
from Crypto.Cipher import AES # pip install pycryptodome
iid = "i-0a1b2c3d4e5f67890" # from cloud-init-output.log
key = hashlib.sha256(("s4lt_" + iid).encode()).digest() # SALT + iid -> SHA-256
blob = open("agent/vault.bin", "rb").read()
iv, ciphertext = blob[:16], blob[16:]
plain = AES.new(key, AES.MODE_CBC, iv).decrypt(ciphertext)
plain = plain[:-plain[-1]] # drop PKCS#7 padding
print(json.loads(plain)){
"config_version": 2,
"collected_region": "ap-south-1",
"operator_note": "InIt{imds_k3y3d_s3cr3t_v4ult}"
}The flag is the operator_note:
And the config matches the evidence: config_version is 2, the same as the syslog
line, and collected_region is ap-south-1, the same as cloud-init. The pieces agree,
so the instance-id was right.
CyberChef route (no Python)
xxd -p agent/vault.binto dump the file as hex. The first 32 hex characters are the IV.- SHA2-256 the string
s4lt_i-0a1b2c3d4e5f67890to get the key. - AES Decrypt, mode CBC, key = that hash (hex), IV = the first 16 bytes (hex), input = the rest. The JSON falls out.
References
- AWS — Instance metadata and user data (IMDS): https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
- AWS — Use IMDSv2: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html
- AWS Security Blog — Defense in depth against SSRF and the metadata service: https://aws.amazon.com/blogs/security/defense-in-depth-open-firewalls-reverse-proxies-ssrf-vulnerabilities-ec2-instance-metadata-service/
- 2019 Capital One data breach: https://en.wikipedia.org/wiki/2019_Capital_One_data_breach
- MITRE ATT&CK — Valid Accounts (T1078): https://attack.mitre.org/techniques/T1078/
- MITRE ATT&CK — Scheduled Task/Job: Cron (T1053.003): https://attack.mitre.org/techniques/T1053/003/
- MITRE ATT&CK — Unsecured Credentials: Cloud Instance Metadata API (T1552.005): https://attack.mitre.org/techniques/T1552/005/
- NIST SP 800-38A — Block cipher modes of operation (CBC): https://csrc.nist.gov/publications/detail/sp/800-38a/final
- RFC 5652 §6.3 — PKCS#7 / CMS padding: https://datatracker.ietf.org/doc/html/rfc5652#section-6.3
- NIST SP 800-132 — Password-based key derivation (why a single SHA-256 is a weak KDF): https://csrc.nist.gov/publications/detail/sp/800-132/final

Comments
0 comments$ no comments yet. start the thread.