Files
moloapp/audit/MOLO-STATIC-001/reproduce.py
T

56 lines
2.0 KiB
Python

#!/usr/bin/env python3
# MOLO-STATIC-001: static-file jail escape via a symlinked intermediate
# directory, even with follow_symlinks=false (the default). See README.md.
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
import molo_audit as m
SECRET = b"MOLO-AUDIT-OUTSIDE-SECRET\n"
def main():
m.arm_watchdog()
if not os.path.exists(m.INTEROP):
m.verdict_skip("%s not built (run make)" % m.INTEROP)
tmp = m.scratch_dir()
www = os.path.join(tmp, "www")
outside = os.path.join(tmp, "outside")
os.makedirs(www)
os.makedirs(outside)
with open(os.path.join(outside, "secret.txt"), "wb") as f:
f.write(SECRET)
# The symlink itself is the LAST path component only when resolving the
# link; "escape/secret.txt" makes it an INTERMEDIATE component, which
# openat's O_NOFOLLOW does not protect (it only blocks the final
# component of the path actually opened).
os.symlink(outside, os.path.join(www, "escape"))
srv = m.Server([m.INTEROP, "0", www])
port = srv.port
resp, _ = m.h1(port, b"GET /static/escape/secret.txt HTTP/1.1\r\nHost: x\r\n"
b"Connection: close\r\n\r\n", 1.5)
srv.stop()
if SECRET in resp:
m.verdict_fail(
"GET /static/escape/secret.txt returned the file from outside the static "
"root (follow_symlinks left at its default of false); DiskProvider::open "
"(disk_provider.cpp) uses openat(..., O_NOFOLLOW) which blocks a symlink "
"only as the FINAL path component, so a symlinked directory earlier in "
"the path is followed transparently, escaping the jail"
)
if m.status_lines(resp) == [404]:
m.verdict_pass(
"GET /static/escape/secret.txt was refused (404); the jail held against a "
"symlinked intermediate directory"
)
m.verdict_fail("unexpected response, neither the secret nor a clean 404: %r" % resp[:200])
if __name__ == "__main__":
main()