The PyRAT Code: Internals of a Python-Based RAT
Detection stack
- AIDR
- Alert
- ETL
- Query
Summary
The article profiles PyRAT, a Python-based remote access trojan packaged as an ELF binary using PyInstaller. It runs on both Linux and Windows, communicates with a C2 server over unencrypted HTTP, and supports command execution, file exfiltration, screenshot capture, and a self-destruct routine. Persistence is implemented through XDG autostart on Linux and a Run registry key on Windows. The report emphasizes how lightweight the tool is and how easily it can be repurposed by cybercriminals.
Investigation
Researchers extracted the embedded Python bytecode with pyinstxtractor, decompiled the modules, and identified the primary Agent class that performs host fingerprinting, UID generation, thread management, and C2 coordination. Static analysis surfaced key imports, filenames, registry interaction, and the HTTP POST endpoint used for beaconing. Behavioral testing showed the full workflow: persistence creation, command execution, file enumeration, upload/download operations, ZIP archive creation, screenshot capture, and optional artifact cleanup.
Mitigation
Monitor for suspicious autostart .desktop files created in user profiles and for additions to the HKCU\Run registry key using unfamiliar executable names. At the network layer, watch for outbound HTTP POST traffic to unknown hosts targeting the /api/{uid}/hello endpoint. On endpoints, flag unknown ELF binaries containing embedded Python content and anomalous process behavior such as frequent subprocess execution.
Response
If detected, isolate the system, collect the ELF sample and any associated .desktop files, and preserve relevant registry state. Extract the UID, C2 address, and any files transferred during execution as part of forensics. Block the identified C2 infrastructure, remove persistence, and complete a full cleanup. Rotate credentials for accounts that may have been exposed.
"graph TB %% Class Definitions classDef malware fill:#ff9999 classDef technique fill:#99ccff classDef action fill:#ccffcc classDef persistence fill:#ffcc99 classDef cleanup fill:#dddddd %% Node Definitions malware_pyrat["<b>Malware</b> – <b>Name</b>: PyRAT ELF<br/><b>Description</b>: Linux/Windows RAT executed as ELF binary"] class malware_pyrat malware tech_system_info["<b>Technique</b> – <b>T1082 System Information Discovery</b><br/>Collects OS version, hostname, hardware details"] class tech_system_info technique tech_user_discovery["<b>Technique</b> – <b>T1033 System Owner/User Discovery</b><br/>Enumerates loggedu2011in user and account details"] class tech_user_discovery technique action_establish_c2["<b>Action</b> – Establish C2 via unencrypted HTTP POST"] class action_establish_c2 action tech_web_service["<b>Technique</b> – <b>T1102.003 Web Service: Oneu2011Way Communication</b><br/>Uses HTTP POST to send data to a remote server"] class tech_web_service technique tech_exfil_unenc["<b>Technique</b> – <b>T1048.003 Exfiltration Over Unencrypted Nonu2011C2 Protocol</b><br/>Transfers data without encryption"] class tech_exfil_unenc technique tech_data_encoding["<b>Technique</b> – <b>T1132.002 Data Encoding: Nonu2011Standard Encoding</b><br/>Applies custom encoding before transmission"] class tech_data_encoding technique action_execute_cmd["<b>Action</b> – Execute Commands"] class action_execute_cmd action tech_windows_shell["<b>Technique</b> – <b>T1059.003 Windows Command Shell</b><br/>Runs commands via cmd.exe"] class tech_windows_shell technique tech_unix_shell["<b>Technique</b> – <b>T1059.004 Unix Shell</b><br/>Runs commands via /bin/sh"] class tech_unix_shell technique action_collect_files["<b>Action</b> – Collect Files & Enumerate Filesystem"] class action_collect_files action tech_archive["<b>Technique</b> – <b>T1560.002 Archive Collected Data: Archive via Library</b><br/>Compresses data using a ZIP library"] class tech_archive technique tech_screenshot["<b>Technique</b> – <b>T1113 Screen Capture</b><br/>Captures the desktop image"] class tech_screenshot technique tech_persist_linux["<b>Technique</b> – <b>T1547.013 XDG Autostart</b><br/>Adds a .desktop entry to user autostart directory"] class tech_persist_linux persistence tech_persist_windows["<b>Technique</b> – <b>T1547.014 Registry Run Keys/Startup Folder</b><br/>Creates entry in HKCU\Run for persistence"] class tech_persist_windows persistence tech_cleanup["<b>Technique</b> – <b>T1070.009 Indicator Removal: Clear Persistence</b><br/>Deletes persistence artifacts and logs"] class tech_cleanup cleanup %% Connections malware_pyrat –>|leads_to| tech_system_info malware_pyrat –>|leads_to| tech_user_discovery tech_system_info –>|provides info for| action_establish_c2 tech_user_discovery –>|provides info for| action_establish_c2 action_establish_c2 –>|uses| tech_web_service action_establish_c2 –>|uses| tech_exfil_unenc action_establish_c2 –>|uses| tech_data_encoding action_establish_c2 –>|enables| action_execute_cmd action_execute_cmd –>|uses| tech_windows_shell action_execute_cmd –>|uses| tech_unix_shell action_execute_cmd –>|leads_to| action_collect_files action_collect_files –>|leads_to| tech_archive tech_archive –>|leads_to| tech_screenshot tech_screenshot –>|enables| tech_persist_linux tech_screenshot –>|enables| tech_persist_windows tech_persist_linux –>|later leads to| tech_cleanup tech_persist_windows –>|later leads to| tech_cleanup "
Attack Flow
Detections
Potential Malware Self-Removal or Stderr Concealment Operation (via cmdline)
View
Archive Was Created In Linux Tmp Folder (via file_event)
View
Possible Persistence Points [ASEPs – Software/NTUSER Hive] (via cmdline)
View
Possible Registry Keys Removal Attempt (via process_creation)
View
Possible Linux XDG Autostart Files Modification Attempt (via cmdline)
View
Dangerous Permissions for a Binary/Script/Folder was set (via cmdline)
View
IOCs (HashMd5) to detect: The PyRAT Code: Python Based RAT and its Internals
View
Windows Persistence via Current User’s Run Key [Windows Registry Event]
View
Detection of PyRAT Persistence on Linux [Linux Process Creation]
View
Simulation Execution
Prerequisite: The Telemetry & Baseline Pre‑flight Check must have passed.
Rationale: This section details the precise execution of the adversary technique (TTP) designed to trigger the detection rule. The commands and narrative MUST directly reflect the TTPs identified and aim to generate the exact telemetry expected by the detection logic.
-
Attack Narrative & Commands:
An attacker has obtained a compromised SSH credential for a regular user. After login, they execute a living‑off‑the‑land script that:
- Writes the malicious ELF payload (
agent-svc.pyc) to the user’s home directory, disguising it as a Python byte‑code file while it is actually an ELF binary. - Copies a masqueraded Debian‑package tool named
dpkgninto/usr/local/bin, granting it executable rights. The tool is a thin wrapper that simply launches the ELF payload. - Installs a autostart desktop entry (
dpkgn.desktop) into~/.config/autostartso the payload runs on every graphical session start. - Sets appropriate permissions to avoid immediate suspicion.
The combined file events (
agent-svc.pycanddpkgn) satisfy the rule’s first condition, while the.desktopfile satisfies the second condition. - Writes the malicious ELF payload (
-
Regression Test Script:
#!/usr/bin/env bash set -euo pipefail # 1. Deploy malicious ELF disguised as .pyc MALWARE_PATH="$HOME/agent-svc.pyc" echo "Creating fake ELF payload (dummy binary) ..." dd if=/dev/zero bs=1 count=1024 of="$MALWARE_PATH" status=none chmod +x "$MALWARE_PATH" # 2. Deploy fake Debian tool named dpkgn FAKE_TOOL="/usr/local/bin/dpkgn" echo "#!/usr/bin/env bash" | sudo tee "$FAKE_TOOL" > /dev/null echo "exec "$MALWARE_PATH" "$@"" | sudo tee -a "$FAKE_TOOL" > /dev/null sudo chmod +x "$FAKE_TOOL" # 3. Create autostart desktop entry AUTOSTART_DIR="$HOME/.config/autostart" mkdir -p "$AUTOSTART_DIR" DESKTOP_FILE="$AUTOSTART_DIR/dpkgn.desktop" cat > "$DESKTOP_FILE" <<EOF [Desktop Entry] Type=Application Exec=$FAKE_TOOL Hidden=false NoDisplay=false X-GNOME-Autostart-enabled=true Name=Package Manager Comment=System Update Service EOF echo "PyRAT persistence artifacts deployed." -
Cleanup Commands:
#!/usr/bin/env bash set -euo pipefail # Remove malicious ELF rm -f "$HOME/agent-svc.pyc" # Remove fake tool sudo rm -f /usr/local/bin/dpkgn # Remove autostart entry rm -f "$HOME/.config/autostart/dpkgn.desktop" echo "Cleanup completed."