x86 Linux汇编程序从_start获取程序参数

Lef*_*ler 5 linux x86 assembly gnu-assembler

我正在尝试创建一个程序来在屏幕上编写参数.我创建了一些程序来获取C函数参数,或者我使用C将参数发送到我的asm程序.有没有办法只使用汇编程序获取程序参数

EX:

./Program "text"
Run Code Online (Sandbox Code Playgroud)

我正在使用(Gnu Assembler)

通常我会使用这些参数

[esp+4]
Run Code Online (Sandbox Code Playgroud)

因为esp是程序/函数调用指针,但在纯asm中它没有得到命令行参数.

有没有办法做到这一点?

我用Google搜索,但我无法找到更多信息

sco*_*ttt 13

在Linux上,来自C 的熟悉argcargv变量总是由内核传递到堆栈中,甚至可用于完全独立且不与C库中的启动代码链接的汇编程序.这在i386 System V ABI中有记录,以及过程启动环境的其他详细信息(寄存器值,堆栈对齐).

_startx86 Linux可执行文件的ELF入口点(aka ):

  1. ESP指向argc
  2. ESP + 4argv[0],阵列的开始.即价值,你应该传递给主为char **argvlea eax, [esp+4],不是mov eax, [esp+4])

最小装配程序如何获得argc和argv

我将展示如何阅读argvargc[0]广发行.

CMDLINE-x86.S

#include <sys/syscall.h>

    .global _start
_start:
    /* Cause a breakpoint trap */
    int $0x03

    /* exit_group(0) */
    mov $SYS_exit_group, %eax
    mov $0, %ebx
    int $0x80
Run Code Online (Sandbox Code Playgroud)

CMDLINE-x86.gdb

set confirm off
file cmdline-x86
run
# We'll regain control here after the breakpoint trap
printf "argc: %d\n", *(int*)$esp
printf "argv[0]: %s\n",  ((char**)($esp + 4))[0]
quit
Run Code Online (Sandbox Code Playgroud)

示例会话

$ cc -nostdlib -g3 -m32 cmdline-x86.S -o cmdline-x86
$ gdb -q -x cmdline-x86.gdb cmdline-x86
<...>  
Program received signal SIGTRAP, Trace/breakpoint trap.
_start () at cmdline-x86.S:8
8   mov $SYS_exit_group, %eax
argc: 1
argv[0]: /home/scottt/Dropbox/stackoverflow/cmdline-x86
Run Code Online (Sandbox Code Playgroud)

说明

  • 我放置了一个软件断点(int $0x03),使程序在ELF入口点(_start)之后立即陷入调试器.
  • 然后我用printfGDB脚本进行打印
    1. argc 随着表达 *(int*)$esp
    2. argv 随着表达 ((char**)($esp + 4))[0]

x86-64版本

差异很小:

  • RSP替换ESP
  • 将地址大小从4更改为8
  • 当我们调用exit_group(0)正确终止进程时,遵守不同的Linux系统调用约定

cmdline.S

#include <sys/syscall.h>

    .global _start
_start:
    /* Cause a breakpoint trap */
    int $0x03

    /* exit_group(0) */
    mov $SYS_exit_group, %rax
    mov $0, %rdi
    syscall
Run Code Online (Sandbox Code Playgroud)

cmdline.gdb

set confirm off
file cmdline
run
printf "argc: %d\n", *(int*)$rsp
printf "argv[0]: %s\n",  ((char**)($rsp + 8))[0]
quit
Run Code Online (Sandbox Code Playgroud)

常规C程序如何获得argc和argv

您可以_start从常规C程序中进行反汇编,以查看它是如何从堆栈中获取的,argcargv在调用时传递它们__libc_start_main.以/bin/true我的x86-64机器上的程序为例:

$ gdb -q /bin/true
Reading symbols from /usr/bin/true...Reading symbols from /usr/lib/debug/usr/bin/true.debug...done.
done.
(gdb) disassemble _start
Dump of assembler code for function _start:
   0x0000000000401580 <+0>: xor    %ebp,%ebp
   0x0000000000401582 <+2>: mov    %rdx,%r9
   0x0000000000401585 <+5>: pop    %rsi
   0x0000000000401586 <+6>: mov    %rsp,%rdx
   0x0000000000401589 <+9>: and    $0xfffffffffffffff0,%rsp
   0x000000000040158d <+13>:    push   %rax
   0x000000000040158e <+14>:    push   %rsp
   0x000000000040158f <+15>:    mov    $0x404040,%r8
   0x0000000000401596 <+22>:    mov    $0x403fb0,%rcx
   0x000000000040159d <+29>:    mov    $0x4014c0,%rdi
   0x00000000004015a4 <+36>:    callq  0x401310 <__libc_start_main@plt>
   0x00000000004015a9 <+41>:    hlt    
   0x00000000004015aa <+42>:    xchg   %ax,%ax
   0x00000000004015ac <+44>:    nopl   0x0(%rax)
Run Code Online (Sandbox Code Playgroud)

前三个论点__libc_start_main()是:

  1. RDI:指向main()
  2. RSI:argc你可以看到它是如何从堆栈中弹出的
  3. RDX:argv,价值RSP权后,argc被弹出.(ubp_av在GLIBC来源中)

x86 _start非常相似:

Dump of assembler code for function _start:
   0x0804842c <+0>: xor    %ebp,%ebp
   0x0804842e <+2>: pop    %esi
   0x0804842f <+3>: mov    %esp,%ecx
   0x08048431 <+5>: and    $0xfffffff0,%esp
   0x08048434 <+8>: push   %eax
   0x08048435 <+9>: push   %esp
   0x08048436 <+10>:    push   %edx
   0x08048437 <+11>:    push   $0x80485e0
   0x0804843c <+16>:    push   $0x8048570
   0x08048441 <+21>:    push   %ecx
   0x08048442 <+22>:    push   %esi
   0x08048443 <+23>:    push   $0x80483d0
   0x08048448 <+28>:    call   0x80483b0 <__libc_start_main@plt>
   0x0804844d <+33>:    hlt    
   0x0804844e <+34>:    xchg   %ax,%ax
End of assembler dump.
Run Code Online (Sandbox Code Playgroud)