-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLinjection.py
More file actions
97 lines (78 loc) · 3.12 KB
/
Copy pathSQLinjection.py
File metadata and controls
97 lines (78 loc) · 3.12 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
# What the progam does :
# Create a database with dummy data
# deploy it localy
# make a simple web app that connects to the database with
# a search bar that takes user input and queries the database
# but a SQL injection vulnerability in the search bar
# and demonstrate how to exploit it
# and how to fix it
from flask import Flask, request, jsonify, render_template
import sqlite3
import os
app = Flask(__name__)
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
def initialize_db():
if not os.path.exists("database.db"): # Check if the database already exists
conn = get_db_connection()
cursor = conn.cursor()
# Create table
cursor.execute('''
CREATE TABLE items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price INTEGER
)
''')
# Insert sample data
sample_data = [('Apple', 2.43), ('Banana', 1.39), ('Orange', 0.99), ('Grape', 1.83), ('Mango', 5.39)]
cursor.executemany("INSERT INTO items (name, price) VALUES (?, ?)", sample_data)
# Create users table
cursor.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL
)
''')
# Insert sample data into users table
sample_users = [('admin', 'admin@example.com', 'FR2Z5x9n6h2ygP'), ('user1', 'user1@example.com', 'jc3RgHJ84nd27B'), ('user2', 'user2@example.com', 'd4n2B7jgH3J8cR')]
cursor.executemany("INSERT INTO users (username, email, password) VALUES (?, ?, ?)", sample_users)
conn.commit()
conn.close()
@app.route('/search', methods=['POST'])
def search():
data = request.get_json()
query = data.get('query', '')
security = data.get('security', False)
if not query:
return jsonify({"error": "Search query cannot be empty"}), 400
conn = get_db_connection()
cursor = conn.cursor()
print(data)
if security:
# Secure query using parameterized queries (Protection against SQL Injection)
# It uses precompiled sql request.
cursor.execute("SELECT name, price FROM items WHERE name LIKE ?", (f"%{query}%",))
results = cursor.fetchall()
else:
# Insecure query (Vulnerable to SQL Injection, do not use in production)
insecure_query = f"SELECT name, price FROM items WHERE name LIKE '%{query}%'"
cursor.execute(insecure_query)
results = cursor.fetchall()
conn.close()
# for row in results:
# print(row)
return jsonify({
"mode": security,
"results": [dict(row) for row in results],
})
@app.route('/')
def base():
return render_template('injection.html')
if __name__ == '__main__':
initialize_db() # Populate the database at the start
app.run(host="0.0.0.0", port=5000)