#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>voidvuln(char*string){volatileinttarget;charbuffer[64];target=0;sprintf(buffer,string);// int sprintf(char *str, const char *format, ...);if(target==0xdeadbeef){printf("you have hit the target correctly :)\n");}}intmain(intargc,char**argv){vuln(argv[1]);}
#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>inttarget;voidvuln(char*string){printf(string);if(target){printf("you have modified the target :)\n");}}intmain(intargc,char**argv){vuln(argv[1]);}
printf 与 sprintf 类似,只是根据格式字符串从栈中取参数
可用于查看内存信息
man 3 printf
Code such as printf(foo); If foo comes from untrusted user input, it may contain %n, causing the printf() call to write to memory and creating a security hole.
#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>inttarget;voidvuln(){charbuffer[512];fgets(buffer,sizeof(buffer),stdin);// char *fgets(char *s, int size, FILE *stream);printf(buffer);if(target==64){printf("you have modified the target :)\n");}else{printf("target is %d :(\n",target);}}intmain(intargc,char**argv){vuln();}
#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>inttarget;voidprintbuffer(char*string){printf(string);}voidvuln(){charbuffer[512];fgets(buffer,sizeof(buffer),stdin);printbuffer(buffer);if(target==0x01025544){printf("you have modified the target :)\n");}else{printf("target is %08x :(\n",target);}}intmain(intargc,char**argv){vuln();}
#include<stdlib.h>#include<unistd.h>#include<stdio.h>#include<string.h>inttarget;voidhello(){printf("code execution redirected! you win\n");_exit(1);}voidvuln(){charbuffer[512];fgets(buffer,sizeof(buffer),stdin);printf(buffer);exit(1);}intmain(intargc,char**argv){vuln();}