buffer overflow & return address attack

20
Buffer Overflow & Return Address Attack

Upload: howard-chang

Post on 07-Aug-2015

51 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Buffer Overflow  & Return Address Attack

Buffer Overflow & Return Address Attack

Page 2: Buffer Overflow  & Return Address Attack

#include <stdio.h>#include <stdlib.h>#include <string.h>void f2(char* buf,char* input){ strcpy(&buf[8],&input[0]); strcpy(&buf[-4],&input[0]);}void f1(char* input){ int i; char buf[8]; int j

printfi = 0xaaaaaaaa;j = 0xaaaaaaaa;f2(buf,input);printfint main(int argc, char *argv[]){

f1(argv[1]);return 0;}

Page 3: Buffer Overflow  & Return Address Attack

i小

j buf

Page 4: Buffer Overflow  & Return Address Attack
Page 5: Buffer Overflow  & Return Address Attack

#include <stdio.h>void function(int a, int b, int c) { char buffer[5]; int *ret; ret = (int *)(buffer + 24); (*ret) += 7; }int main(){ int x = 0; function(1, 2, 3); x = 1; printf("\n\nx = %i \n\n", x); return 0; }

Page 6: Buffer Overflow  & Return Address Attack
Page 7: Buffer Overflow  & Return Address Attack

return address < main +35 >return address+7 < main +42 >

char buffer[5]; int *ret; ret = (int *)(buffer + 24); (*ret) += 7;

Page 8: Buffer Overflow  & Return Address Attack

#include <string.h>#include <stdio.h>void foo() { char s[4]; int *ret; ret = (int *)(s + 24); (*ret) += 21;}int main(){ foo(); printf("\n\n\nreturned!\n\n\n"); return 0;}void bar() {printf("\n\n\nhacked!\n\n\n");}

Page 9: Buffer Overflow  & Return Address Attack
Page 10: Buffer Overflow  & Return Address Attack

return address + 21 < main + 35 >

return address < main +14 >

char s[4];int *ret;ret = (int *)(s + 24);(*ret) += 21;

Page 11: Buffer Overflow  & Return Address Attack

Shellcode

Shellcode is defined as a set of instructions injected and then executed by an exploited program. Shellcode is used to directly manipulate registers and the function of a program, so it is generally written in assembler and translated into hexadecimal opcodes.

The term shellcode is derived from its original purpose—it was the specific portion of an exploit used to spawn a root shell.

Page 12: Buffer Overflow  & Return Address Attack

Registers in stack(32bit)

EIP:儲存 cpu下次要執行的 instruction pointerEBP:儲存的是 Bottom of the stack pointer ESP:儲存的是 Top of the stack pointer

Page 13: Buffer Overflow  & Return Address Attack
Page 14: Buffer Overflow  & Return Address Attack
Page 15: Buffer Overflow  & Return Address Attack
Page 16: Buffer Overflow  & Return Address Attack
Page 17: Buffer Overflow  & Return Address Attack
Page 18: Buffer Overflow  & Return Address Attack
Page 19: Buffer Overflow  & Return Address Attack
Page 20: Buffer Overflow  & Return Address Attack