Some actions performed by a user might also be bound to executing specific payloads for persistence. Windows operating systems present several ways to link payloads with particular interactions. This task will look at ways to plant payloads that will get executed when a user logs into the system.
Startup folder
Each user has a folder under C:\Users\<your_username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
where you can put executables to be run whenever the user logs in. An attacker can achieve persistence just by dropping a payload in there. Notice that each user will only run whatever is available in their folder.
If we want to force all users to run a payload while logging in, we can use the folder under C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
ย in the same way.
For this task, letโs generate a reverse shell payload using msfvenom:
We will then copy our payload into the victim machine. You can spawn an http.server
with Python3 and use wget on the victim machine to pull your file:
We then store the payload into the C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
folder to get a shell back for any user logging into the machine.
Now be sure to sign out of your session from the start menu (closing the RDP window is not enough as it leaves your session open):
And log back via RDP. You should immediately receive a connection back to your attackerโs machine.
Run / RunOnce
You can also force a user to execute a program on logon via the registry. Instead of delivering your payload into a specific directory, you can use the following registry entries to specify applications to run at logon:
HKCU\Software\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce
The registry entries under HKCU
will only apply to the current user, and those under HKLM
will apply to everyone. Any program specified under the Run
keys will run every time the user logs on. Programs specified under the RunOnce
keys will only be executed a single time.
For this task, letโs create a new reverse shell with msfvenom:
After transferring it to the victim machine, letโs move it to C:\Windows\
:
Letโs then create a REG_EXPAND_SZ
registry entry under HKLM\Software\Microsoft\Windows\CurrentVersion\Run
. The entryโs name can be anything you like, and the value will be the command we want to execute.
Note: While in a real-world set-up you could use any name for your registry entry, for this task you are required to use MyBackdoor
ย to receive the flag.
After doing this, sign out of your current session and log in again, and you should receive a shell (it will probably take around 10-20 seconds).
Winlogon
Another alternative to automatically start programs on logon is abusing Winlogon, the Windows component that loads your user profile right after authentication (amongst other things).
Winlogon uses some registry keys under HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\
that could be interesting to gain persistence:
Userinit
points touserinit.exe
, which is in charge of restoring your user profile preferences.shell
points to the systemโs shell, which is usuallyexplorer.exe
.
If weโd replace any of the executables with some reverse shell, we would break the logon sequence, which isnโt desired. Interestingly, you can append commands separated by a comma, and Winlogon will process them all.
Letโs start by creating a shell:
Weโll transfer the shell to our victim machine as we did previously. We can then copy the shell to any directory we like. In this case, we will use C:\Windows
:
We then alter either shell
or Userinit
in HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\
. In this case we will use Userinit
, but the procedure with shell
is the same.
Note: While both shell
and Userinit
could be used to achieve persistence in a real-world scenario, to get the flag in this room, you will need to use Userinit
.
After doing this, sign out of your current session and log in again, and you should receive a shell (it will probably take around 10 seconds).
Logon scripts
One of the things userinit.exe
does while loading your user profile is to check for an environment variable called UserInitMprLogonScript
. We can use this environment variable to assign a logon script to a user that will get run when logging into the machine. The variable isnโt set by default, so we can just create it and assign any script we like.
Notice that each user has its own environment variables; therefore, you will need to backdoor each separately.
Letโs first create a reverse shell to use for this technique:
Weโll transfer the shell to our victim machine as we did previously. We can then copy the shell to any directory we like. In this case, we will use C:\Windows
:
To create an environment variable for a user, you can go to its HKCU\Environment
in the registry. We will use the UserInitMprLogonScript
entry to point to our payload so it gets loaded when the users logs in:
Notice that this registry key has no equivalent in HKLM
, making your backdoor apply to the current user only.
After doing this, sign out of your current session and log in again, and you should receive a shell (it will probably take around 10 seconds).