diff --git a/introduction-to-sql-injection/example/README.md b/introduction-to-sql-injection/example/README.md index f125322..1685530 100644 --- a/introduction-to-sql-injection/example/README.md +++ b/introduction-to-sql-injection/example/README.md @@ -1,9 +1,9 @@ # Example project of a website including an SQL injection This implementation is meant to be used for training purposes. -Do not use the code in production or development. +Do not use this code in production or as a blueprint for development! -## Usage +## Installation Use python poetry to install dependencies in the following way. @@ -11,6 +11,20 @@ Use python poetry to install dependencies in the following way. poetry install ``` +Dependencies can be found inside the `./pyproject.toml` file. + +After installation has been done, start the flask server. + +### Usage + +```sh +poetry run python3 ./flask_sqli.py +``` + +Now, the website is accessible at [localhost:5000](http://localhost:5000/) + +### Manual Installation + If you want to install the dependencies manually use a venv in the following way. ```sh @@ -19,12 +33,11 @@ source venv/bin/activate pip install flask ``` -Dependencies can be found inside the `./pyproject.toml` file. +### Usage after manual installation -After installation has been done, start the flask server. +Start the flask server without poetry in the following way. ```sh -poetry run python3 ./flask_sqli.py +source venv/bin/activate +python3 ./flask_sqli.py ``` - -Now, the website is accessible at [localhost:5000](http://localhost:5000/) diff --git a/introduction-to-sql-injection/example/flask_sqli.py b/introduction-to-sql-injection/example/flask_sqli.py index 04449ad..ba5a581 100644 --- a/introduction-to-sql-injection/example/flask_sqli.py +++ b/introduction-to-sql-injection/example/flask_sqli.py @@ -1,9 +1,10 @@ -from flask import Flask, request, render_template +from flask import Flask, flash, request, render_template import sqlite3 app = Flask(__name__) app.secret_key = 'secret_key' + def db_connection(): conn = sqlite3.connect('users.db') c = conn.cursor() @@ -21,23 +22,27 @@ def login(): password = request.form['password'] # Vulnerable code with SQL injection vulnerability - query = "SELECT * FROM users WHERE username='" + username + "' AND \ - password='" + password + "'" + query = "SELECT * FROM users WHERE username = '%s' AND password = '%s'" \ + % (username, password) - c = db_connection() - c.execute(query) - user = c.fetchone() + # YOU CAN ALSO WRITE IT LIKE THIS: + # query = "SELECT * FROM users WHERE username='" + username + "' AND \ + # password='" + password + "'" try: + c = db_connection() + c.execute(query) + user = c.fetchone() + if user: login_failed = False return render_template('profile.html') else: login_failed = True - return render_template('login.html', login_failed=login_failed, error_message=user) + return render_template('login.html', login_failed=login_failed) except sqlite3.Error as e: flash(f"{e}") - return render_template('login.html') + return render_template('login.html', error=e) if __name__ == '__main__': app.run(host='0.0.0.0', debug=True) diff --git a/introduction-to-sql-injection/example/templates/login.html b/introduction-to-sql-injection/example/templates/login.html index b1caada..e998d2f 100644 --- a/introduction-to-sql-injection/example/templates/login.html +++ b/introduction-to-sql-injection/example/templates/login.html @@ -5,7 +5,7 @@ {% with messages = get_flashed_messages(with_categories=True) %} {% if messages %} {% for category, message in messages %} -
=
sql_query
- cursor.execute("SELECT * FROM user_data where username = 'admin' and password = 's3cur3P4ssw0rd'"
- )