Spaces:
Sleeping
Sleeping
Upload 10 files
Browse files- app.py +542 -0
- cnn_model.h5 +3 -0
- lstm_model.h5 +3 -0
- requirements.txt +4 -0
- rf_model.pkl +3 -0
- sql_injection_cnn.h5 +3 -0
- sql_tokenizer.pkl +3 -0
- svm_model.pkl +3 -0
- tfidf_vectorizer.pkl +3 -0
- tokenizer.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,542 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras.models import load_model
|
4 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
5 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
6 |
+
import pickle
|
7 |
+
import re
|
8 |
+
import time
|
9 |
+
import numpy as np
|
10 |
+
from sklearn.ensemble import RandomForestClassifier
|
11 |
+
from sklearn.svm import SVC
|
12 |
+
|
13 |
+
# Load models and preprocessing components
|
14 |
+
@st.cache_resource
|
15 |
+
def load_components():
|
16 |
+
# Load deep learning models
|
17 |
+
cnn_model = load_model('cnn_model.h5')
|
18 |
+
lstm_model = load_model('lstm_model.h5')
|
19 |
+
|
20 |
+
# Load traditional ML models
|
21 |
+
with open('rf_model.pkl', 'rb') as f:
|
22 |
+
rf_model = pickle.load(f)
|
23 |
+
with open('svm_model.pkl', 'rb') as f:
|
24 |
+
svm_model = pickle.load(f)
|
25 |
+
|
26 |
+
# Load tokenizer and vectorizer
|
27 |
+
with open('sql_tokenizer.pkl', 'rb') as f:
|
28 |
+
tokenizer_data = pickle.load(f)
|
29 |
+
with open('tfidf_vectorizer.pkl', 'rb') as f:
|
30 |
+
tfidf_vectorizer = pickle.load(f)
|
31 |
+
|
32 |
+
return {
|
33 |
+
'cnn_model': cnn_model,
|
34 |
+
'lstm_model': lstm_model,
|
35 |
+
'rf_model': rf_model,
|
36 |
+
'svm_model': svm_model,
|
37 |
+
'tokenizer': tokenizer_data['tokenizer'],
|
38 |
+
'max_sequence_length': tokenizer_data['max_sequence_length'],
|
39 |
+
'tfidf_vectorizer': tfidf_vectorizer
|
40 |
+
}
|
41 |
+
|
42 |
+
# Try to load all components
|
43 |
+
try:
|
44 |
+
components = load_components()
|
45 |
+
model_loading_error = None
|
46 |
+
except Exception as e:
|
47 |
+
model_loading_error = str(e)
|
48 |
+
components = None
|
49 |
+
|
50 |
+
# Preprocess functions
|
51 |
+
def preprocess_query_for_deep_learning(query, tokenizer, max_sequence_length):
|
52 |
+
"""
|
53 |
+
Tokenizes and pads the input query to prepare it for deep learning models.
|
54 |
+
"""
|
55 |
+
sequences = tokenizer.texts_to_sequences([query])
|
56 |
+
padded = pad_sequences(sequences, maxlen=max_sequence_length, padding='post')
|
57 |
+
return padded
|
58 |
+
|
59 |
+
def preprocess_query_for_traditional_ml(query, tfidf_vectorizer):
|
60 |
+
"""
|
61 |
+
Transforms the input query using TF-IDF for traditional ML models.
|
62 |
+
"""
|
63 |
+
return tfidf_vectorizer.transform([query])
|
64 |
+
|
65 |
+
# Define improved regex patterns for SQL injection attempts
|
66 |
+
SQL_INJECTION_PATTERNS = [
|
67 |
+
# SQL comment syntax that follows a quote (likely injection)
|
68 |
+
r"(?i)'.*--",
|
69 |
+
|
70 |
+
# Quote followed by OR/AND with comparison (classic injection pattern)
|
71 |
+
r"(?i)'\s*(OR|AND)\s*['\d\w]+=\s*['\d\w]+",
|
72 |
+
|
73 |
+
# SQL Comment without preceding from a query context
|
74 |
+
r"(?i)(\s|^)--",
|
75 |
+
|
76 |
+
# Multiple query execution with semicolon
|
77 |
+
r"(?i)'.*;.*--",
|
78 |
+
|
79 |
+
# UNION-based injections
|
80 |
+
r"(?i)'\s*UNION\s+(ALL\s+)?SELECT",
|
81 |
+
|
82 |
+
# Time-delay attacks
|
83 |
+
r"(?i)'\s*;\s*WAITFOR\s+DELAY",
|
84 |
+
|
85 |
+
# DROP/ALTER table attacks
|
86 |
+
r"(?i)'\s*;\s*(DROP|ALTER)",
|
87 |
+
|
88 |
+
# Quote followed by a true condition
|
89 |
+
r"(?i)'\s*OR\s*'?\d+'?\s*=\s*'?\d+'?",
|
90 |
+
|
91 |
+
# Quote followed by always true condition like 1=1
|
92 |
+
r"(?i)'\s*OR\s*(['\"]\d+['\"])=(['\"]\d+['\"])",
|
93 |
+
|
94 |
+
# Batch queries
|
95 |
+
r"(?i);\s*(SELECT|INSERT|UPDATE|DELETE|DROP)",
|
96 |
+
|
97 |
+
# CAST attacks
|
98 |
+
r"(?i)CAST\s*\(.+AS\s+.+\)",
|
99 |
+
|
100 |
+
# Typical SQL function calls in injections
|
101 |
+
r"(?i)'\s*;\s*(EXEC|EXECUTE).*",
|
102 |
+
]
|
103 |
+
|
104 |
+
# Safe SQL patterns that should not trigger false positives
|
105 |
+
SAFE_SQL_PATTERNS = [
|
106 |
+
# Standard SELECT query
|
107 |
+
r"(?i)^SELECT\s+[\w\d\s,*]+\s+FROM\s+[\w\d]+(\s+WHERE\s+[\w\d\s=<>']+)?$",
|
108 |
+
|
109 |
+
# Standard INSERT query
|
110 |
+
r"(?i)^INSERT\s+INTO\s+[\w\d]+\s*\([^)]+\)\s*VALUES\s*\([^)]+\)$",
|
111 |
+
|
112 |
+
# Standard UPDATE query
|
113 |
+
r"(?i)^UPDATE\s+[\w\d]+\s+SET\s+[\w\d\s=',]+(\s+WHERE\s+[\w\d\s=<>']+)?$",
|
114 |
+
]
|
115 |
+
|
116 |
+
# Rule-based detection function
|
117 |
+
def detect_sql_injection_with_regex(query):
|
118 |
+
"""
|
119 |
+
Detects potential SQL injection patterns using improved regex.
|
120 |
+
Returns True if any malicious pattern matches and no safe pattern matches.
|
121 |
+
"""
|
122 |
+
# First check if the query matches any safe pattern
|
123 |
+
for pattern in SAFE_SQL_PATTERNS:
|
124 |
+
if re.search(pattern, query.strip()):
|
125 |
+
# Query matches a safe pattern
|
126 |
+
return False, None
|
127 |
+
|
128 |
+
# Then check for malicious patterns
|
129 |
+
for pattern in SQL_INJECTION_PATTERNS:
|
130 |
+
match = re.search(pattern, query)
|
131 |
+
if match:
|
132 |
+
return True, match.group(0)
|
133 |
+
|
134 |
+
# If no malicious pattern found
|
135 |
+
return False, None
|
136 |
+
|
137 |
+
# Ensemble prediction function
|
138 |
+
def predict_with_ensemble(query, components):
|
139 |
+
"""
|
140 |
+
Uses an ensemble of models to predict if the query is malicious.
|
141 |
+
Returns predictions from individual models and ensemble vote.
|
142 |
+
"""
|
143 |
+
# Get individual model predictions
|
144 |
+
|
145 |
+
# Random Forest prediction
|
146 |
+
query_tfidf = preprocess_query_for_traditional_ml(query, components['tfidf_vectorizer'])
|
147 |
+
rf_pred = int(components['rf_model'].predict(query_tfidf)[0])
|
148 |
+
|
149 |
+
# SVM prediction
|
150 |
+
svm_pred = int(components['svm_model'].predict(query_tfidf)[0])
|
151 |
+
|
152 |
+
# CNN prediction
|
153 |
+
query_padded = preprocess_query_for_deep_learning(query, components['tokenizer'], components['max_sequence_length'])
|
154 |
+
cnn_probability = components['cnn_model'].predict(query_padded)[0][0]
|
155 |
+
cnn_pred = int(cnn_probability > 0.5)
|
156 |
+
|
157 |
+
# LSTM prediction
|
158 |
+
lstm_probability = components['lstm_model'].predict(query_padded)[0][0]
|
159 |
+
lstm_pred = int(lstm_probability > 0.5)
|
160 |
+
|
161 |
+
# Majority voting
|
162 |
+
votes = [rf_pred, svm_pred, cnn_pred, lstm_pred]
|
163 |
+
ensemble_pred = np.bincount(votes).argmax()
|
164 |
+
|
165 |
+
return {
|
166 |
+
'rf': rf_pred,
|
167 |
+
'svm': svm_pred,
|
168 |
+
'cnn': {'prediction': cnn_pred, 'probability': float(cnn_probability)},
|
169 |
+
'lstm': {'prediction': lstm_pred, 'probability': float(lstm_probability)},
|
170 |
+
'ensemble': int(ensemble_pred),
|
171 |
+
'vote_count': {0: list(votes).count(0), 1: list(votes).count(1)}
|
172 |
+
}
|
173 |
+
|
174 |
+
# Initialize session state for UI flow control
|
175 |
+
if 'analysis_stage' not in st.session_state:
|
176 |
+
st.session_state.analysis_stage = 0 # 0: not started, 1: regex done, 2: ensemble done
|
177 |
+
|
178 |
+
if 'regex_result' not in st.session_state:
|
179 |
+
st.session_state.regex_result = None
|
180 |
+
|
181 |
+
if 'ensemble_result' not in st.session_state:
|
182 |
+
st.session_state.ensemble_result = None
|
183 |
+
|
184 |
+
# App title and description
|
185 |
+
st.title("🛡️ SQL Injection Detection")
|
186 |
+
st.markdown("""
|
187 |
+
This application uses a multi-layered approach to detect potentially malicious SQL queries:
|
188 |
+
1. **Rule-based detection** using improved regex patterns
|
189 |
+
2. **Ensemble learning** with majority voting from 4 models:
|
190 |
+
- Random Forest
|
191 |
+
- Support Vector Machine
|
192 |
+
- Convolutional Neural Network
|
193 |
+
- Long Short-Term Memory Network
|
194 |
+
|
195 |
+
Enter a query below or select from the examples to begin analysis.
|
196 |
+
""")
|
197 |
+
|
198 |
+
# Display warning if models couldn't be loaded
|
199 |
+
if model_loading_error:
|
200 |
+
st.warning(f"⚠️ Some models could not be loaded. The application will only use rule-based detection. Error: {model_loading_error}")
|
201 |
+
|
202 |
+
# Example queries in a dropdown
|
203 |
+
st.subheader("Select an Example or Enter Your Own Query")
|
204 |
+
|
205 |
+
example_categories = {
|
206 |
+
"Benign SQL Queries": [
|
207 |
+
"SELECT * FROM users WHERE username='admin'",
|
208 |
+
"SELECT id, name, price FROM products WHERE category_id=5",
|
209 |
+
"SELECT COUNT(*) FROM orders WHERE date > '2023-01-01'",
|
210 |
+
"INSERT INTO logs (user_id, action) VALUES (42, 'login')",
|
211 |
+
"UPDATE customers SET last_login='2023-06-15' WHERE id=101",
|
212 |
+
"DELETE FROM sessions WHERE last_activity < '2023-01-01'",
|
213 |
+
"SELECT email FROM subscribers WHERE active=1",
|
214 |
+
"INSERT INTO feedback (user_id, message) VALUES (87, 'Great service!')",
|
215 |
+
"UPDATE inventory SET stock = stock - 1 WHERE product_id = 300",
|
216 |
+
"SELECT name FROM employees WHERE department = 'Sales'",
|
217 |
+
"SELECT AVG(rating) FROM reviews WHERE product_id = 55",
|
218 |
+
"INSERT INTO audit_log (timestamp, event) VALUES (CURRENT_TIMESTAMP, 'update')",
|
219 |
+
"SELECT * FROM appointments WHERE doctor_id = 10 AND status = 'confirmed'",
|
220 |
+
"UPDATE settings SET value='dark' WHERE key='theme'",
|
221 |
+
"SELECT DISTINCT city FROM customers WHERE country='USA'",
|
222 |
+
"DELETE FROM cart_items WHERE user_id=12 AND product_id=78",
|
223 |
+
"SELECT MAX(salary) FROM employees WHERE role='manager'",
|
224 |
+
"INSERT INTO payments (user_id, amount, method) VALUES (33, 99.99, 'credit')",
|
225 |
+
"UPDATE products SET price = price * 1.1 WHERE category_id = 7",
|
226 |
+
"SELECT * FROM messages WHERE sender_id = 5 AND is_read = 0"
|
227 |
+
],
|
228 |
+
"Malicious SQL Queries": [
|
229 |
+
"' OR 1=1 --",
|
230 |
+
"admin'; DROP TABLE users; --",
|
231 |
+
"SELECT * FROM users WHERE username='' UNION SELECT username,password FROM admin_users --",
|
232 |
+
"'; WAITFOR DELAY '0:0:10' --",
|
233 |
+
"admin' OR '1'='1",
|
234 |
+
"' OR 'a'='a",
|
235 |
+
"' OR 1=1#",
|
236 |
+
"' OR 1=1/*",
|
237 |
+
"admin'--",
|
238 |
+
"'; EXEC xp_cmdshell('dir'); --",
|
239 |
+
"' OR EXISTS(SELECT * FROM users WHERE username = 'admin') --",
|
240 |
+
"1; DROP TABLE sessions --",
|
241 |
+
"'; SHUTDOWN --",
|
242 |
+
"' OR SLEEP(5) --",
|
243 |
+
"' AND 1=(SELECT COUNT(*) FROM users) --",
|
244 |
+
"admin' AND SUBSTRING(password, 1, 1) = 'a' --",
|
245 |
+
"' UNION ALL SELECT NULL,NULL,NULL --",
|
246 |
+
"0' OR 1=1 ORDER BY 1 --",
|
247 |
+
"1' AND (SELECT COUNT(*) FROM users) > 0 --",
|
248 |
+
"' OR (SELECT ASCII(SUBSTRING(password,1,1)) FROM users WHERE username='admin') > 64 --"
|
249 |
+
]
|
250 |
+
}
|
251 |
+
|
252 |
+
# First create category selection
|
253 |
+
category = st.selectbox(
|
254 |
+
"Choose query category:",
|
255 |
+
options=list(example_categories.keys()),
|
256 |
+
key="category"
|
257 |
+
)
|
258 |
+
|
259 |
+
# Then show examples from selected category
|
260 |
+
example = st.selectbox(
|
261 |
+
"Select an example:",
|
262 |
+
options=example_categories[category],
|
263 |
+
key="example"
|
264 |
+
)
|
265 |
+
|
266 |
+
# Allow user to use the selected example or enter their own
|
267 |
+
query_source = st.radio(
|
268 |
+
"Query source:",
|
269 |
+
["Use selected example", "Enter my own query"],
|
270 |
+
key="query_source"
|
271 |
+
)
|
272 |
+
|
273 |
+
if query_source == "Enter my own query":
|
274 |
+
query = st.text_area(
|
275 |
+
"Enter SQL Query:",
|
276 |
+
height=100,
|
277 |
+
placeholder="Type your SQL query here..."
|
278 |
+
)
|
279 |
+
else:
|
280 |
+
query = example
|
281 |
+
st.code(query, language="sql")
|
282 |
+
|
283 |
+
# Analysis process
|
284 |
+
if st.button("Start Analysis") and query:
|
285 |
+
# Reset analysis state
|
286 |
+
st.session_state.analysis_stage = 1
|
287 |
+
|
288 |
+
# Step 1: Rule-based detection
|
289 |
+
with st.spinner("Running rule-based detection..."):
|
290 |
+
time.sleep(0.5) # Simulate processing time
|
291 |
+
is_malicious, matched_pattern = detect_sql_injection_with_regex(query)
|
292 |
+
st.session_state.regex_result = (is_malicious, matched_pattern)
|
293 |
+
|
294 |
+
# If we have completed the regex analysis
|
295 |
+
if st.session_state.analysis_stage >= 1 and st.session_state.regex_result is not None:
|
296 |
+
is_malicious, matched_pattern = st.session_state.regex_result
|
297 |
+
st.subheader("Step 1: Rule-Based Detection")
|
298 |
+
|
299 |
+
if is_malicious:
|
300 |
+
st.error("🚨 SQL Injection Detected (Rule-Based)!")
|
301 |
+
st.warning(f"Matched pattern: `{matched_pattern}`")
|
302 |
+
|
303 |
+
# Show details in expander
|
304 |
+
with st.expander("Rule-Based Detection Details"):
|
305 |
+
st.markdown("""
|
306 |
+
**What was detected:**
|
307 |
+
- The query matched one or more known SQL injection patterns
|
308 |
+
- This type of pattern is commonly used in SQL injection attacks
|
309 |
+
- Review the query for security implications
|
310 |
+
""")
|
311 |
+
|
312 |
+
st.markdown("**Common SQL injection techniques detected:**")
|
313 |
+
st.markdown("""
|
314 |
+
- Comment sequences (`--`) after quotes
|
315 |
+
- Always true conditions (`OR 1=1`)
|
316 |
+
- Union-based injections
|
317 |
+
- SQL command injections
|
318 |
+
""")
|
319 |
+
else:
|
320 |
+
st.success("✅ No SQL injection patterns detected using rules")
|
321 |
+
|
322 |
+
with st.expander("Rule-Based Detection Details"):
|
323 |
+
st.markdown("""
|
324 |
+
**Analysis Details:**
|
325 |
+
- The query did not match any known SQL injection patterns
|
326 |
+
- The structure appears to be standard SQL syntax
|
327 |
+
- No suspicious patterns were identified
|
328 |
+
""")
|
329 |
+
|
330 |
+
# Ask if user wants to proceed with ensemble detection
|
331 |
+
proceed = st.radio(
|
332 |
+
"Would you like to proceed with ensemble model detection?",
|
333 |
+
["Yes", "No"],
|
334 |
+
index=0, # Default to Yes
|
335 |
+
key="proceed"
|
336 |
+
)
|
337 |
+
|
338 |
+
# Check if models are loaded before allowing ensemble analysis
|
339 |
+
if proceed == "Yes" and not model_loading_error:
|
340 |
+
if st.button("Run Ensemble Analysis"):
|
341 |
+
st.session_state.analysis_stage = 2
|
342 |
+
with st.spinner("Running ensemble models..."):
|
343 |
+
time.sleep(1) # Simulate processing time
|
344 |
+
ensemble_results = predict_with_ensemble(query, components)
|
345 |
+
st.session_state.ensemble_result = ensemble_results
|
346 |
+
elif proceed == "Yes" and model_loading_error:
|
347 |
+
st.error("Cannot run ensemble analysis because models failed to load.")
|
348 |
+
|
349 |
+
# If we have completed the ensemble analysis
|
350 |
+
if st.session_state.analysis_stage >= 2 and st.session_state.ensemble_result is not None:
|
351 |
+
results = st.session_state.ensemble_result
|
352 |
+
|
353 |
+
st.subheader("Step 2: Ensemble Model Detection")
|
354 |
+
|
355 |
+
# Create a visual representation of voting
|
356 |
+
vote_benign = results['vote_count'][0]
|
357 |
+
vote_malicious = results['vote_count'][1]
|
358 |
+
|
359 |
+
st.markdown(f"### Model Votes")
|
360 |
+
|
361 |
+
# Create columns for the voting visualization
|
362 |
+
col1, col2 = st.columns(2)
|
363 |
+
|
364 |
+
with col1:
|
365 |
+
st.metric("Safe Votes", vote_benign)
|
366 |
+
|
367 |
+
with col2:
|
368 |
+
st.metric("Malicious Votes", vote_malicious)
|
369 |
+
|
370 |
+
# Create a progress bar to visualize the voting ratio
|
371 |
+
vote_ratio = vote_malicious / (vote_benign + vote_malicious)
|
372 |
+
st.progress(vote_ratio, text=f"Malicious vote ratio: {vote_ratio*100:.0f}%")
|
373 |
+
|
374 |
+
# Display individual model results
|
375 |
+
st.markdown("### Individual Model Results")
|
376 |
+
|
377 |
+
model_cols = st.columns(4)
|
378 |
+
|
379 |
+
with model_cols[0]:
|
380 |
+
st.markdown("**Random Forest**")
|
381 |
+
if results['rf'] == 1:
|
382 |
+
st.error("⚠️ Malicious")
|
383 |
+
else:
|
384 |
+
st.success("✅ Safe")
|
385 |
+
|
386 |
+
with model_cols[1]:
|
387 |
+
st.markdown("**SVM**")
|
388 |
+
if results['svm'] == 1:
|
389 |
+
st.error("⚠️ Malicious")
|
390 |
+
else:
|
391 |
+
st.success("✅ Safe")
|
392 |
+
|
393 |
+
with model_cols[2]:
|
394 |
+
st.markdown("**CNN**")
|
395 |
+
cnn_prob = results['cnn']['probability'] * 100
|
396 |
+
if results['cnn']['prediction'] == 1:
|
397 |
+
st.error(f"⚠️ Malicious ({cnn_prob:.1f}%)")
|
398 |
+
else:
|
399 |
+
st.success(f"✅ Safe ({100-cnn_prob:.1f}%)")
|
400 |
+
|
401 |
+
with model_cols[3]:
|
402 |
+
st.markdown("**LSTM**")
|
403 |
+
lstm_prob = results['lstm']['probability'] * 100
|
404 |
+
if results['lstm']['prediction'] == 1:
|
405 |
+
st.error(f"⚠️ Malicious ({lstm_prob:.1f}%)")
|
406 |
+
else:
|
407 |
+
st.success(f"✅ Safe ({100-lstm_prob:.1f}%)")
|
408 |
+
|
409 |
+
# Final ensemble verdict
|
410 |
+
st.markdown("### Ensemble Verdict")
|
411 |
+
if results['ensemble'] == 1:
|
412 |
+
st.error("🚨 SQL Injection Detected by Majority Vote!")
|
413 |
+
else:
|
414 |
+
st.success("✅ Query deemed safe by majority vote")
|
415 |
+
|
416 |
+
# Explanation in expander
|
417 |
+
with st.expander("Ensemble Detection Details"):
|
418 |
+
st.markdown("""
|
419 |
+
**How ensemble voting works:**
|
420 |
+
- Each model casts a vote (0 for safe, 1 for malicious)
|
421 |
+
- The final decision is based on majority vote
|
422 |
+
- This approach combines the strengths of different model architectures
|
423 |
+
- More robust than any single model alone
|
424 |
+
""")
|
425 |
+
|
426 |
+
if results['ensemble'] == 1:
|
427 |
+
st.markdown(f"""
|
428 |
+
**Why was this flagged:**
|
429 |
+
- {vote_malicious} out of 4 models identified this query as potentially malicious
|
430 |
+
- The majority vote indicates suspicious patterns
|
431 |
+
- This query should be carefully reviewed before execution
|
432 |
+
""")
|
433 |
+
else:
|
434 |
+
st.markdown(f"""
|
435 |
+
**Why was this considered safe:**
|
436 |
+
- {vote_benign} out of 4 models identified this query as likely safe
|
437 |
+
- The majority vote indicates standard SQL patterns
|
438 |
+
- No significant red flags were detected in the ensemble
|
439 |
+
""")
|
440 |
+
|
441 |
+
# Final verdict combining both approaches
|
442 |
+
st.subheader("Final Analysis")
|
443 |
+
|
444 |
+
is_malicious_regex, _ = st.session_state.regex_result
|
445 |
+
is_malicious_ensemble = results['ensemble'] == 1
|
446 |
+
|
447 |
+
if is_malicious_regex or is_malicious_ensemble:
|
448 |
+
st.error("⚠️ This query appears to contain SQL injection patterns. Review carefully before executing.")
|
449 |
+
else:
|
450 |
+
st.success("✅ This query appears safe based on both rule-based and ensemble detection.")
|
451 |
+
|
452 |
+
st.info("ℹ️ Remember: Always use parameterized queries and proper input validation in production systems.")
|
453 |
+
|
454 |
+
# Reset button
|
455 |
+
if st.button("Analyze Another Query"):
|
456 |
+
st.session_state.analysis_stage = 0
|
457 |
+
st.session_state.regex_result = None
|
458 |
+
st.session_state.ensemble_result = None
|
459 |
+
st.experimental_rerun()
|
460 |
+
|
461 |
+
# Sidebar with additional info
|
462 |
+
with st.sidebar:
|
463 |
+
st.header("About This App")
|
464 |
+
st.markdown("""
|
465 |
+
### Multi-Layer Detection Process
|
466 |
+
|
467 |
+
1. **Rule-Based Detection**
|
468 |
+
- Fast, pattern-matching approach
|
469 |
+
- Uses improved regex to identify SQL injection patterns
|
470 |
+
- Reduces false positives with safe pattern recognition
|
471 |
+
|
472 |
+
2. **Ensemble Detection**
|
473 |
+
- Combines 4 different machine learning models:
|
474 |
+
- Random Forest
|
475 |
+
- Support Vector Machine (SVM)
|
476 |
+
- Convolutional Neural Network (CNN)
|
477 |
+
- Long Short-Term Memory Network (LSTM)
|
478 |
+
- Final decision by majority voting
|
479 |
+
""")
|
480 |
+
|
481 |
+
st.markdown("### Machine Learning Architecture")
|
482 |
+
st.code("""
|
483 |
+
# Traditional ML
|
484 |
+
- Random Forest (n_estimators=100)
|
485 |
+
- SVM (kernel='linear')
|
486 |
+
|
487 |
+
# CNN Architecture
|
488 |
+
Sequential([
|
489 |
+
Embedding(input_dim=10000, output_dim=128),
|
490 |
+
Conv1D(filters=64, kernel_size=3, activation='relu'),
|
491 |
+
MaxPooling1D(pool_size=2),
|
492 |
+
Dropout(0.5),
|
493 |
+
Conv1D(filters=128, kernel_size=3, activation='relu'),
|
494 |
+
MaxPooling1D(pool_size=2),
|
495 |
+
Flatten(),
|
496 |
+
Dense(64, activation='relu'),
|
497 |
+
Dropout(0.5),
|
498 |
+
Dense(1, activation='sigmoid')
|
499 |
+
])
|
500 |
+
|
501 |
+
# LSTM Architecture
|
502 |
+
Sequential([
|
503 |
+
Embedding(input_dim=10000, output_dim=128),
|
504 |
+
Bidirectional(LSTM(64, return_sequences=True)),
|
505 |
+
Dropout(0.5),
|
506 |
+
Bidirectional(LSTM(32)),
|
507 |
+
Dropout(0.5),
|
508 |
+
Dense(32, activation='relu'),
|
509 |
+
Dense(1, activation='sigmoid')
|
510 |
+
])
|
511 |
+
""")
|
512 |
+
|
513 |
+
st.markdown("### How It Works")
|
514 |
+
st.markdown("""
|
515 |
+
1. **Step 1:** Rule-based patterns scan for known SQL injection techniques
|
516 |
+
2. **Step 2:** Ensemble of 4 models evaluates the query structure
|
517 |
+
3. **Final Analysis:** Combined verdict from both approaches
|
518 |
+
""")
|
519 |
+
|
520 |
+
st.markdown("---")
|
521 |
+
st.warning("**Note:** This is a demonstration tool, not a replacement for proper security measures.")
|
522 |
+
|
523 |
+
# Footer
|
524 |
+
st.markdown("---")
|
525 |
+
st.markdown("""
|
526 |
+
<style>
|
527 |
+
.footer {
|
528 |
+
position: fixed;
|
529 |
+
left: 0;
|
530 |
+
bottom: 0;
|
531 |
+
width: 100%;
|
532 |
+
background-color: white;
|
533 |
+
color: black;
|
534 |
+
text-align: center;
|
535 |
+
padding: 10px;
|
536 |
+
border-top: 1px solid #e5e5e5;
|
537 |
+
}
|
538 |
+
</style>
|
539 |
+
<div class="footer">
|
540 |
+
<p>Developed with ❤️ using Streamlit | SQL Injection Detection System</p>
|
541 |
+
</div>
|
542 |
+
""", unsafe_allow_html=True)
|
cnn_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:782470a371d9c9464a7a59217351a2e2b4800d0149714d84bb3d4d946050698b
|
3 |
+
size 18261328
|
lstm_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:043aaf51992b65a67fcd4f63d26c065b9a53a5a0ff928e9266f4ef339e742347
|
3 |
+
size 17135816
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas==2.1.4
|
2 |
+
numpy==1.26.4
|
3 |
+
tensorflow==2.17.0
|
4 |
+
scikit-learn==1.2.2
|
rf_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:83d54671b38025d44fd595dd48f8cb8c1e0f6527b99d719389cffdfa7ee99de6
|
3 |
+
size 7197896
|
sql_injection_cnn.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:01e9a8f7523ca79a470358cca96b74cb97bd99397b4dec3ebe0f5d04dc0b6380
|
3 |
+
size 18105704
|
sql_tokenizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:24843be9335666b16e01e6b1063f488e08fded636e13374c5694213d800b3fc1
|
3 |
+
size 1116870
|
svm_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fe95f6dfd1704ae1cafa9a91c2768b7933a482520e2cfb887690afdc8f9f9282
|
3 |
+
size 234315
|
tfidf_vectorizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:80bf747b75db956d1d1dd7b00ff2c29dbb2fe935272d98499376a12a77613b53
|
3 |
+
size 2583307
|
tokenizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1c917ea2e95e0038bb6f4d7de90e3d6164f3458a42e357f7487e27aa9d0a6e9c
|
3 |
+
size 951873
|