循环0xb7汇编但无法链接

时间:2019-05-08 22:29:32

标签: linux gcc assembly x86 ld

我在Debian上有以下ASM代码,我正在尝试编译这些代码,但ldgcc都有问题。

uname -a:Linux kali 4.18.0-kali3-amd64 #1 SMP Debian 4.18.20-2kali2 (2018-11-30) x86_64 GNU/Linux

我首先运行:nasm -f elf shellcode.asm

这没有问题。

海湾合作委员会问题... 命令:gcc -m32 -o key shellcode.o 错误:

/usr/bin/ld: shellcode.o: in function `_start':
shellcode.asm:(.text+0x0): multiple definition of `_start'; /usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib32/Scrt1.o:(.text+0x0): first defined here
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../../lib32/Scrt1.o: in function `_start':
(.text+0x28): undefined reference to `main'
/usr/bin/ld: shellcode.o: in function `_start':
shellcode.asm:(.text+0xbc): relocation truncated to fit: R_386_PC8 against `*UND*'
collect2: error: ld returned 1 exit status

我也尝试过ld

命令:ld -m elf_i386 -s -o key shellcode.o

错误...

ld: shellcode.o: in function `_start':
shellcode.asm:(.text+0xbc): relocation truncated to fit: R_386_PC8 against `*UND*'

后者似乎不太容易出错,并指出ASM语法有问题。

然后我的问题是,在正确的命令中应该使用哪些命令?我在做什么错了?

shellcode.asm

global _start
_start:
    xor eax,eax
    push eax
    push dword 0x76767975
    push dword 0x22717172
    push dword 0x22737972
    push dword 0x77207922
    push dword 0x78272079
    push dword 0x27277976
    push dword 0x77707470
    push dword 0x22777272
    push dword 0x22277622
    push dword 0x79727473
    push dword 0x27727377
    push dword 0x75747078
    push dword 0x70227479
    push dword 0x75222073
    push dword 0x24747176
    push dword 0x74782324
    push dword 0x72727320
    push dword 0x27762779
    push dword 0x20277777
    push dword 0x22207573
    push dword 0x70247827
    push dword 0x70277479
    push dword 0x24712379
    push dword 0x77742027
    push dword 0x76242379
    push dword 0x22702270
    push dword 0x73762577
    push dword 0x24752272
    push dword 0x20277172
    push dword 0x23712720
    push dword 0x72722478
    push dword 0x70252723
    push esp
    pop esi
    mov edi,esi
    mov edx,edi
    cld
    mov ecx,0x80
    mov ebx,0x41
    xor eax,eax
    push eax
    lodsb
    xor eax,ebx
    stosb
    loop 0xb7
    push esp
    pop esi
    int3
    db 0x0a

1 个答案:

答案 0 :(得分:2)

要使用gcc进行构建,您要定义_start而不是main,因此需要使用-nostdlib。像gcc -m32 -nostdlib -static -o key shellcode.o。这将使gcc像手动操作一样调用ld


loop仅可用于rel8位移,因此它无法从0xb7将其放置在代码段中的更高地址到达绝对地址ld

如果您确实确实想这样做(但几乎可以肯定不这样做),则可以使用dec ecx / jnz 0xb7,而该{{ 1}}编码,因此可以通过jcc rel32到达任何绝对地址。或使用链接描述文件在非常低的虚拟地址处链接TEXT段,以便EIP + rel32可以访问。

(但是这两种方式都不是位置无关的。通常,shellcode应该在注入未知地址后才能工作。如果您真的想跳到位置无关的shellcode中的绝对地址,例如触发特定的页面错误(?),您需要在寄存器中输入地址并使用loop rel8


但是您更有可能希望跳转到代码中的其他地方,而不是跳转到绝对地址较低的地方。 在分支目标上放置标签,然后使用jmp eax 。 (或者使用dec ecx / jnz label指令,因为您可能正在针对代码大小进行优化,而不考虑它的速度如何。)


如果您从某处借来此代码,则可能是在loop设置loop 0xb7位移值的汇编器中。抑或是使用rel8org 0x0制作平面二进制文件的NASM?但是,如果您想分支回到此代码中的某个位置,那么仅使用标签就更有意义了。