-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcreate_webapp.py
68 lines (52 loc) · 1.42 KB
/
create_webapp.py
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
63
64
65
66
67
68
# coding: utf8
import sys
import os
import os.path
from fnmatch import fnmatch
if len(sys.argv) < 2:
print(f"[-] Usage: {sys.argv[0]} <folder>")
sys.exit(1)
src_dir = sys.argv[1]
if not os.path.isdir(src_dir):
print(f"[-] Error: {src_dir} is not a folder!")
sys.exit(2)
WEBAPP = """
import io
import os
import sys
import os.path
from flask import Flask, request
from contextlib import redirect_stdout
app = Flask(__name__)
WEBROOT = os.getcwd()
def php_include_file(fname, redirect=False):
if fname.startswith("/"):
fname = "." + fname
tmp = os.path.abspath(os.path.join(WEBROOT, fname))
filename, ext = os.path.splitext(tmp)
with open(f"{filename}.py") as src:
code = src.read()
if redirect:
f = io.StringIO()
with redirect_stdout(f):
exec(code)
return f.getvalue()
exec(code)
@app.errorhandler(404)
def page_not_found(e):
return php_include_file(request.path, True)
"""
def print_route(fname):
_, filename = os.path.split(fname)
fn = fname.lower().replace("-", "_").replace("/", "_").replace(".", "").strip()
print(f"""
@app.route("/{fname}")
def php_{fn}():
return php_include_file("{fname}", True)""")
print(WEBAPP)
for root, _, files in os.walk(src_dir):
for name in files:
fname = os.path.join(root, name)
if fnmatch(fname, "*.py"):
print_route(fname)
print("app.run(debug=True)")