简体   繁体   English

在运行任何代码之前发生分段错误

[英]Segmentation Fault happening before any code is run Assembly

I am trying to write a shell code program that will call execve and spawn a shell. 我正在尝试编写一个将调用execve并生成一个shell的shell代码程序。 I am working in a 32 bit virtual machine that was offered for this class . 我正在为此课程提供的32位虚拟机中工作。 The code is as follows: 代码如下:

section .text

global _start

_start:
;clear out registers
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
;exacve("/bin/sh",Null,NULL)
;ascii for /bin/sh;
;2f 62 9 6e 2f 73 68 3b
push 0x3b68732f
push 0x6e69622f
mov ebx, esp
mov al, 11
int 0x80
;exit(int status)
movv al, 1
xor ebx, ebx
int 0x80

I compile with nasm -f elf -g shell.asm and link with ld -o shell shell.o When I try to run it, I get a segmentation fault. 我使用nasm -f elf -g shell.asm编译,并使用ld -o shell shell.o链接,当我尝试运行它时,出现了段错误。 I tried using gdb to see where I made the mistake, but, it segfaults even if a set a break point at _start+0. 我尝试使用gdb来查看出错的地方,但是,即使在_start + 0处设置了断点,它也会出现段错误。 It says that there was a segfault at the address after the last instruction for the code. 它说在最后一条代码指令之后,该地址存在段错误。 ie if The last line has an address of 0x804807c then the segmentation fault happens at 0x804807e before any of the code has a chance to run. 即,如果最后一行的地址为0x804807c,则分段错误在任何代码有机会运行之前发生在0x804807e。

Could any one point me in the right direction so I can figure out how to fix this? 谁能指出我正确的方向,以便我找出解决方法?

One mistake in your code is, that there is no 0x3b in ascii code of the string: 您的代码中的一个错误是,字符串的ascii代码中没有0x3b

;exacve("/bin/sh",Null,NULL)
;ascii for /bin/sh;
;2f 62 9 6e 2f 73 68 3b
push 0x3b68732f
push 0x6e69622f

This following code shall fix this problem (assuming you do work with a little-endian machine): 以下代码可以解决此问题(假设您使用的是Little-endian机器):

;exacve("/bin/sh",Null,NULL)
;ascii for /bin/sh;
;2f 62 99 6e 2f 73 68 00
push 0x0068732f
push 0x6e69622f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM