gpt4 book ai didi

linux - 如何避免不适合缓冲区的标准输入被发送到 Linux 64 位 Intel (x86-64) 程序集中的 shell

转载 作者:IT王子 更新时间:2023-10-29 01:10:11 25 4
gpt4 key购买 nike

编辑:标题已更改,因为@Gunner 指出这不是缓冲区溢出。

在 Linux 64 位 Intel 汇编中使用 NR_readstdin 读取用户输入时,我想知道如何避免不适合输入缓冲区的输入被发送到 Linux shell 例如。庆典?例如,在这个示例程序中,我定义了一个 255 字节的输入缓冲区(缓冲区的大小可以是 >= 1)。超过 255 字节的输入的其余部分被发送到 bash(如果从 bash 运行),这显然是一个严重的漏洞。在 Linux 64 位程序集中应如何读取输入以避免此漏洞?

这是我的代码:

[bits 64]

section .text
global _start

; can be compiled eg. with nasm or yasm.
; nasm:
; nasm -f elf64 read_stdin_64.asm; ld read_stdin_64.o -o read_stdin_64
; yasm:
; yasm -f elf64 -m amd64 read_stdin_64.asm -o read_stdin_64.o; ld read_stdin_64.o -o read_stdin_64

NR_read equ 0
NR_exit equ 60

STDIN equ 1

; input:
; rax number of syscall
; rdi parameter 1
; rsi parameter 2
; rdx parameter 3
; r10 parameter 4
; r8 parameter 5
; r9 parameter 6
;
; output:
; rax syscall's output
@do_syscall:
push rcx
push r11
syscall ; 64-bit syscall, overwrites rcx and r11
pop r11 ; syscall's return value in rax
pop rcx
ret

@read_stdin:
push rdi
push rsi
push rdx
mov rdi,STDIN ; file handle to read. STDIN = 1.
lea rsi,[input_buffer]
mov rdx,input_buffer_length ; length of string
mov rax,NR_read ; number of syscall (0)
call @do_syscall
sub rax,1 ; get the number of writable characters.
pop rdx
pop rsi
pop rdi
ret

_start: ; linker entry point
call @read_stdin

@end_program:
xor rdi,rdi
mov rax,NR_exit ; number of syscall (60)
syscall

section .data

input_buffer times 255 db 0
input_buffer_length equ $-input_buffer

最佳答案

这不是其他人所说的缓冲区溢出。我写了一篇关于在 Linux 中从终端读取的教程,其中还展示了如何处理这个问题。它使用 32 位 Int 0x80,但您可以轻松更改它以满足您的需要。

http://www.dreamincode.net/forums/topic/286248-nasm-linux-terminal-inputoutput-wint-80h/

关于linux - 如何避免不适合缓冲区的标准输入被发送到 Linux 64 位 Intel (x86-64) 程序集中的 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12459729/

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