Teaching the stack for fun and profit Hugh Nowlan A HackerWeek - - PowerPoint PPT Presentation

teaching the stack for fun and profit
SMART_READER_LITE
LIVE PREVIEW

Teaching the stack for fun and profit Hugh Nowlan A HackerWeek - - PowerPoint PPT Presentation

Teaching the stack for fun and profit Hugh Nowlan A HackerWeek presentation Me TCD Grand DCU Grand Warnings Lecture pertains to IA32 for the most part No insta-l33t Frames, pointers, bounds Stack discipline is


slide-1
SLIDE 1

Teaching the stack for fun and profit

Hugh Nowlan A HackerWeek presentation

slide-2
SLIDE 2

Me

  • TCD
  • Grand
  • DCU
  • Grand
slide-3
SLIDE 3

Warnings

  • Lecture pertains to IA32 for the most part
  • No insta-l33t
  • Frames, pointers, bounds
  • Stack discipline is boooring
  • Complex topic
slide-4
SLIDE 4

Apology before one is required

  • Target audience
slide-5
SLIDE 5

The stack

  • Keep track of functions called
  • Arguments
  • Where to return to
  • Local variables
  • Stored in frames
slide-6
SLIDE 6

Stack buffer overflows

  • Debilitating
  • Widespread (still)
  • Easily introduced
  • Successful exploitation alters control flow
  • Flow altered via modification of return

values

slide-7
SLIDE 7

Function execution

  • Before function execution
  • Address of next instruction stored
  • Frame pointer stored
  • Frame pointer becomes function address
  • After function execution - ret
  • Return address popped from stack
slide-8
SLIDE 8
slide-9
SLIDE 9

Bounds

  • User controlled input = potential threat
  • SQL injection, XSS, BoFs
  • Programming practice should protect
  • Some core elements are insecure
  • Input should go into input buffers
  • Nowhere else...
slide-10
SLIDE 10

C checklist

  • gets
  • printf
  • strcpy
  • memcpy
  • Others...
slide-11
SLIDE 11

Unchecked bounds

  • Unchecked bounds can mean compromise
  • What happens here? Inside function x:
  • input = pointer to 18 bytes of data
  • destination = 10 byte array local to x
  • strcpy(destination, input)
slide-12
SLIDE 12

Oh no.

slide-13
SLIDE 13

What now?

  • Unsurprisingly, a crash
  • Function postlude goes ahead unaware
  • Stored frame pointer becomes “rrrp”
  • Derp
  • Let’s boost it a little
slide-14
SLIDE 14
slide-15
SLIDE 15

Exploitation at last

  • Input is simply a \x escaped hex memory

address

  • This points to a function from which to

return to

  • Handy if you have the right function
  • No arguments, calls execl(“/bin/sh”,null);
slide-16
SLIDE 16

Best-case

  • Assuming this function
  • Get the address (say 0x04112308)
  • Exploited function executes “ret”
  • Code “returns” to the exploit
  • Usually not an option
slide-17
SLIDE 17

Shellcode

  • Expansion of previous technique
  • Injection of code alongside memory

redirection

  • Encoded assembly instructions to execute

exploit code

  • The shorter your code, the leeter your

skilz

slide-18
SLIDE 18

Pre-assembly

void shellcode(void) { char *execarg[2]; execarg[0] = “/bin/sh”; execarg[1] = NULL; execve(execarg[0],execarg,NULL); }

slide-19
SLIDE 19

Preparing payloads

  • Injecting the code isn’t enough
  • NOPs used to make up loading space
  • Buffer location is unclear to attacker
slide-20
SLIDE 20

Assembly

  • Produced assembly is optimised
  • \xeb\x1f\x5e\xb8\x00\x00\x00\x00\xc7

\x46\x07\x00\x00\x00\x00\x50...

  • Nulls need to be removed
  • Strings need to be loaded carefully
slide-21
SLIDE 21

Caveats

  • Executing /bin/sh runs as exploiting user
  • SUID bit changes this
  • Program executes as owner
  • Usually aids less-privileged users
  • chmod +s aprogram
slide-22
SLIDE 22

Prevention

  • Writing safe code
  • Non-executable stack
  • Bounds checking
  • Stack Guard
  • Instruction randomisation
slide-23
SLIDE 23

Correct code

  • Easier than it sounds
  • Cliches appear time and time again
  • Static analysis can help
slide-24
SLIDE 24

Bounds checking

  • Ensure reads and writes are in-bounds
  • Only array references
  • Can’t see variables beyond where they are

declared

  • Some operations slowed by large factors
slide-25
SLIDE 25

Randomised instructions

  • Encrypted executables
  • On the fly decryption
  • Most injected code lacks valid instructions
  • Only protects against injected code
  • Performance hit
slide-26
SLIDE 26

Canaries

  • Present in GCC 4.x
  • Augmented function prologues and

epilogues

  • Canary near return address
  • Value integrity is checked on return
slide-27
SLIDE 27

No-exec stack

  • Non-executable segments in stack
  • Disable execute on all but .text
  • Code section
  • Still vulnerable to
  • Data modification
  • Existing dangerous code
slide-28
SLIDE 28

Prognosis

  • Grim
  • Only you etc. etc.
slide-29
SLIDE 29

Further reading

  • “Smashing the Stack for Fun and Profit”
  • Aleph One
  • Real world examples!
  • exploit-db
  • Shellcode archive
slide-30
SLIDE 30

Reading is only so entertaining

  • intruded.net
  • Great for learning
  • overthewire.org
  • A little more advanced
slide-31
SLIDE 31

kthxbai

  • Questions?
  • Presentation available soon
  • http://www.netsoc.tcd.ie/~nosmo