Top Banner
ANTHONY ROSE JACOB KRASNOV VINCENT ROSE 1 @bcsecurity1
103

ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Mar 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

ANTHONY ROSE

JACOB KRASNOV

VINCENT ROSE

1

@bcsecurity1

Page 2: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

2

Legal Stuff…So we don’t go to jailTraining is for informational and research purposes only. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. We believe that it is impossible to defend yourself from hackers without knowing how hacking is done. The information provided by us is only for those who are interested to learn about Ethical Hacking, Security, Penetration Testing and malware analysis.

Page 3: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

ANTHONY ROSEC01И

◦ Co-founder, BC Security◦ Lead Researcher, Merculite Security◦ MS in Electrical Engineering◦ Lockpicking Hobbyist◦ Bluetooth & Wireless Security

Enthusiast

3

whoami

JACOB KRASNOVHUBBLE

◦ Co-founder, BC Security◦ BS in Astronautical Engineering, MBA◦ Red Team Lead◦ Currently focused on embedded

system security

VINCENT ROSEHALCYON

◦ Security Researcher, BC Security◦ BS in Computer Science◦ Software Engineer

Introduction

Page 4: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ How to mask your malware to avoid AMSI and Sandboxes

4

Why are we here?

Introduction

Page 5: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ Antimalware Scan Interface (AMSI)◦ Malware Triggering◦ Empire◦ Obfuscation Techniques◦ Invoke-Obfuscation◦ AMSI Bypasses◦ Sandbox Evasion◦ Put it all together

5

Overview

Page 6: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ Introduce Microsoft’s Antimalware Scan Interface (AMSI) and explain its importance

◦ Learn to analyze malware scripts before and after execution

◦ Understand how obfuscate code to avoid AMSI and Windows Defender

◦ Detect and avoid sandbox environments

6

Goals

Introduction

Page 7: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

We will teach you to…◦ operate Empire◦ obfuscate Powershell◦ avoid AMSI and Sandboxes

We are not going to teach you…◦ how to be a “leet hacker”

7

Expectations

Introduction

Page 8: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

-h What is Malware?

8

Page 9: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ Obfuscation is the main means by which Malware achieves survival

◦ Defeat signature-based Antivirus◦ Makes analysis more difficult

9

Overview of the Evolution of Malware Obfuscation

What is Malware

I’m Obfuscated

You can’t find me…

Page 10: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

The first virus to obfuscate itself was the Brain Virus in 1986◦ Would display unchanged data from a different disk sector instead of the one it

had modified

The first virus to use encryption was the Cascade Virus and also appeared in 1986◦ Used simple XOR encryption

First commercial AV products came out in 1987◦ This included heuristic based AV products!

10

The Early Days

What is Malware

Page 11: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

The Malware Arms Race continued and by 1992 polymorphic virus engines had been released◦ Could be attached to non-

polymorphic viruses to make them more effective

11

Coming into Its Own

What is Malware

Page 12: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

AV wasn’t far behind and soon started to include emulation code to sandbox the malware◦ There were evasion techniques but

we will talk about this later

By the 2000s malware had moved on to so called metamorphic viruses◦ Polymorphic viruses only change

their decryptor while metamorphic change the code body as well

12

Coming into Its Own

Page 13: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Not really completely Fileless◦ Usually requires some kind of initial script/executable to kick off infection◦ Persistence methods may leave traces in places like the registry (e.g.,

Poweliks)

This created a big problem for AV as it has traditionally relied on scanning files/executables

All of this leads into…

13

Going Fileless

What is Malware

Page 14: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Antimalware Scan Interface (AMSI)

14

Page 15: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

The Windows Antimalware Scan Interface (AMSI) is a versatile interface standard that allows your applications and services to integrate with any antimalware product that's present on a machine. AMSI provides enhanced malware protection for your end-users and their data, applications, and workloads.

15

What Is AMSI?

AMSI

Page 16: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ Evaluates commands at run time◦ Handles multiple scripting languages (Powershell, JaveScript, VBA)◦ Provides an API that is AV agnostic ◦ Identify fileless threats

16

That’s Great But What Does that Mean?

AMSI

Page 17: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

17

Data Flow

AMSI

Page 18: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

The code is evaluated when it is readable by the scripting engine This means that:

becomes:

However:

Does not become:

This is what allows us to still be able to obfuscate our code

18

One point of clarification (Powershell)

AMSI

Page 19: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Malware Triggering

19

Page 20: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ Windows Defender◦ Antimalware Scan Interface (AMSI)◦ Control flow guard◦ Data Execution Prevention (DEP)◦ Randomized memory allocations◦ Arbitrary code guard (ACG)◦ Block child processes◦ Simulated execution (SimExec)◦ Valid stack integrity (StackPivot)

20

Types of Windows Mitigations

Malware Triggering

Page 21: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

22

Flagged Malware

Malware Triggering

Page 22: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Get-WinEvent 'Microsoft-Windows-Windows Defender/Operational' -MaxEvents 10 | Where-Object Id -eq 1116 | Format-List

23

Windows Defender Logs

Malware Triggering

Page 23: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

1. Run Powershell ISE2. Look in the sample folder3. Try out samples 1-3

24

Try Some Code Samples

Malware Triggering

Page 24: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Building/Customizing Your Malware

25

Page 25: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Prioritize what you want to complete1. Get working base code first

◦ Empire, Metasploit, Etc

2. Customize Functions3. Obfuscate Code4. Test Against AV

26

Don’t Do Too Much at Once

Building/Customizing Malware

Page 26: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

New-ItemProperty -Path "HKLM:\Software\policies\microsoft\windows defender" -name disableantispyware -value 0 –Force

Restart computer/VM

27

Disabling Windows Defender

Building/Customizing Malware

Page 27: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Empire Tutorial

28

Page 28: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Post-exploitation framework built around Powershell◦ Merger of Powershell Empire and Python EmPyre projects◦ Runs on Python 2.6/2.7◦ Encrypted C2 channel◦ Adaptable modules

◦ .bat, .vbs, .dll

◦ Released at BSidesLV 2015

29

What is Empire?

Empire

Page 29: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

30

Why Go After Powershell?◦ Full .NET access◦ Direct access to Win32 API◦ Operates in memory◦ Installed by default in Windows◦ Admins typically leave it enabled

Empire

Page 30: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Relatively small payload (stager) that calls back to a listener

31

How Empire is Deployed?

Empire

Page 31: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Preloaded into VMhttps://github.com/BC-SECURITY/EmpireInstall our forked version (Do not use version 2.5)◦ sudo ./setup/install.sh◦ setup ./reset.sh

32

Empire Tutorial

Empire

Page 32: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Splash page◦Version running (We are using a modified dev version)

◦How many modules loaded◦Active Listeners◦Active Agents

33

Empire Tutorial

Empire

Page 33: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

“Help” lists out all available commands◦ Agents – Active payloads available◦ Interact – Control a payload/host◦ Preobfuscate – Obfuscates Powershell

module (not needed)◦ Set – Modify payload settings◦ Usemodule – Select Empire Module◦ Uselistener – Select Listener◦ Usestager – Select Empire stager (we will

be using macros)

34

Empire Tutorial

Empire

Page 34: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Setting up your listener

Select “uselistener http”

35

Empire Tutorial

Empire

Page 35: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Use edit to modify Listener info◦“set Name LISTENERNAME”◦“set Host YOURIPADDRESS”◦“set Port PORTNUMBER”◦“execute”

36

Empire Tutorial

Empire

Page 36: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

37

Empire TutorialUsestager◦Tailor the stager to what the target is◦“Multi/Launcher”

◦ Useful for testing VM setups

Empire

Page 37: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Setting the stager and listener

Successful callback to Empire

38

Testing the Launcher

Empire

Page 38: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

New-ItemProperty -Path "HKLM:\Software\policies\microsoft\windows defender" -name disableantispyware -value 1 –Force

Restart computer/VM

39

Enabling Windows Defender

Empire

Page 39: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Setting the stager and listener

40

Testing the Launcher

Empire

Page 40: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Build the stager◦ Select “usestager multi/launcher”◦ “info” to view settings◦ Turn off Obfuscation

◦ Turn off AMSIBypass

41

Test your Empire Payload

Empire

Page 41: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Final check on settings◦ All obfuscations are disabled◦ Both AMSI Bypasses are disabled◦ Good to Go!

◦ “execute”

42

Test your Empire Payload

Empire

Page 42: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

43

Test your Empire Payload

Empire

Page 43: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Default Empire will not get past AMSI◦ Obfuscation or changes are needed◦ Default Empire will get you caught

44

Empire Tutorial

Empire

Page 44: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Obfuscation Techniques

45

Page 45: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Powershell ignores capitalization

◦ Create a standard variable

◦ This makes and

◦ The same as…

◦ AMSI ignores capitalization, but changing your hash is a best practice

46

Randomized Capitalization

Obfuscation Techniques

Page 46: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

AMSI is still heavily dependent upon signatures, simple concatenation can circumvent most alerts

will be flagged

But, is not flagged

47

Concatenation

Obfuscation Techniques

Page 47: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Powershell recognizes $ as a special character in a string and will fetch the associated variable.

We embedded into

Which gives us

48

Variable Insertion

Obfuscation Techniques

Page 48: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Powershell allows for the use of {} inside a string to allow for variable insertion. This is an implicit reference to the format string function.

will be flagged

But,Returns…

49

Format String

Obfuscation Techniques

Page 49: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Uses:◦ Pseudorandom number generation◦ Error detection◦ Encryption/Decryption

◦ Reversable function

50

XOR || ⊕

A B A XOR B

0 0 0

0 1 1

1 0 1

1 1 0

Obfuscation Techniques

Page 50: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Using Samples 1-3 from the early exercise attempt to obfuscate them so that they will run

Sample 3 can be difficult to figure out what is causing the issueSave your modified versions as a different name. We will reuse the unobfuscated samples latter

Close/Open Powershell ISE between samples

51

Obfuscate the Samples

Obfuscation Techniques

Page 51: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ Break large sections of code into smaller pieces◦ Isolate fewer lines to determine what is being flagged◦ Good place to start is looking for “AMSI”

52

Hints

Page 52: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

53

The Answers

Page 53: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Invoke-Obfuscation

54

Page 54: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Install here◦https://github.com/danielbohannon/Invoke-Obfuscation◦“Start-up.ps1”◦“Import-Module ./Invoke-Obfuscation.psd1”

◦Run “Invoke-Obfuscation”

55

Invoke-Obfuscation

Obfuscation Techniques

Page 55: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Type “Tutorial” for high level directions◦ Extremely helpful for

learning/remembering the basics

56

Invoke-Obfuscation

Obfuscation Techniques

Page 56: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Example code◦ Use Sample 4◦ SET SCRIPTBLOCK…

57

Invoke-Obfuscation

Obfuscation Techniques

Page 57: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Token-layer Obfuscation◦ Token\Variable (extremely useful for

masking variable names to AMSI)◦ Token\All (if you are super lazy)

◦ This will get you caught◦ Typically run whitespace last (2-3

times)

58

Invoke-Obfuscation

Obfuscation Techniques

Page 58: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Encoding◦ Used to further mask the payload

by converting the format (e.g., Hex, Binary, AES, etc)

◦ Beware: running too much encoding will break the 8,191 character limit

61

Invoke-Obfuscation

Obfuscation Techniques

Page 59: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

String◦ Obfuscate Powershell code as a

string◦ Breaks up the code with reversing

techniques and concatenation

62

Invoke-Obfuscation

Obfuscation Techniques

Page 60: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Compress◦ Can be used in conjunction with

Encoding to reduce the overall size of the payload.

63

Invoke-Obfuscation

Obfuscation Techniques

Page 61: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Launcher◦ Not needed since Empire already

includes a launcher

64

Invoke-Obfuscation

Obfuscation Techniques

Page 62: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Order of operations◦Mix it up to avoid detection◦Example:◦Token\String\1,2◦Whitespace\1◦Encoding\1◦Compress\1

65

Invoke-Obfuscation

Obfuscation Techniques

Page 63: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

66

Invoke-Obfuscation in Empire

Obfuscation Techniques

Page 64: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

AMSI Bypasses

67

Page 65: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

If our payload is already obfuscated enough to evade AMSI why bother?◦ Only the first part of the stager is obfuscated!

68

Why do we need this?

AMSI Bypasses

Page 66: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

AMSI bypasses let us load whatever future modules we may want without issues◦ Mimikatz, PS-Inject, Powerup

69

Why do we need this?

AMSI Bypasses

Page 67: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦ AMSI_RESULT_CLEAN = 0◦ AMSI_RESULT_NOT_DETECTED = 1◦ AMSI_RESULT_BLOCKED_BY_ADMIN_START = 16384◦ AMSI_RESULT_BLOCKED_BY_ADMIN_END = 20479◦ AMSI_RESULT_DETECTED = 32768

70

AMSI results

AMSI Bypasses

Page 68: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

71

Keep It Simple Stupid

Page 69: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

72

Keep It Simple Stupid

Page 70: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Simplest Bypass that currently works◦ $Ref=[REF].Assembly.GetType('System.Management.Automation.AmsiUtils');◦ $Ref.GetField('amsiInitFailed', 'NonPublic, Static').SetValue($NULL, $TRUE);

73

Bypass 1: Reflective Bypass

AMSI Bypasses

Page 71: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Using reflection we are exposing functions from AMSIWe are setting the AmsiInitField to True which source code shows causes AMSI to return:◦ AMSI_SCAN_RESULT_NOT_FOUND

74

What Does it Do?

AMSI Bypasses

AMSI.dll

Page 72: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

AMSI is loaded into the Powershell process at start up so it has the same permission levels as the process the malware is in

75

Why does this work?

AMSI Bypasses

Page 73: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

More complicated bypass, but still allows AMSI to load

76

Bypass 2: Patching AMSI.dll in Memory

AMSI Bypasses

Page 74: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

We use C# to export a few functions from kernel32 that allows to identify where in memory amsi.dll has been loaded

77

Bypass 2: Patching AMSI.dll in Memory

AMSI Bypasses

Page 75: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

We modify the memory permissions to ensure we have access

78

Bypass 2: Patching AMSI.dll in Memory

AMSI Bypasses

Page 76: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Modifies the return function to all always return a value of RESULT_NOT_DETECTED

79

Bypass 2: Patching AMSI.dll in Memory

AMSI Bypasses

Page 77: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

AMSI.dll is loaded into the same memory space as Powershell. This means that we have unrestricted access to the memory space that AMSI runs in and can modify it however we pleaseTells the function to return a clean result prior to actually scanning

80

Why does this work?

AMSI Bypasses

Page 78: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Ensure that ObfuscateCommandand AMSI Bypass both display values◦ “set ObfuscateCommand Token\All\1”◦ “set AMSIBypass True”

81

AMSI Bypasses in Empire

Page 79: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Re-enable Defender and run your Empire launcher

82

Test time!

AMSI Bypasses

Page 80: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Sandbox Detection and Evasion

83

Page 81: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

A software created environment that isolates and limits the rights and accesses of a process being executed

An effective way of doing behavioral analysis for AV

84

What is a Sandbox?

Sandboxes

Page 82: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

85

Who is using Sandboxes?

Sandboxes

Page 83: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

As we talked about earlier, obfuscating code to break signatures can be relatively trivial◦ AV would need an almost unlimited

number of signatures

Heavily obfuscated code can make it almost impossible for human analysis to be effective

Instead evaluate behavior

86

Automated Sandbox Malware analysis

Sandboxes

Page 84: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Sandbox Indicators

87

Page 85: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

They use a lot of resources which can be expensive

End users don't want to wait to receive their messages

Email scanning requires thousands of attachments to be evaluated constantly

88

Sandbox Limitations

Sandboxes

Page 86: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

These limitations provide us with several means to try and detect or evade them◦ Password Protection◦ Time Delays◦ Auto open vs close◦ Check for limited resources (small amount of ram, single core, etc.)◦ Look for virtualization processes (sandboxie, VMWare tools)

89

Sandbox Limitations

Sandboxes

Page 87: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Embedding Macros

90

Page 88: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

91

Back to EmpireUsestager◦Tailor the stager to what the target is◦Our focus is Windows using a Macro (will be used later) ◦“Windows/macro”

Empire

Page 89: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

◦Set stager and listener◦Copy macro over to Word

92

Creating a Payload

Page 90: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Open Word DocumentSelect Developer OptionsClick on Macros

93

Embedding the Macro

Page 91: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

94

Embedding the Macro

Page 92: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Evasion Techniques

95

Page 93: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Before we do suspicious things such as…◦ Starting a new process◦ Reaching out to the internet

The checks could be suspicious themselves◦ Sandbox Evasion is becoming more

prevalent

96

When do we want to do this?

Evasion Techniques

Page 94: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

The sandbox doesn't know the password and therefore can't open the file. No results are found so the file is passed on.The password is usually sent in the body of the email with instructions to use it.◦ Lower success rate

97

Password Protection

Evasion Techniques

Page 95: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Email filters have a limited amount of time to scan files so delay until it the scan is completed

This is less practical in a macro as it will keep the document open until done waiting

98

Time Delay

Evasion Techniques

Page 96: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Using WMI Objects you can enumerate the hardware and system configurationsSome malware looks for things like the presence of a fan◦ Note: WMI objects are very inconsistently implemented by manufacturers.

99

Checking for Resources

Evasion Techniques

Page 97: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Some Useful WMI Objects◦ Win32_ComputerSystem◦ Win32_LogicalDisk◦ Win32_Fan

100

Checking for Resources

Evasion Techniques

Page 98: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Most if not all sandboxes result in the addition of management processes that we can look for◦ Win32_Process contains all the processes currently running

Some common processes to look for:◦ Sbiesvc, SbieCtrl◦ Vmtools◦ VBoxService

101

Checking for Processes

Evasion Techniques

Page 99: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Because of the control many developers have on implementing WMI objects or naming processes there is no one check that is guaranteed to work.◦ Learn as much as possible about the target environment◦ Use multiple halting conditions◦ Check places like attack.mitre.org to look for new techniques if old ones fail

102

There is no one way guaranteed to work

Evasion Techniques

Page 100: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Covert tactic used to develop signatures on malware and prevent attacksWhat do we know so far?

◦ Zero documentation anywhere◦ We can get a callback out

103

Sandbox Evasion Example

Page 101: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

Put it all togetherYOUR TURN TO TRY IT ALL

104Conclusion

Page 102: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

1. Build payload in Empire◦ AMSI Bypass◦ Obfuscation

2. Embed into Word Doc◦ Verification

3. Add in Macro Checks to avoid “Sandbox”4. (Optional) Test on host machine

105

Put it all together

Page 103: ANTHONY ROSE JACOB KRASNOV VINCENT ROSE CON 27/DEF CON 27... · 2019-09-30 · ANTHONY ROSE C01И Co-founder, BC Security Lead Researcher, MerculiteSecurity MS in Electrical Engineering

[email protected]

@BCSECURITY1

HTTPS://GITHUB.COM/BC-SECURITY/DEFCON27

106