Skip to content

Commit e1eb7f3

Browse files
committed
finishedChallenge
1 parent 998346d commit e1eb7f3

8 files changed

Lines changed: 69 additions & 28 deletions

File tree

challenge/create_db.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sqlite3
2+
3+
def create_db():
4+
# Connect to the SQLite database
5+
conn = sqlite3.connect('database.db')
6+
cursor = conn.cursor()
7+
8+
cursor.execute('''
9+
CREATE TABLE IF NOT EXISTS users (
10+
id INTEGER PRIMARY KEY AUTOINCREMENT,
11+
username TEXT NOT NULL,
12+
password TEXT NOT NULL
13+
)
14+
''')
15+
16+
cursor.execute('''
17+
INSERT INTO users (username, password)
18+
VALUES ('admin', 'admin999445')
19+
''')
20+
21+
22+
conn.commit()
23+
users = cursor.fetchall()
24+
print(users)
25+
conn.close()
26+
print("Database created and test user inserted.")
27+
28+
29+
if __name__ == '__main__':
30+
create_db()

challenge/database.db

12 KB
Binary file not shown.

challenge/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
class LoginForm(FlaskForm):
77
username = StringField('username', validators=[DataRequired()])
8-
password = PasswordField('password', validators=[DataRequired()])
8+
password = PasswordField('password', validators=[DataRequired()])

challenge/templates/challenge1.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,14 @@
22

33
{% block content %}
44
<h1>Challenge 1</h1>
5-
<p>flag{some-flag}</p>
5+
<form method="post">
6+
<label>Username:</label><br />
7+
{{ form.username }}
8+
<label>Password:</label><br />
9+
{{ form.password }}
10+
<button type="submit">Submit</button>
11+
</form>
12+
<p>
13+
{{ response }}
14+
</p>
615
{% endblock %}

challenge/templates/challenge2.html

Lines changed: 0 additions & 15 deletions
This file was deleted.

challenge/templates/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ <h1>Home</h1>
66
<div id="challenges">
77
<h2>Challenges</h2>
88
<ul>
9-
<li><a href="/1">Example Challenge 1</a></li>
10-
<li><a href="/2">Example Challenge 2</a></li>
9+
<li><a href="/1">Challenge 1</a></li>
1110
</ul>
1211
</div>
1312
{% endblock %}

challenge/views.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
import flask
22
from flask import render_template
3+
import sqlite3
34

45
from challenge import app, forms
56

7+
def get_db_connection():
8+
conn = sqlite3.connect("database.db")
9+
conn.row_factory = sqlite3.Row
10+
return conn
11+
12+
def get_db_connection_3():
13+
conn = sqlite3.connect("challenge3.db")
14+
conn.row_factory = sqlite3.Row
15+
return conn
16+
617

718
@app.route('/', methods=['GET'])
819
def index() -> str:
920
return render_template("index.html")
1021

11-
@app.route('/1', methods=['GET'])
22+
@app.route('/1', methods=['GET', 'POST'])
1223
def ch_1() -> str:
13-
return render_template("challenge1.html")
14-
15-
@app.route('/2', methods=['GET', 'POST'])
16-
def ch_2() -> str:
1724
form = forms.LoginForm()
25+
response = ""
1826
if flask.request.method == 'POST':
19-
if form.username.data == "admin" and form.password.data == "admin":
20-
return render_template("challenge2.html", form=form, response="flag{some-flag-idk-lol}")
27+
username = form.username.data
28+
password = form.password.data
29+
30+
conn = get_db_connection()
31+
query = f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'"
32+
result = conn.execute(query).fetchall()
33+
conn.close()
34+
35+
if result:
36+
response = f"flag{{simple_sql_challenge}}"
2137
else:
22-
return render_template("challenge2.html", form=form, response="Incorrect username/password")
23-
return render_template("challenge2.html", form=form)
38+
response = "Invalid credentials."
39+
40+
return render_template("challenge1.html", form=form, response=response)
41+
2442

database.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)