-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvulnerable_app.py
More file actions
292 lines (280 loc) · 14.4 KB
/
Copy pathvulnerable_app.py
File metadata and controls
292 lines (280 loc) · 14.4 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from flask import Flask, request, render_template_string
import sqlite3
import os
import platform
app = Flask(__name__)
# Hardcoded Secret (Vulnerability 1: Sensitive Data Exposure)
SECRET_API_KEY = "12345-abcde-super-secret-key"
def init_db():
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT, password TEXT)''')
cursor.execute('''INSERT OR IGNORE INTO users (id, username, password) VALUES (1, 'admin', 'admin123')''')
conn.commit()
conn.close()
@app.route('/')
def index():
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vulnerable App Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; border-bottom: 1px solid #edf2f7; padding: 15px 20px; }
.card-body { padding: 20px; }
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Vulnerable Dashboard</div>
<div class="card-body">
<p class="mb-4">Welcome to the vulnerable app! Select a module to test:</p>
<div class="d-grid gap-2">
<a href="/login" class="btn btn-outline-danger">Login (SQLi)</a>
<a href="/greet" class="btn btn-outline-danger">Greet (XSS)</a>
<a href="/ping" class="btn btn-outline-danger">Ping (Command Injection)</a>
<a href="/admin" class="btn btn-outline-danger">Admin (Broken Access Control)</a>
</div>
</div>
</div>
</div>
</body>
</html>
'''
# SQL Injection (Vulnerability 2)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
# Vulnerable SQL query using string formatting
query = f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'"
cursor.execute(query)
user = cursor.fetchone()
if user:
return f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Success</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {{ background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }}
.banner {{ background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }}
.container-main {{ max-width: 600px; margin: 40px auto; }}
.card {{ border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }}
.card-header {{ background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }}
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Login Success</div>
<div class="card-body text-center p-4">
<h4 class="text-success mb-3">Welcome, {username}!</h4>
<p class="text-muted">You have successfully authenticated.</p>
<a href="/" class="btn btn-secondary mt-3">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
'''
else:
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login Failed</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Login Failed</div>
<div class="card-body text-center p-4">
<h4 class="text-danger mb-3">Invalid credentials.</h4>
<a href="/login" class="btn btn-secondary mt-3">Try Again</a>
</div>
</div>
</div>
</body>
</html>
'''
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }
.container-main { max-width: 500px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Account Login</div>
<div class="card-body p-4">
<form method="POST">
<div class="mb-3">
<label class="form-label text-muted">Username</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="mb-4">
<label class="form-label text-muted">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<button type="submit" class="btn btn-danger w-100 mb-2">Sign In</button>
<div class="text-center">
<a href="/" class="text-decoration-none text-muted small">Return to Dashboard</a>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
'''
# Cross-Site Scripting - XSS (Vulnerability 3)
@app.route('/greet')
def greet():
name = request.args.get('name', 'Guest')
# Vulnerable to Reflected XSS
template = f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Greet</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {{ background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }}
.banner {{ background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }}
.container-main {{ max-width: 600px; margin: 40px auto; }}
.card {{ border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }}
.card-header {{ background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }}
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Greeting Service</div>
<div class="card-body text-center p-4">
<h3 class="mb-4">Hello, {name}!</h3>
<p class="text-muted small mb-4">Try adding ?name=<script>alert(1)</script> to the URL.</p>
<a href="/" class="btn btn-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
'''
return render_template_string(template)
# Command Injection (Vulnerability 4)
@app.route('/ping')
def ping():
ip = request.args.get('ip', '127.0.0.1')
# Vulnerable to Command Injection
count_flag = "-n" if platform.system() == "Windows" else "-c"
response = os.popen(f"ping {count_flag} 1 {ip}").read()
return f'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Ping Utility</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {{ background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }}
.banner {{ background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }}
.container-main {{ max-width: 700px; margin: 40px auto; }}
.card {{ border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }}
.card-header {{ background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }}
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card">
<div class="card-header">Network Ping Tool</div>
<div class="card-body p-4">
<p class="text-muted small mb-3">Ping results for IP: {ip}</p>
<div class="bg-light p-3 rounded border mb-4">
<pre class="mb-0" style="white-space: pre-wrap;">{response}</pre>
</div>
<div class="text-center">
<a href="/" class="btn btn-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</div>
</body>
</html>
'''
# Missing Authentication (Vulnerability 5)
@app.route('/admin')
def admin():
# Anyone can access this page without logging in!
return '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { background-color: #f8f9fa; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; }
.banner { background-color: #800000; color: #ffffff; padding: 15px; text-align: center; font-weight: 500; border-bottom: 1px solid #500000; }
.container-main { max-width: 600px; margin: 40px auto; }
.card { border: none; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); }
.card-header { background-color: #fff; font-weight: 600; border-top-left-radius: 10px !important; border-top-right-radius: 10px !important; padding: 15px 20px; }
</style>
</head>
<body>
<div class="banner">⚠️ This application intentionally contains security vulnerabilities for educational purposes.</div>
<div class="container container-main">
<div class="card border-danger">
<div class="card-header bg-danger text-white">Admin Dashboard</div>
<div class="card-body p-4">
<h5 class="text-danger mb-3">Warning: Unauthenticated Access Detected</h5>
<p class="mb-4">Welcome to the secret admin area. You have full control because access control is broken.</p>
<a href="/" class="btn btn-outline-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</body>
</html>
'''
if __name__ == '__main__':
init_db()
app.run(debug=True)