gpt4 book ai didi

c - exe C 时出现段错误

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

因此,在我编译并执行我的程序后,我收到以下错误消息:“Segmentation fault”,strace 错误消息为:

--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
Segmentation fault

问题是,有什么办法可以解决这个错误并在 shell 代码中显示消息吗?

汇编代码:

;r3v.asm

;r3v3rs3c - 3x_z3r0
[SECTION .text]

global _start

_start:

jmp short ender

starter:

xor eax, eax
xor ebx, ebx
xor edx, edx
xor ecx, ecx
mov al, 4
mov bl, 1
pop ecx
mov dl, 18
int 0x80
xor ebx, ebx
int 0x80
ender:
call starter
db 'r3v3rs3c'

组装它:nasm -f elf r3v.asm链接:ld -o r3v r3v.o转储它:objdump -d r3v将 shell 代码提取到测试程序中:

/*shelltest.c
r3v3s3c - 3x_z3r0*/
char code[] =
"\xeb\x15\x31\xc0\x31\xdb\x31\xd2\x31\xc9\xb0\x04\xb3\x01\x59\xb2\x12\xcd\x80\31\xdb\xcd\x80\xe8\xe6\xff\xff\xff\x72\x33\x76\x33\x72\x73\x33\x63";
;
int main(int argc, char **argv)
{
int (*exeshell)();
exeshell = (int (*)()) code;
(int)(*exeshell)();
}

然后我编译:gcc shelltest.c -o shelltest执行它:./shelltest输出为“段错误”。

最佳答案

当前,您的字符串代码将被放置在程序内存的一部分中,当您将数组声明为可变(非常量)时,该部分内存被声明为不可执行。当您尝试将它作为函数运行时,您的操作系统会发现您正试图在无法执行的内存区域中运行代码,并且会因段错误而终止您的程序。

要解决此问题,请将 code 的声明更改为 const char

const char code[] = "\xeb......."

这将允许编译器将它放入可执行内存中,从而允许它运行。

关于c - exe C 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076525/

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