cracking the perimeter with sharpshooter - hack in paris · •establishing initial access can...

46
Cracking the Perimeter with SharpShooter Dominic Chell June 2019

Upload: others

Post on 28-May-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

Cracking the Perimeter with SharpShooter

Dominic Chell June 2019

Page 2: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Dominic Chell:

•Offensive Security @ MDSec

•Responsible for *BEST, STAR and TIBER services

•Twitter : @domchell

# whoami

•Projects:

•SharpShooter

•SharpPack

•Chameleon

•LyncSniper

•PowerDNS

Page 3: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Background

•SharpShooter Overview

•Reconnaissance

•Delivery

•Sandbox Evasion

•Staging

•“Free Styling” with SharpShooter

•Exploring AMSI

•Macro Support

•Tradecraft

•Detection / Prevention

OUTLINE

Page 4: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Establishing initial access can often be complex

•Increased focus from defenders on PowerShell attacks

•Easy to signature both statically and with process spawn chains

•AMSI provides engines direct access to memory

•Rise of sandboxing tech, “Next Gen Anti-Virus”, EDR and EDP

•Increased difficulties introducing payloads to environments

•Red teaming is getting harder!

BACKGROUND

Page 5: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Internally developed tool; SharpShooter

•Successful on a number of adversary simulations

•Some success in bypassing traditional and “Next Gen” security controls

OVERVIEW: SharpShooter

Page 6: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

OVERVIEW: SharpShooter

Page 7: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Staged and stageless payload creation framework for Windows based Scripting file formats:

•HTML Applications

•JavaScript

•VBScript

•Windows Script Files

•VBA and Excel4 Macro Support

•Arbitrary execution of CSharp source

•Anti-Sandboxing and HTML Smuggling

OVERVIEW: SharpShooter

Page 8: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Script payloads execute DotNet using DotNetToJScript

•Staged payloads:

•Arbitrary CSharp source code is retrieved via DNS or web

•CSharp source code is compiled and executed using reflection

OVERVIEW: SharpShooter

Page 9: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Targeted reconnaissance provides better chance of success

•Payload should be targeted for correct version of DotNet framework

•If executing in-process shellcode, it should correspond to the target’s architecture

•Alternatively, an x86 process can be spawned and injected in to

RECONNAISSANCE

Page 10: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Reconnaissance e-mail with image and system profiling links

•Embed in e-mail:

•Monitor web logs for results:

RECONNAISSANCE

<img src=“http://attacker.net/logo.png?uid=1234” />

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/8.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Microsoft Outlook 16.0.6366; ms-office; MSOffice 16)

Page 11: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Delivery can leverage the “HTML smuggling” technique from @buffaloverflow

•RC4 encrypted file decrypted in the browser using JavaScript’s WebCrypto APIs

•navigator.mssaveBlob forces the browser to save the decrypted blob locally

•Proxy sees text/html or attachment rather than the content type of the payload (e.g. text/vbscript)

•SharpShooter provides two pre-defined template examples

DELIVERY

Page 12: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Attempts to avoid automated analysis, inspired by CheckPlease:

•Domain keying

•Domain member

•Sandbox artefacts

•Bad MACs

•Debugging

SANDBOX EVASION

Page 13: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Obtaining Active Directory name example:

SANDBOX EVASION

Page 14: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Obtaining Active Directory name example:

SANDBOX EVASION

Page 15: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DEMO: PALO ALTO TRAPS

Page 16: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Shortly after release signatures began to emerge

•Defender AMSI signature detects all DotNetToJScript

•Proclaimed dead by @subTee

DETECTION STATUS

Page 17: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DETECTION STATUS

Page 18: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DETECTION STATUS

Page 19: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

SharpShooter RESURRECTION

Page 20: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Microsoft introduced AMSI in Windows 10

•Standard interface to provide file, memory and stream scanning for any application

•Analysis at the scripting engine therefore access to the plain, deobfuscated code

•Supported in PowerShell, Windows Script Host, JavaScript and VBScript and Office VBA macros

ANTIMALWARE SCAN INTERFACE

Page 21: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

ANTIMALWARE SCAN INTERFACE

Page 22: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

ANTIMALWARE SCAN INTERFACE

Page 23: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Mid-April 2018 @subTee released “SquiblyTwo” attack

•Script execution through Stylesheets using wmic.exe

•Defender AMSI did not trigger

ANTIMALWARE SCAN INTERFACE

Page 24: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Updates to SharpShooter to include “COM Staging” and XSL / SCT generation

•Several known COM methods allow command execution:

•Outlook.CreateObject,

• WScript.Run,

• Shellbrowserwindow.Document.Application.Run,

• WMI StartWin32Process

•Leverage COM to execute wmic.exe or regsvr32.exe on the command line to perform “Squiblydoo” and “SquiblyTwo” attacks

COM STAGING

Page 25: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

COM STAGING

HTA, JS, VBSCOM Interface

(Outlook, WScript, WMI etc)

wmic.exe / regsvr32.exe

Remotely Hosted XSL or SCT

Page 26: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

FREE STYLING WITH SharpShooter

Page 27: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Research in to COM objects supporting XSL processing identified Microsoft.XMLDOM interface

•Inline and remotely hosted transformation of XML against a given stylesheet, providing following benefits:

•No command line execution,

•Regsvr32.exe has known IOCs e.g. User-Agent,

•XSL retrieval via HTTP/HTTPS

•AMSI not supported in scriptlets; added early 2019

•Later used by @bohops to bypass WDAC in CVE-2018-8492

FREE STYLING WITH SharpShooter

Page 28: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

FREE STYLING WITH SharpShooter

Page 29: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DEMO: WINDOWS DEFENDER XSL

Page 30: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•@Tal_Liberman discovered an AMSI bypass using the “AmsiEnable” registry key (HKCU\Software\Microsoft\Windows Script\Settings\AmsiEnable)

•Requires the user to “open” the payload twice:

•First pass checks the registry to determine if the key is set and if not set it

•Second pass opens the payload from the user’s download folder

AMSI BYPASSES

Page 31: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

AMSI BYPASSES

Page 32: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•@tiraniddo discovered a DLL hijacking vulnerability in AMSI

• The technique prevents LoadLibrary from loading the AMSI.dll by convincing it that it’s already loaded

•The scripting engine is unable to find the AMSI DLL exports and fails safe

•Copy wscript.exe to known location with name amsi.dll and run the script file

AMSI BYPASSES

Page 33: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•@Tal_Liberman discovered another bypass in AMSI by patching the amsi.dll’s exported functions

•AmsiScanBuffer handles the buffer that is being scanned

•Function patched in memory to return AMSI_RESULT_CLEAN

• mov eax, 0x80070057; retn

AMSI BYPASSES

Page 34: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DEMO: DEFENDER AMSI BYPASS

Page 35: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•In Feb 2019, SharpShooter added additional support for VBA and Excel 4.0 macros

•VBA support introduced using XMLDOM and XSL technique

•@StanHacked discovered a legacy feature of Office to execute macros using Excel 4.0

•Excel 4.0 does not support AMSI and not recognised by many EDR/EDP solutions

•SharpShooter generates an SLK file to directly execute shellcode in Excel

MACRO SUPPORT

Page 36: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DEMO: MACRO EXECUTION

Page 37: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Default SharpShooter templates do not employ OpSec tradecraft, stageless template:

•Allocates memory EXECUTE_READWRITE for shellcode execution

•Executes shellcode “in process”, e.g. mshta.exe performing C2

•Spawns from the default parent, e.g. wscript.exe launched from chrome.exe

•Indicators discussed in detail by defenders:

•https://countercept.com/blog/analyzing-sharpshooter-part-1/

•https://countercept.com/blog/analyzing-sharpshooter-part-2/

TRADECRAFT

Page 38: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Reducing memory indicators is a trivial step:

•Firstly allocate memory using PAGE_READWRITE

•Reset the page permissions to PAGE_EXECUTE_READ using VirtualProtect

TRADECRAFT

Page 39: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Reducing process indicators can be achieved using injection:

•Spawn innocuous process e.g. iexplore.exe

•Inject shellcode using chosen technique, e.g. ALPC, SetThreadContext, CreateRemoteThread etc.

TRADECRAFT

Page 40: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Parent PID spoofing can be performed using UpdateProcThreadAttribute

•CreateProcess using STARTUPINFOEX struct

TRADECRAFT

Page 41: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

DEMO: TRADECRAFT

Page 42: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Staged mode CSharp compilation using CodeDom with the CompilerParameters.GenerateInMemory = true; parameter

•Command line logging:

•csc.exe invocation

•nslookup.exe for DNS delivery

•Modifications to AmsiEnable registry key for AMSI bypasses

DETECTION

Page 43: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Endpoint prevention strategies:

•Device Guard code integrity policy

•Application whitelisting, block mshta.exe etc.

•Modify default handlers for scripting extensions

•Network:

•Outbound DNS filtering

•Monitor for HTML Smuggling, e.g. WebCrypto APIs

PREVENTION

Page 44: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•Windows Scripting file formats provide a number of interesting opportunities for initial access

•Leveraging COM these can be harnessed for code execution using scriptlets and execution cradles

•Creating weaponised tools raises ethical dilemmas, particularly when observed in the wild

•Red team research/tooling can however provide a rare opportunity to raise the bar in detection at scale

CONCLUSIONS

Page 45: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

•SharpShooter available from https://github.com/mdsecactivebreach/SharpShooter

•Thanks to the following people:

•@tiraniddo: DotNetToJScript

•@Arno0x0x: EmbedInHTML

•@buffaloverflow: Demiguise

•@arvanaghi and @ChrisTruncer: CheckPlease

•@subTee: Squiblydoo/Two

•@StanHacked: Excel4.0 research

REFERENCES

Page 46: Cracking the Perimeter with SharpShooter - Hack In Paris · •Establishing initial access can often be complex •Increased focus from defenders on PowerShell attacks •Easy to

QUESTIONS