# Reversing Pickles with r2pickledec

DevFeed: [Reversing Pickles with r2pickledec](<https://devfeed.tech/articles/reversing-pickles-with-r2pickledec-92033.md>)

Original publisher: [Read original article](<https://blog.doyensec.com/2023/06/01/r2pickledec.html>)

Published: 2023-05-31T22:00:00Z

Content type: tutorial

Language: en

Sources: [Doyensec](<https://devfeed.tech/sources/doyensec.md>)

Topics: [code analysis](<https://devfeed.tech/topics/code-analysis.md>), [Data serialization](<https://devfeed.tech/topics/data-serialization.md>), [Tool](<https://devfeed.tech/topics/tool.md>), [Python](<https://devfeed.tech/topics/python.md>), [Assembly](<https://devfeed.tech/topics/assembly.md>)

Tags: [auditing](<https://devfeed.tech/tags/auditing.md>), [automation](<https://devfeed.tech/tags/automation.md>), [decompiler](<https://devfeed.tech/tags/decompiler.md>), [doyensec](<https://devfeed.tech/tags/doyensec.md>), [doyensecurity](<https://devfeed.tech/tags/doyensecurity.md>), [exploit](<https://devfeed.tech/tags/exploit.md>), [hacking](<https://devfeed.tech/tags/hacking.md>), [infosec](<https://devfeed.tech/tags/infosec.md>), [obfuscation](<https://devfeed.tech/tags/obfuscation.md>), [offensive](<https://devfeed.tech/tags/offensive.md>), [pentest](<https://devfeed.tech/tags/pentest.md>), [pentester](<https://devfeed.tech/tags/pentester.md>), [protocol](<https://devfeed.tech/tags/protocol.md>), [python](<https://devfeed.tech/tags/python.md>), [r2](<https://devfeed.tech/tags/r2.md>), [remote-code-execution](<https://devfeed.tech/tags/remote-code-execution.md>), [reverse-engineering](<https://devfeed.tech/tags/reverse-engineering.md>), [reversing](<https://devfeed.tech/tags/reversing.md>), [security](<https://devfeed.tech/tags/security.md>), [serialization](<https://devfeed.tech/tags/serialization.md>), [stack](<https://devfeed.tech/tags/stack.md>), [stream](<https://devfeed.tech/tags/stream.md>), [tool](<https://devfeed.tech/tags/tool.md>), [vulnerability](<https://devfeed.tech/tags/vulnerability.md>)

## AI overview

r2pickledec decompiles Python pickles through Radare2, supporting instructions through protocol 5. The article explains that pickle instructions mostly manipulate a stack, with a memo for retrieving earlier values, and warns that unpickling untrusted data can lead to remote code execution. In its example, the decompiler reveals a serialized API object's requests session and an auth tuple containing credentials, then locates and patches the object's base URL. Radare2 flags help identify and rename variables; the plugin's JSON AST includes offsets that can support scripted searches and edits with r2pipe. The author says packaged Radare2 versions may be old but should work for this use, and suggests installing from source if bugs occur.

## Source excerpt

R2pickledec is the first pickle decompiler to support all instructions up to protocol 5 (the current). In this post we will go over what Python pickles are, how they work and how to reverse them with Radare2 and r2pickledec. An upcoming blog post will go even deeper into pickles and share some advanced obfuscation techniques. What are pickles? Pickles are the built-in serialization algorithm in Python. They can turn any Python object into a byte stream so it may be stored on disk or sent over a network. Pickles are notoriously dangerous. You should never unpickle data from an untrusted source. Doing so will likely result in remote code execution. Please refer to the documentation for more details. Pickle Basics Pickles are implemented as a very simple assembly language. There are only 68 instructions and they mostly operate on a stack. The instruction names are pretty easy to understand. For example, the instruction empty_dict will push an empty dictionary onto the stack. The stack only allows access to the top item, or items in some cases. If you want to grab something else, you must use the memo. The memo is implemented as a dictionary with positive integer indexes. You will often see memoize instructions. Naively, the memoize instruction will copy the item at the top of the stack into the next index in the memo. Then, if that item is needed later, a binget n can be used to get the object at index n. To learn more about pickles, I recommend playing with some pickles. Enable descriptions in Radare2 with e asm.describe = true to get short descriptions of each instruction. Decompile simple pickles that you build yourself, and see if you can understand the instructions. Installing Radare2 and r2pickledec For reversing pickles, our tool of choice is Radare2 (r2 for short). Package managers tend to ship really old r2 versions. In this case it's probably fine, I added the pickle arch to r2 a long time ago. But if you run into any bugs I suggest installing from source. In