【问题标题】:Assembly code automatically running a terminal command汇编代码自动运行终端命令
【发布时间】:2015-01-01 01:31:24
【问题描述】:

最近,我写了一段汇编代码,要求输入密码,如果用户输入内部存储的正确密码,它会打印出“正确!”。否则,它会打印出“不正确!”。

代码如下:

section .text
    global _start
_start:
    mov edx, len_whatis
    mov ecx, whatis
    mov ebx, 1
    mov eax, 4
    int 80h ; outputs: "What is the password?"

    mov edx, 5 ; expect 5 bytes of input(so 4 numbers)
    mov ecx, pass
    mov ebx, 0
    mov eax, 3
    int 80h ; accepts intput and stores in pass

    mov eax, [pass] ; move the pass variable into eax
    sub eax, '0' ; change the ascii number in eax to a numerical number
    mov ebx, [thepass] ; move the thepass variable into ebx
    sub ebx, '0' ; change the ascii number in ebx to a numerical number

    cmp eax, ebx ; compare the 2 numbers
    je correct ; if they are equal, jump to correct
    jmp incorrect ; if not, jump to incorrect
correct:
    mov edx, len_corr
    mov ecx, corr
    mov ebx, 1
    mov eax, 4
    int 80h ; outputs: "Correct!"

    mov ebx, 0
    mov eax, 1
    int 80h ; exits with status 0
incorrect:
    mov edx, len_incor
    mov ecx, incor
    mov ebx, 1
    mov eax, 4
    int 80h ; outputs: "Incorrect!"

    mov eax, 1
    int 80h ; exits with status: 1
section .data
    whatis db "What is the password?", 0xA
    len_whatis equ $ - whatis

    thepass db "12345"

    corr db "Correct!", 0xA
    len_corr equ $ - corr

    incor db "Incorrect!", 0xA
    len_incor equ $ - incor
section .bss
    pass resb 5

集合:nasm -f elf password.s

链接:ld -m elf_i386 -s -o password password.o

(如果您确实尝试组装链接并运行它,您可能会注意到它错误地检查了密码 - 忽略它。这是“离题”)

然后,我进行了测试:

  1. 我使用./password 运行代码
  2. 当我被提示输入密码时,我输入了123456,比代码预期的多一个字节
  3. 在我敲回车并退出代码后,终端立即尝试运行命令6

是什么导致了这种行为?是和汇编器有关,还是我的电脑是如何读取代码的?

编辑:

而且,当我使用12345 运行代码时,程序关闭时终端会提示输入命令两次,就好像有人在没有输入命令的情况下按了回车键。

【问题讨论】:

    标签: linux assembly terminal runtime-error nasm


    【解决方案1】:

    您只从标准输入读取五个字节,因此当您键入123456↵ 时,您的应用程序最终会读取12345 并将6↵ 留在缓冲区中。这会传递给 shell。

    如果要读取整行,请使用更大的缓冲区。

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 2019-12-22
      • 1970-01-01
      • 2022-11-17
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多