Buffer Overflow in Stack: SEED Workshop Lab

 
SEED Workshop
Buffer Overflow Lab
 
 
Outline
 
Principle
1.
High Level Picture
2.
Program Memory Layout
3.
Function Stack Layout
4.
Function Call Chain
Practice
1.
Vulnerable Program
2.
Task Breakdown
3.
Environment Setup
4.
Run Tasks
5.
Run the Exploit
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
High Level Picture
 
 
Principle
Program Memory Layout
 
 
Function Stack Layout
 
 
void func(int a, int b)
{
   int x,y ;
}
 
 
Function Call Chain
 
void 
f(int a, int b)
{
  int x;
}
void main()
{
  
f(1,2);
  printf("hello world");
}
 
 
 
Practice
Vulnerable Program (stack.c)
 
int main(int argc, char **argv)
{   char str[517];
    FILE *badfile;
    
// 1. Opens badfile
    badfile = fopen("badfile", "r");
   
 // 2. Reads upto 517 bytes from badfile
    fread(str, sizeof(char), 517, badfile);
    
// 3. Call vulnerable function
    bof(str);
    printf("Returned Properly\n");
    return 1;
}
 
Buffer Overflow in stack.c
 
Program Behavior
 
Show program behavior for badfile of length:
< 24  bytes
> 24 bytes
 
Goal
 
 
Use of NOP’s
 
 
Task Breakdown - Prepare “badfile”
 
 
Environment Setup for Tasks
 
1.
Turn off address randomization (countermeasure)
  
 
% sudo sysctl -w kernel.randomize_va_space=0
 
1.
Compile set-uid root version of stack.c
% gcc -o stack -z execstack -fno-stack-protector stack.c
% sudo chown root stack
% sudo chmod 4755 stack
 
 
 
Goal - Task A
 
 
Goal - Task A
 
 
Goal - Task A
 
1.
Need for debugging
a.
Buffer size may exceed 24 bytes at run time
b.
Need accurate buffer size
 
Goal - Task A
 
1.
Need for debugging
a.
Buffer size may exceed 24 bytes at run time
b.
Need accurate buffer size
 
1.
Compile debug version of stack.c
 
 
% gcc -z execstack -fno-stack-protector
       -g -o stack_dbg stack.c
 
Task A
 
1.
Start debugging using gdb
2.
Set breakpoint
3.
Print buffer address
4.
Print frame pointer address
5.
Calculate distance
 
Task Breakdown - Prepare “badfile”
 
 
Goal - Task B
 
 
Task B
 
 
Task B
 
1.
Calculate lowest address for shellcode
2.
Add offset
 
Task Breakdown - Prepare “badfile”
 
 
Construct the badfile - exploit.c
 
void main(int argc, char **argv)
{
   
 // Initialize buffer with 0x90 (NOP instruction)
    memset(&buffer, 0x90, 517);
 
    
// From tasks A and B
    
*((long *) (buffer + <distance - task A>)) = <address - task B>;
 
    
// Place the shellcode towards the end of buffer
    memcpy(buffer + sizeof(buffer) - sizeof(shellcode), shellcode, sizeof(shellcode));
}
 
Run the exploit
 
Compile and run exploit.c to generate badfile
Run set-uid root compiled stack.c
 
Countermeasures
 
ASLR
StackGuard
Non-Executable (NX) Stack
 
For each of these, refer to lab description or research.
 
Slide Note
Embed
Share

This content provides a comprehensive overview of buffer overflow in stack focusing on SEED Workshop Lab scenarios. It covers principles, practice, high-level pictures, program memory layout, function stack layout, function call chains, vulnerable program examples, and more. Through detailed images and explanations, readers can grasp the concepts and how to run the exploit effectively.

  • Buffer Overflow
  • Stack
  • SEED Workshop
  • Vulnerabilities
  • Exploit

Uploaded on Oct 03, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. SEED Workshop Buffer Overflow Lab

  2. Outline Principle 1. 2. 3. 4. Practice 1. 2. 3. 4. 5. High Level Picture Program Memory Layout Function Stack Layout Function Call Chain Vulnerable Program Task Breakdown Environment Setup Run Tasks Run the Exploit

  3. High Level Picture

  4. High Level Picture

  5. High Level Picture

  6. High Level Picture

  7. High Level Picture

  8. High Level Picture

  9. High Level Picture

  10. High Level Picture

  11. High Level Picture

  12. Principle Program Memory Layout

  13. Function Stack Layout void func(int a, int b) { int x,y ; }

  14. Function Call Chain void f(int a, int b) { int x; } void main() { f(1,2); printf("hello world"); }

  15. Practice Vulnerable Program (stack.c) int main(int argc, char **argv) { char str[517]; FILE *badfile; // 1. Opens badfile badfile = fopen("badfile", "r"); // 2. Reads upto 517 bytes from badfile fread(str, sizeof(char), 517, badfile); // 3. Call vulnerable function bof(str); printf("Returned Properly\n"); return 1; }

  16. Buffer Overflow in stack.c

  17. Program Behavior Show program behavior for badfile of length: < 24 bytes > 24 bytes

  18. Goal

  19. Use of NOPs

  20. Task Breakdown - Prepare badfile

  21. Environment Setup for Tasks 1. Turn off address randomization (countermeasure) % sudo sysctl -w kernel.randomize_va_space=0 1. Compile set-uid root version of stack.c % gcc -o stack -z execstack -fno-stack-protector stack.c % sudo chown root stack % sudo chmod 4755 stack

  22. Goal - Task A

  23. Goal - Task A

  24. Goal - Task A 1. Need for debugging a. Buffer size may exceed 24 bytes at run time b. Need accurate buffer size

  25. Goal - Task A 1. Need for debugging a. Buffer size may exceed 24 bytes at run time b. Need accurate buffer size 1. Compile debug version of stack.c % gcc -z execstack -fno-stack-protector -g -o stack_dbg stack.c

  26. Task A 1. Start debugging using gdb 2. Set breakpoint 3. Print buffer address 4. Print frame pointer address 5. Calculate distance

  27. Task Breakdown - Prepare badfile

  28. Goal - Task B

  29. Task B

  30. Task B 1. Calculate lowest address for shellcode 2. Add offset

  31. Task Breakdown - Prepare badfile

  32. Construct the badfile - exploit.c void main(int argc, char **argv) { // Initialize buffer with 0x90 (NOP instruction) memset(&buffer, 0x90, 517); // From tasks A and B *((long *) (buffer + <distance - task A>)) = <address - task B>; // Place the shellcode towards the end of buffer memcpy(buffer + sizeof(buffer) - sizeof(shellcode), shellcode, sizeof(shellcode)); }

  33. Run the exploit Compile and run exploit.c to generate badfile Run set-uid root compiled stack.c

  34. Countermeasures ASLR StackGuard Non-Executable (NX) Stack For each of these, refer to lab description or research.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#