break out of the truman show - black hat briefings · reference dynamic binary instrumentation...

54
BREAK OUT OF THE TRUMAN SHOW ACTIVE DETECTION AND ESCAPE OF DYNAMIC BINARY INSTRUMENTATION Ke Sun [email protected] Xiaoning Li [email protected] Ya Ou [email protected]

Upload: others

Post on 21-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

BREAK OUT OF THE TRUMAN SHOW

ACTIVE DETECTION AND ESCAPE OF DYNAMIC

BINARY INSTRUMENTATION

Ke Sun [email protected]

Xiaoning Li [email protected]

Ya Ou [email protected]

Page 2: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

About Us

Xiaoning

Security Researcher

Dr Ke Sun/Dr Ya Ou

Independent Security Researcher

Page 3: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Typical DBI’s Software Architecture

Instrumentation Plugins

Applic

ation

Instrumentation Interface

Code

CacheJIT Compiler

Emulation Interface

OS/Hardware

Page 4: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Detections Talks

Pintools DynamoRIO

Page 5: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Published DBI Detections Methodologies

Page 6: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

New Approaches to Detect DBI

Page 7: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

New Detections

Passive Detections with Gating Code

Unsupported Instructions

Unsupported Behaviors

Active Detection

Xmode Code

Code Cache Detection

Thread Local Storage

Unexpected Context

Page 8: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Passive Detections with Gating Code

Unsupported instructions

Retf

Unsupported behaviors

Does not support mode switch in WoW64

8

Page 9: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

CPU mode is determined by the “L” bit in the segment

descriptor of the code segment (CS).

Active Detections with Xmode Code

Segment Descriptors

CS = 0023: 32-bit (L=0)

CS = 0033: 64-bit (L=1)

Page 10: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Dynamic mode switch can be carried out by far branches to

the corresponding segment

Far Jump

Far Call

Far Return

IRet

Active Detections with Xmode Code

db 0eahdd Enter64bit_Retdb 033hdb 000h

jmp far 0033: Enter64bit_Ret

Switch from 32-bit

to 64-bit mode

Page 11: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Instruction compatibility

Compatible instructions

Same binary code has same meaning under 32-bit/64-bit mode

Incompatible instructions

Same binary code has different meaning under 32-bit/64-bit mode

compatible code

incompatible code

Active Detections with Xmode Code

Page 12: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Compatible instructions has exactly the same binary &

disassembly under 32-bit and 64-bit mode, but still can have

different results due to different stack frame size.

64-bit mode 32-bit mode

after code execution

eax = 8

after code execution

eax = 4

Active Detections with Xmode Code

Page 13: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Active Detections with Xmode Code

Direct execution in command line

Executed under DBI tools (DynamoRIO)

Page 14: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Active Detection with Code Cache

Code Cache Signature

0xfeedbeaf in Pin Code Cache

Direct execution by command line

Executed by Pin

Page 15: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Use predefined signature and memory search Direct execution only 1 hit

Execution under DBI gives 2 hits: one in original PE image one in

code cache

Signature can be certain code or data

Active Detection with Code Cache

Signature: 90 90 50 58 Signature: 78 56 34 12

Page 16: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Execute & Search

Active Detection: Code Cache Detection

Signature Function Main Function

Page 17: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Direct execution by command line

Active Detection: Code Cache Detection

Executed by Pin

Executed by DynamoRIO

Page 18: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Signature location in code cache can be confirmed to be RWE

without calling memory APIs

Active Detection: Code Cache Detection

Executed by Pin

Executed by DynamoRIO

Page 19: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Thread Local Storage (TLS) is the method by which each

thread in a given multithreaded process can allocate locations

in which to store thread-specific data.

Dynamically bound (run-time) thread-specific data is supported

by way of the TLS API (TlsAlloc, TlsGetValue, TlsSetValue, and

TlsFree).

DBI tools use TLS to store tool-specific data, which can be

detected by using TLS API: TlsGetValue

Active Detection with Thread Local Storage

MSDN: Thread Local Storage https://msdn.microsoft.com/en-us/library/6yh4a9k1.aspx

Page 20: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Thread Local Storage in Native App

Executed by command line

Page 21: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Executed by Pin

Thread Local Storage in Pin Context

Page 22: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Thread Local Storage in DynamoRIO Context

Executed by DynamoRIO

Page 23: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Active Detection with Pin-specific

Context

Pin JIT hides EBX from application usage in

code cache

Page 24: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Pin use a specific location in memory for EBX backup

Real EBX value in runtime control by Pin is the base address for

registers’ backup location

Active Detection with Pin-specific Context

Page 25: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Part of Code Cache Area

Pin-specific Context in EBX

Page 26: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Original Registers in Pin-specific Context

ebx

ebx+1Ch ebp backup location

ebx+24h ebx backup location

ebx+2Ch ecx backup locationebx+30h eax backup location

ebx+20h esp backup location

Page 27: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Detection Method:

Directly write signature code to EBX backup location [EBX+24h]

Read EBX to see if signature can be found

Active Detection with EBX signature

Direct execution by command line

Executed by Pin

Page 28: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Escape Criteria

Page 29: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

How to measure DBI escape

Run banned instructions

Run controlled instructions with controlled DBI context

Run controlled instructions with DBI stack

Run controlled instructions in DBI critical context

Run controlled instructions hijacking DBI control flow

Run controlled instructions tampering instrumentation client

All around how to break the limitation from DBI

Page 30: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Escape Tracing with Hardware Features Performance Monitor Counter

Branch instrumentation framework

Event Handler

Target Process Context

User Policy Callbacks

Kernel Space

User Space

CPU

Target Codes Branches

Eve

nt

Eve

nt

Kernel Policy

Callbacks

Page 31: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Hardware Event with Native/DBI

Indirect calls captured by PMI for the same binary with/

without DBI

Page 32: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Indirect Calls with Hardware Events

Captured Indirect Calls for dummy_func

Without DBI With DBI

Page 33: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Red dot is the Ind Call to

dummy_func at 1880

Running Without DBI

Running under Pin

Escaped from Pin

Calling dummy_func under

Pin without escape

1880 Address is absent

Pin

Page 34: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Red dot is the Ind Call to

dummy_func at 1880

Running Without DBI

Running under DR

Escaped from DR

Calling dummy_func under

DR without escape

1880 Address is absent

DynamoRIO

Page 35: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Escape Approaches

Page 36: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Simplified Attack Surfaces

Instrumentation Plugins

Applic

ation

Instrumentation Interface

Code

CacheJIT Compiler

Emulation Interface

OS/Hardware

3

1

2

4

5

Page 37: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Escape Approaches

Code Cache Manipulation

Run, Modify, Run

Run, Modify Current Code Cache

Critical Data Structures

Pin Stack

Pin/Pinclient callbacks

Pin/Pinclient Data

Demo with retf and Xmode Code

Page 38: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Escape Research in Past

Page 39: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Escape under Pin

Extra codes executed while escape not counted by Pin

Code Cache – Run/Modify/Run

Extra codes not executed Extra codes executed

while escape

Extra codes executed

when no escape

Page 40: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Code Cache – Run/Modify/Run

Escape under DynamoRIO

Extra codes executed while escape not counted by DynamoRIO

Extra codes not executed

Extra codes executed

while escape

Extra codes executed

when no escape

Page 41: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Code Cache –Self Modify in Code Cache

Search Signature & Modify NOPs

Signature

NOPs

Modify

Code

Cache

Page 42: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Key Context - Stack

Pin has dedicated stack to run Pin’s code

Jitted code in code cache uses OS allocated stack

Page 43: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

DBI Key Context - TLS

DBI has critical context point saved in TLS

Page 44: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Pin/Pinclient Critical Sections

.charmve section in Pin Dll

Both .charmve and .pinclie sections in Pintool Plugin

Page 45: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Pin Callbacks/Data

Page 46: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Pinclient Callbacks/Data

PinClient Callbacks can be addressed as data structure

from memory

Page 47: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Escape with Xmode Code

Escape under Pin

32-bit / 64-bit mode switch can be carried out after escape

Page 48: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Escape with Xmode Code

Escape under DynamoRIO

32-bit / 64-bit mode switch can be carried out after escape

Page 49: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Demo

Page 50: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Negative Impacts on Exploit Defense

Page 51: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Create New Attack Surfaces

Code cache provides prefect place for shell code with full

memory read/write

DBI escape can be easily applied by exploit to hijack

control flow and activate exploit/shell code

Defend old exploits, but make new exploit easier!

Page 52: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Summary

Disclosed New Detection Methodologies

Discussed DBI Escape Criteria

In-depth Discussion of DBI Escape with Different Ways

Tampering Code Cache

Tampering Critical DBI Contexts/Callbacks/Data

DBI is a powerful tool to defend existing exploits, but it

also opens a big surface for new exploit utilizing RWE

code cache

Page 53: BREAK OUT OF THE TRUMAN SHOW - Black Hat Briefings · Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying on me, Francisco Falcón / Nahuel Riva, RECon

Reference Dynamic Binary Instrumentation Frameworks: I know you're there spying

on me, Francisco Falcón / Nahuel Riva, RECon 2012

SafeMachine malware needs love, too, Martin Hron / Jakub Jermář, VB 2014

Defeating the Transparency Features of Dynamic Binary Instrumentation,

Xiaoning Li / Kang Li, BlackHat USA 2014

Obfuscation to Defeat Static Analysis Using Cross-mode Coding, Ke Sun /

Xiaoning Li, Source Seattle 2015

https://github.com/lgeek/dynamorio_pin_escape

https://software.intel.com/en-us/articles/pin-a-dynamic-binary-

instrumentation-tool

http://www.dynamorio.org/