forked from braedenherman97/Vulnerable-Flask-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vulnerable-flask-app.py
228 lines (191 loc) · 6.05 KB
/
vulnerable-flask-app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from flask import Flask,jsonify,render_template_string,request,Response,render_template
import subprocess
from werkzeug.datastructures import Headers
from werkzeug.utils import secure_filename
import sqlite3
app = Flask(__name__)
app.config['UPLOAD_FOLDER']="/home/kali/Desktop/upload"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000
@app.route("/")
def main_page():
return "REST API"
@app.route("/user/<string:name>")
def search_user(name):
con = sqlite3.connect("test.db")
cur = con.cursor()
cur.execute("select * from test where username = '%s'" % name)
data = str(cur.fetchall())
con.close()
import logging
logging.basicConfig(filename="restapi.log", filemode='w', level=logging.DEBUG)
logging.debug(data)
return jsonify(data=data),200
@app.route("/welcome/<string:name>")
def welcome(name):
data="Welcome "+name
return jsonify(data=data),200
@app.route("/welcome2/<string:name>")
def welcome2(name):
data="Welcome "+name
return data
@app.route("/hello")
def hello_ssti():
if request.args.get('name'):
name = request.args.get('name')
template = f'''<div>
<h1>Hello</h1>
{name}
</div>
'''
import logging
logging.basicConfig(filename="restapi.log", filemode='w', level=logging.DEBUG)
logging.debug(str(template))
return render_template_string(template)
@app.route("/get_users")
def get_users():
try:
hostname = request.args.get('hostname')
command = "dig " + hostname
data = subprocess.check_output(command, shell=True)
return data
except:
data = str(hostname) + " username didn't found"
return data
@app.route("/get_log/")
def get_log():
try:
command="cat restapi.log"
data=subprocess.check_output(command,shell=True)
return data
except:
return jsonify(data="Command didn't run"), 200
@app.route("/read_file")
def read_file():
filename = request.args.get('filename')
file = open(filename, "r")
data = file.read()
file.close()
import logging
logging.basicConfig(filename="restapi.log", filemode='w', level=logging.DEBUG)
logging.debug(str(data))
return jsonify(data=data),200
@app.route("/deserialization/")
def deserialization():
try:
import socket, pickle
HOST = "0.0.0.0"
PORT = 8001
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
connection, address = s.accept()
with connection:
received_data = connection.recv(1024)
data=pickle.loads(received_data)
return str(data)
except:
return jsonify(data="You must connect 8001 port"), 200
@app.route("/get_admin_mail/<string:control>")
def get_admin_mail(control):
if control=="admin":
data="admin@cybersecurity.intra"
import logging
logging.basicConfig(filename="restapi.log", filemode='w', level=logging.DEBUG)
logging.debug(data)
return jsonify(data=data),200
else:
return jsonify(data="Control didn't set admin"), 200
@app.route("/run_file")
def run_file():
try:
filename=request.args.get("filename")
command="/bin/bash "+filename
data=subprocess.check_output(command,shell=True)
return data
except:
return jsonify(data="File failed to run"), 200
@app.route("/create_file")
def create_file():
try:
filename=request.args.get("filename")
text=request.args.get("text")
file=open(filename,"w")
file.write(text)
file.close()
return jsonify(data="File created"), 200
except:
return jsonify(data="File didn't create"), 200
connection = {}
max_con = 50
def factorial(number):
if number == 1:
return 1
else:
return number * factorial(number - 1)
@app.route('/factorial/<int:n>')
def factroial(n:int):
if request.remote_addr in connection:
if connection[request.remote_addr] > 2:
return jsonify(data="Too many req."), 403
connection[request.remote_addr] += 1
else:
connection[request.remote_addr] = 1
result=factorial(n)
if connection[request.remote_addr] == 1:
del connection[request.remote_addr]
else:
connection[request.remote_addr] -= 1
return jsonify(data=result), 200
@app.route('/login',methods=["GET"])
def login():
username=request.args.get("username")
passwd=request.args.get("password")
if "anil" in username and "cyber" in passwd:
return jsonify(data="Login successful"), 200
else:
return jsonify(data="Login unsuccessful"), 403
@app.route('/route')
def route():
content_type = request.args.get("Content-Type")
response = Response()
headers = Headers()
headers.add("Content-Type", content_type)
response.headers = headers
return response
@app.route('/logs')
def ImproperOutputNeutralizationforLogs():
data = request.args.get('data')
import logging
logging.basicConfig(filename="restapi.log", filemode='w', level=logging.DEBUG)
logging.debug(data)
return jsonify(data="Logging ok"), 200
@app.route("/user_pass_control")
def user_pass_control():
import re
username=request.form.get("username")
password=request.form.get("password")
if re.search(username,password):
return jsonify(data="Password include username"), 200
else:
return jsonify(data="Password doesn't include username"), 200
@app.route('/upload', methods = ['GET','POST'])
def uploadfile():
import os
if request.method == 'POST':
f = request.files['file']
filename=secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return 'File uploaded successfully'
else:
return '''
<html>
<body>
<form method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
'''
if __name__ == '__main__':
app.run(host="0.0.0.0",port=8081)