【问题标题】:File path or file descriptor for ARM execute system callARM 执行系统调用的文件路径或文件描述符
【发布时间】:2017-03-20 14:43:48
【问题描述】:

我想在运行时执行 ARM“执行”系统调用时检索可执行文件的名称。

了解如何在 ARM 程序集中翻译“执行”系统调用可能会有所帮助。我会知道存储文件名的寄存器并在运行时检索它。

谢谢

【问题讨论】:

  • 您在询问中假设的操作系统是什么?我假设您具体指的是
  • @InfinitelyManic 是的,我指的是系统调用号 11。操作系统是 Android 6.0.1。谢谢

标签: arm


【解决方案1】:

此示例说明了 ARMv7 中 execu 的简单用法。

假设您有一个简单的文件,其中包含一些要排序的文本。

手册页指出了指向可执行文件的指针的位置。在我的示例中,“/bin/sh”是可执行文件。

所以你在 R0 处寻找一个数组结构指针。

NAME
       execve - execute program

SYNOPSIS
       #include <unistd.h>

       int execve(const char *filename, char *const argv[],
                  char *const envp[]);

DESCRIPTION
       execve() executes the program pointed to by filename.  filename must be either a binary executable, or a script starting with a line of the form:

           #! interpreter [optional-arg]

       For details of the latter case, see "Interpreter scripts" below.

       argv  is  an array of argument strings passed to the new program.  By convention, the first of these strings should contain the filename associated with the file being executed.
       envp is an array of strings, conventionally of the form key=value, which are passed as environment to the new program.  Both argv and envp must be terminated by a null  pointer.
       The argument vector and environment can be accessed by the called program's main function, when it is defined as:

           int main(int argc, char *argv[], char *envp[])

       execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded.

示例代码:

.data
        _filename:      .string "/bin/sh"
        arg0:           .string "/bin/sh"
        arg1:           .string "-c"
        arg2:           .string "sort -n myfile.txt"
        args:
                .word arg0
                .word arg1
                .word arg2
.text
        .global  main
main:
        bl _work

_work:
        push {lr}
        mov r7, #11             // execve syscall
        ldr r0,=_filename
        ldr r1,=args
        svc #0
        pop {pc}

简单的文本文件:

  $ cat myfile.txt
        9
        1
        5
        233
        5
        6
        723
        91
        0
        3
        2
        4576
        557
        6
        353
        3553

输出示例:

 $ ./simple_exec
0
1
2
3
5
5
6
6
9
91
233
353
557
723
3553
4576

【讨论】:

  • 文件名好像存储在r0中。我会试一试,让你知道。
  • @Giuseppe - 我认为它是一个指针,因为除其他原因外,文件名长度可能超过寄存器的大小。但你在正确的轨道上。
  • 能否运行不同的可执行文件并检查文件名是否总是加载到 r0 中?
  • 或者告诉我你是如何打印出汇编代码的
  • @Giuseppe - 我从头开始编写汇编代码。如果我执行 '_filename: .string "/usr/bin/top"' ,汇编代码将发出 shell top 命令。在这种情况下,您使用什么语言?
猜你喜欢
  • 2014-05-08
  • 2017-02-12
  • 1970-01-01
  • 2020-01-05
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多