Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Dynamic Analysis

Registry and Persistence Mechanisms

Learn how the Windows registry works and how malware persists through Run keys, scheduled tasks, services, startup folders and DLL hijacking, with safe parsing examples.

Malware that vanishes at the next reboot is not much of a threat, so most samples arrange to start again. That arrangement is called persistence, and it is one of the most valuable things to find: it tells you how to remove the infection and gives defenders reliable detections. Many persistence methods live in the Windows registry, a hierarchical database of settings, so this lesson starts there.

None of the examples here touch a real registry. They parse text exports and XML you create in the lesson, which is exactly what you would do with evidence collected from a lab VM.

The registry in five minutes

The registry is organized as keys (like folders) containing values (name, type, data). Top-level groups are hives:

Hive Short Holds
HKEY_LOCAL_MACHINE HKLM Settings for the whole computer
HKEY_CURRENT_USER HKCU Settings for the logged-in user
HKEY_CLASSES_ROOT HKCR File associations and COM classes
HKEY_USERS HKU All user profiles
HKEY_CURRENT_CONFIG HKCC Current hardware profile

Data types you will see: REG_SZ (text), REG_EXPAND_SZ (text with variables like %APPDATA%), REG_DWORD (number), REG_BINARY (raw bytes), REG_MULTI_SZ (list of strings).

Writing to HKCU needs no special rights, which makes it attractive for malware running as an ordinary user. HKLM needs administrator rights but affects every user.

Common persistence locations

Mechanism Where Notes
Run / RunOnce keys HKCU\Software\Microsoft\Windows\CurrentVersion\Run and HKLM\...\Run Runs at logon; the most common
Startup folder %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup Any shortcut or program runs at logon
Scheduled tasks Task Scheduler (files in C:\Windows\System32\Tasks) Run at logon, on a timer or on events
Services HKLM\SYSTEM\CurrentControlSet\Services Run in the background, often as SYSTEM
Winlogon helpers HKLM\...\Winlogon\Shell and Userinit Replacing these starts code at logon
Image File Execution Options HKLM\...\Image File Execution Options\<exe> with a Debugger value Redirects a program to another
WMI event subscriptions WMI repository Fileless; triggers on an event
DLL search-order hijacking / side-loading A DLL placed where a trusted program will load it Runs inside a trusted process
COM hijacking HKCU\Software\Classes\CLSID\... Redirects a COM object to the attacker's DLL
Browser extensions, shortcuts, login scripts Various Less common

Mapping these to ATT&CK helps in reports, for example T1547.001 Registry Run Keys / Startup Folder, T1053.005 Scheduled Task, T1543.003 Windows Service. You do not need to memorize identifiers, but knowing the family of method does help.

Reading a registry export

Analysts compare registry exports (the .reg text format) from before and after a run. Here is the parsing logic on two small made-up exports. It finds new and changed values and flags autorun locations.

Python Diff two .reg exports

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A new service in Windows\Temp is suspicious twice: services normally live in System32 or Program Files, and Temp is a writable scratch folder. Start=2 means automatic start at boot.

Scheduled tasks

Scheduled tasks are stored as XML. A task that runs a program from a user-writable folder, at logon, with the highest privileges deserves attention. The XML below is invented; the code pulls out the fields analysts read first.

Python Inspect a scheduled task definition

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real scheduled-task persistence often imitates the \Microsoft\Windows\... namespace to blend in. The repetition interval PT5M (every five minutes) is also useful: it matches a beacon rhythm you may see in the network traffic.

DLL side-loading and hijacking

Many legitimate programs load DLLs by name without a full path. Windows searches in a defined order, starting with the program's own folder. If an attacker places a malicious DLL with the expected name beside a trusted, signed program, that program loads it, and the malicious code runs under the trusted program's name. This is side-loading. In a lab you see it as a signed program from an unusual folder, loading an unsigned DLL from the same folder. Defenders look for exactly that mismatch.

Finding persistence in practice

  1. Run an autoruns-style tool in the lab before and after the sample (Sysinternals Autoruns lists most locations at once).
  2. Diff registry exports and the list of scheduled tasks and services.
  3. Check the Startup folders.
  4. For every new entry, record the location, the exact value or command, the user context and the file it points to; then hash and collect that file.
  5. Note the removal steps: delete the value, remove the task, stop and delete the service, delete the files.
flowchart LR
    A["Sample runs"] --> B["Writes Run key"]
    A --> C["Creates scheduled task"]
    A --> D["Installs service"]
    B --> E["Starts at logon"]
    C --> E
    D --> F["Starts at boot (SYSTEM)"]
    E --> G["Payload runs again"]
    F --> G

Several redundant mechanisms are common, so removing only one leaves the infection in place.

Common mistakes

  • Removing the file but leaving the registry entry, or the reverse.
  • Forgetting HKLM versus HKCU and which user context applies.
  • Missing scheduled tasks because only registry keys were checked.
  • Assuming a legitimate-looking name means a legitimate program: check the path and signature.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is the HKCU Run key attractive to malware running as an ordinary user?

    HKCU belongs to the current user, so writing there does not require elevation.
  2. 2. A service whose ImagePath is C:\Windows\Temp\syshelper.exe is suspicious mainly because:

    Real services normally live in System32 or Program Files, not in a scratch folder.
  3. 3. What is DLL side-loading?

    The trusted program loads the attacker's DLL from its own folder, giving the code a legitimate-looking host.
  4. 4. After finding one persistence entry, what should you do?

    Redundant persistence is common, and removing just one leaves the infection alive.

Summary

  • Persistence makes malware survive reboots; the registry, startup folders, scheduled tasks and services are the usual places.
  • Run keys, services in odd folders and tasks imitating Microsoft names are classic findings.
  • Parse and diff registry exports and task XML to find changes.
  • DLL side-loading hides malicious code inside trusted programs.
  • Record location, value, user context and file for each entry, and remove every mechanism.

Next: Network Behaviour Analysis