-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflaskApp.py
More file actions
118 lines (90 loc) · 3.23 KB
/
flaskApp.py
File metadata and controls
118 lines (90 loc) · 3.23 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
import os
from flask import Flask, request, render_template, session, redirect, url_for
import sqlite3
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
app = Flask(__name__)
app.secret_key = "supersecretkey123" # Intentionally weak for demo purposes
# Initialize the database
def init_db():
# Connect to the database
conn = sqlite3.connect("users.db")
# Create a cursor object to interact with the database
c = conn.cursor()
# Create the 'users' table if it doesn't already exist
c.execute(
"""CREATE TABLE IF NOT EXISTS users
(username TEXT PRIMARY KEY, password TEXT, role TEXT)"""
)
# Insert test users with obvious hints
c.execute(
"INSERT OR IGNORE INTO users VALUES (?, ?, ?)", ("guest", "guest123", "user")
)
c.execute(
"INSERT OR IGNORE INTO users VALUES (?, ?, ?)",
("admin", "admin123", "admin"),
)
# Commit the changes to the database
conn.commit()
# Close the database connection
conn.close()
init_db()
@app.route("/")
def index():
return render_template("index.html")
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
# Connect to the database
conn = sqlite3.connect("users.db")
c = conn.cursor()
# Execute the SQL query with the user input
query = "SELECT * FROM users WHERE username = '{}' AND password = '{}'".format(
username, password
)
try:
c.execute(query)
user = c.fetchone()
if user:
# Login successful!
session["username"] = username
session["role"] = user[2]
return redirect(url_for("dashboard"))
else:
# Login failed
return "Invalid username or password", 401
except sqlite3.OperationalError as e:
# Handle SQL errors
return "Error: {}".format(e), 500
finally:
conn.close()
return render_template("login.html")
@app.route("/dashboard")
def dashboard():
# Check if the user is logged in
if "username" not in session:
# Redirect the user to the login page if they are not logged in
return redirect(url_for("login"))
# Render the dashboard page with the username and role of the logged in user
return render_template(
"dashboard.html", username=session["username"], role=session["role"]
)
@app.route("/logout")
def logout():
# Clear the session data
session.clear()
# Redirect the user to the login page
return redirect(url_for("login"))
@app.route("/flag")
def flag():
# Check if the current user's role in the session is not 'admin'
if session.get("role") != "admin":
# If the user is not an admin, return an access denied message
# with a 403 Forbidden HTTP status code
return "Access Denied: Admin privileges required", 403
# If the user is an admin, return the flag string
return "FLAG{easy_sql_injection}"
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=8080)