iOS ARM64系统调用

时间:2019-07-11 09:22:28

标签: ios assembly arm64

我正在学习有关shellcode和在iOS设备上的arm64中进行系统调用的更多信息。我正在测试的设备是iPhone 6S。

我从此链接(https://github.com/radare/radare2/blob/master/libr/include/sflib/darwin-arm-64/ios-syscalls.txt)中获得了系统调用列表。

我了解到x8用于从此处(http://arm.ninja/2016/03/07/decoding-syscalls-in-arm64/)放置arm64的系统调用号。

我发现用于传递arm64参数的各种寄存器应该与arm相同,因此我引用了此链接(https://w3challs.com/syscalls/?arch=arm_strong),该链接取自https://azeria-labs.com/writing-arm-shellcode/

我用Xcode编写了内联汇编,这里有一些代码片段

//exit syscall
__asm__ volatile("mov x8, #1");
__asm__ volatile("mov x0, #0");
__asm__ volatile("svc 0x80");

但是,当我跳过这些代码时,应用程序不会终止。

char write_buffer[]="console_text";
int write_buffer_size = sizeof(write_buffer);

__asm__ volatile("mov x8,#4;"     //arm64 uses x8 for syscall number
                 "mov x0,#1;"     //1 for stdout file descriptor
                 "mov x1,%0;"    //the buffer to display
                 "mov x2,%1;"    //buffer size
                 "svc 0x80;"
                 :
                 :"r"(write_buffer),"r"(write_buffer_size)
                 :"x0","x1","x2","x8"
                 );

如果此系统调用有效,则应在Xcode的控制台输出屏幕中打印出一些文本。但是,什么也没打印。

关于ARM汇编的在线文章很多,有些使用svc 0x80,有些使用svc 0等,因此可能会有一些变化。我尝试了各种方法,但无法使两个代码段正常工作。

有人可以提供一些指导吗?

编辑: 这是我编写C函数syscall int return_value=syscall(1,0);

时Xcode在其Assembly视图中显示的内容。
    mov x1, sp
    mov x30, #0
    str x30, [x1]
    orr w8, wzr, #0x1
    stur    x0, [x29, #-32]         ; 8-byte Folded Spill
    mov x0, x8
    bl  _syscall

我不确定为什么会发出此代码。

2 个答案:

答案 0 :(得分:2)

用于syscall的寄存器是完全任意的,对于XNU,您选择的资源肯定是错误的。

据我所知,arm64的XNU syscall ABI是完全私有的,如有更改,恕不另行通知,因此没有遵循的发布标准,但是您可以通过获取XNU的副本来了解其工作原理源代码(如tarballsviewing it online(如果您愿意的话)),grep用于handle_svc函数,并紧跟代码。
我不会详细介绍您到底在哪里找到哪些位,但是最终结果是:

  • 传递给svc的立即数将被忽略,但是标准库使用svc 0x80
  • x16拥有系统调用号码
  • x0x8最多可容纳9个参数*
  • 堆栈上没有参数
  • x0x1最多保留2个返回值(例如,在fork的情况下)
  • 进位位用于报告错误,在这种情况下,x0保存错误代码

*仅在具有8个参数的间接syscall(x16 = 0)的情况下使用。
* XNU源代码中的注释中也提到了x9,但似乎编写该文件的工程师应该克服一次性错误。

然后是可用的实际系统调用号:

  • “ UNIX系统调用”的规范源是XNU源树中的文件bsd/kern/syscalls.master。在最新的iOS 13 Beta中,这些系统调用的系统调用编号从0到大约540
  • “ Mach系统调用”的规范源是XNU源树中的文件osfmk/kern/syscall_sw.c。那些系统调用使用-10-100之间的编号进行调用(例如-28task_self_trap)。
  • 与最后一点无关,可以分别用两个系统调用编号mach_absolute_timemach_continuous_time调用两个-3-4
  • 可以通过platform_syscall使用系统调用号0x80000000进行一些低级操作。

答案 1 :(得分:2)

这应该可以帮助您。如@Siguza所述,您必须使用x16而不是x8作为系统调用号码。

#import <sys/syscall.h>
char testStringGlobal[] = "helloWorld from global variable\n";
int main(int argc, char * argv[]) {
    char testStringOnStack[] = "helloWorld from stack variable\n";
#if TARGET_CPU_ARM64

    //VARIANT 1 suggested by @PeterCordes
    //an an input it's a file descriptor set to STD_OUT 1 so the syscall write output appears in Xcode debug output
    //as an output this will be used for returning syscall return value;
    register long x0 asm("x0") = 1;
    //as an input string to write
    //as an output this will be used for returning syscall return value higher half (in this particular case 0)
    register char *x1 asm("x1") = testStringOnStack;
    //string length
    register long x2 asm("x2") = strlen(testStringOnStack);
    //syscall write is 4
    register long x16 asm("x16") = SYS_write; //syscall write definition - see my footnote below

    //full variant using stack local variables for register x0,x1,x2,x16 input
    //syscall result collected in x0 & x1 using "semi" intrinsic assembler
    asm volatile(//all args prepared, make the syscall
                 "svc #0x80"
                 :"=r"(x0),"=r"(x1) //mark x0 & x1 as syscall outputs
                 :"r"(x0), "r"(x1), "r"(x2), "r"(x16): //mark the inputs
                 //inform the compiler we read the memory
                 "memory",
                 //inform the compiler we clobber carry flag (during the syscall itself)
                 "cc");

    //VARIANT 2
    //syscall write for globals variable using "semi" intrinsic assembler
    //args hardcoded
    //output of syscall is ignored
    asm volatile(//prepare x1 with the help of x8 register
                 "mov x1, %0 \t\n"
                 //set file descriptor to STD_OUT 1 so it appears in Xcode debug output
                 "mov x0, #1 \t\n"
                 //hardcoded length
                 "mov x2, #32 \t\n"
                 //syscall write is 4
                 "mov x16, #0x4 \t\n"
                 //all args prepared, make the syscall
                 "svc #0x80"
                 ::"r"(testStringGlobal):
                 //clobbered registers list
                 "x1","x0","x2","x16",
                 //inform the compiler we read the memory
                 "memory",
                 //inform the compiler we clobber carry flag (during the syscall itself)
                 "cc");

    //VARIANT 3 - only applicable to global variables using "page" address
    //which is  PC-relative addressing to load addresses at a fixed offset from the current location (PIC code).
    //syscall write for global variable using "semi" intrinsic assembler
    asm volatile(//set x1 on proper PAGE
                 "adrp x1,_testStringGlobal@PAGE \t\n" //notice the underscore preceding variable name by convention
                 //add the offset of the testStringGlobal variable
                 "add x1,x1,_testStringGlobal@PAGEOFF \t\n"
                 //set file descriptor to STD_OUT 1 so it appears in Xcode debug output
                 "mov x0, #1 \t\n"
                 //hardcoded length
                 "mov x2, #32 \t\n"
                 //syscall write is 4
                 "mov x16, #0x4 \t\n"
                 //all args prepared, make the syscall
                 "svc #0x80"
                 :::
                 //clobbered registers list
                 "x1","x0","x2","x16",
                 //inform the compiler we read the memory
                 "memory",
                 //inform the compiler we clobber carry flag (during the syscall itself)
                 "cc");
#endif
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

编辑

要对@PeterCordes做出出色的评论,是的,在变式1中,我包含在上面的代码片段^中的syscall数字定义标头<sys/syscall.h>。但重要的是要在其中由Apple定义,如下所示:

#ifdef __APPLE_API_PRIVATE
#define SYS_syscall        0
#define SYS_exit           1
#define SYS_fork           2
#define SYS_read           3
#define SYS_write          4

我还没有听说过因为直接通过svc 0x80使用系统调用而导致iOS应用AppStore被拒绝的情况,尽管它肯定是非公开 API。

对于@PeterCordes建议的"=@ccc",例如,进位标志(错误时由syscall设置)作为输出约束,即使对于x86,最新XCode11 beta / LLVM 8.0.0也不支持,甚至对于ARM绝对不支持

相关问题