Linux 64位外壳代码 [英] Linux 64-bit shellcode

查看:16
本文介绍了Linux 64位外壳代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我的64位Ubuntu上编写我的第一个"Hello world"外壳代码,但它不能工作。

我有文件hello.asm

; 64-bit "Hello World!" in Linux NASM

global _start            ; global entry point export for ld

section .text
_start:

    ; sys_write(stdout, message, length)

    mov    rax, 1        ; sys_write
    mov    rdi, 1        ; stdout
    mov    rsi, message    ; message address
    mov    rdx, length    ; message string length
    syscall

    ; sys_exit(return_code)

    mov    rax, 60        ; sys_exit
    mov    rdi, 0        ; return 0 (success)
    syscall

section .data
    message: db 'Hello, world!',0x0A    ; message and newline
    length:    equ    $-message        ; NASM definition pseudo-instruction

我使用了以下命令:

nasm -felf64 hello.asm -o hello.o
ld -o hello hello.o
objdump -d hello

我将objump中的外壳代码放入C程序:

char code[] = "xb8x01x00x00x00xbfx01x00x00x00x48xbexd8x00x60x00x00x00x00x00xbax0ex00x00x00x0fx05xb8x3cx00x00x00xbfx00x00x00x00x0fx05";

int main(int argc, char **argv)
{
    int (*func)();
    func = (int (*)()) code;
    (int)(*func)();
    return 0;
}

并在GCC中编译,但运行后出现"分段错误(核心转储)"。

我不知道我做错了什么。汇编代码似乎可以工作,因为当我运行./Hello时,它会打印"Hello world"。

推荐答案

如果要将空字节注入缓冲区,最好删除它,但主要问题可能是数据段中有字符串?

我像这样重写了它,并使其正常工作。

    global _start
    _start:
    jmp short string

    code:
    pop rsi
    xor rax, rax
    mov al, 1
    mov rdi, rax
    mov rdx, rdi
    add rdx, 14
    syscall

    xor rax, rax
    add rax, 60
    xor rdi, rdi
    syscall

    string:
    call code
    db  'Hello, world!',0x0A

$ nasm -felf64 hello.asm -o hello.o
$ ld -s -o hello hello.o
$ for i in $(objdump -d hello |grep "^ " |cut -f2); do echo -n 'x'$i; done; echo
xebx1ex5ex48x31xc0xb0x01x48x89xc7x48x89xfax48x83xc2x0ex0fx05x48x31xc0x48x83xc0x3cx48x31xffx0fx05xe8xddxffxffxffx48x65x6cx6cx6fx2cx20x77x6fx72x6cx64x21x0a

char code[] = "xebx1ex5ex48x31xc0xb0x01x48x89xc7x4x89xfa  x48x83xc2x0ex0fx05x48x31xc0x48x83xc0x3cx48x31xffx0fx05xe8xddxffxffxffx48x65x6cx6cx6fx2cx20x77x6fx72x6cx64x21x0a";

int main(int argc, char **argv)
{
    int (*func)();
    func = (int (*)()) code;
    (int)(*func)();
     return 0;
}

$ gcc -fno-stack-protector -z execstack -o code code.c    
$./code
Hello, world!

这篇关于Linux 64位外壳代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆