1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| from flask import Flask, request import pickle import base64 import io
BLACKLISTED_CLASSES = [ 'subprocess.check_output','builtins.eval','builtins.exec', 'os.system', 'os.popen', 'os.popen2', 'os.popen3', 'os.popen4', 'pickle.load', 'pickle.loads', 'cPickle.load', 'cPickle.loads', 'subprocess.call', 'subprocess.check_call', 'subprocess.Popen', 'commands.getstatusoutput', 'commands.getoutput', 'commands.getstatus', 'pty.spawn', 'posixfile.open', 'posixfile.fileopen', '__import__','os.spawn*','sh.Command','imp.load_module','builtins.compile' 'eval', 'builtins.execfile', 'compile', 'builtins.open', 'builtins.file', 'os.system', 'os.fdopen', 'os.tmpfile', 'os.fchmod', 'os.fchown', 'os.open', 'os.openpty', 'os.read', 'os.pipe', 'os.chdir', 'os.fchdir', 'os.chroot', 'os.chmod', 'os.chown', 'os.link', 'os.lchown', 'os.listdir', 'os.lstat', 'os.mkfifo', 'os.mknod', 'os.access', 'os.mkdir', 'os.makedirs', 'os.readlink', 'os.remove', 'os.removedirs', 'os.rename', 'os.renames', 'os.rmdir', 'os.tempnam', 'os.tmpnam', 'os.unlink', 'os.walk', 'os.execl', 'os.execle', 'os.execlp', 'os.execv', 'os.execve', 'os.dup', 'os.dup2', 'os.execvp', 'os.execvpe', 'os.fork', 'os.forkpty', 'os.kill', 'os.spawnl', 'os.spawnle', 'os.spawnlp', 'os.spawnlpe', 'os.spawnv', 'os.spawnve', 'os.spawnvp', 'os.spawnvpe', 'pickle.load', 'pickle.loads', 'cPickle.load', 'cPickle.loads', 'subprocess.call', 'subprocess.check_call', 'subprocess.check_output', 'subprocess.Popen', 'commands.getstatusoutput', 'commands.getoutput', 'commands.getstatus', 'glob.glob', 'linecache.getline', 'shutil.copyfileobj', 'shutil.copyfile', 'shutil.copy', 'shutil.copy2', 'shutil.move', 'shutil.make_archive', 'popen2.popen2', 'popen2.popen3', 'popen2.popen4', 'timeit.timeit', 'sys.call_tracing', 'code.interact', 'code.compile_command', 'codeop.compile_command', 'pty.spawn', 'posixfile.open', 'posixfile.fileopen' ]
class SafeUnpickler(pickle.Unpickler): def find_class(self, module, name): if f"{module}.{name}" in BLACKLISTED_CLASSES: raise pickle.UnpicklingError("Forbidden class: %s.%s" % (module, name)) return super().find_class(module, name)
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"]) def index(): if request.method == "POST": encoded_data = request.form["data"] decoded_data = base64.b64decode(encoded_data) try: data_stream = io.BytesIO(decoded_data) unpickler = SafeUnpickler(data_stream) result = unpickler.load() return f"Deserialized data: {list(result)}" except Exception as e: return f"Error during deserialization: {str(e)}" else: return """ <form method="post"> <label for="data">Enter your serialized data:</label><br> <textarea id="data" name="data"></textarea><br> <input type="submit" value="Submit"> </form> """
if __name__ == "__main__": app.run(port=8080)
|