DrFuzz SecDev 2016
DrFuzz SecDev 2016
Memory, and
Custom Dynamic Tools for Secure
Development
● Introduction
● Dr. Memory
● DynamoRIO
● Conclusion
2
Introduction
● DynamoRIO
● A dynamic binary instrumentation framework
■ Windows, Linux, Android
■ IA-32, AMD64, ARM, AArch64
● Custom tools
■ Dr. Memory, drstrace, drltrace, drcachesim, drcov, etc.
● Dr. Memory
● A memory checking tool build on top of DynamoRIO
● Dr. Fuzz
● Dr. Fuzz tool
■ Dr. Memory fuzz testing mode
● Dr. Fuzz framework
■ DynamoRIO fuzz testing extension
3
DynamoRIO
A
A A
C
B C
DynamoRIO D
D
4
DynamoRIO + Client
A’
A
A’
C’
B C
DynamoRIO D’
D
5
Outline
● Introduction
● Dr. Memory
● DynamoRIO
● Conclusion
6
Dr. Fuzz Tool
● Fuzzing Test
● Generate (random) input data
● Discover coding errors and security vulnerabilities
● Dr. Fuzz Tool
● Dr. Memory Fuzz Testing Mode
■ Dr. Memory: a memory checking tool
■ In-process function-level fuzzing
■ Feedback guided fuzzing
■ Customizable input generation
● Dr. Fuzz vs LibFuzzer
7
Dr. Fuzz Tool Outline
● Fuzzing Test
● Generate (random) input data
● Discover coding errors and security vulnerabilities
● Dr. Fuzz Tool
● Dr. Memory Fuzz Testing Mode
■ Dr. Memory: a memory checking tool
■ In-process function-level fuzzing
■ Feedback guided fuzzing
■ Customizable input generation
● Dr. Fuzz vs LibFuzzer
8
Dr. Memory: A Memory Checking Tool
9
Dr. Memory: A Memory Checking Tool
● Demo
10
Dr. Fuzz Tool Outline
● Fuzzing Test
● Generate (random) input data
● Discover coding errors and security vulnerabilities
● Dr. Fuzz Tool
● Dr. Memory Fuzz Testing Mode
■ Dr. Memory: a memory checking tool
■ In-process function-level fuzzing
■ Feedback guided fuzzing
■ Customizable input generation
● Dr. Fuzz vs LibFuzzer
11
In-process Function-level Fuzzing
A
A
F
E
C
B C
DynamoRIO D
D
pre_func()
E E
F post_func()
F
12
In-process Function-level Fuzzing
● In-process Fuzzing
● Run init/finit code only once
● Amortizing code cache building cost
■ Building code cache is expensive in Dr. Memory
● Context switches, code instrumentation/emission, …
● Worse in testing environment: short run, little code reuse
■ Much better code reuse with Dr. Fuzz
13
In-process Function-level Fuzzing
● Function-level Fuzzing
● Focus On The Relevant Code
● Library API Fuzzing
● Runtime Options
■ -fuzz_module
■ -fuzz_function
■ -fuzz_offset
■ -fuzz_num_iters
■ -fuzz_call_convention
■ -fuzz_num_args
■ -fuzz_data_idx
■ -fuzz_size_idx
14
In-process Function-level Fuzzing
● Demo
15
Dr. Fuzz Tool Outline
● Fuzzing Test
● Generate (random) input data
● Discover coding errors and security vulnerabilities
● Dr. Fuzz Tool
● Dr. Memory Fuzz Testing Mode
■ Dr. Memory: a memory checking tool
■ In-process function-level fuzzing
■ Feedback guided fuzzing
■ Customizable input generation
● Dr. Fuzz vs LibFuzzer
16
Feedback Guided Fuzzing
● Execution Feedback
● Code/branch/path coverage
● System/function calls
● Trace
● Guidance
● Quantitatively measure the quality of input data
■ Bias to better input data mutation
■ Coverage guided fuzzing (-fuzz_coverage)
● Flipping each bit of initial input data
● Add current input as the mutator seed if discover new code
● Trace based analysis and symbolic execution
■ Smart input generation
17
Basic Block Coverage
A C D
A
A A
C
B C
DynamoRIO D
D
18
Feedback Guided Fuzzing
● Demo
19
Dr. Fuzz Tool Outline
● Fuzzing Test
● Generate (random) input data
● Discover coding errors and security vulnerabilities
● Dr. Fuzz Tool
● Dr. Memory Fuzz Testing Mode
■ Dr. Memory: a memory checking tool
■ In-process function-level fuzzing
■ Feedback guided fuzzing
■ Customizable input generation
● Dr. Fuzz vs LibFuzzer
20
Customized Input Generation
21
Customized Input Generation
● Demo
22
Dr. Fuzz Tool Outline
● Fuzzing Test
● Generate (random) input data
● Discover coding errors and security vulnerabilities
● Dr. Fuzz Tool
● Dr. Memory Fuzz Testing Mode
■ Dr. Memory: a memory checking tool
■ In-process function-level fuzzing
■ Feedback guided fuzzing
■ Customizable input generation
● Dr. Fuzz vs LibFuzzer
23
Dr. Fuzz vs LibFuzzer
● LibFuzzer
● A library for coverage-guided fuzz testing
● http://llvm.org/docs/LibFuzzer.html
● LibFuzzer
Performance = XSan (ASan: ~2x, MSan: ~3x) < DrM (Light: ~3x, Full: ~10x)
24
Outline
● Introduction
● Dr. Memory
● DynamoRIO
25
Dr. Fuzz Framework
A
A
F
E
C
B C
DynamoRIO D
D
pre_func()
E E
F post_func()
F
26
Dr. Fuzz API
● Basic API
● drfuzz_fuzz_target(func_pc, ..., pre_func, post_func)
● drfuzz_get_arg()
● drfuzz_set_arg()
● pre_func()
● Set target function’s arguments
● Set other execution context if necessary
● post_func()
● Decide whether to repeat fuzzing or continue execution
● Provide feedback to the mutator
27
Dr. Fuzz API
● Demo
28
Dr. Fuzz API
● Load Mutator
● drfuzz_mutator_load
● drfuzz_mutator_unload
● Mutator Library API
● drfuzz_mutator_start
● drfuzz_mutator_has_next_value
● drfuzz_mutator_get_current_value
● drfuzz_mutator_get_next_value
● drfuzz_mutator_stop
● drfuzz_mutator_feedback
29
Dr. Fuzz API
● Demo
30
Dr. Fuzz API
● Misc API
● drfuzz_get_target_num_bbs
● drfuzz_get_target_user_data
● drfuzz_set_target_user_data
● ...
31
Outline
● Introduction
● Dr. Memory
● DynamoRIO
● Conclusion
32
Motivation: Memory Bugs
33
Meet the Doctor
34
Dr. Memory
35
Deployment
36
Dr. Memory Demo
37
Implementation Strategy
38
Shadow Metadata
allocate:
malloc, stack write
deallocate
deallocate
39
Shadow Memory
defined defined
malloc uninit
unaddr defined
redzone unaddr
padding unaddr
header unaddr
freed unaddr
40
The Uninitialized Whole Word Problem
Compare 16 bits
Copy 32 bits
byte 0 init init init
Initialize 16
41
Memory Leaks
42
Memory Leak Categories
requested size
aligned, for application
initialized data
pointer
malloc padding
43
Possibly Reachable Memory
malloc header
requested size
aligned, for application
initialized data
pointer
malloc padding
44
Layered Heap Routines
application
C++: new
Heap 3
C library: malloc
Windows API:
HeapAlloc
Heap 1
Heap 2
Native API:
RtlAllocateHeap
45
Monitoring on Windows
46
Memory Reads and Writes by the Kernel
Thus, must know the size and shape of all in and out
parameters to all system calls
47
Dr. Syscall
48
Dr. Strace
49
Dr. Strace
NtOpenKeyEx
arg 0: 0x001fcd0c (type=HANDLE*, size=0x4)
arg 1: 0x109 (type=unsigned int, size=0x4)
arg 2: len=0x18, root=0x3c, name=150/152 "SOFTWARE\Microsoft\Windows NT\
CurrentVersion\LanguagePack\SurrogateFallback", att=0x40,
sd=0x00000000, sqos=0x00000000 (type=OBJECT_ATTRIBUTES*, size=0x4)
arg 3: REG_OPTION_RESERVED or REG_OPTION_NON_VOLATILE
(type=named constant, value=0x0, size=0x4)
succeeded =>
arg 0: 0x001fcd0c => 0x134 (type=HANDLE*, size=0x4)
retval: 0x0 (type=NTSTATUS, size=0x4)
NtQueryKey.KeyCachedInformation
arg 0: 0x134 (type=HANDLE, size=0x4)
arg 1: 0x4 (type=named constant, size=0x4)
arg 2: 0x001fcb5c (type=*, size=0x4)
arg 3: 0xb0 (type=unsigned int, size=0x4)
arg 4: 0x001fca34 (type=unsigned int*, size=0x4)
succeeded =>
arg 2: _KEY_CACHED_INFORMATION {_LARGE_INTEGER {0x1ca043f05a7a595},
int=0x0, int=0x4, int=0x1a, int=0x1, int=0xc, int=0x18, int=0x22}
(type=*, size=0x4)
arg 4: 0x001fca34 => 0x28 (type=unsigned int*, size=0x4)
retval: 0x0 (type=NTSTATUS, size=0x4)
50
Performance Comparison
51
Light Mode
52
Outline
● Introduction
● Dr. Memory
● DynamoRIO
● Conclusion
53
Goals
54
Reach of Toolchain Control Points
runtime inspector
“DynamoRIO”?!?
Dynamo Dynamo
@HP Labs @HP Labs
on PA-RISC on x86
late 1990’s 2000
RIO @MIT
Dynamo + RIO →
(Runtime Introspection
DynamoRIO
and Optimization)
1999 2001
56
DynamoRIO History
VMware Google
DynamoRIO Determina
acquires sponsors
@MIT security startup
Determina Dr. Memory
2001 2003 2007 2010
open-sourced
binary releases
BSD license
2002 2009
57
DynamoRIO Tool Platform Design Goals
Efficient
• Near-native performance
Transparent
• Match native behavior
Comprehensive
• Control every instruction, in any application
Customizable
• Adapt to satisfy disparate tool needs
58
Outline
● Introduction
● Dr. Fuzz Tool
● Dr. Fuzz Framework
● Dr. Memory
● DynamoRIO
● Efficient
● Transparent
● Comprehensive
● Customizable
● Conclusion
59
Basic Interpreter
application code
foo() bar()
A
interpreter
B C
fetch decode execute
Slowdown: 300x
60
Improvement #1: Basic Block Cache
A A
B C C
DynamoRIO
D D
E E
F F
A A
B C C
DynamoRIO
D D
E E
F F
A A
B C C
DynamoRIO
D D
E E
indirect
branch
F F lookup
A A
B C C ind. br.
DynamoRIO stays
on
A
D D trace? C
D
E E E
indirect
?
branch
F F F
lookup
65
Avoiding Intermediate Layers
36.3
7.2 6.1
5.0
1.3 1.0
66
No Intermediate Layer
67
Outline
● Introduction
● Dr. Fuzz Tool
● Dr. Fuzz Framework
● Dr. Memory
● DynamoRIO
● Efficient
● Transparent
● Comprehensive
● Customizable
● Conclusion
68
Unavoidably Intrusive
processprocess
app cache
A A
process B C C process
DynamoRIO
D D
thread
thread
thread
thread
thread
thread
E E
look
up
F F
operating system
69
Arbitrary Interleaving
A call malloc() A
B C DynamoRIO C
E
indirect
branch
F thread-safe lookup
re-entrant!
70
Separate Resources
application
Win32 API
Linux Windows
71
Private Libraries
application client
Win32 API
operating system
72
Dynamically Modified Code
A A
B C C
DynamoRIO
A
D
X D C
D
E E E
indirect
?
branch
F F F
lookup
73
Code Cache Consistency
ARM x86
I-Cache D-Cache I-Cache D-Cache
A: A: A: A:
B: B: B: B:
C: C: C: C:
D: D: D: D:
store B store B
flush B jump B
jump B
74
Outline
● Introduction
● Dr. Fuzz Tool
● Dr. Fuzz Framework
● Dr. Memory
● DynamoRIO
● Efficient
● Transparent
● Comprehensive
● Customizable
● Conclusion
75
Above the Operating System
processprocess
app cache
A A
process B C C process
DynamoRIO
D D
thread
thread
thread
thread
thread
thread
E E
look
up
F F
operating system
76
Intercepting Windows Messages
user mode kernel mode
message pending
modify save user context
shared library
memory image
dispatcher
majority of
executed
time
code in a dispatcher
typical message handler
Windows
application
no message pending
restore context
77
Outline
● Introduction
● Dr. Fuzz Tool
● Dr. Fuzz Framework
● Dr. Memory
● DynamoRIO
● Efficient
● Transparent
● Comprehensive
● Customizable
● Conclusion
78
DynamoRIO + Client Custom Inspector
A
A
B C C
DynamoRIO A
D D C
D
E E E
indirect
?
branch
F F F
lookup
79
Primary Client Events: Code Stream
81
Secondary Client Events
Application exception/signal
• Client chooses whether to deliver, suppress, bypass the app handler,
or redirect control
82
DynamoRIO API: General Utilities
Application control
• Suspend and resume all other threads
Application inspection
• Address space querying
• Module iterator
• Processor feature identification
83
DynamoRIO API: Code Manipulation
State preservation
• Eflags, arith flags, floating-point state, MMX/SSE state
• Spill slots, TLS, CLS
Dynamic instrumentation
• Replace code in the code cache
84
DynamoRIO API: Extension Libraries
85
DynamoRIO Client with Extensions
client code
drx drutil
basic trace
application code client code
drmgr block cache
cache
foo() bar()
A
A
A C
C D
B C
DynamoRIO D
D
86
drdecodelib
87
Powerpoint Under Inspector
88
DynamoRIO versus Pin
● Introduction
● Dr. Fuzz Tool
● Dr. Fuzz Framework
● Dr. Memory
● DynamoRIO
● Implementation
■ Efficient
■ Transparent
■ Comprehensive
■ Customizable
● Security applications
■ Program shepherding
● Conclusion
90
Anatomy of a Memory-Based Attack
network
ENTER
COMPROMISE
kernel
91
Critical Data: Control Flow Indirection
• Subroutine calls
– Return address and activation records on visible stack
• Dynamic library linking
– Function exports and imports
• Object oriented polymorphism: dynamic dispatch
– Vtables
• Callbacks – registered function pointers
– Event dispatch, atexit
• Exception handling
92
Critical Data: Control Flow Exploits
93
Preventing Data Corruption Is Difficult
94
Insight: Hijack Violates Execution Model
Hardware
Interface
Typical
Application Security Attack
Execution Model
95
Goal: Shrink Hardware Interface
Constrained
Hardware Interface
Typical
Application Security Attack
Execution Model
96
Program Shepherding
97
Technique 1: Restricted Code Origins
A
unmodified
code
C
D
program D
modified shepherding
code
indirect
E
branch
lookup
98
Technique 2: Restricted Control Transfers
A A
B C C
A
program
D D C
shepherding
D
E E call E
return ?
F F F
jump
99
Technique 3: Un-circumventable Sandboxing
foo() bar()
A
B
A pre-check
B system call
jump system call post-check
C C
pre-check
system call
jump post-check
C
10
Security Policies: Restricted Code Origins
Self-contained
dynamically generated
code with no system LoadLibrary(),
calls dlopen()
code origins
10
Restricted Code Origins In Practice
102
Security Policy: Function Returns
Direct call
targeted by only StackGhost
Unrestricted one return transparent xor
function returns
103
Security Policy: Inter-Module Calls + Jumps
Only to import of
Unrestricted source module
Only to bindings
Only to export of given in an
target module interface list
104
Security Policy: Intra-module Calls + Jumps
105
DynamoRIO for Malware Analysis
• Performance
▪ QEMU software virtualization 6x+ slower than DR
• Ease of use
▪ Tool API
106
Conclusion
● Summary
● Dr. Fuzz Tool
● Dr. Fuzz Framework
● Dr. Memory
● DynamoRIO
● More information:
● http://dynamorio.org
● http://drmemory.org
● http://groups.google.com/group/dynamorio-users
● http://groups.google.com/group/drmemory-users
107
Optional Slides
Direct Code Modification
e9 37 6f 48 92 jmp <callout>
Kernel32!TerminateProcess:
7d4d1028 7c 05 jl 7d4d102f
7d4d102a 33 c0 xor %eax,%eax
7d4d102c 40 inc %eax
7d4d102d eb 08 jmp 7d4d1037
7d4d102f 50 push %eax
7d4d1030 e8 ed 7c 00 00 call 7d4d8d22
Debugger Trap Too Expensive
cc int3 (breakpoint)
Kernel32!TerminateProcess:
7d4d1028 7c 05 jl 7d4d102f
7d4d102a 33 c0 xor %eax,%eax
7d4d102c 40 inc %eax
7d4d102d eb 08 jmp 7d4d1037
7d4d102f 50 push %eax
7d4d1030 e8 ed 7c 00 00 call 7d4d8d22
Base Performance
11
Results for Restricted Code Origins
Technique 1
Technique 2 Technique 3
Attack Type Restricted Code
Origins
Injected code STOPPED
Existing code
Restricted
Restricted Code
Attack Type Origins
Control Technique 3
Transfers
Return HINDERED
Existing code
Other transfer
Imported
No info
117
Results for Un-circumventable Sandboxing
Imported HINDERED
Intra-module info
Func entry HINDERED
No info HINDERED
118
Determina, Inc.
Write-protected memory
• Data sections in DR library
• DR’s own generated code (indirect branch lookup, etc.)
• Code cache and heap is option-controlled: perf tradeoff