代码之家  ›  专栏  ›  技术社区  ›  warsang

x64 helloworld外壳代码未打印任何内容

  •  1
  • warsang  · 技术社区  · 7 年前

    x64 shellcode 在…上 exploit.courses linux容器和我在运行我编写的hello world x64外壳代码时遇到了问题。 我试图将“Hi there”缓冲区直接移动到寄存器,因此我不使用 .data 部分

    section .data
    
    ;msg db "Hi there"
    
    section .text
    
    global _start
    _start:
    ;zeroed out these registers
    xor rax, rax
    xor rbx, rbx
    xor rsi, rsi
    xor rdi, rdi
    xor rdx, rdx
    
    ;write (int fd, char *msg, unsigned int len);
    mov rax,1 ; syscall 1 is write in 64bit arch
    mov rdi,1 ; rdi is fd
    mov rbx, 0x6572656874206948
    mov rdx, 9; rdx is size (9 for null byte)
    syscall ; instead of int 0x80 for 32 bit architecture
    
    ;exit (int ret)
    mov rax, 60 ; syscall 60 is exit in 64bit arch
    mov rdi, 0 ; rdi is error code
    syscall
    

    我组装代码并运行它:

    $nasm -f elf64 -o print2.o print2.asm
    $ld -o print2 print2.o               
    $./print2
    

    虽然print2似乎正常退出,但什么也没有发生。。。有人能解释一下为什么吗?

    对不起,如果已经问过这个问题。我试着找一个类似的,但什么也找不到。

    1 回复  |  直到 4 年前
        1
  •  1
  •   invictus1306    7 年前

    作为第一步,请查看 write documentation

       ssize_t write(int fd, const void *buf, size_t count);
    

    第二个参数必须是 const void *

    但对于linux,调用约定是:

     RDI, RSI, RDX, RCX, R8, R9, XMM0–7
    

    那么您的实现是不正确的。

    你应该这样做

    global _start
    
    
    _start:
    
        jmp short msg
    
        routine:
    
        ...
        pop rsi         ;address of the string from the stack
        ...
    
    
        msg:
        call routine    
        db 'Hi here'