使用OpCode绕过Python沙箱的方法详解
Python  /  管理员 发布于 7年前   182
0x01 OpCode
opcode又称为操作码,是将python源代码进行编译之后的结果,python虚拟机无法直接执行human-readable的源代码,因此python编译器第一步先将源代码进行编译,以此得到opcode。例如在执行python程序时一般会先生成一个pyc文件,pyc文件就是编译后的结果,其中含有opcode序列。
如何查看一个函数的OpCode?
def a(): if 1 == 2: print("flag{****}")print "Opcode of a():",a.__code__.co_code.encode('hex')
通过此方法我们可以得到a函数的OpCode
Opcode of a(): 6401006402006b020072140064030047486e000064000053
我们可以通过dis库获得相应的解析结果。
import disdis.dis('6401006402006b020072140064030047486e000064000053'.decode('hex'))
得到反编译的结果
0 LOAD_CONST 1 (1)
3 LOAD_CONST 2 (2)
6 COMPARE_OP 2 (==)
9 POP_JUMP_IF_FALSE 20
12 LOAD_CONST 3 (3)
15 LOAD_BUILD_CLASS
16 YIELD_FROM
17 JUMP_FORWARD 0 (to 20)
>> 20 LOAD_CONST 0 (0)
23 RETURN_VALUE
常见的字节码指令
为了进一步研究OpCode,我们可以对dis的disassemble_string函数进行patch
在124行加入
print hex(op).ljust(6),
可以查看具体的字节码。
0 LOAD_CONST 0x64 1 (1)
3 LOAD_CONST 0x64 2 (2)
6 COMPARE_OP 0x6b 2 (==)
9 POP_JUMP_IF_FALSE 0x72 20
12 LOAD_CONST 0x64 3 (3)
15 LOAD_BUILD_CLASS 0x47
16 YIELD_FROM 0x48
17 JUMP_FORWARD 0x6e 0 (to 20)
>> 20 LOAD_CONST 0x64 0 (0)
23 RETURN_VALUE 0x53
变量
指令名 | 操作 |
---|---|
LOAD_GLOBAL | 读取全局变量 |
STORE_GLOBAL | 给全局变量赋值 |
LOAD_FAST | 读取局部变量 |
STORE_FAST | 给局部变量赋值 |
LOAD_CONST | 读取常量 |
IF
指令名 | 操作 |
---|---|
POP_JUMP_IF_FALSE | 当条件为假的时候跳转 |
JUMP_FORWARD | 直接跳转 |
CMP_OP
cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is','is not', 'exception match', 'BAD')
其余的指令参考OpCode源码
0x02 利用OpCode改变程序运行逻辑
在Python中,我们可以对任意函数的__code__参数进行赋值,通过对其进行赋值,我们可以改变程序运行逻辑。
Example1
def a(): if 1 == 2: print("flag{****}")
在沙箱环境中我们需要调用这个函数,但是此函数我们无法执行到print语句。因此我们需要通过某种方法得到flag
Solution 1
我们直接获取a.__code__.co_consts,查看所有的常量。即可知道flag
(None, 1, 2, 'flag{****}')
Solution 2
更改程序运行逻辑
CodeType构造函数
def __init__(self, argcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, firstlineno, lnotab, freevars=None, cellvars=None):
上述函数其余参数均可通过__code.__.co_xxx获得
因此我们
def a(): if 1 == 2: print("flag{****}")for name in dir(a.__code__): print name,getattr(a.__code__,name)
输出
co_argcount 0
co_cellvars ()
co_code ddkrdGHndS
co_consts (None, 1, 2, 'flag{****}')
co_filename example1.py
co_firstlineno 1
co_flags 67
co_freevars ()
co_lnotabco_name a
co_names ()
co_nlocals 0
co_stacksize 2
co_varnames ()
构造相应目标代码
def a(): if 1 != 2: print("flag{****}")print "Opcode of a():",a.__code__.co_code.encode('hex')
得到code
6401006402006b030072140064030047486e000064000053
构造payload
def a(): if 1 == 2: print("flag{****}")newcode = type(a.__code__)code = "6401006402006b030072140064030047486e000064000053".decode('hex')code = newcode(0,0,2,67,code,(None, 1, 2, 'flag{****}'),(),(),"xxx","a",1,"")a.__code__ = codea()
即可输出flag
Example 2
def target(flag): def printflag(): if flag == "": print flag return printflagflag = target("flag{*******}")
这一次因为是通过变量传入参数,我们无法通过上一次读co_consts获得变量。但是我们这次依旧可以通过重写code获得flag。
构造替代函数
def target(flag): def printflag(): if flag != "": print flag return printflaga = target("xxx")import typescode = a.__code__.co_code.encode('hex')print code
EXP
newcode = type(flag.__code__)code = "8800006401006b030072140088000047486e000064000053".decode('hex')code = newcode(0,0,2,19,code,(None, ''),(),(),"example2.py","printflag",2,"",('flag',),())flag.__code__ = codeflag()
➜ python example2exp.py
8800006401006b030072140088000047486e000064000053
➜ python example2.py
flag{*******}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家的支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号