我正在为面试做准备,一位在那里工作的朋友给了我这段代码,他说这段代码很好地为我将被问到的安全问题做了准备。我不太了解这方面的事情,我希望能学到一点,这样我至少可以说一些我应该问。
这是代码:
#include <stdio.h>
#include <string.h>
#include <limits.h>
int main(int argc, char *argv[]){
FILE *inputFile;
char filename[128];
char strings[USHRT_MAX][40];
unsigned short c = 0;
strcpy(filename, argv[1]);
inputFile = fopen (filename, "r");
if (inputFile == NULL) {
perror("Error: ");
return(-1);
}
while (fscanf(inputFile, "%s", strings[c++] ) != EOF);
fclose(inputFile);
return(0);
}
我知道这与缓冲区溢出有关,因为这是他告诉我的,但除此之外,我陷入了困境。有人能解释为什么这段代码容易受到攻击吗?
最佳答案
首先,我建议您阅读this book,它很好地解释了漏洞利用的工作原理。它附带了一个Linux VM,您可以尝试利用代码。(仅供参考,我与这本书没有任何关系,我只是觉得它很好。)
其次,我想在评论中回答philippe的问题:当程序执行时,从main()
返回并不是最后发生的事情。例如,您可以从return
跨过main()
看到许多有趣的事情发生:
Breakpoint 2, main (argc=1, argv=0x7fff5fbff320) at main.c:6
6 return 0;
(gdb) stepi
0x0000000100000f17 6 return 0;
(gdb)
0x0000000100000f1a 6 return 0;
(gdb)
0x0000000100000f1d 6 return 0;
(gdb)
0x0000000100000f20 6 return 0;
(gdb)
0x0000000100000f24 6 return 0;
(gdb)
Cannot access memory at address 0x0
0x0000000100000f25 in main (argc=1, argv=0x7fff5fbff310) at main.c:6
6 return 0;
(gdb)
Cannot access memory at address 0x0
0x0000000100000ee4 in start ()
(gdb)
Cannot access memory at address 0x0
0x0000000100000ee6 in start ()
(gdb)
0x0000000100000f26 in dyld_stub_exit ()
(gdb)
0x0000000100000f44 in dyld_stub_puts ()
(gdb)
0x0000000100000f49 in dyld_stub_puts ()
(gdb)
0x0000000100000f34 in dyld_stub_puts ()
(gdb)
0x0000000100000f3b in dyld_stub_puts ()
(gdb)
0x0000000100000f3d in dyld_stub_puts ()
(gdb)
0x00007fff83b396a0 in dyld_stub_binder ()
(gdb)
0x00007fff83b396a1 in dyld_stub_binder ()
(gdb)
0x00007fff83b396a4 in dyld_stub_binder ()
(gdb)
0x00007fff83b396ab in dyld_stub_binder ()
(gdb)
0x00007fff83b396af in dyld_stub_binder ()
(gdb)
0x00007fff83b396b4 in dyld_stub_binder ()
(gdb)
0x00007fff83b396b9 in dyld_stub_binder ()
(gdb)
0x00007fff83b396be in dyld_stub_binder ()
(gdb)
0x00007fff83b396c3 in dyld_stub_binder ()
(gdb)
0x00007fff83b396c8 in dyld_stub_binder ()
(gdb)
... 等。
关于c - 对漏洞利用示例感到困惑?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15510571/