Lab 2: Buffer Overflows Fengwei Zhang SUSTech CS 315 Computer - - PowerPoint PPT Presentation

lab 2 buffer overflows
SMART_READER_LITE
LIVE PREVIEW

Lab 2: Buffer Overflows Fengwei Zhang SUSTech CS 315 Computer - - PowerPoint PPT Presentation

Lab 2: Buffer Overflows Fengwei Zhang SUSTech CS 315 Computer Security 1 Buffer Overflows One of the most common vulnerabilities in software Programming languages commonly associated with buffer overflows including C and C++


slide-1
SLIDE 1

Lab 2: Buffer Overflows

Fengwei Zhang

SUSTech CS 315 Computer Security 1

slide-2
SLIDE 2

Buffer Overflows

  • One of the most common vulnerabilities in

software

  • Programming languages commonly associated

with buffer overflows including C and C++

  • Operating systems including Windows, Linux

and Mac OS X are written in C or C++

SUSTech CS 315 Computer Security 2

slide-3
SLIDE 3

How It Works

  • Applications define buffers in the memory

– Unsigned char c [10]

  • Applications use adjacent memory to store

variables, arguments, and return address of a function.

  • Buffer Overflows occurs when data written to

a buffer exceeds its size.

SUSTech CS 315 Computer Security 3

slide-4
SLIDE 4

Overflowing A Buffer

  • Defining a buffer in C

– char buf [10];

  • Overflowing the buffer

– Char buf [10] = ‘x’; – strcpy(buf, “AAAAAAAAAAAAAAAAAAAAAAA”)

SUSTech CS 315 Computer Security 4

slide-5
SLIDE 5

Why We Care

  • Because adjacent memory stores program

variables, parameters, and arguments

  • Attackers can change these values through
  • verflowing a buffer
  • Attackers can gain control over the program

flow to execute arbitrary code

SUSTech CS 315 Computer Security 5

slide-6
SLIDE 6

Process Memory Layout

SUSTech CS 315 Computer Security 6

Stack Heap Data Segment Text Segment High memory Low memory

slide-7
SLIDE 7

Memory Layout for 32-bit Linux

SUSTech CS 315 Computer Security 7

Kernel Space Stack Heap BSS Segment Data Segment Text Segment (ELF) 1GB 3GB Local variable: int a Function malloc() Uninitialized static variables: static char *u static char *s = “Hello world” Binary of the program

slide-8
SLIDE 8

Virtual Memory Layout

SUSTech CS 315 Computer Security 8

slide-9
SLIDE 9

Stack Frame

  • The stack contains activation frames including

local variables, function parameters, and return address

  • Starting at the highest memory address and

growing downwards

  • Last in first out

SUSTech CS 315 Computer Security 9

slide-10
SLIDE 10

A Simple Program

Add (2,3)

SUSTech CS 315 Computer Security 10

3 2 Ret Address EBP C High memory Low memory ESP int add (int a, int b) { int c; c = 1+b; return c; }

slide-11
SLIDE 11

Another Program

int func (char * str) { char mybuff[512]; strcpy(myBuff, str); return 1; } int main (int argc, char ** argv) { func (argv[1]); return 1; }

SUSTech CS 315 Computer Security 11

Draw the Stack Frame!

slide-12
SLIDE 12

Overflowing “myBuff”

SUSTech CS 315 Computer Security 12

(A) str(A) Ret addr(A) EBP(A) A A A A A A High memory Low memory ESP

slide-13
SLIDE 13

Buffer Overflow Defenses

  • The attack described is a classical stack smashing

attack which execute the code on the stack

  • It does not work today

– NX – non-executable stack. Most compilers now default to a non-executable stack. Meaning a segmentation fault occurs if running code from the stack (i.e., Data Execution Prevention - DEP)

  • Disable it with –zexecstack option
  • Check it with readelf –e <PROGRAM> | grep STACK

– StackGuard: Canaries

  • Disable it with –fno-stack-protector option
  • Enable it with –fstack-protector option

SUSTech CS 315 Computer Security 13

slide-14
SLIDE 14

Stack Canaries

  • Stack smashing attacks do two things

– Overwrite the return address – Wait for algorithm to complete and call RET

  • Stack Canaries: Stack Smashing Protector (SSP)

– Placing a integer value to stack just before the return address – To overwrite the return address, the canary value would also be modified – Checking this value before the function returns

SUSTech CS 315 Computer Security 14

slide-15
SLIDE 15

Stack Canaries (cont’d)

SUSTech CS 315 Computer Security 15

(A) str(A) Ret addr(A) EBP(A) Canary(A) A A A A A High memory Low memory ESP

slide-16
SLIDE 16

Bypassing NX and Canaries

  • NX - non-executable stack

– Executing code in the heap – Data Execution Prevention (DEP) – Return Oriented Programming (ROP)

  • Stack Canaries

– Overwriting the Canary with the same value – Brute force attack (e.g., DynaGuard in ACSAC’15)

SUSTech CS 315 Computer Security 16

slide-17
SLIDE 17

Reminders

SUSTech CS 315 Computer Security 17