TExcelTAD
Home › Malware Analysis, Digital Forensics & Incident Response › Handling Samples: Storage, Transfer and Defanging

Handling Samples: Storage, Transfer and Defanging

A suspicious file arrives in a ticket: an email attachment called Invoice_0115.pdf.exe, forwarded by a worried user. Before any analysis, three things can already go wrong. Someone double-clicks it while "having a quick look". The mail system or the analyst's anti-virus deletes it, and the evidence is gone. Or the analyst pastes the URL found inside it into a chat channel, where the chat client helpfully fetches a preview from the attacker's server. Handling discipline prevents all three.

Definitions

  • Sample: one file under analysis, identified by its SHA-256, never only by its name.
  • Intake: the first step: hash the file, record where it came from, rename it to a non-executable name, store it.
  • Quarantine store: the place samples live: an encrypted volume or a dedicated repository, not the analyst's Downloads folder.
  • Password-protected archive: the conventional way to move samples. By long community convention the password is infected (sometimes malware). The password is not secrecy; it stops mail filters and anti-virus from deleting the file in transit and stops anyone from running it by accident. Use AES encryption in the archiver (for example 7-Zip's .7z format, which encrypts with AES-256, with -p and -mhe=on to hide file names too; in .zip archives ask for AES with -mem=AES256), since legacy ZIP encryption (ZipCrypto) is weak.
  • Defanging: rewriting indicators so that they cannot be clicked or resolved by accident: hxxp://update[.]beacon[.]lab[.]test/ping, 10[.]10[.]0[.]66, ops[@]lab[.]test. Refanging reverses it inside a tool that needs the real value.

Intake in code

The example implements intake for a synthetic sample: it hashes the bytes, derives a storage name from the hash with a harmless extension, records the provenance in a manifest, and defangs the indicators that the analyst will paste into the ticket. The "sample" is a benign text blob; the URL points to a name under lab.test, which never resolves outside the lab.

python · try it yourself
import hashlib, json, re

def defang(s: str) -> str:
    s = re.sub(r"^http", "hxxp", s, flags=re.I)
    s = re.sub(r"(?<=\w)\.(?=\w)", "[.]", s)          # dots between word characters: domains and IPs
    return s.replace("@", "[@]")

def refang(s: str) -> str:
    return re.sub(r"^hxxp", "http", s, flags=re.I).replace("[.]", ".").replace("[@]", "@")

received = {"name": "Invoice_0115.pdf.exe", "from": "ticket INC-0042, forwarded by a user",
            "received_utc": "2026-01-15T09:20:00Z", "handler": "analyst-1"}
sample = b"MZ\x90\x00 LABSAMPLE benign stand-in: http://update.beacon.lab.test/ping contact ops@lab.test 10.10.0.66"

entry = {
    "sha256": hashlib.sha256(sample).hexdigest(),
    "md5": hashlib.md5(sample).hexdigest(),          # still requested by some feeds; never the only identifier
    "size": len(sample),
    "stored_as": hashlib.sha256(sample).hexdigest() + ".bin",
    **received,
}
print(json.dumps(entry, indent=2))

iocs = ["http://update.beacon.lab.test/ping", "10.10.0.66", "ops@lab.test"]
for ioc in iocs:
    d = defang(ioc)
    print(f"{ioc:<36} -> {d:<44} round trip ok: {refang(d) == ioc}")
{
  "sha256": "254365ef23aae2f72f1a8c188f7e3e24a73371847d9594eed72fb8f7b2497197",
  "md5": "6b71a104aad85c12901ad1d514d0af02",
  "size": 98,
  "stored_as": "254365ef23aae2f72f1a8c188f7e3e24a73371847d9594eed72fb8f7b2497197.bin",
  "name": "Invoice_0115.pdf.exe",
  "from": "ticket INC-0042, forwarded by a user",
  "received_utc": "2026-01-15T09:20:00Z",
  "handler": "analyst-1"
}
http://update.beacon.lab.test/ping   -> hxxp://update[.]beacon[.]lab[.]test/ping     round trip ok: True
10.10.0.66                           -> 10[.]10[.]0[.]66                             round trip ok: True
ops@lab.test                         -> ops[@]lab[.]test                             round trip ok: True

Notice what the manifest keeps: the original file name as a field (it is evidence: the double extension .pdf.exe is a classic trick that relies on hidden extensions), while the stored name is the hash with .bin, which no operating system will execute on a double-click. The MD5 is recorded because some feeds still use it, but the SHA-256 is the identifier (why).

Moving and sharing samples

Situation Practice
Into the lab Encrypted archive copied over a dedicated transfer path (a read-only ISO attached to the VM works well); hash checked after the copy
Between analysts Archive with the agreed password, sent through the team's sample repository, never plain email attachments
To another organisation Only through trusted sharing channels (an ISAC, a CERT, a vendor's private submission portal), with permission from whoever owns the incident
To a public scanner or sandbox Only if policy allows. Many services share uploads with paying customers; a targeted sample can contain the victim's names, internal hosts or documents, and the attacker may watch for their file appearing. Searching by hash reveals much less than uploading the file

Possessing and distributing working malicious code can also be a legal matter in some jurisdictions, depending on intent and context (UK and EU law, Vietnamese law). Professional labs have written procedures: follow them.

How mishandling is detected

  • Inventory checks: every file in the quarantine store must have a manifest entry and a matching hash; a file without one means someone bypassed intake.
  • Leak checks: search chat and ticket systems for undefanged indicators from the current case (a regular expression on http:// plus the case's domains).
  • Endpoint alerts: analyst workstations outside the lab should never execute files from the quarantine path; alert on that.

How it is done right

  • Hash first, before anything else touches the file, and record the hash in the ticket.
  • Rename to <sha256>.bin (or .malware, .vir); keep the original name in the manifest.
  • Keep samples encrypted at rest and in transit, with access logged.
  • Defang every indicator you write for humans; keep machine-readable feeds (STIX, CSV for a firewall) in their real form, in the right system (STIX).
  • Keep the context: where it came from, who touched it, when. That record becomes part of the chain of custody if the case goes further.

Pitfalls

  • Trusting the file name or extension. Invoice_0115.pdf.exe shows as Invoice_0115.pdf when extensions are hidden; the type comes from the bytes (file types).
  • Half-defanging. hxxp://update.beacon.lab.test still contains a resolvable domain; defang every dot.
  • Defanged values in tools. A firewall given 10.10.0[.]66 blocks nothing. Refang before feeding tools.
  • Previews. Chat clients, wiki engines and some mail clients fetch URLs to build previews: that is a request to the attacker's server from your network.

Practice

1. Defang https://cdn.files.lab.test/a.zip by hand.

hxxps://cdn[.]files[.]lab[.]test/a[.]zip. The scheme becomes hxxps and every dot between word characters becomes [.] (the file name's dot too, harmless but consistent).

2. Why is the archive password infected public knowledge, and why does that not matter?

Its purpose is not confidentiality. It stops mail gateways and anti-virus from scanning and deleting the sample, and stops accidental execution. Confidentiality comes from the channel and from who is given access.

3. Extend defang to turn ftp:// into fxp://.

Add s = re.sub(r"^ftp", "fxp", s, flags=re.I) (and the reverse in refang). Agree on one defanging convention in your team and in your tools.

Test yourself

What should a sample be identified by?

  1. Its original file name
  2. Its SHA-256 hash
  3. The analyst's name
  4. Its file extension

Which defanged form cannot be resolved or clicked by accident?

  1. hxxp://update.beacon.lab.test
  2. http://update[.]beacon[.]lab[.]test
  3. hxxp://update[.]beacon[.]lab[.]test
  4. update.beacon.lab.test

Why is a sample stored as <hash>.bin rather than under its original name?

  1. To save space
  2. So no operating system executes it on a double-click, and the hash names it uniquely
  3. Because YARA requires it
  4. To hide it from the analyst

What is the main risk of uploading a targeted sample to a public sandbox?

  1. It may be too large
  2. Other users, possibly the attacker, can see the file and any victim data inside it
  3. Sandboxes cannot analyse executables
  4. It changes the file's hash