Recently, I wrote a bit of assembly code that asks for the password and if the user enters the correct password as stored internally, it prints out "Correct!". Else, it prints out "Incorrect!".
最近,我编写了一段程序集代码,要求输入密码,如果用户输入了内部存储的正确密码,它会打印出“正确!”否则,它会打印出“错误!”
Here is the code:
这是代码:
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
Assemble:nasm -f elf password.s
组装:nasm - f精灵password.s
Link:ld -m elf_i386 -s -o password password.o
链接:ld -m elf_i386 -s -o密码密码密码
(If you did try to assemble link and run this, you may notice that it checks the password incorrectly - ignore this. It is "off topic")
(如果您尝试组装链接并运行此链接,您可能会注意到它检查密码不正确——忽略它。这是“主题”)
Then, I ran a test:
然后,我做了一个测试:
- I ran the code with
./password
- 我用./密码运行代码
- When I was prompted for the password, I typed in
123456
, one more byte than the code expects - 当我被提示输入密码时,我输入了123456,比代码预期的多一个字节
- After I hit enter and the code exits, the terminal immediately tries to run a command
6
- 在我点击enter并退出代码后,终端立即尝试运行命令6
What is causing this behavior? Is it something to do with the assembler, or how my computer is reading the code?
是什么导致了这种行为?这与汇编程序有关吗?或者我的计算机是如何读取代码的?
EDIT:
编辑:
And, when I run the code with 12345
, the terminal prompts for a command twice when the program closes, as if someone just hit the enter button without entering a command.
而且,当我用12345运行代码时,当程序关闭时,终端会提示两次命令,就好像有人在没有输入命令的情况下按下了enter键。
1 个解决方案
#1
2
You're only reading five bytes from standard input, so when you type 123456↵
, your application ends up reading 12345
and leaving 6↵
in the buffer. That gets passed on to the shell.
你只是读五个字节从标准输入,所以当你123456型↵,您的应用程序最终阅读12345,留下6↵缓冲区。它传递给壳层。
If you want to read the whole line, use a larger buffer.
如果要读取整行,请使用较大的缓冲区。
#1
2
You're only reading five bytes from standard input, so when you type 123456↵
, your application ends up reading 12345
and leaving 6↵
in the buffer. That gets passed on to the shell.
你只是读五个字节从标准输入,所以当你123456型↵,您的应用程序最终阅读12345,留下6↵缓冲区。它传递给壳层。
If you want to read the whole line, use a larger buffer.
如果要读取整行,请使用较大的缓冲区。