How to produce remote code execution via pickling an object

I’m working on a challenge that was presented to me without much prior knowledge of Python and the pickle library.

I’m unable to post the challenge’s code for reasons. In short though I am accessing a web app that allows for user’s to upload files of any type and size.

The code given to me then creates an object for the file that contains user information (my actual username for the website), content type, and the data of the file.

The web application then creates a file handle for writing to effectively put the serialized data into the file on the system.

For example:

f = open(filename, 'w') pickle.dump(fileObject ,f) 

From my understanding of pickle.load based on what I read it takes the object and produces strings / tuples that contain the information provided from the file I uploaded.

I would imagine I would then use pickle.load(filehandle) to unserialize (deserialize?) the file and perform the remote code execution of gaining a reverse shell or whatever it is I’d like to do.

However, when I supply code to the file I uploaded, and I try to have the pickle.load get called from the file download function I never see what I expect. I usually just get the contents of my uploaded file back to me without code execution.

I am typically uploading a file with the following contents:

import os  class Exploit:    def __init():       executePayload()     def executePayload()       return os.system("/bin/echo blah") 

Do I need to include something myself that will then serialize the above code again and then something to unserialize it?

I found the following question over on stackoverflow: that made me think I need to do make a payload that creates a payload.

I’d appreciate if someone could provide reading materials to better understand what’s happening or just an easy to follow explanation + example.