gpt4 book ai didi

linux - 在汇编中从/dev/random 返回一个字节

转载 作者:太空宇宙 更新时间:2023-11-04 11:08:24 27 4
gpt4 key购买 nike

我正在尝试打开 /dev/random,从中读取一个字节并以状态退出...在 x86 程序集中。这似乎会返回随机数,但我不确定它是否如我所想。

# Where do we get our random numbers from
.data
random: .ascii "/dev/random\0"

# Program goes here
.text
.global _start
_start:

# Get /dev/random fd
movl $5, %eax # sys_open (originally $0 by accident)
movl $random, %ebx # Filename string
movl $0, %ecx # O_RDONLY flag
int $0x80

# Read one byte onto stack
movl %eax, %ebx # The result of sys_open
movl $3, %eax # sys_read
subl $1, %esp # Make stack space
movl %esp, %ecx # Make stack our buffer
movl $1, %edx # size_t bytes? only 1
int $0x80

# Exit with a random result
movl %esp, %ebx # random result as exit status
movl $1, %eax # sys_exit
int $0x80

当我通过 strace 运行它时,我得到以下结果:

execve("./random", ["./random"], [/* 44 vars */]) = 0
fstat(0, NULL) = 3
close(0) = -1 EFAULT (Bad address)
write(0, NULL, 1 <unfinished ...>
+++ exited with 12 +++

注意缺少 open()read()!另外,exit() 去了哪里?

我做错了什么?

最佳答案

要进行系统调用,您必须遵循内核的系统调用API。因为我不知道它是什么,所以我总是look it up online .然后我发现 sys_open 应该是这样的:

mov $5, %eax          ;; syscall number "open"
mov $random, %ebx ;; arg 1: char * filename
mov $0, %ecx ;; arg 2: int flags
int $0x80

关于linux - 在汇编中从/dev/random 返回一个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222302/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com