【问题标题】:x86-64 Assembly code not runningx86-64 汇编代码未运行
【发布时间】:2015-12-23 03:21:36
【问题描述】:

我是组装新手。我试图通过参考一本名为“Writing Security Tools and Exploits”的书来学习。熟悉这本书的人都知道,汇编代码是为 32 位编写的,但我是在 64 位系统上构建的。

因此,我重写了代码以在 64 位上运行。成功编译代码后,当我尝试运行程序时,没有完成所需的输出。相反,我没有收到任何输出。

我在 AMD64 Debian Linux 系统上构建它。这是我试图从以下位置接收输出的代码:

global _start
_start:
xor             rax,rax

jmp short string
code:
pop             rsi
push byte       15
push            rsi
push byte       1
mov             al,4
push            rax
int             0x80

xor             rax,rax
push            rax
push            rax
mov             al,1
int             0x80

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

我使用以下命令编译它

$> nasm -f elf64 hello.asm $> ld -s -o hello hello.o

当我尝试运行时没有输出。

关于我哪里出错了有什么建议吗?

【问题讨论】:

  • 我假设从这段代码中您正在尝试为基于堆栈的缓冲区利用创建代码。对于 64 位代码,您应该使用 syscall 而不是 int 0x80,并且 64 位调用约定在寄存器中传递大多数 syscall 参数,而不是堆栈。
  • 如果您在strace 下运行您的代码,您将获得它所做的系统调用的跟踪以及错误代码。

标签: linux assembly x86-64


【解决方案1】:

您的代码中主要存在一个大问题,您似乎认为通过int 0x80 进行系统调用的调用约定(传递参数)是通过将其压入堆栈来传递的。但实际上,您需要遍历寄存器eaxebxecx(更多详细信息请参见here)。

因此,编写代码的正确方法是:

global _start
_start:
xor             rax,rax

jmp short string
code:
pop           rsi
mov           rdx, 15   
mov             rcx, rsi
mov             rbx, 1
mov             al,4
int             0x80

xor             rax,rax
mov             rbx, rax
mov             al,1
int             0x80

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

然后,做:

$> nasm -f elf64 hello.asm
$> gcc -nostdlib -o hello hello.o

【讨论】:

  • 感谢大家的所有反馈。你说的很对。你能推荐任何与 x64 Assembly 相关的书吗?我可以跟进学习,因为我的很多阅读都来自过时的书。如果这些都是以安全为重点的,那将有很大帮助。
猜你喜欢
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多