Beyond good ol’ Run key, Part 39

In my previous post in this series I talked about the Logon scripts. This nice concept of being able to run some code when system-wide, or user-wide events happen is very useful in a managed environment.

The Logon event is one of a number of events that can be recognized by the system and as a response, an appropriate script can be executed. Other events include Logoff, Startup and Shutdown events. Apart from the already described mechanism, one can rely on Group Policies and use GPEDIT.MSC to set up event-aware scripts on the local system.

For example:

  • Startup and Shutdown

pic1

  • Logon and Logoff

pic2

(note, the screenshots are from XP cuz it’s holiday and I am lazy, but same applies to newer versions of Windows)

Once the appropriate Event is selected f.ex. Startup:

pic3

We can add the script using a dialog box:

pic4

We can also repeat the sequence and add a number of scripts:

pic6

The system stores the scripts in the following location:

  • c:\WINDOWS\System32\GroupPolicy\Machine\Scripts

pic5

Inside the folder we can see the following files:

  • The config file:
    • c:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.ini
  • and our scripts:
    • c:\WINDOWS\System32\GroupPolicy\Machine\Scripts\Startup\Startup1.bat
    • c:\WINDOWS\System32\GroupPolicy\Machine\Scripts\Startup\Startup2.bat
    • c:\WINDOWS\System32\GroupPolicy\Machine\Scripts\Shutdown\Shutdown1.bat
    • c:\WINDOWS\System32\GroupPolicy\Machine\Scripts\Shutdown\Shutdown2.bat

Note: I have added 2 scripts for each event.

The content of the files is as follows:

pich

And the same story for Logon/Logoff events:

  • The config file:
    • c:\WINDOWS\System32\GroupPolicy\User\Scripts\scripts.ini
  • and our scripts:
    • c:\WINDOWS\System32\GroupPolicy\User\Scripts\Logoff\Logoff1.bat
    • c:\WINDOWS\System32\GroupPolicy\User\Scripts\Logoff\Logoff2.bat
    • c:\WINDOWS\System32\GroupPolicy\User\Scripts\Logon\Logon1.bat
    • c:\WINDOWS\System32\GroupPolicy\User\Scripts\Logon\Logon2.bat

with the content as follows:

picf
If we restart the computer now (making all the events happen: Logoff, Shutdown, Startup, Logon), we can observe that our scripts are all executed one by one – as indicated by the ‘trace’ files created by each script:

piceI also temporarily modified the startup script to pause before continuing – just to show that the scripts are executed by userinit.exe:

pic8

Trivia fact: while preparing the post I noticed that an old autoruns present on my test VM shows some of the scripts incorrectly i.e. under the HKLM\…\Run key:

picj

The latest version works like a charm… or not 🙂 – note that scripts are duplicated and Logoff event shows 6 entries instead of 2:

picmIf in doubt, we can always visit the Registry entries where the information about these 4 events is stored:

picl

pick

Inspecting the Registry entries we can notice that the …\Scripts\Logon\<number>\FileSysPath properties can be modified:

picr

In my case I modify it to c:\test and I am recreating the Logon/Logoff scripts folder structure there:

pico

Luckily, autoruns picks it up too:

picp

Last, but not last – if such entries already exist on the system, an attacker could simply append some commands to existing scripts, or hijack execution of existing commands using many of existing tricks (f.ex. path interception, path companion, etc.).

Beyond good ol’ Run key, Part 38

It’s been a while since my last post about persistence tricks. Today I decided to fix this and write about yet another trick – kinda old, yet still cool – that works even today despite being as old as Windows NT.

The userinit.exe process was featured in a number of persistence posts before (here , here and here). Turns out, we have not given it all the attention it needs yet.

When you add a new user to the system, you have an option to change some properties of the user account as shown on the below screenshot. One of these properties is responsible for loading the user logon script (I named it foobar123.bat on the test system).

pic0

The alternative to GUI is using the following command:

  • net user /scriptpath:<Relative Path>

Once added to the user properties, the script will be executed anytime user logs on:

pic3

You may be wondering where on the system it has to be placed to ensure it is executed.

There are two places:

  • You can place it on Netlogon share:
    • either the real one from the domain controller (where all user scripts reside),
      or
    • you can create a fake, local one by using the trick shown below:

pic1

In such case the script will be loaded like this:

pic4

  • You can place it inside the %systemroot%\System32\Repl\Import\Scripts directory

In such case it will be executed like this:

pic2If you use net user command, the relative path is relative to %systemroot%\System32\Repl\Import\Scripts.

This trick is not my idea and is described in various places on the internet – I shamelessly ‘borrowed’ most of the bits and ideas from here.