Windows Post Exploitation: Kyle Avery
Windows Post Exploitation: Kyle Avery
Kyle Avery
Introduction
Class Scope
• What we will not cover:
• Every possible technique in each category we discuss
• Anything involved with initial compromise (See Joff’s and Michael’s classes!)
• Anything cloud related
• Anything Linux related
• 0days or undocumented techniques
• Many existing courses and online resources focus on performing TTPs,
this course will help you build a process for evaluating Windows
systems to find opportunities to use that knowledge
• Don’t worry, we’ll go over specific techniques as well!
Lab Environment
• You should have received instructions on how to set up a
development environment for this course – if not, send a message in
Discord ASAP!
• The VM you created will be used to connect to an Active Directory lab
hosted in AWS
• We will use Sliver for labs and demos, but you could use a different C2
framework if you prefer
So, you popped a shell,
now what?
Cyber Attack Lifecycle
• What is involved in a red team exercise or adversary simulation?
• Steps we will cover:
• Enumeration
• Persistence
• Privilege Escalation
• Lateral Movement
https://i.blackhat.com/eu-18/Thu-Dec-6/eu-18-Warner-Sirr-Network-Defender-Archeology-An-NSM-Case-Study-In-Lateral-Movement-With-DCOM-wp.pdf
U COM Hijacking
• Typically, COM class implementations are defined in HKLM
• These implementations can be overridden by writing to the same
path in HKCU as it is higher than HKLM in the COM search order
• This means that any user can “hijack” existing COM classes by creating
an entry in the HKCU registry hive
U COM Hijacking
WMI
• WMI is an interface available locally and remotely (through DCOM or
WinRS) for administering Windows systems
• Provides information about the system through a query language
• Hostname and domain
• Running processes
• All services and their status
• Local and remote system drives
• Active network interfaces
• AV products
• Directory and file contents
• And much more!
WMI Queries
• Queries are written in WMI Query Language (WQL)
• C# example:
var session = CimSession.Create("127.0.0.1");
var query = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_NetworkAdapter");
(&(objectClass=group)(managedBy=*)),
enum_ad_managedby_groups (Metasploit)
(&(objectClass=group)(managedBy=*)(groupType:1.2.840.113556.1.4.803:=2147483648))
https://github.com/sbousseaden/EVTX-ATTACK-SAMPLES/tree/master/Privilege%20Escalation
Service Accounts - Utilizing a Potato
Technique
• At BlackHat 2015, James Forshaw presented a talk that led to the
development of RottenPotato, a local privilege escalation from
Windows service accounts to SYSTEM
• This tool influenced the creation of several other tools, including
RottenPotatoNG, Juicy Potato, and SweetPotato
• Juicy Potato and SweetPotato currently work on Windows 10 and
Server 2019, the biggest difference between the two is that one is
unmanaged, and the other is a .NET assembly
Types of Hashes
• NTLM – Used for NTLM (non-Kerberos) authentication, passable
• Modified MD4, Hashcat mode 1000
• b4b9b02e6f09a9bd760f388b67351e2b
https://www.unh4ck.com/detection-engineering-and-threat-hunting/lateral-movement/detecting-lateral-movement-via-service-configuration-manager
Manipulating Services with WMI
• WMI offers a Win32_Service class in the default namespace that can
be used to create and start services on an endpoint
• There are actually a few different classes that offer the same
functionality, Win32_BaseService can be used to change the footprint
even more
PowerShell Remoting
• WinRM was built as a successor to DCOM for remote operations
• The protocol is a SOAP API that goes across TCP/5985 or TCP/5986
• PowerShell Remoting (among other things) uses WinRM as a
transport
• Not the best form of lateral movement in many scenarios but can be
very valuable if administrators are already using PowerShell Remoting
in the environment!
• WinRM is typically associated with PowerShell execution, and most
tools that perform WinRM execution are using the protocol to execute
PS commands or scripts
Evil-WinRM
• Evil-WinRM is a Ruby tool that can be used to interact with hosts
through a SOCKS proxy
• This tool also has many other convenience features including loading
DLLs and .NET assemblies in memory on the target and built-in
upload/download commands
• This tool uses the WinRM library which utilizes the raw WinRS layer,
totally avoiding PowerShell logging
CrackMapExec WinRM
• CrackMapExec is a post-exploitation tool that “helps automate
assessing the security” of AD networks
• CME uses Impacket for many features, but the WinRM functionality
comes from the pypsrp library
• The execute_cmd function of this library also uses the raw WinRS
layer
SharpWSManWinRM and
WSManWinRM.vbs
• The WSMan-WinRM project includes several examples, including a C#
and even VBS POC for execute commands on a remote host using raw
WinRS
• These are the only examples which can be used with currently
credentials in memory, and do not require a proxy into the
environment
PowerShell Remoting
LAB: .NET WinRS Lateral
Movement
Remote Desktop Protocol
• RDP can be used to move laterally, and can be used without creating a
remote desktop session with SharpRDP
• There isn’t an Impacket RDP execution script, but you could use
rdesktop or even SharpRDP over proxychains
• SharpRDP does this by writing to a specific registry key which can be
heavily monitored
Remote Desktop Protocol
DCOM Execution
• Distributed COM is a way for software running on an endpoint to
expose functionality over the network using RPC
• Code execution using DCOM will occur in the context of the process
hosting the exposed interface
• This MMC COM object provides a method called
ExecuteShellCommand that we can take advantage of
• A service on the host will spawn the executable
• svchost.exe > EXE OR svchost.exe > dllhost.exe > DLL
• An alternative is the ShellBrowserWindow method which makes
explorer.exe the parent process
DCOMExec.py
• DCOMExec.py offers
three different methods
of code execution
• Each uses MMC DCOM
methods, making them
easier to signature
DCOMExec.py
DCOM Execution in .NET
• The SharpSploit library provides great example of remote code
execution in .NET and includes multiple examples of various DCOM
execution methods
• This implementation can take plaintext credentials or the current user
session
• There is also a “pth” branch of SharpSploit that can use a hash!
• SharpSploit has four DCOM examples, including an ExcelDDE option
that is different than Impacket’s method but only works on older
versions of Office
LAB: .NET DCOM Lateral
Movement
DCOM Execution
Excel XLM Macros
• Excel features an alternative to VBA macros called Excel 4.0 or XLM
macros
• This language has historically been more difficult for AV solutions to
scan, and did not integrate with AMSI until recently
• XLM can be used to make Windows API calls and can be accessed with
a DCOM interface called “ExecuteExcel4Macro”
• The C# tool SharpExcel4-DCOM from rvrsh3ll combines these two
features to execute shellcode on a remote host
Excel XLM Macros
https://outflank.nl/blog/2018/10/06/old-school-evil-excel-4-0-macros-xlm/
Excel XLM Macros
Type ComType = Type.GetTypeFromProgID("Excel.Application", computername);
object RemoteComObject = Activator.CreateInstance(ComType);
int count = 0;
foreach (var mybyte in shellcode)
{
var charbyte = String.Format("CHAR({0})", mybyte);
var ret = RemoteComObject.GetType().InvokeMember("ExecuteExcel4Macro", BindingFlags.InvokeMethod, null,
RemoteComObject, new object[] { "CALL(\"Kernel32\",\"WriteProcessMemory\",\"JJJCJJ\",-1, " + (memaddr +
count) + "," + charbyte + ", 1, 0)"});
count = count + 1;
}
RemoteComObject.GetType().InvokeMember("ExecuteExcel4Macro", BindingFlags.InvokeMethod, null, RemoteComObject,
new object[] { "CALL(\"Kernel32\",\"CreateThread\",\"JJJJJJJ\",0, 0, " + memaddr + ", 0, 0, 0)"});
Excel4DCOM OPSEC
• Publicly available tools like SharpExcel4-DCOM and Invoke-
Excel4DCOM.ps1 both make a sequence of suspicious API calls
• VirtualAlloc, WriteProcessMemory, and CreateThread
• Shellcode executed this way will reside in the Excel process, so
monitoring for abnormal process generating network traffic could be
effective
• All these techniques use COM to execute code so they will have the
DCOMLaunch service as a parent process
• Additionally, remotely instantiated DCOM objects will have a socket listening
on a high port
• Office applications will have the “-Embedding” or “/automation -Embedding“
command line flags
WMI Execution
• Several WMI classes can be used to execute code in one way or
another, but all of them require some file on disk to be called
• Typically, cmd.exe is used, but it would be better to call our payload
directly by hosting it on a file share
• Another popular combination is msbuild.exe with a project xml
hosted on a file share
• Popular != Stealthy
WMIExec.py
• WMIExec.py is another Impacket script that can be used to execute
commands using the Win32_Process WMI class
• All processes will have wmiprvse.exe as a parent
• This script also has some OPSEC issues
• Still uses cmd.exe /Q /c
• Writes to disk twice for each command! Once for input command and once
for output command
WMIExec.py
WMIExec.py
WMI Execution in .NET
• The SharpSploit library also provides multiple examples of various
WMI execution methods
• This implementation can take plaintext credentials or the current user
session
• There is also a “pth” branch of SharpSploit that can use a hash instead
of plaintext creds
• The WMIExecute method implements many of the improvements we
discussed for WMIExec.py
WMI Execution in .NET
LAB: .NET WMI Lateral
Movement
WMI Execution in .NET
DLL Hijacking
• DLL hijacking is a powerful persistence technique, but it can also be
used for lateral movement by utilizing any remote file-write method
• SMB is probably the easiest
• We’ve already learned how to perform hijacking using Koppeling
• MDSec wrote a blog last year about using WMI and DCOM to invoke
nonexistent DLLs on a remote system
DLL Hijacking with WMI
• Whenever a WMI connection is initiated on a remote host, the
wmiprvse.exe executable is spawned to handle the connection
• You can make any simple query, or even just authenticate to the remote
host to start this process
• Valid credentials are not necessary to trigger this process!
• Many nonexistent DLLs will be called by the WMI provider host