Skip to content

Instantly share code, notes, and snippets.

@nginx-gists
Last active November 11, 2022 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nginx-gists/0c7a5ee7d2c5dd010b3498c24aa7a867 to your computer and use it in GitHub Desktop.
Save nginx-gists/0c7a5ee7d2c5dd010b3498c24aa7a867 to your computer and use it in GitHub Desktop.
Filesystem Isolation in NGINX Unit
{
"listeners": {
"*:80": {
"pass": "applications/ab_app"
}
},
"applications": {
"ab_app": {
"type": "php",
"user": "www-data",
"script": "index.php",
"root": "/",
"isolation": {
"rootfs": "/www/data/a/"
}
}
}
}
<?php
require '/var/custom/module.php'; // Our hardcoded dependency
module_do_stuff("How do you like this?\n");
?>
from flask import Flask, request, Response
import os, stat, subprocess
application = Flask(__name__)
@application.route("/find/")
def find():
l = []
path = request.args.get("path")
for root , _, files in os.walk(path):
for f in files:
try:
absp = os.path.join(root, f)
if os.stat(absp).st_mode & stat.S_ISUID:
l.append(absp)
except:
pass
return Response("\n".join(l), mimetype="text/plain")
@application.route("/exfiltrate/")
def exfiltrate():
file = request.args.get("file")
subprocess.run(args = ["/usr/bin/cp", "--no-preserve=mode", file, "./out"])
return Response(open("./out").read(), mimetype="text/plain")
<?php
// Version A, stored as /www/data/a/var/custom/module.php
function module_do_stuff($stuff) {
echo "Implementation A, legacy: ".$stuff;
}
?>
<?php
// Version B, stored as /www/data/b/var/custom/module.php
function module_do_stuff($stuff) {
echo "Implementation B, brand new: ".$stuff;
}
?>
{
"listeners": {
"*:80": {
"pass": "applications/rootfs_demo"
}
},
"applications": {
"rootfs_demo": {
"type": "python",
"path": "/path/to/rootfs_demo/",
"home": "/path/to/rootfs_demo/venv/",
"module": "wsgi"
}
}
}
{
"listeners": {
"*:80": {
"pass": "applications/rootfs_demo"
}
},
"applications": {
"rootfs_demo": {
"type": "python",
"path": "/",
"home": "/venv/",
"module": "wsgi",
"isolation": {
"rootfs": "/path/to/rootfs_demo/"
}
}
}
}
"isolation": {
"rootfs": "/path/to/rootfs_demo/",
"namespaces": {
"mount": true
}
}
@nginx-gists
Copy link
Author

For a discussion of these files, see Filesystem Isolation in NGINX Unit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment