/* 有缓冲区溢出漏洞的程序 retlib.c */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef BUF_SIZE
#define BUF_SIZE 12
#endif
int bof(char *str)
{
char buffer[BUF_SIZE];
unsigned int *framep;
// 将ebp复制到framep
asm("movl %%ebp, %0" : "=r" (framep));
/* 为了实验目的打印信息 */
printf("Address of buffer[] inside bof(): 0x%.8x\n", (unsigned)buffer);
printf("Frame Pointer value inside bof(): 0x%.8x\n", (unsigned)framep);
strcpy(buffer, str); (*@\reflectbox{\ding{222}} \textbf{buffer overflow!} @*)
return 1;
}
int main(int argc, char **argv)
{
char input[1000];
FILE *badfile;
badfile = fopen("badfile", "r");
int length = fread(input, sizeof(char), 1000, badfile);
printf("Address of input[] inside main(): 0x%x\n", (unsigned int) input);
printf("Input size: %d\n", length);
bof(input);
printf("(^_^)(^_^) Returned Properly (^_^)(^_^)\n");
return 1;
}
// 此函数将在可选任务中使用
void foo(){
static int i = 1;
printf("Function foo() is invoked %d times\n", i++);
return;
}