Basics
Common user types
Common Windows user types
- Local Admin
- Local User
- Guest User
- Domain User
- Domain Admin
Note that non-admin domain users may still be local admins.
Link to original
Important local service accounts
Windows local service accounts
Link to original
- LocalSystem — The account used by the OS itself; essentially more powerful than an normal user account in the Administrators group.
- Local Service — The default account used by Windows services; it has minimal privileges and uses anonymous network connections.
- Network Service — Essentially the same as the Local Service user, but uses the OS (“computer”) credentials when authenticating over the network.
Permissions
Windows permissions
Windows access is default-deny.
Windows folder permissions:
- Read — Permits viewing and listing of files and subfolders
- Write — Permits adding of files and subfolders
- Read & Execute — Permits viewing and listing of files and subfolders as well as executing of files; inherited by files and folders
- List Folder Contents — Permits viewing and listing of files and subfolders as well as executing of files; inherited by folders only
- Modify — Permits reading and writing of files and subfolders as well as executing of files; allows deletion of the folder
- Full Control — Permits reading, writing, changing, and deleting of files and subfolders
Windows file permissions:
- Read — Permits viewing or accessing of the file’s contents
- Write — Permits writing to a file
- Read & Execute — Permits viewing and accessing of the file’s contents as well as executing of the file
- List Folder Contents — N/A
- Modify — Permits reading and writing of the file as well as executing of the file; allows deletion of the file
- Full Control — Permits reading, writing, changing and deleting of the file
The biggest differences between Windows and UNIX permissions:
- Windows doesn’t have as fine-grained of control for a given user or group.
- Windows has much more fine-grained control across users and groups (there’s no limit of three permission sets).
- The ability to delete a folder or file, and to change its permissions, are essentially considered to be distinct “sub-permissions”.
As much as it pains me to say it, in many ways the Windows permission mode is much better than the (pre-ACL) Linux model.
Link to original
Paths
Unquoted path handling in Windows
When Windows encounters an unquoted path it tries all possible paths, from shortest to longest. Thus
C:\Program Files\Application Path\App.exe
will cause Windows to look for the following executables, in order:
C:\Program.exe
C:\Program Files\Application.exe
C:\Program Files\Application Path\App.exe
If an executable is found on a shorter path, then the remainder of the (unquoted) path is treated as command line parameters.
Link to original
Startup
Startup folder
Windows Startup folder
Individual startup folders are located at
C:\Users\$USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
.The shared startup folder is at
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
; any shortcut placed here will spawn an application on any (normal) user login.(The “contents” of a user’s Startup folder is the union of these two folders. If memory serves, the user folder will override the system folder for items with exactly the same name. Note that executables can be dropped into this folder in addition to shortcuts, though doing so may make it more difficult to hide a backdoor…)
Link to original
Run/RunOnce
Windows Run and RunOnce Registry keys
Startup programs can also be configured in the registry:
HKCU\Software\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKLM\Software\Microsoft\Windows\CurrentVersion\Run
HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKCU applies to the current user, while HLKM applies to everyone.
Entries under Run will run every time the user logs on, while those under RunOnce will only be executed a single time.
Create a
Link to originalREG_EXPAND_SZ
registry entry under the appropriate key that points to the appropriate payload.
WinLogon
How to exploit the WinLogon initialization sequence
WinLogin is a Windows component that loads a user profile right after authentication (amongst other things). The WinLogin initialization sequence is defined in the
HKLM\Software\Microsoft\Windows NT\CurrentVersion\WinLogon\
registry key. This will contain two values:
Userinit
, which points to userinit.exe, andshell
which usually points to explorer.exe.Do not replace these files! Instead, follow the initial command with your payload, separating the two with a comma:
Link to originalC:\Windows\System32\userinit.exe,C:\Windows\System32\evil.exe
Logon scripts
Windows logon scripts
The userinit.exe binary checks for an environment variable called UserInitMprLogonScript. This variable isn’t set by default, but if defined then userinit.exe will run any script it specifies.
Unfortunately, there’s no way to set this globally — you have to do it one user at a time in the
Link to originalHKCU\Environment
Registry key. Just create a new Expandable String Value entry called UserInitMprLogonScript in this key that points to the location of your payload.
Services
Windows services
Note that Windows services need to use an additional set of API calls that are not normally implemented in regular executables. While it is possible to run a normal executable as a service, the command will not survive for long (making a stable reverse shell impossible) and will always registered as failed (potentially alerting defenders).
Service security
Windows service security
Service configurations are stored in the Registry under
Link to originalHKLM\SYSTEM\CurrentControlSet\Services
; here the user is represented by the ObjectName value. DACLs are stored under each service as a Security sub-key.Loading drivers
Link to originalHow to load drivers in Windows
If binPath points to a driver (.sys) rather than an executable, then that driver will be loaded into the kernel when the service is started!
Link to original
Firewall
netsh
Check Windows Firewall state:
See all Windows Firewall rules for inbound connections:
Be aware that most built-in rules on Windows are bound to particular applications. But admins are lazy, and seldom bind custom rules to explicit applications. There’s no 100% reliable way to identify custom rules from the command line, but they’ll often have obvious naming conventions.
See all Windows Firewall rules for outbound connections:
Punch a hole in the Windows Firewall for ports 80, 443, and 4444:
In general you don’t want to turn off the Windows Firewall, as doing so will generate a popup for any users currently logged into the machine.
Link to original
Event logs
Windows event logs
Log types
- Application — Contains events logged by applications. For example, a database application might record a file error. The application developer decides which events to record.
- Security — Contains events such as valid and invalid logon attempts, as well as events related to resource use such as creating, opening, or deleting files or other objects. An administrator can start auditing to record events in the security log.
- System — Contains events logged by system components, such as the failure of a driver or other system component to load during startup.
- [CustomLog] — Contains events logged by applications that create a custom log. Using a custom log enables an application to control the size of the log or attach ACLs for security purposes without affecting other applications.
Log fields
- Type — Warning, error, information, etc.
- Time — Date/time for the computer sending the log
- Computer — Computer name
- Provider Type — The facility that generated the event (generally the Windows Event Log)
- Provider Name — The name of the log (Application, Security, etc.)
- Source — Application
- Event ID — Standardized (not unique!) identifier
- Description — Self-explanatory
Microsoft has more detailed documentation.
Event types
- Error — An event that indicates a significant problem such as loss of data or loss of functionality. For example, if a service fails to load during startup, an Error event is logged.
- Warning — An event that is not necessarily significant, but may indicate a possible future problem. For example, when disk space is low, a Warning event is logged. If an application can recover from an event without loss of functionality or data, it can generally classify the event as a Warning event.
- Information — An event that describes the successful operation of an application, driver, or service. For example, when a network driver loads successfully, it may be appropriate to log an Information event. Note that it is generally inappropriate for a desktop application to log an event each time it starts.
- Success Audit — An event that records an audited security access attempt that is successful. For example, a user’s successful attempt to log on to the system is logged as a Success Audit event.
- Failure Audit — An event that records an audited security access attempt that fails. For example, if a user tries to access a network drive and fails, the attempt is logged as a Failure Audit event.
Microsoft has more detailed documentation.
Event IDs
Link to originalWindows event IDs
- 104 — Event log was cleared
- 1102 — Audit log was cleared (517 on Windows 2003 and earlier)
- 4104 — PowerShell command and script logging
- 4626 — Successful logon
- LogonType 3 represents a (generic) network login
- LogonType 9 represents a logon where the outbound credentials are different than the credentials used to authenticate to the account that is initiating that login (only logged by the host initiating the connection, however)
It’s hard to find documentation about event ID, and the meaning seems to shift between versions of Windows.
Link to original
DLLs
Windows DLL search order
The search order varies depending on the state of SafeDllSearchMode.
Windows DLL search order if SafeDllSearchMode is enabled:
- The application directory
- The system directory (as returned by
GetSystemDirectory()
)- The 16-bit (!) system directory
- The Windows directory (as returned by
GetWindowsDirectory()
)- The current directory (!)
- The system PATH (!)
Windows DLL search order if SafeDllSearchMode is disabled:
- The application directory
- The current directory (!)
- The system directory (as returned by
GetSystemDirectory()
)- The 16-bit (!) system directory
- The Windows directory (as returned by
GetWindowsDirectory()
)- The system PATH (!)
Note that it seems more-or-less impossible to determine what DLLs an application is searching for without having SYSTEM access already (so tools like ProcMon can be run).
Link to original
NTLM hashes
NTLM hashes
The Windows NT family uses NTLM for (network) logins, and stores hashes in the NT hash format, which is really just the md4 hash of the UTF-16-LE encoding of the user password (UTF-16-LE is the little endian version of UTF-16). While NTLM is the login protocol, people commonly call NT hashes “NTLM” hashes.
The “LM” in “NTLM” references the pre-NT password hashing mechanism, LM. LM hashes are constructed in an absolutely batshit crazy fashion, and are much weaker than (the already weak) NT hashes.
Windows passwords are stored in the SAM, but NT and LM hashes (which can coexist on the same system) are stored in different data structures.
Link to original
WinRM
Windows remote management
Windows Remote Management (WinRM) is basically PowerShell-over-HTTP. It requires access to TCP 5985 (unencrypted) or TCP 5986 (encrypted).
Link to original
Reconnaissance
Important commands
Useful built-in commands for Windows reconnaissance
Link to original
arp -a
— display the ARP cache (find other machines on the network!)cmdkey /list
— show saved credentialsdriverquery
— list installed drivershostname
— return system hostname- net accounts — local machine policies
- net accounts /domain — domain policies
- net group — list domain groups
- net group “Domain Admins” /domain — list domain admins
- net localgroup — list all (local) groups
- net localgroup administrators — list local admins
- net share — list all shares (made available by the current machine)
- net start — list all running services (lots!)
- net user — list all (local) users
- net user $USERNAME — get details for user
$USERNAME
- netstat — query open/listening ports
query session
— list other users who are currently logged in- reg — query (and manipulate) registry entries
sc
— query (and manipulate) services (conflicts with a PowerShell built-in!)schtasks
— list scheduled tasks- systeminfo — return system info
- whoami /groups — list current user’s groups
- whoami /priv — current user + privileges
Permissions
icacls
Use icacls or
Get-Acl $PATH | Format-List
in PowerShell to check permissions at the command line. The icacls tool can also be used to update Windows ACLs.Both of these tools produce output that is somewhat different than that of the “Security” tab in the file or folder properties:
(I)
— permission inherited from the parent container(F)
— full access (full control)(M)
— modify right/access(OI)
— object inherit(IO)
— inherit only(CI)
— container inherit(RX)
— read and execute(AD)
— append data (add subdirectories)(WD)
— write data and add filesNote that the Windows File Explorer only displays the first ACL for a particular user or group, but that Windows allows multiple ACLs to be applied! This means that the File Explorer does not always show you the actual permissions a user/group will have — you really do need to use icacls or Get-Acl.
In the case of multiple ACLs, or when a user is part of two groups with different groups, keep in mind that allow permissions only override inherited deny permissions. Explicitly set deny permissions cannot be overridden.
Link to original
Querying the registry
reg
The reg binary is used to interact with the Windows registry from the command line.
Registry paths will typically start with HKLM (HKey Local Machine), HKCU (HKey Current User), etc.
Link to original
netstat
netstat
The netstat command is found on both Linux and Windows, though the two versions have slightly different options.
Linux Windows Description -a
-a
Shows all sockets (listening and established) -i
Shows per interface statistics -l
Show only listening ports -n
-n
Do not resolve IP addresses or port numbers -p
-ob
Show PID and binary using the socket (needs root) -s
Show protocol statistics -t
-p TCP
Show TCP sockets only -u
-p UDP
Show UDP sockets only -x
Show UNIX sockets (kernel-only “network”) only On Windows, if you know the PID of a process, you can use netstat + findstr to quickly find out what ports its listening on:
Link to original
systeminfo
systeminfo
The Windows systeminfo command displays a lot of information! Use findstr to filter its output:
This can be used to semi-reliably determine if a machine is part of a domain.
(Non-AD joined machines will almost always use WORKGROUP for the
Link to originalDomain
, while AD-joined machines will often use a fully qualified DNS domain here.)
Potentially interesting files
PowerShell history
PowerShell history file
View PowerShell’s history.
Use %USERPROFILE% instead of
Link to original$Env:USERPROFILE
if running from cmd.exe.
Unattended Windows installation data
Windows unattended installation data
Unattended Windows installations still need an administrative account; credentials can be left in one of the following files (if not cleaned up):
C:\Unattend.xml
C:\Windows\Panther\Unattend.xml
C:\Windows\Panther\Unattend\Unattend.xml
C:\Windows\system32\sysprep.inf
C:\Windows\system32\sysprep\sysprep.xml
Look for the
Link to original<Credentials/>
block.
IIS configuration data
IIS configuration data
Placed where IIS stores passwords:
C:\inetpub\wwwroot\web.config
C:\Windows\Microsoft.NET\Framework64\$SOME_VERSION_STRING\Config\web.config
Look for the
Link to originalconnectionString
directive.
wmic
wmic
The wmic command on Windows is extremely useful, but is also deprecated (because of its usefulness to attackers!). It can be used on Windows 10 21H1 and earlier. For later systems, PowerShell command-lets will need to be used instead (which increases the risk that activity will be logged).
wmic product get name,version,vendor
— list all installed software (but misses 32-bit applications installed on a 64-bit OS)wmic service get name,displayname,pathname,startmode
— list all serviceswmic qfe get caption,description,hotfixid,installedon
— list installed updateswimc service list brief
— another way of listing serviceswmic service where "name like '$SERVICE_NAME'" get name,pathname
— get information about a particular servicewmic /namespace:\root\securitycenter2 path antivirusproduct
— enumerate antiviruswmic useraccount get name,sid
— Display SIDs for all (local) usersRemote process creation
How to run a remote command with wmic
Link to originalInstall an MSI package
Link to originalHow to remotely install a Windows package with wmic
Link to original
PowerShell
Windows reconnaissance with PowerShell
There are a lot of PowerShell commands that can be used for enumerating Windows.
When checking to see if Sysmon is running, you can also examine the
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Channels\Microsoft-Windows-Sysmon\Operational
Registry entry.PowerShell Command History
Link to originalPowerShell history file
View PowerShell’s history.
Use %USERPROFILE% instead of
Link to original$Env:USERPROFILE
if running from cmd.exe.
Useful scripts
Useful scripts for Windows reconnaissance
Notes
Link to original
- WinPEAS is detected and quarantined by Microsoft Defender (service windefend) by default.
- PowerUp may require an unrestricted PowerShell session (
powershell -nop -exec bypass
), which can raise alerts.- Windows Exploit Suggester analyzes the output of systeminfo, and can be run on the attacker’s machine.
- The
multi/recon/local_exploit_suggester
module works through meterpreter to analyze a Windows system for potential vulnerabilities.
Exploitation
Permissions
Admin-like Windows permissions
SeBackup / SeRestore
Windows SeBackup and SeRestore permissions
These permission allow full read (SeBackup) and write (SeRestore) access to any file. The first of these allows for exfiltration, while the second allows binaries to be replaced at will (combine with service- or task-based attacks!). The “Backup Operators” group has both of these permissions!
Backup useful registry hives:
Run a local SMB server with Impacket:
Then, just use copy on Windows:
Use Impacket to dump hashes from a hive and perform a pass-the-hash attack:
Link to originalSeTakeOwnership
Windows SeTakeOwnership permission
This permission allows a user to take ownership of any file or object (!!!).
The “standard” file to replace with cmd.exe with this trick is
Link to originalC:\Windows\System32\Utilman.exe
, which provides accessibility services access from lock and login screens.SeImpersonate / SeAssignPrimaryToken
Link to originalWindows SeImpersonate and SeAssignPrimaryToken permissions
These permissions allow for user impersonation. On Windows, the
Local Service
andNetwork Service
accounts already have these privileges; if IIS is installed, there will also often be anIIS AppPool/DefaultAppPool
service account with these permissions as well.However, it isn’t enough to just have access to a service running as a user with these permissions, as Windows will not allow an application to arbitrarily impersonate a user. Instead, we must have a service and then trick/force a highly privileged account to connect to it, at which point impersonation will be allowed.
One way to do this is using the RogueWinRM exploit. The idea here is that when a user logs in, the BITS service creates a connection on port 5985 to the (local) WinRM service (which is used to execute PowerShell commands) as SYSTEM. If the WinRM service isn’t running, RogueWinRM can be run instead to capture these connections (I’m guessing that the WinRM service can also be back-doored using RogueWinRM directly, but that doing so may interfere with system functionality?).
Example RogueWinRM command line:
Link to original
Bulk editing permissions
How to bulk edit Windows permissions
Privileges can also be manipulated/assigned to users in bulk using the secedit tool
Link to original
- Dump system privileges:
secedit /export /cfg config.inf
.- Edit config.inf, appending the user you want to have a given privilege to that privilege’s assignment line (comma-delineated list).
- Re-import the system privileges:
secedit /import /cfg config.inf /db config.sdb & secedit /configure /db config.sdb /cfg config.inf
Service ACLs
Windows service ACLs
For connecting to services (such as WinRM), it’s often possible to manipulate the service ACL rather than the user’s privileges. For example, adding a user to the
Microsoft.PowerShell
security descriptor with the “Full Control” permission will enable access to the WinRM service, regardless of the permissions explicitly assigned to the user.The advantage to manipulating user privileges and service ACLs directly is that it’s less obvious that a user is back-doored.
Link to original
DLLs
How to exploit the Windows DLL search order
Exploiting the Windows DLL search order is basically the same idea as exploiting the LD_LIBRARY_PATH on Linux.
A stub malicious DLL:
Compile with mingw (on Linux!):
Link to original
Windows scripting host
Windows Scripting Host
The Windows Scripting Host (WSH) consists of two tools, cscript.exe for command-line scripts and wscript.exe for UI scripts, and executes Visual Basic scripts (though it doesn’t care about the extension). WSH operates with regular user privileges, not admin privileges.
An example VBScript:
Executing a program requires creating a “shell” object:
It appears that recent versions of Windows may severely limit which binaries can be called from the
Link to originalwscript.Shell
object; calc.exe works, but pretty much nothing else does.
WinRM
How to use Windows Remote Management
IMPORTANT
By default, UAC restricts WinRM calls to domain admins and the default local “Administrator” account. Local Windows admins cannot call this service without first disabling UAC!
WARNING
Admin-ish privileges (including privileges associated with the Backup Operators group) are stripped by default when using WinRM. To enable this access, we need to set the LocalAccountTokenFilterPolicy registry key to 1.
Windows remote management
Windows Remote Management (WinRM) is basically PowerShell-over-HTTP. It requires access to TCP 5985 (unencrypted) or TCP 5986 (encrypted).
Link to originalWinRS
winrs
winrs.exe is an older application used to interact with WinRM.
This interface has been largely deprecated in favor of using PowerShell, and may not even be present on recent versions of Windows.
Link to originalPowerShell
How to use WinRM with PowerShell
Many large companies will enable PowerShell remoting on all machines in order to ease IT support burdens (by default, remoting is only enabled on domain controllers).
Link to originalEvil-WinRM
Link to originalFile transfers in Evil-WinRM
Evil-WinRM does have a built-in download command for transferring files, but it’s sloooow…
Link to original
Services
How to exploit Windows services
Local services
How to manipulate local Windows services
Existing Windows services can have backdoors added to them using Metasploit’s msfvenom. Begin by finding a candidate service:
Create a replacement service file using msfvenom (alternately, a backdoor could be added directly to the binary):
The use of
exe-service
here rather thanexe
ensures that the correct APIs are available. Alternately, if you’re not trying to be stealthy than a simple application can be used instead (this will run successfully, but register as a failure in the Windows event logs).USERNAME and PASSWORD obviously need to be updated to fit the current use case. Unlike msfvenom payloads, as of August 17th 2022 binaries compiled from this code are not detected as malicious by Windows Defender.
Finally, update the service definition:
If the service executable is specified with an unquoted path, then it’s only necessary to place the malicious binary earlier in the implicit search path.
Link to originalRemote services
Link to originalHow to manipulate remote Windows services
IMPORTANT
By default, UAC restricts remote sc.exe calls to domain admins and the default local “Administrator” account. Local Windows admins cannot call this service remotely without first disabling UAC!
The sc.exe command can also be used to create and manipulate services on remote machines. This uses the Windows RPC (TCP 135), but will fall back to named pipes over SMB (TCP 445) or NetBIOS (TCP 139). (In fact, this is how sc.exe works locally as well; the only difference is that in this case a local named pipe is always used.)
Note that this is a blind attack — there will be no output indicating success or failure.
Link to original
Tasks
How to exploit Windows tasks
Local tasks
How to manipulate local Windows tasks
Use the built-in schtasks command to create and manipulate Windows tasks (basically the equivalent of *NIX cron jobs).
If a task points to a file that you’ve obtained write access to, then that file can be altered without changing the underlying task.
It may also be possible to create your own tasks.
Stealthy tasks
Link to originalHow to hide Windows tasks using PsExec
Deleting the task Security Descriptor will make the task invisible to any user in the system. Note that this requires PsExec!
Link to original
Use
PsExec64.exe -s -i regedit
to open regedit with SYSTEM privileges.Search in
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree\
for the task to hide. Under the key for that task, there will be a value named SD that contains the security descriptor. Simply delete it.Remote tasks
Link to originalHow to manipulate remote Windows tasks
IMPORTANT
By default, UAC restricts remote schtasks calls to domain admins and the default local “Administrator” account. Local Windows admins cannot call this service remotely without first disabling UAC!
The schtasks command can also be used to create and manipulate services on remote machines. This uses the Windows RPC (TCP 135), but will fall back to named pipes over SMB (TCP 445) or NetBIOS (TCP 139). (In fact, this is how
Link to originalschtasks
works locally as well; the only difference is that in this case a local named pipe is always used.)
Shortcut files
How to exploit Windows shortcut files
Shortcuts can be modified to run a script instead. A simple PowerShell script that fires off one executable and then another might look like:
To ensure that the script itself doesn’t generate a PowerShell window, it will need to be called like so:
Link to original
File associations
How to exploit Windows file associations
Hijacking file associations is the same basic idea as hijacking shortcut files. This is a two-step process.
- Locate the “programmatic ID” of the extension you wish to hijack under
HKLM\Software\Classes
(this will be the key’s(Default)
value).- The programmatic ID will also be in
HKLM\Software\Classes
and will have the command path as the(Default)
value for itsshell\open\command
key.Note that the programmatic ID command will probably include the
%1
placeholder, which is used to pass in the file path. This requires a slightly different script than is used to backdoor shortcuts:Note the quoting here, as well as the inclusion of the entire
Link to original$args
array. Most of the documentation I’ve found online suggests using$args[0]
as an unquoted argument, but this approach did not work in my testing.
“Feature on demand” helper
How to exploit the Windows “Feature on Demand” Helper
The fodhelper binary can be abused to gain an admin shell on Windows boxes with a couple of registry key changes. They key (heh) here is to use the CurVer registry key to point fodhelper.exe to a different registry location; otherwise most AV solutions will catch this change and block it (though sometimes the command will still execute).
Link to original
Use the Windows Firewall to relay ports
How to use the Windows Firewall to relay ports
Adapted from slyth11907 / Cheatsheets / Cheatsheet_UsefulCommands.txt.
This works on Windows 7 and later.
Link to original