使用OpCode绕过Python沙箱的方法详解
0x01OpCode
opcode又称为操作码,是将python源代码进行编译之后的结果,python虚拟机无法直接执行human-readable的源代码,因此python编译器第一步先将源代码进行编译,以此得到opcode。例如在执行python程序时一般会先生成一个pyc文件,pyc文件就是编译后的结果,其中含有opcode序列。
如何查看一个函数的OpCode?
defa():
if1==2:
print("flag{****}")
print"Opcodeofa():",a.__code__.co_code.encode('hex')
通过此方法我们可以得到a函数的OpCode
Opcodeofa():6401006402006b020072140064030047486e000064000053
我们可以通过dis库获得相应的解析结果。
importdis
dis.dis('6401006402006b020072140064030047486e000064000053'.decode('hex'))
得到反编译的结果
0LOAD_CONST 1(1)
3LOAD_CONST 2(2)
6COMPARE_OP 2(==)
9POP_JUMP_IF_FALSE 20
12LOAD_CONST 3(3)
15LOAD_BUILD_CLASS
16YIELD_FROM
17JUMP_FORWARD 0(to20)
>> 20LOAD_CONST 0(0)
23RETURN_VALUE
常见的字节码指令
为了进一步研究OpCode,我们可以对dis的disassemble_string函数进行patch
在124行加入
printhex(op).ljust(6),
可以查看具体的字节码。
0LOAD_CONST 0x64 1(1)
3LOAD_CONST 0x64 2(2)
6COMPARE_OP 0x6b 2(==)
9POP_JUMP_IF_FALSE 0x72 20
12LOAD_CONST 0x64 3(3)
15LOAD_BUILD_CLASS 0x47
16YIELD_FROM 0x48
17JUMP_FORWARD 0x6e 0(to20)
>> 20LOAD_CONST 0x64 0(0)
23RETURN_VALUE 0x53
变量
| 指令名 | 操作 |
|---|---|
| LOAD_GLOBAL | 读取全局变量 |
| STORE_GLOBAL | 给全局变量赋值 |
| LOAD_FAST | 读取局部变量 |
| STORE_FAST | 给局部变量赋值 |
| LOAD_CONST | 读取常量 |
IF
| 指令名 | 操作 |
|---|---|
| POP_JUMP_IF_FALSE | 当条件为假的时候跳转 |
| JUMP_FORWARD | 直接跳转 |
CMP_OP
cmp_op=('<','<=','==','!=','>','>=','in','notin','is','isnot','exceptionmatch','BAD')
其余的指令参考OpCode源码
0x02利用OpCode改变程序运行逻辑
在Python中,我们可以对任意函数的__code__参数进行赋值,通过对其进行赋值,我们可以改变程序运行逻辑。
Example1
defa():
if1==2:
print("flag{****}")
在沙箱环境中我们需要调用这个函数,但是此函数我们无法执行到print语句。因此我们需要通过某种方法得到flag
Solution1
我们直接获取a.__code__.co_consts,查看所有的常量。即可知道flag
(None,1,2,'flag{****}')
Solution2
更改程序运行逻辑
CodeType构造函数
def__init__(self,argcount,nlocals,stacksize,flags,code, consts,names,varnames,filename,name, firstlineno,lnotab,freevars=None,cellvars=None):
上述函数其余参数均可通过__code.__.co_xxx获得
因此我们
defa():
if1==2:
print("flag{****}")
fornameindir(a.__code__):
printname,getattr(a.__code__,name)
输出
co_argcount0
co_cellvars()
co_codeddkrdGHndS
co_consts(None,1,2,'flag{****}')
co_filenameexample1.py
co_firstlineno1
co_flags67
co_freevars()
co_lnotabco_namea
co_names()
co_nlocals0
co_stacksize2
co_varnames()
构造相应目标代码
defa():
if1!=2:
print("flag{****}")
print"Opcodeofa():",a.__code__.co_code.encode('hex')
得到code
6401006402006b030072140064030047486e000064000053
构造payload
defa():
if1==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__=code
a()
即可输出flag
Example2
deftarget(flag):
defprintflag():
ifflag=="":
printflag
returnprintflag
flag=target("flag{*******}")
这一次因为是通过变量传入参数,我们无法通过上一次读co_consts获得变量。但是我们这次依旧可以通过重写code获得flag。
构造替代函数
deftarget(flag):
defprintflag():
ifflag!="":
printflag
returnprintflag
a=target("xxx")
importtypes
code=a.__code__.co_code.encode('hex')
printcode
EXP
newcode=type(flag.__code__)
code="8800006401006b030072140088000047486e000064000053".decode('hex')
code=newcode(0,0,2,19,code,(None,''),(),(),"example2.py","printflag",2,"",('flag',),())
flag.__code__=code
flag()
➜ pythonexample2exp.py
8800006401006b030072140088000047486e000064000053
➜ pythonexample2.py
flag{*******}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。