【发布时间】:2012-04-11 17:16:32
【问题描述】:
首先我很抱歉这篇文章的长度,但我想清楚地解释这个问题。
我尝试用 C 编写一种小型的自修改程序,但我遇到了一些麻烦,我不知道具体原因。
平台是: Ubuntu/Linux 2.6.32-40 x86_64,prog 基于 x86 arch,gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3,GNU ld (GNU Binutils对于 Ubuntu)2.20.1-system.20100303
该程序的目的是创建一个 read/write/execute 内存块(使用 memalign(3) 和 mprotect(2) strong>),在这块内存中复制一个名为p()(定义在.text段中)的小函数,然后通过指针执行复制的函数。 p() 函数只是使用printf(puts) 显示一条消息。
为了得到p()的代码的起始地址和结束地址(复制它),我使用了函数本身的地址和dummy()函数的地址在@中的p()之后创建987654328@.
int p() { ... } <- address where the copy starts
int dummy() { ... } <- address where the copy stops
块内存创建和复制已成功完成,但运行块中的代码时会发生段错误。通过使用gdb,很明显我们输入了块的代码(复制函数的主体),但对 printf 的调用失败。在反汇编p()函数和块中的代码时,我看到'调用'中使用的地址不一样。
而且我不知道为什么地址不正确,当代码被复制时它被显示,它与我反汇编p()函数时给我的objdump(或gdb)一样。
二进制文件是用-static 创建的,以避免got/plt 或ld.so 重定位过程的潜在问题。在heap 上运行代码似乎没有问题,因为复制函数的开头已执行(检查gdb 下)。
程序的简化src:
<... skip include/checks ...>
#define newline() putchar('\n')
/* - function copied in the chunk */
int p()
{
printf("hello world\n");
return 0;
}
/* - dummy function to get last address of p() */
int dummy() { return 0; }
int main()
{
char *s, *code, *chunk;
unsigned int pagesz, sz;
int (*ptr)(void);
pagesz = sysconf(_SC_PAGE_SIZE);
chunk = (char*)memalign(pagesz, 4 * pagesz);
mprotect(chunk, 4 * pagesz, PROT_WRITE|PROT_EXEC|PROT_READ);
/* - get size, display addr */
sz = (char *)dummy - (char *)p;
printf("Copy between : %p - %p\n", (char *)p, (char *)dummy);
printf("Copy in chunk : %p - %p\n", chunk, chunk + sz, sz);
/* - copy code (1 byte in supp) */
printf("Copied code : ");
for(s = (char *)p, code = chunk; \
s <= (char *)dummy; s++, code++) {
*code = *s;
/* - write in console -- to check if code of p() after disas
* it with objdump(1) is the same, RESULT : ok */
printf("%02x ", *(unsigned char *)code);
}
newline();
/* - run once orginal function */
ptr = p;
ptr();
/* - run copied function (in chunk) */
ptr = (int (*)(void))chunk;
ptr();
newline();
free(chunk);
return 0;
}
objdump(1)反汇编的p()函数:
080483c3 <p>:
80482c0: 55 push %ebp
80482c1: 89 e5 mov %esp,%ebp
80482c3: 83 ec 18 sub $0x18,%esp
80482c6: c7 04 24 a8 d9 0a 08 movl $0x80ad9a8,(%esp)
80482cd: e8 7e 0c 00 00 call 8048f50 <_IO_puts>
80482d2: b8 00 00 00 00 mov $0x0,%eax
80482d7: c9 leave
80482d8: c3 ret
080483dc <dummy>:
....
当程序在 gdb(1) 下运行时,复制的代码与上面提供的 objdump(1) 相同(十六进制值):
# gcc -m32 -o selfmodif_light selfmodif_light.c -static -g -O0
# gdb -q ./selfmodif_light
Reading symbols from /path/.../selfmodif_light...done.
(gdb) list 55
50 /* - run once orginal function */
51 ptr = p;
52 ptr();
53
54 /* - run copied function (in chunk) */
55 ptr = (int (*)(void))chunk;
<<< The problem is here >>>
56 ptr();
57
58 newline();
59 free(chunk);
(gdb) br 56
Breakpoint 1 at 0x8048413: file tmp.c, line 56.
(gdb) run
Starting program: /path/.../selfmodif_light
Copy between : 0x80482c0 - 0x80482d9
Copy in chunk : 0x80d2000 - 0x80d2019
Copied code : 55 89 e5 83 ec 18 c7 04 24 a8 d9 0a 08 e8 7e 0c 00 00 b8 00 00 00 00 c9 c3 55
hello world
Breakpoint 1, main () at tmp.c:56
56 ptr();
如果我们在 main 中查看,我们接下来会进入块:
(gdb) disas main
Dump of assembler code for function main:
0x080482e3 <+0>: push %ebp
... <skip> ...
=> 0x08048413 <+304>: mov 0x18(%esp),%eax
0x08048417 <+308>: call *%eax
... <skip> ...
0x08048437 <+340>: ret
End of assembler dump.
但是当 p() 和 chunk 被反汇编时,我们在内存块中有一个 call 0x80d2c90 而不是像 p() 函数中的 call 0x8048f50 <puts> 吗?为什么显示的地址不一样。
(gdb) disas p
Dump of assembler code for function p:
0x080482c0 <+0>: push %ebp
0x080482c1 <+1>: mov %esp,%ebp
0x080482c3 <+3>: sub $0x18,%esp
0x080482c6 <+6>: movl $0x80ad9a8,(%esp)
0x080482cd <+13>: call 0x8048f50 <puts> <<= it is not the same address
0x080482d2 <+18>: mov $0x0,%eax
0x080482d7 <+23>: leave
0x080482d8 <+24>: ret
End of assembler dump.
(gdb) disas 0x80d2000,0x80d2019
Dump of assembler code from 0x80d2000 to 0x80d2019:
0x080d2000: push %ebp
0x080d2001: mov %esp,%ebp
0x080d2003: sub $0x18,%esp
0x080d2006: movl $0x80ad9a8,(%esp)
0x080d200d: call 0x80d2c90 <<= than here (but it should be ??)
0x080d2012: mov $0x0,%eax
0x080d2017: leave
0x080d2018: ret
End of assembler dump.
检查内存时,代码似乎相同。在这一点上我不明白发生了什么,有什么问题? gdb的解释失败,复制代码还是什么?
(gdb) x/25bx p // code of p in .text
0x80482c0 <p>: 0x55 0x89 0xe5 0x83 0xec 0x18 0xc7 0x04
0x80482c8 <p+8>: 0x24 0xa8 0xd9 0x0a 0x08 0xe8 0x7e 0x0c
0x80482d0 <p+16>: 0x00 0x00 0xb8 0x00 0x00 0x00 0x00 0xc9
0x80482d8 <p+24>: 0xc3
(gdb) x/25bx 0x80d2000 // code of copy in the chunk
0x80d2000: 0x55 0x89 0xe5 0x83 0xec 0x18 0xc7 0x04
0x80d2008: 0x24 0xa8 0xd9 0x0a 0x08 0xe8 0x7e 0x0c
0x80d2010: 0x00 0x00 0xb8 0x00 0x00 0x00 0x00 0xc9
0x80d2018: 0xc3
如果设置了断点,则在内存块中继续执行:
(gdb) br *0x080d200d
Breakpoint 2 at 0x80d200d
(gdb) cont
Continuing.
Breakpoint 2, 0x080d200d in ?? ()
(gdb) disas 0x80d2000,0x80d2019
Dump of assembler code from 0x80d2000 to 0x80d2019:
0x080d2000: push %ebp
0x080d2001: mov %esp,%ebp
0x080d2003: sub $0x18,%esp
0x080d2006: movl $0x80ad9a8,(%esp)
=> 0x080d200d: call 0x80d2c90
0x080d2012: mov $0x0,%eax
0x080d2017: leave
0x080d2018: ret
End of assembler dump.
(gdb) info reg eip
eip 0x80d200d 0x80d200d
(gdb) nexti
0x080d2c90 in ?? ()
(gdb) info reg eip
eip 0x80d2c90 0x80d2c90
(gdb) bt
#0 0x080d2c90 in ?? ()
#1 0x08048419 in main () at selfmodif_light.c:56
所以此时程序要么像那样运行并发生段错误,要么 $eip 被更改并且程序结束而没有错误。
(gdb) set $eip = 0x8048f50
(gdb) cont
Continuing.
hello world
Program exited normally.
(gdb)
我不明白发生了什么,什么失败了。代码的副本似乎还可以,跳转到内存块也一样,那为什么(调用的)地址不好?
感谢您的回答和时间
【问题讨论】:
-
你到底是个什么样的黑客???请!如果你要编写自修改代码,你必须自己做:-)
标签: c assembly gdb objdump self-modifying