string oriented programming: when aslr is not …...* many references, e.g., haas, defcon 18 17...

33
String Oriented Programming: When ASLR is not enough Mathias Payer* and Thomas R. Gross Department of Computer Science ETH Zürich, Switzerland * now at UC Berkeley

Upload: others

Post on 14-Jul-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

String Oriented Programming:When ASLR is not enough

Mathias Payer* and Thomas R. GrossDepartment of Computer ScienceETH Zürich, Switzerland

* now at UC Berkeley

Page 2: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

2

Current protection is not complete

http://creoflick.net

Page 3: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

3

Motivation: circumvent protections

Format string exploits are often overlooked ● Drawback: hard to construct (due to protection mechanisms)

● Define a way to deterministically exploit format string bugs

1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 20120

100

200

300

400

500

600

700

800

900

1000

Common Vulnerabilities and ExposuresBuffer {over|under}flow Use-after-free Format string

Year

Num

ber

of v

ulne

rabi

litie

s

Page 4: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

5

Attack model

Attacker with restricted privileges forces escalation

Attacker knows both source code and binary

Definition of a successful attack● Redirect control flow to alternate location

● Injected code is executed or alternate data is used for existing code

Page 5: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

6

Outline

Motivation

Attack model

Current protection and their weaknesses

Attack building blocks

String Oriented Programming

Conclusion

Page 6: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

7

Current protection

Page 7: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

8

Current protectionData ExecutionPrevention

http://www.computing.co.uk

Address SpaceLayout Randomization

Ted Atchley, http://torwars.com

Canaries

http://socialcanary.com

Page 8: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

9

Data Execution Prevention (DEP)

DEP protects from code-injection attacks● Based on page table modifications

● A memory page is either executable or writable (not both)

Weaknesses and limitations:● No protection against code-reuse attacks like return-oriented

programming or jump-oriented programming

● Self-modifying code not supported

Page 9: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

10

Addr. Space Layout Rand. (ASLR)

ASLR randomizes code and data layout● Probabilistic protection against attacks based on the loader

● Locations of all non-static memory regions are randomized during startup

Weaknesses and limitations● Some regions remain static for every run

● Prone to information leaks: randomization remains static during execution

● Performance impact on randomized code (~10%)

Page 10: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

11

ASLR Performance Overhead

ASLR uses one register for PIC / ASLR code● On IA32 this leads to a performance degradation

458.sjeng400.perlbench453.povray401.bzip2482.sphinx3445.gobm

k462.libquantum464.h264ref403.gcc471.om

netpp483.xalancbm

k456.hm

mer

450.soplex473.astar429.m

cf433.m

ilc447.dealII444.nam

d470.lbmA

verageG

eo. Mean

0.00%

5.00%

10.00%

15.00%

20.00%

25.00%

30.00%Overhead for -fPIE

-O3 -fPIE

-O2 -fPIE

SPEC CPU2006 benchmark

Ove

rhe

ad

Page 11: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

12

Canaries

Canaries protect against buffer overflows● Compiler modifies stack and structure layout

● Canaries are placed between buffers and other variables, content is verified after buffer operations

Weaknesses and limitations● Only protect against continuous writes (buffer overflows)

● No protection against targeted writes (or reads)

● Prone to information leaks: usually one canary value per execution

Page 12: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

13

Defense summary

Both Canaries and ASLR are probabilistic and prone to information leaks

● One shot attack becomes two shot attack

● Performance issues or data-layout issues

DEP gives us code integrity● Protects against code injection, not code-reuse

● Code-reuse attacks needed to exploit DEP

● Hardware extension

Page 13: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

14

Outline

Motivation

Attack model

Current protection and their weaknesses

Attack building blocks

String Oriented Programming

Mitigation

Conclusion

Page 14: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

15

printf functionality

Format string contains tokens● Each token consumes a stack slot and prints it

● Format dependent on token

printf("fooo") "fooo"printf("%s", "bar") "bar"printf("%d", 0x24) "36"printf("%2s", "foo", "bar") "bar"printf("%3c", 'A', 'B', 'C') "C"printf("%$2c", 'A' ) " A"printf("%2$3c", 'A', 'B') " B"printf("foo%n", &counter) "foo"

Page 15: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

16

Format string attack*

Attacker controlled format results in arbitrary writes● Format strings consume parameters on the stack

● %n token inverses order of input, results in indirect memory write

● Often string is on stack and can be used to store pointers

Write 0xc0f3babe to 0x41414141:● printf("AAAACAAA%1$49387c%6$hn%1$63947c%5$hn");

Random writes are used to:● Redirect control flow

● Prepare/inject malicious data

printf( "AAAACAAA" /* encode 2 halfword pointers */ "%1$49387c" /* write 0xc0f3 – 8 bytes */ "%6$hn" /* store at second HW */ "%1$63947c%5$hn" /* repeat with 0xbabe */);

* many references, e.g., Haas, Defcon 18

Page 16: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

17

Return Oriented Programming (ROP)*

ROP based on stack invocation frames● Executes arbitrary code

● Initial bug prepares stack invocation frames

return address

(data)

return address

(data)

(don't care)

(don't care)

0xffe0

return address

(data)0xffff

insns … … ret

insns … … ret

insns … … ret

insns … … ret

Gadget catalog (at static addrs)

len

gth

of

use

r in

pu

t

* Shacham, CCS'07

Page 17: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

18

Outline

Motivation

Attack model

Current protection and their weaknesses

Format String Exploits

String Oriented Programming● Technique

● Example

Mitigation

Conclusion

Page 18: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

19

String Oriented Programming (SOP)

Observation: format string attacks inject data into running applications

Executing arbitrary code (through data)● Needed: format string bug, attacker-controlled buffer on stack

● Not needed: buffer overflow, executable writable memory regions

SOP builds on ROP/JOP● Overwrites static instruction pointers

Page 19: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

20

String Oriented Programming (SOP)

Patching and resolving addresses● Application is static (this includes application's .plt and .got)

● Static program locations used to resolve relative addresses

Resolving hidden functions● ASLR randomizes ~10bit for libraries

● Modify parts of static .got pointers

● Hidden functions can be called without loader support

Page 20: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

21

Running example

void foo(char *arg) { char text[1024]; // buffer on stack if (strlen(arg) >= 1024) // length check return; strcpy(text, arg); printf(text); // vulnerable printf}

foo(user_str); // unchecked user data

Page 21: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

22

text[1024]

SOP: No ProtectionAll addresses are known, no execution protection, no stack protection

● Redirects control flow to code in the format string itself

&arg

… ? ...0xFFF0

eip to callersaved ebp

...12b unused

...

random write &exploit code

copy of &argptr to 0xFBD4

RIP to foosaved ebp (0xFFE4)

0xFFD4

0xFBD4

printf data

RIP to 0xFBD4

void foo(char *arg) { char text[1024]; if (strlen(arg) >= 1024) return; strcpy(text, arg); printf(text);}

foo(user_str);

Page 22: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

23

SOP: Only DEP

DEP prevents code injection, rely on ROP/JOP instead

GNU C compiler adds frame_lift gadget

&arg

… ? ...0xFFF0

eip to callersaved ebp

...12b unused

...

copy of &argptr to 0xFBD4

RIP to foosaved ebp (0xFFE4)

0xFFD4

0xFBD4

printf data

add $0x1c,%esppop %ebxpop %esipop %edipop %ebpret

RIP to frame_lift

text[1024]random write &stack invocation frames

0xFFD4

0xFBD4

void foo(char *arg) { char text[1024]; if (strlen(arg) >= 1024) return; strcpy(text, arg); printf(text);}

foo(user_str);

Page 23: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

24

SOP: DEP & Canaries

ProPolice uses/enforces stack canaries● Reuse attack mechanism, keep canaries intact

12b unused

&arg

… ? ...0xFFF0

eip to callersaved ebp

8b unused

text[1024]

copy of canary &argptr to 0xFBD8

RIP to foosaved ebp (0xFFE4)

random write &stack invocation frames

0xFFD8

0xFBD8

printf data

16b unused

copy of canary &arg

stack canary

add $0x1c,%esppop %ebxpop %esipop %edipop %ebpret

RIP to frame_liftvoid foo(char *arg) { char text[1024]; if (strlen(arg) >= 1024) return; strcpy(text, arg); printf(text);}

foo(user_str);

Page 24: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

25

SOP: ASLR, DEP, Canaries

Combined defenses force SOP to reuse existing code● Static code sequences in the application object

● Imported functions in the application (.plt and .got)

Use random byte-writes to adjust .got entries● Enable other functions / gadgets that are not imported

● Combine stack invocation frames and indirect jump/call gadgets

void foo(char *prn){ char text[1000]; // protected on stack strcpy(text, prn); printf(text); // vulnerable printf puts("logged in\n"); // 'some' function}

Page 25: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

26

SOP: ASLR, DEP, Canaries

.init.plt

.text

.fini

.got:…

.got.plt:system

Application (static) Libraries, heap, stack(s) (dynamic)

libc(text, data, got)

RX

RW

printf__stack_chk_fail

puts

system@pltputs@plt

lift_esp_gadget

12b unused

&arg

… ? ...0xFFF0

eip to callersaved ebp

8b unused

string array

copy of canary &argptr to 0xFBD8

RIP to foosaved ebp (0xFFE4)

printf data

16b unused

copy of canary &arg

stack canary

heap

0xFFD8

0xFBD8

void foo(char *prn) { char text[1000]; strcpy(text, prn); printf(text); puts("logged in\n");}

Page 26: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

27

SOP: ASLR, DEP, Canaries

.init.plt

.text

.fini

.got:…

.got.plt:system

Application (static) Libraries, heap, stack(s) (dynamic)

libc(text, data, got)

RX

RW

printf__stack_chk_fail

puts

system@pltputs@plt

lift_esp_gadget

12b unused

&arg

… ? ...0xFFF0

eip to callersaved ebp

8b unused

string array

copy of canary &argptr to 0xFBD8

RIP to foosaved ebp (0xFFE4)

printf data

16b unused

copy of canary &arg

stack canary

3 random writes &stack invocation frames

heap

puts

"/bin/sh\0"

0xFFD8

0xFBD8

void foo(char *prn) { char text[1000]; strcpy(text, prn); printf(text); puts("logged in\n");}

Page 27: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

28

Outline

Motivation

Attack model

Current protection and their weaknesses

Format String Exploits

String Oriented Programming

Mitigation

Conclusion

Page 28: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

29

Mitigation

Control-flow protection● Use a shadow stack to protect the RIP

● Protect indirect control flow transfers

Disable writes in format strings● Remove %n processing

● Add (static and dynamic) compiler checks for valid targets

Page 29: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

30

Outline

Motivation

Attack model

Current protection and their weaknesses

Format String Exploits

String Oriented Programming

Mitigation

Conclusion

Page 30: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

31

Conclusion

String Oriented Programming (SOP)● Based on format string exploit

● Extends code-reuse attacks (ROP / JOP)

● Naturally circumvents DEP and Canaries

● Reconstructs pointers and circumvents ASLR

Format string bugs result in complete compromise of the application and full control for the attacker

● SOP protection needs more work (virtualization, or secure libc?)

● Look at the complete toolchain

Page 31: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

32

Questions?

?

Page 32: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

33

Other protection mechanisms

Stack integrity (StackGuard, Propolice)

Verify library usage (Libsafe / Libverify)

Pointer encryption (PointGuard)

ISA modifications (ISA randomization)

Format string protection (FormatGuard)

Randomize memory locations (ASLR)

Check/verify control flow transfer (CFI / XFI)

Page 33: String Oriented Programming: When ASLR is not …...* many references, e.g., Haas, Defcon 18 17 Return Oriented Programming (ROP)* ROP based on stack invocation frames Executes arbitrary

34

Software based fault isolation

● Translates individual basic blocks● Checks branch targets and origins● Weaves guards into translated code

1 1'2 2'3 3'… ...

Original code Code cacheMapping table

Dynamic translator

1

2

43

1'

2'

3'

R RX

Indirect control flow transfers use a dynamic check to verify target and origin