Python - 模块 pickle

pickle

  1. pickle 模块可以用来(解)序列化 python 对象
  2. pickle 功能可以将程序运行中的对象保存为文件。如果加载保存过的 pickle 文件,可以立刻复原之前程序运行中的对象。
  3. 官方文档地址
  4. pickle.dump(obj, file, protocol=None, *, fix_imports=True)
    1. 将对象(obj)的pickle表示写入打开的文件对象(file)中。
  5. pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")
    1. Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein.

Python object serialization

  1. The pickle module implements binary protocols for serializing and de-serializing a Python object structure.
  2. The data format used by pickle is Python-specific. This has the advantage that there are no restrictions imposed by external standards such as JSON or XDR (which can’t represent pointer sharing); however it means that non-Python programs may not be able to reconstruct pickled Python objects.

By default, the pickle data format uses a relatively compact binary representation. If you need optimal size characteristics, you can efficiently compress pickled data.

The module pickletools contains tools for analyzing data streams generated by pickle. pickletools source code has extensive comments about opcodes used by pickle protocols.

There are currently 5 different protocols which can be used for pickling. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

  1. The pickle module can transform a complex object into a byte stream and it can transform the byte stream into an object with the same internal structure. Perhaps the most obvious thing to do with these byte streams is to write them onto a file, but it is also conceivable to send them across a network or store them in a database. The shelve module provides a simple interface to pickle and unpickle objects on DBM-style database files.

pkl文件是python里面保存文件的一种格式,如果直接打开会显示一堆序列化的东西。
3. pkl文件是什么样的?
4. 为什么要使用pkl文件
5. 怎样使用pkl文件

### Python Pickle 模块的命令执行漏洞及其使用 `pickle` 是 Python 提供的一个用于对象序列化和反序列化的模块。它允许开发者将复杂的 Python 对象保存到文件或者网络传输中,并能够重新加载这些对象以便继续使用[^1]。然而,由于 `pickle` 使用的是二进制协议来存储数据,在反序列化过程中可能会触发恶意代码的执行。 #### 脆弱性分析 当程序接受来自不可信源的数据并尝试对其进行 unpickle 处理时,攻击者可以构造特制的 pickle 数据流,从而实现远程代码执行 (RCE)[^2]。这是因为 `pickle` 不仅恢复原始数据结构,还会调用类方法或函数完成重建过程中的某些逻辑运算。如果被反序列化的对象定义了特殊的方法(如 `__reduce__()` 或 `__setstate__()`) 并且该方法包含了危险的操作,则可能造成安全风险。 以下是利用此漏洞的一般原理说明: - 攻击者创建一个自定义类实例,其中包含恶意行为; - 将此类实例通过标准库中的 `pickle.dumps()` 方法转换成字符串形式; - 把这个经过编码后的 payload 发送给目标应用; - 当受害者运行 `pickle.loads(payload)` 时即会激活隐藏于内部的功能。 #### 实际案例演示 下面给出一段简单的例子展示如何滥用上述机制达到任意 shell 命令的目的: ```python import os import subprocess import _pickle as cPickle class RunBinSh(object): def __reduce__(self): return (subprocess.Popen, (['/bin/sh'],)) evil = RunBinSh() serialized = cPickle.dumps(evil) # 这里模拟接收外部输入的情况下的不安全性 deserialized = cPickle.loads(serialized) ``` 在这个脚本里面我们定义了一个名为 `RunBinSh` 的新类型,它的唯一目的是返回一对元组给内置函数 `__reduce__`, 此处指定要执行的动作就是启动一个新的交互式终端窗口(`/bin/sh`). 如果有人把这个序列化之后的内容传回来再解码出来的话就会打开shell. #### 防护措施建议 为了防止这类问题的发生,应该采取如下策略之一或多组合起来加以防范: - **避免直接处理不受信任方提交过来的对象**: 只有确知其来源可靠才可放心地运用pickling/un-pickling技术. - **采用更安全替代方案**, 如 JSON/XML/YAML 等纯文本格式来进行跨平台间通信; 它们虽然功能有限但相对稳定得多. - **验证接收到的信息合法性之前绝不应先做任何解析动作**.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值