2024-04-16 15:11:51 +02:00
|
|
|
from flask import Flask, flash, request, render_template
|
2024-04-12 16:21:09 +02:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.secret_key = 'secret_key'
|
|
|
|
|
2024-04-16 15:11:51 +02:00
|
|
|
|
2024-04-12 16:21:09 +02:00
|
|
|
def db_connection():
|
|
|
|
conn = sqlite3.connect('users.db')
|
|
|
|
c = conn.cursor()
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
return render_template('login.html')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def login():
|
|
|
|
username = request.form['username']
|
|
|
|
password = request.form['password']
|
|
|
|
|
|
|
|
# Vulnerable code with SQL injection vulnerability
|
2024-04-16 15:11:51 +02:00
|
|
|
query = "SELECT * FROM users WHERE username = '%s' AND password = '%s'" \
|
|
|
|
% (username, password)
|
2024-04-12 16:21:09 +02:00
|
|
|
|
2024-04-16 15:11:51 +02:00
|
|
|
# YOU CAN ALSO WRITE IT LIKE THIS:
|
|
|
|
# query = "SELECT * FROM users WHERE username='" + username + "' AND \
|
|
|
|
# password='" + password + "'"
|
2024-04-12 16:21:09 +02:00
|
|
|
|
|
|
|
try:
|
2024-04-16 15:11:51 +02:00
|
|
|
c = db_connection()
|
|
|
|
c.execute(query)
|
|
|
|
user = c.fetchone()
|
|
|
|
|
2024-04-12 16:21:09 +02:00
|
|
|
if user:
|
|
|
|
login_failed = False
|
|
|
|
return render_template('profile.html')
|
|
|
|
else:
|
|
|
|
login_failed = True
|
2024-04-16 15:11:51 +02:00
|
|
|
return render_template('login.html', login_failed=login_failed)
|
2024-04-12 16:21:09 +02:00
|
|
|
except sqlite3.Error as e:
|
|
|
|
flash(f"{e}")
|
2024-04-16 15:11:51 +02:00
|
|
|
return render_template('login.html', error=e)
|
2024-04-12 16:21:09 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0', debug=True)
|