Buffer-Overflow Attacks on the Stack

Similar documents
Buffer-Overflow Attacks on the Stack

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Buffer Overflows. Buffer Overflow. Many of the following slides are based on those from

Program Exploitation Intro

Buffer Overflow Attacks

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

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

CSC 2400: Computer Systems. Using the Stack for Function Calls

Project 1 Notes and Demo

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

buffer overflow exploitation

Sungkyunkwan University

The geometry of innocent flesh on the bone: Return-into-libc without function calls (on the x86) Hovav Shacham presented by: Fabian Fäßler

Practical Malware Analysis

CSC 591 Systems Attacks and Defenses Return-into-libc & ROP

Intro x86 Part 3: Linux Tools & Analysis

Lab 3. The Art of Assembly Language (II)

BUFFER OVERFLOW. Jo, Heeseung

Buffer Overflow. Jo, Heeseung

Buffer Overflows Defending against arbitrary code insertion and execution

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

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

Stack overflow exploitation

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

Is stack overflow still a problem?

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

Buffer Overflow. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSC 405 Computer Security Stack Canaries & ASLR

Buffer Overflow. An Introduction

16.317: Microprocessor Systems Design I Fall 2015

Recitation: Bomb Lab. September 17 th 2018

Buffer Overflow Attack

CSE 361S Intro to Systems Software Lab Assignment #4

Blossom Hands-on exercises for computer forensics and security. Buffer Overflow

Buffer overflows. Specific topics:

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CSC 2400: Computer Systems. Using the Stack for Function Calls

Chapter 7: User Defined Functions and Stack Mechanics

MWR InfoSecurity Security Advisory. Intersystems Caché CSP (Caché Server Pages) Stack Overflow. 17 th December 2009

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CSC 8400: Computer Systems. Using the Stack for Function Calls

Obstacle Course Buffer Overflow Hacking Exercise

Lecture 08 Control-flow Hijacking Defenses

Full Name: CISC 360, Fall 2008 Example of Exam

ANITA S SUPER AWESOME RECITATION SLIDES

Exploiting Stack Buffer Overflows Learning how blackhats smash the stack for fun and profit so we can prevent it

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

Machine- Level Programming V: Advanced Topics

ROP It Like It s Hot!

CSC 373, Winter 2012 Lab Assignment 3: The Buffer Bomb

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

CSE509 System Security

Mitchell Adair January, 2014

x86 assembly CS449 Fall 2017


Offensive Security My First Buffer Overflow: Tutorial

Lab 10: Introduction to x86 Assembly

Non-stack Based Exploitation of Buffer Overrun Vulnerabilities on Windows NT/2000/XP

Machine Language, Assemblers and Linkers"

Università Ca Foscari Venezia

PRESENTED BY: SANTOSH SANGUMANI & SHARAN NARANG

BUFFER OVERFLOW DEFENSES & COUNTERMEASURES

Machine-Level Programming V: Buffer overflow

Function Call Convention

Hands-on Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Machine-Level Programming V: Advanced Topics

Linux Memory Layout The course that gives CMU its Zip! Machine-Level Programming IV: Miscellaneous Topics Sept. 24, Text & Stack Example

Buffer Overflow Attack (AskCypert CLaaS)

Recitation: Attack Lab

18-600: Recitation #3

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Introduction to Reverse Engineering. Alan Padilla, Ricardo Alanis, Stephen Ballenger, Luke Castro, Jake Rawlins

18-600: Recitation #4 Exploits

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Machine-Level Programming V: Miscellaneous Topics Sept. 24, 2002

Computer Architecture and Assembly Language. Practical Session 3

CSC 405 Computer Security Reverse Engineering Part 1

20: Exploits and Containment

FLARE-On 4: Challenge 3 Solution greek_to_me.exe

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

CS 105, Fall Lab 4: The Buffer Bomb. See Calendar for Dates

16.317: Microprocessor Systems Design I Fall 2014

Advanced Buffer Overflow

THEORY OF COMPILATION

15-213/18-243, Fall 2010 Exam 1 - Version A

Giving credit where credit is due

EECS 213, Fall 2009 Exploit Lab

Biography. Background

From Over ow to Shell

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

Ethical Hacking: Preventing & Writing Buffer Overflow Exploits

Buffer overflow is still one of the most common vulnerabilities being discovered and exploited in commodity software.

War Industries Presents: An Introduction to Programming for Hackers Part V - Functions. By Lovepump, Visit:

Instruction Set Architectures

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

Return Oriented Programming

Transcription:

Computer Systems Buffer-Overflow Attacks on the Stack Introduction A buffer overflow occurs when a program, while writing data to a buffer, overruns the buffer's boundary and overwrites memory in adjacent locations. Programming languages commonly associated with buffer overflows include C and C++, which have no built-in protection against writing anywhere in memory. Buffer overflows are the basis of many software vulnerabilities and can be maliciously exploited. They have been used by malicious hackers and virus writers for a long time. In this lab we will experiment with harmless buffer overflows that trigger calls to unused functions in our program. A classic exploit would however fill the buffer with its own malicious code. If you are interested in digging deeper into buffer-overflow techniques, refer to https://www.symantec.com/avcenter/reference/blended.attacks.pdf Part1 Understanding Array Allocation and Vulnerable C Functions 1. Copy bufdemo.c from /mnt/a/mdamian/x86 into your ~/systems/x86 directory. #include <stdio.h> void Echo() char buf[4]; gets(buf); puts(buf); int main() printf("\n Type in a string: "); Echo(); return 0; 1. Compile with gcc bufdemo.c o xbufdemo. Ignore the warnings.

2. Load xbufdemo into the debugger (gdb), then disassemble the code for main: gdb xbufdemo set disassembly-flavor intel disas main Disassembled code for main: 0x0804840c <main+10>: push 0x0804840d <main+11>: mov 0x0804840f <main+13>: push 0x08048410 <main+14>: sub 0x08048413 <main+17>: mov 0x0804841a <main+24>: call 0x0804841f <main+29>: call 0x08048424 <main+34>: mov 0x08048429 <main+39>: add 0x0804842c <main+42>: pop 0x0804842d <main+43>: pop 0x0804842e <main+44>: lea 0x08048431 <main+47>: ret ebp ebp,esp ecx esp,0x4 DWORD PTR [esp],0x8048504 0x8048320 <puts@plt> 0x80483e4 <Echo> eax,0x0 esp,0x4 ecx ebp esp,[ecx-0x4] 3. Next disassemble the code for Echo: Echo: 0x080483e4 <Echo+0>: push ebp 0x080483e5 <Echo+1>: mov ebp,esp 0x080483e7 <Echo+3>: sub esp,0x18 0x080483ea <Echo+6>: lea eax,[ebp-0x4] # lea stands for Load Effective Address # The effect is eax ß ebp-4 0x080483ed <Echo+9>: mov DWORD PTR [esp],eax # The value placed on the stack is buf (argument of gets) 0x080483f0 <Echo+12>: call 0x8048300 <gets@plt> 0x080483f5 <Echo+17>: lea eax,[ebp-0x4] 0x080483f8 <Echo+20>: mov DWORD PTR [esp],eax 0x080483fb <Echo+23>: call 0x8048320 <puts@plt> 0x08048400 <Echo+28>: leave 0x08048401 <Echo+29>: ret

4. Inspect the stack contents as you step through the program with the debugger: break *(Echo+12) display /i $eip run x /x $ebp-4 nexti x /4bc ($ebp-4) x /4bx ($ebp-4) x /1wx ($ebp-4) x /4wx ($ebp-4) disas main # Just before gets gets called # This is the buf address # Use nexti so that you don t go into the gets function # Type in the string 123 # Display 4 bytes as chars starting at memory address $ebp-4 # Display 4 bytes in hex starting at memory address $ebp-4 # Display 1 word (4 bytes) in hex starting at address $ebp-4 # Is this machine little endian or big endian? # Display 4 words (16 bytes) in hex starting at address $ebp-4 # You ll see the user string (4 bytes), the old EBP, and the # address to return to the main function RA (main) # Find out the return address, and identify it on the stack 5. Rerun xbufdemo (in the debugger) with input string 1234. What are the four values printed by x /4wx $ebp-4 0x34333231 0x??????00 0x08048424 0x???????? Marked in red above is the return address for main. Marked in blue above is your input string (ASCII codes of the characters you entered).

6. Rerun xbufdemo (in the debugger) with input string 1234567. What are the four values printed by x /4wx $ebp-4 0x34333231 0x00373635 0x08048424 0x???????? Marked in red above is the return address for main. Marked in blue above is your input string (ASCII codes of the characters you entered). 7. Rerun xbufdemo (in the debugger) with input string 123400005678. What are the values printed by x /4wx ($ebp-4) 0x34333231 0x30303030 0x38373635 0x??????00 x /12bx ($ebp-4) 0x31 0x32 0x33 0x34 0x30 0x30 0x30 0x30 0x35 0x36 0x37 0x38 8. What happened to the return address for the main function? Write down the address that the execution jumps to when the ret instruction in Echo gets executed: 0x38373635

Part 2 Understanding Buffer Overflow Attacks 2. Copy bufattack.c and sendstring (an executable file) from /mnt/a/mdamian/x86 into your systems/x86 directory. #include <stdio.h> #include <stdlib.h> /* for exit */ void Echo() char buf[4]; gets(buf); puts(buf); void Fire() printf("\nyou called Fire?!\n"); exit(1); int main() printf("\ntype a string:\n"); Echo(); return 0; 3. Compile with gcc bufattack.c o xbufattack. Ignore the warnings. 4. Load xbufattack into the debugger (gdb), then disassemble the code for main: gdb xbufattack set disassembly-flavor intel disas main Code of interest (main):... 0x08048468 <main+24>: call 0x8048340 <puts@plt> 0x0804846d <main+29>: call 0x8048414 <Echo> 0x08048472 <main+34>: mov eax,0x0... 5. Disassemble the code for Echo: Code of interest (Echo): 0x08048414 <Echo+0>: push ebp 0x08048415 <Echo+1>: mov ebp,esp... 0x0804841a <Echo+6>: lea eax,[ebp-0x4] 0x0804841d <Echo+9>: mov DWORD PTR [esp],eax

0x08048420 <Echo+12>: call 0x8048320 <gets@plt>... 0x08048430 <Echo+28>: leave 0x08048431 <Echo+29>: ret 6. Disassemble the code for Fire: 0x08048432 <Fire+0>: push ebp 0x08048433 <Fire+1>: mov ebp,esp 0x08048435 <Fire+3>: sub esp,0x8 0x08048438 <Fire+6>: mov DWORD PTR [esp],0x8048544 0x0804843f <Fire+13>: call 0x8048340 <puts@plt> 0x08048444 <Fire+18>: mov DWORD PTR [esp],0x0 0x0804844b <Fire+25>: call 0x8048350 <exit@plt> 7. Inspect the stack during the program execution by stepping though with the debugger. Here are some debugger commands to get you started: break *(Echo+12) # Prior to the gets call display /i $eip run x /3xw $ebp-4 nexti # Type in 123400005678 x /3xw $ebp-4 # Note the changes in the stack contents. Identify your input # Notice the little endian representation

8. Write down an exploit string in hexadecimal that causes Fire to execute when gets terminates. Use the address of Fire to overwrite the old EBP as well. Exploit string: The first 4 bytes should fill in the buffer. The next 4 bytes should overwrite the old EBP. You may use any valid address here so just use the address of the Fire function. The last 4 bytes should overwrite the return address for main with the address of Fire. Note that there should be no spaces in the exploit string when passed to the sendstring function (see below). 9. To test out the correctness of your answer in the previous step, we will need to convert your hexadecimal string into a byte string that will serve as input to your program. To do so, first quit the debugger. Write your exploit string into a file, say in.txt, then use sendstring to produce the corresponding byte string as follows: sendstring < in.txt > in.raw The result is a binary file in.raw that contains the bytes you wish to feed to the xbufattack program. 10. Reinvoke the debugger on xbufattack, then repeat the activities from step 6 above, with the only difference that this time you will provide your exploit string as input when running: run < in.raw

11. Step through the code (using nexti or ni). If your exploit string is correct, the execution will jump to the first instruction in Fire after the ret instruction from Echo. 12. Copy bufattack.c into bufattack2.c, then edit bufattack2.c to add two arguments to Fire as shown below. void Fire(int x, int y) printf("\nyou called Fire(%x, %x)?!\n", x, y); exit(1); Compile bufattack2.c into executable xbufattack2, then repeat steps 7 through 10 on the new executable. Your goal is to force Fire(0x11, 0x22) to execute when gets terminates. (Note that the address of Fire may have changed). Write down the exploit string (in hexadecimal): The first 4 bytes should fill in the buffer. The next 4 bytes should overwrite the old EBP. You may use any valid address here so just use the address of the Fire function. The next 4 bytes should overwrite the return address for main with the address of Fire. The next 4 bytes should simulate the return address of the function that called Fire (in this case we forced the call, so place any valid address here). Then come the function arguments, in little endian format. Note that there should be no spaces in the exploit string when passed to the sendstring.