Skip to content

Instantly share code, notes, and snippets.

@jcarlosroldan
Created July 26, 2019 13:19
Show Gist options
  • Save jcarlosroldan/c4acf7315c66a5666d6ee334178f6198 to your computer and use it in GitHub Desktop.
Save jcarlosroldan/c4acf7315c66a5666d6ee334178f6198 to your computer and use it in GitHub Desktop.
Minimal directory listing
from flask import Flask, send_file, request
from hashlib import sha256
from os import listdir
from os.path import join, abspath, dirname, isdir
from sys import stderr
app = Flask(__name__)
template = '<!doctype html><head><meta charset="utf-8"><title>%s</title></head><body><h1>%s</h1><ul>%s</ul></body></html>'
base = abspath('.')
salt = 'changeme' # generated with https://www.random.org/passwords/?num=1&len=24&format=html&rnd=new
key = 'changeme' # change it using hash
def hash(text):
return sha256((text + salt).encode()).hexdigest()
@app.before_request
def check():
if 'key' not in request.args or hash(request.args['key']) != key:
return 'Expected "key" argument to be the password.'
@app.route('/', defaults={'addr': ''})
@app.route('/<path:addr>')
def listing(addr):
path = join(base, addr)
if isdir(path):
title = 'Index of: %s/' % addr.rstrip('/')
files = '\n'.join('<li><a href="/%s?key=%s">%s</a></li>' % (join(addr, f), request.args['key'], f) for f in listdir(path))
return template % (title, title, files)
else:
return send_file(path)
if __name__ == '__main__':
# print(hash('mypassword'))
app.run(port=80, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment