我被困在黑客练习中。
程序在执行时显示:
Build your own string!
Usage:
./4 length command...
Each command consist of a single character followed by its index.
Example:
./4 11 h0 e1 l2 l3 o4 w6 o7 r8 l9 d10
如何使用命令行参数执行缓冲区溢出攻击?
atoi
将字符串参数str转换为整数(类型为int)。
memset
将字符c(unsigned char)复制到str所指向的字符串的前n个字符中。
感谢您的见解!
#include <alloca.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void usage(const char *argv0) {
printf("Build your own string!\n");
printf("\n");
printf("Usage:\n");
printf(" %s length command...\n", argv0);
printf("\n");
printf("Each command consist of a single character followed by its index.\n");
printf("\n");
printf("Example:\n");
printf(" %s 11 h0 e1 l2 l3 o4 w6 o7 r8 l9 d10\n", argv0);
exit(1);
}
int main(int argc, char **argv) {
char *buffer;
unsigned short buffersize, i, index, length;
if (argc < 2) usage(argv[0]);
length = atoi(argv[1]);
if (length <= 0) {
fprintf(stderr, "bad length\n");
return 1;
}
buffersize = length + 1;
buffer = alloca(buffersize);
memset(buffer, ' ', buffersize);
buffer[buffersize - 1] = 0;
for (i = 2; i < argc; i++) {
if (strlen(argv[i]) < 2) {
fprintf(stderr, "bad command \"%s\"\n", argv[i]);
return 1;
}
index = atoi(argv[i] + 1);
if (index >= length) {
fprintf(stderr, "bad index in command \"%s\"\n", argv[i]);
return 1;
}
buffer[index] = argv[i][0];
}
printf("%s\n", buffer);
return 0;
}
答案 0 :(得分:0)
我所知道的缓冲区溢出是覆盖缓冲区内存地址以执行代码的地方,或者只是指向另一个地址的好函数!在你的脚本中,我相信要进入下一级别,你必须将内存地址指向printf(“%s \ n”,缓冲区);.要做到这一点,使用gdb调试代码并覆盖内存地址,无论printf函数有多少。一个体面的时事是issue。但是你应该阅读关于c编程和源代码中所有易受攻击的代码。哪一个是printf(“我很脆弱”);