Investigating Powershell Attacks
Total Page:16
File Type:pdf, Size:1020Kb
Investigating PowerShell Attacks Black Hat USA 2014 August 7, 2014 PRESENTED BY: Ryan Kazanciyan, Matt Hastings © Mandiant, A FireEye Company. All rights reserved. Background Case Study WinRM, Victim VPN SMB, NetBIOS Attacker Victim workstations, Client servers § Fortune 100 organization § Command-and-control via § Compromised for > 3 years § Scheduled tasks § Active Directory § Local execution of § Authenticated access to PowerShell scripts corporate VPN § PowerShell Remoting © Mandiant, A FireEye Company. All rights reserved. 2 Why PowerShell? It can do almost anything… Execute commands Download files from the internet Reflectively load / inject code Interface with Win32 API Enumerate files Interact with the registry Interact with services Examine processes Retrieve event logs Access .NET framework © Mandiant, A FireEye Company. All rights reserved. 3 PowerShell Attack Tools § PowerSploit § Posh-SecMod § Reconnaissance § Veil-PowerView § Code execution § Metasploit § DLL injection § More to come… § Credential harvesting § Reverse engineering § Nishang © Mandiant, A FireEye Company. All rights reserved. 4 PowerShell Malware in the Wild © Mandiant, A FireEye Company. All rights reserved. 5 Investigation Methodology WinRM PowerShell Remoting evil.ps1 backdoor.ps1 Local PowerShell script Persistent PowerShell Network Registry File System Event Logs Memory Traffic Sources of Evidence © Mandiant, A FireEye Company. All rights reserved. 6 Attacker Assumptions § Has admin (local or domain) on target system § Has network access to needed ports on target system § Can use other remote command execution methods to: § Enable execution of unsigned PS scripts § Enable PS remoting © Mandiant, A FireEye Company. All rights reserved. 7 Version Reference 2.0 3.0 4.0 Requires WMF Requires WMF Default (SP1) 3.0 Update 4.0 Update Requires WMF Requires WMF Default (R2 SP1) 3.0 Update 4.0 Update Requires WMF Default 4.0 Update Default Default Default (R2) © Mandiant, A FireEye Company. All rights reserved. 8 Memory Analysis Memory Analysis Scenario: Attacker interacts with target host through PowerShell remoting § What’s left in memory on the accessed system? § How can you find it? § How long does it persist? © Mandiant, A FireEye Company. All rights reserved. 10 WinRM Process Hierarchy svchost.exe Invoke-Command (DcomLaunch) {c:\evil.exe} wsmprovhost.exe Invoke-Command {Get-ChildItem C:\} evil.exe Client wsmprovhost.exe Invoke-Mimikatz.ps1 -DumpCreds –ComputerName “victim" {PS code} Victim © Mandiant, A FireEye Company. All rights reserved. 11 Remnants in Memory svchost.exe (DcomLaunch) Cmd history wsmprovhost.exe Terminate at end of evil.exe session Cmd history wsmprovhost.exe {PS code} svchost.exe Remnants of (WinRM) WinRM SOAP persist Kernel © Mandiant, A FireEye Company. All rights reserved. 12 How Long Will Evidence Remain? svchost.exe wsmprovhost.exe Kernel Memory Pagefile (WinRM) Best source of Fragments of Fragments of Fragments of Evidence command remoting I/O remoting I/O remoting I/O history, output Varies with # Varies with Varies with Single remoting Retention of remoting memory memory session sessions utilization utilization Varies – may End of remoting Max Lifetime Reboot Reboot persist beyond session reboot © Mandiant, A FireEye Company. All rights reserved. 13 Example: In-Memory Remnants SOAP in WinRM service memory, after interactive PsSession with command: echo teststring_pssession > c:\testoutput_possession.txt © Mandiant, A FireEye Company. All rights reserved. 14 Example: In-Memory Remnants WinRM service memory - Invoke-Mimikatz.ps1 executed remotely on target host © Mandiant, A FireEye Company. All rights reserved. 15 What to Look For? § WSMan & MS <rsp:CommandResponse><rsp:CommandId>""xmlns:r sp="http://schemas.microsoft.com/wbem/wsman/ PSRP Syntax 1/windows/shell"""C80927B1-C741-4E99-9F97- /wsman.xsd CBA80F23E595</a:MessageID><w:Locale xml:lang="en-US" s:mustUnderstand="false" / <rsp:Command> ><p:DataLocale xml:lang="en-US" <rsp:CommandLine> s:mustUnderstand="false" /><p:SessionId"/ w:OperationTimeout></ <rsp:Arguments> s:Header><s:Body><rsp:CommandLine <S N="Cmd“> xmlns:rsp="http://schemas.microsoft.com/wbem/ wsman/1/windows/shell" CommandId="9A153F8A- § Known attacker AA3C-4664-8600- AC186539F107"><rsp:Command>prompt""/ filenames rsp:Command><rsp:Arguments>AAAAAAAAAFkAAAAAAA AAAAMAAAajAgAAAAYQAgC2Yc+EDBrbTLq08PrufN § View context +rij8VmjyqZEaGAKwYZTnxB+ +7vzxPYmogUmVmSWQ9IjAiPjxNUz48T2JqIE49IlBvd2V around hits yU2hlbGwiIFJlZklkPSIxIj48TVM +PE9iaiBOPSJDbWRzIiBSZWZJZD0iMiI § Yes, this is painful +PFROIFJlZklkPSIwIj48VD5TeXN0ZW0uQ29sbG . © Mandiant, A FireEye Company. All rights reserved. 16 Memory Analysis Summary § Timing is everything § Challenging to recover evidence § Many variables § System uptime § Memory utilization § Volume of WinRM activity © Mandiant, A FireEye Company. All rights reserved. 17 Event Logs Event Logs Scenario: Attacker interacts with target host through local PowerShell script execution or PowerShell remoting § Which event logs capture activity? § Level of logging detail? § Differences between PowerShell 2.0 and 3.0? © Mandiant, A FireEye Company. All rights reserved. 19 PowerShell Event Logs § Application Logs § Windows PowerShell.evtx § Microsoft-Windows- PowerShell/Operational.evtx § Microsoft-Windows-WinRM/ Operational.evtx § Analytic Logs § Microsoft-Windows- PowerShell/Analytic.etl § Microsoft-Windows-WinRM/ Analytic.etl © Mandiant, A FireEye Company. All rights reserved. 20 Local PowerShell Execution EID 400: Engine state is changed from None to Available. … HostName=ConsoleHost Start & stop times of PowerShell session EID 403: Engine state is changed PowerShell from Available to Stopped. … HostName=ConsoleHost © Mandiant, A FireEye Company. All rights reserved. 21 Local PowerShell Execution EID 40961: PowerShell console is Start time of starting up PowerShell session EID 4100: Error Message = File C: PowerShell \temp\test.ps1 cannot be loaded Error provides path to Operational** because running scripts is disabled PowerShell script on this system ** Events exclusive to PowerShell 3.0 or greater © Mandiant, A FireEye Company. All rights reserved. 22 Local PowerShell Execution EID 7937: Command test.ps1 is Started. Executed cmdlets, scripts, EID 7937: Command Write-Output is Started. or commands PowerShell (no arguments) Analytic** EID 7937: Command dropper.exe is Started ** Log disabled by default. Events exclusive to PowerShell 3.0 or greater © Mandiant, A FireEye Company. All rights reserved. 23 Remoting EID 6: Creating WSMan Session. The Start of remoting connection string is: 192.168.1.1/wsman? session (client host) PowerShell PSVersion=2.0 EID 400: Engine state is changed from None to Available. … HostName=ServerRemoteHost Start & stop of remoting session EID 403: Engine state is changed from (accessed host) PowerShell Available to Stopped. … HostName=ServerRemoteHost © Mandiant, A FireEye Company. All rights reserved. 24 Remoting (Accessed Host) EID 169: User CORP\MattH Who connected via authenticated successfully using NTLM remoting EID 81: Processing client request for operation CreateShell WinRM Timeframe of Operational remoting activity EID 134: Sending response for operation DeleteShell © Mandiant, A FireEye Company. All rights reserved. 25 Remoting (Accessed Host) EID 32850: Request 7873936. Creating a Who connected via server remote session. UserName: CORP \JohnD remoting EID 32867: Received remoting fragment […] Payload Length: 752 Payload Data: 0x020000000200010064D64FA51E7C784 PowerShell 18483DC[…] Analytic Encoded contents of remoting I/O EID 32868: Sent remoting fragment […] Payload Length: 202 Payload Data: 0xEFBBBF3C4F626A2052656649643D22 30223E3[…] © Mandiant, A FireEye Company. All rights reserved. 26 PS Analytic Log: Encoded I/O Invoke-Command {Get-ChildItem C:\} © Mandiant, A FireEye Company. All rights reserved. 27 PS Analytic Log: Decoded Input Invoke-Command {Get-ChildItem C:\} © Mandiant, A FireEye Company. All rights reserved. 28 PS Analytic Log: Decoded Output Invoke-Command {Get-ChildItem C:\} © Mandiant, A FireEye Company. All rights reserved. 29 Logging via PowerShell Profiles %windir%\system32\WindowsPowerShell\v1.0\profile.ps1 § Add code to global profile § Loads with each local PS session § Start-Transcript cmdlet § Overwrite default prompt function § Limitations § Will not log remoting activity § Can launch PowerShell without loading profiles © Mandiant, A FireEye Company. All rights reserved. 30 Logging via AppLocker § Set Audit or Enforce script rules § Captures user, script path © Mandiant, A FireEye Company. All rights reserved. 31 PowerShell 3.0: Module Logging Solves (almost) all our logging problems! Computer Configuration → Administrative Templates → Windows Components → Windows PowerShell → Turn on Module Logging © Mandiant, A FireEye Company. All rights reserved. 32 Module Logging Example: File Listing Get-ChildItem c:\temp -Filter *.txt -Recurse | Select-String password Microsoft-Windows-PowerShell/Operational (EID 4103) ParameterBinding(Get-ChildItem): name="Filter"; value="*.txt" ParameterBinding(Get-ChildItem): name="Recurse"; value="True" ParameterBinding(Get-ChildItem): name="Path"; value="c:\temp" ParameterBinding(Select-String): name="Pattern"; value="password" ParameterBinding(Select-String): name="InputObject"; value="creds.txt" ... Command Name = Get-ChildItem User = CORP\MHastings Logged upon command execution ParameterBinding(Out-Default): name="InputObject"; value="C:\temp\creds.txt:2:password: secret" ParameterBinding(Out-Default):