我正在尝试在 linux x86 架构上制作一个 tcp 绑定(bind) shellcode,并且我正在使用 GAS 语法。 我可以成功调用 socketcall(SYS_SOCKET) 并获得一个文件描述符(它不是 NULL) 现在我正在尝试使用该 fd 并进行 SYS_BIND 调用,我似乎从 bind 获得了一个非零(它应该是)的返回值。我已经调试过了。一切都在堆栈上,但是......
这里是注释代码:
.text
.globl _start
_start:
xorl %eax, %eax
xorl %ebx, %ebx
movb $1, %bl
subl $30, %esp
movl %esp, %esi
# array of arguments for socketcall->SYS_SOCKET:
movb $2, 8(%esi)
movb $1, 12(%esi)
movl %eax, 16(%esi)
# load the address of that array in ecx:
leal 8(%esi), %ecx
movb $102, %al
int $0x80
jz exit0 # exit(0) if eax(return value)==0
# ---------------------------------------------------------------------
bind:
movl %eax, %edx # save the file descriptor
xorl %eax, %eax
inc %bl # bl == 2 -> bind
movw $2, 8(%esi) # sa_family == AF_INET
movw $2222, 10(%esi) # sin_port == 2222
movl %eax, 12(%esi) # INADDR_ANY
movl %eax, 16(%esi) # sin_zero
movl %eax, 20(%esi) # sin_zero
addl $16, %eax
pushl %eax # size of struct sockaddr(16) as the third argument of bind,pushed first
leal 8(%esi), %ecx # leal the address of argument array into ecx
pushl %ecx # push it onto the stack
pushl %edx # file descriptor (first argument)
movb $102, %al # socketcall
int $0x80
jnz exit1 # exit(1) if bind() return value is not zero (zero flag is not set)
# ---------------------------------------------------------------------------
exit0:
xorl %eax, %eax
inc %eax
xorl %ebx, %ebx
int $0x80
# --------------------------------------------------------------------------
exit1:
xorl %eax, %eax
inc %eax
xorl %ebx, %ebx
inc %ebx
int $0x80
编译:
as -ggstabs -o sys_socket.o sys_socket.s
链接:
ld -o sys_socket sys_socket.o
在最后一个内核中断 (GDB) 之前的堆栈和寄存器:
(gdb) x/8xw $esp
0xbffff5d6: 0x00000007 0xbffff5ea 0x00000010 0x00000000
0xbffff5e6: 0x00000000 0x08ae0002 0x00000000 0x00000000
(gdb) x/1xb 0xbffff5ea
0xbffff5ea: 0x02
(gdb) i r
eax 0xfffffff7 -9
ecx 0xbffff5ea -1073744406
edx 0x7 7
ebx 0x2 2
esp 0xbffff5d6 0xbffff5d6
ebp 0x0 0x0
esi 0xbffff5e2 -1073744414
edi 0x0 0
eip 0x8048099 0x8048099 <bind+40>
eflags 0x202 [ IF ]
cs 0x73 115
ss 0x7b 123
ds 0x7b 123
es 0x7b 123
fs 0x0 0
gs 0x0 0
(gdb)
那么问题出在哪里,如何解决呢?
提前致谢。
最佳答案
你在栈上组装了参数数组,但你需要传递地址供系统调用使用。你应该稍微清理一下你的代码,很难理解你在做什么。作为一个快速的 hack,movl %esp, %ecx
before int $0x80
似乎可以工作,尽管不确定你是否想要这个:
bind(3, {sa_family=AF_INET, sin_port=htons(44552), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
另请注意,jz
和 jnz
不会自动将 eax
与 0
进行比较,也就是说您可以不要在 int $0x80
之后直接使用它们。他们只是测试零标志。你必须自己添加代码根据eax
设置它,比如teSTL %eax, %eax
。
此外,你真的应该保持堆栈指针 4 字节对齐。 subl $30, %esp
是一个非常糟糕的主意。
对于这个调用,你需要内存中的两件事。一,参数数组。您需要在 ecx
中传递一个指向它的指针。第二,第二个参数本身必须是指向 sockaddr
结构的指针。
您在 esi+8
指向的内存中构建 sockaddr。您的评论错误地说那是参数数组,但事实并非如此。随后,您通过堆栈上的 3 个 push
指令构建参数数组。因此,参数数组的地址是 esp
,esp+4
的值(第二个参数)指向 esi+8
是你的 sockaddr。您错误地使用了 esi+8
(前 8 个字节在那里未使用),但至少它是一致的并且您分配了足够的内存,所以它恰好可以工作。
另请注意,端口号应按网络(大端)字节顺序排列,这就是为什么您的 2222
(=0x08AE
) 将被解释为 44552
(=0xAE08
).
关于linux x86 tcp bind shellcode(GAS语法)bind()不返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27626828/