S-Dreamer commited on
Commit
6662f88
·
verified ·
1 Parent(s): 70df493

Add 3 files

Browse files
Files changed (3) hide show
  1. README.md +6 -4
  2. index.html +287 -19
  3. prompts.txt +1 -0
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Databaseinjectionattacks
3
- emoji: 🦀
4
- colorFrom: yellow
5
  colorTo: yellow
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: databaseinjectionattacks
3
+ emoji: 🐳
4
+ colorFrom: pink
5
  colorTo: yellow
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
index.html CHANGED
@@ -1,19 +1,287 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Database Injection Attacks Explained</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
9
+ <style>
10
+ .code-block {
11
+ font-family: 'Courier New', monospace;
12
+ background-color: #2d2d2d;
13
+ color: #f8f8f2;
14
+ border-radius: 0.5rem;
15
+ padding: 1rem;
16
+ overflow-x: auto;
17
+ }
18
+ .tab-content {
19
+ display: none;
20
+ }
21
+ .tab-content.active {
22
+ display: block;
23
+ animation: fadeIn 0.3s ease-in-out;
24
+ }
25
+ @keyframes fadeIn {
26
+ from { opacity: 0; }
27
+ to { opacity: 1; }
28
+ }
29
+ .attack-card {
30
+ transition: all 0.3s ease;
31
+ }
32
+ .attack-card:hover {
33
+ transform: translateY(-5px);
34
+ box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
35
+ }
36
+ </style>
37
+ </head>
38
+ <body class="bg-gray-50 min-h-screen">
39
+ <div class="container mx-auto px-4 py-8">
40
+ <!-- Header -->
41
+ <header class="text-center mb-12">
42
+ <h1 class="text-4xl font-bold text-gray-800 mb-4">Database Injection Attacks</h1>
43
+ <p class="text-xl text-gray-600 max-w-3xl mx-auto">
44
+ Understanding SQL Injection and NoSQL Injection vulnerabilities and how to protect against them
45
+ </p>
46
+ </header>
47
+
48
+ <!-- Main Content -->
49
+ <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-12">
50
+ <!-- SQL Injection Card -->
51
+ <div class="bg-white rounded-xl shadow-md overflow-hidden attack-card">
52
+ <div class="p-6">
53
+ <div class="flex items-center mb-4">
54
+ <div class="bg-red-100 p-3 rounded-full mr-4">
55
+ <i class="fas fa-database text-red-500 text-xl"></i>
56
+ </div>
57
+ <h2 class="text-2xl font-bold text-gray-800">SQL Injection</h2>
58
+ </div>
59
+ <p class="text-gray-600 mb-4">
60
+ SQL Injection occurs when an attacker injects malicious SQL code into a web application's database query.
61
+ This can lead to unauthorized data access, modification, or deletion.
62
+ </p>
63
+ <div class="mb-4">
64
+ <h3 class="font-semibold text-gray-700 mb-2">Example Attack:</h3>
65
+ <div class="code-block mb-2">
66
+ SELECT * FROM users WHERE username = 'admin' --' AND password = 'anything'
67
+ </div>
68
+ <p class="text-sm text-gray-500">
69
+ The <span class="text-red-500">--</span> comments out the password check, allowing login as admin without a password.
70
+ </p>
71
+ </div>
72
+ <div class="mb-4">
73
+ <h3 class="font-semibold text-gray-700 mb-2">Privilege Escalation:</h3>
74
+ <div class="code-block mb-2">
75
+ '; UPDATE users SET role = 'admin' WHERE username = 'attacker' --
76
+ </div>
77
+ <p class="text-sm text-gray-500">
78
+ This modifies the database to give the attacker admin privileges.
79
+ </p>
80
+ </div>
81
+ <div class="mb-4">
82
+ <h3 class="font-semibold text-gray-700 mb-2">System Command Execution:</h3>
83
+ <div class="code-block mb-2">
84
+ '; DROP TABLE users; --
85
+ </div>
86
+ <p class="text-sm text-gray-500">
87
+ This deletes the entire users table (destructive attack).
88
+ </div>
89
+ </div>
90
+ <div class="bg-gray-50 px-6 py-4">
91
+ <h3 class="font-semibold text-gray-700 mb-2">Mitigation in Flask:</h3>
92
+ <div class="code-block text-sm">
93
+ # Use parameterized queries with SQLAlchemy<br>
94
+ user = db.session.execute(<br>
95
+ &nbsp;&nbsp;db.select(User).where(User.username == :username),<br>
96
+ &nbsp;&nbsp;{"username": request.form['username']}<br>
97
+ ).scalar_one()
98
+ </div>
99
+ </div>
100
+ </div>
101
+
102
+ <!-- NoSQL Injection Card -->
103
+ <div class="bg-white rounded-xl shadow-md overflow-hidden attack-card">
104
+ <div class="p-6">
105
+ <div class="flex items-center mb-4">
106
+ <div class="bg-blue-100 p-3 rounded-full mr-4">
107
+ <i class="fas fa-server text-blue-500 text-xl"></i>
108
+ </div>
109
+ <h2 class="text-2xl font-bold text-gray-800">NoSQL Injection</h2>
110
+ </div>
111
+ <p class="text-gray-600 mb-4">
112
+ NoSQL Injection targets NoSQL databases like MongoDB by injecting malicious queries or commands,
113
+ often bypassing authentication or extracting data.
114
+ </p>
115
+ <div class="mb-4">
116
+ <h3 class="font-semibold text-gray-700 mb-2">Example Attack:</h3>
117
+ <div class="code-block mb-2">
118
+ { "username": "admin", "password": {"$ne": ""} }
119
+ </div>
120
+ <p class="text-sm text-gray-500">
121
+ The <span class="text-blue-500">$ne</span> operator makes the query match any admin with non-empty password.
122
+ </p>
123
+ </div>
124
+ <div class="mb-4">
125
+ <h3 class="font-semibold text-gray-700 mb-2">Privilege Escalation:</h3>
126
+ <div class="code-block mb-2">
127
+ { "username": "attacker", "$set": { "role": "admin" } }
128
+ </div>
129
+ <p class="text-sm text-gray-500">
130
+ This updates the attacker's role to admin in MongoDB.
131
+ </p>
132
+ </div>
133
+ <div class="mb-4">
134
+ <h3 class="font-semibold text-gray-700 mb-2">System Command Execution:</h3>
135
+ <div class="code-block mb-2">
136
+ { "username": {"$gt": ""}, "$where": "function() { exec('rm -rf /'); }" }
137
+ </div>
138
+ <p class="text-sm text-gray-500">
139
+ JavaScript injection in MongoDB can lead to command execution (if enabled).
140
+ </p>
141
+ </div>
142
+ </div>
143
+ <div class="bg-gray-50 px-6 py-4">
144
+ <h3 class="font-semibold text-gray-700 mb-2">Mitigation in Flask:</h3>
145
+ <div class="code-block text-sm">
146
+ # Validate and sanitize all inputs<br>
147
+ from bson.regex import Regex<br>
148
+ username = Regex("^[a-zA-Z0-9_]+$")<br>
149
+ users.find_one({"username": username})
150
+ </div>
151
+ </div>
152
+ </div>
153
+ </div>
154
+
155
+ <!-- Comparison Section -->
156
+ <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12">
157
+ <div class="p-6">
158
+ <h2 class="text-2xl font-bold text-gray-800 mb-6">Key Differences</h2>
159
+ <div class="overflow-x-auto">
160
+ <table class="min-w-full divide-y divide-gray-200">
161
+ <thead class="bg-gray-50">
162
+ <tr>
163
+ <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Aspect</th>
164
+ <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">SQL Injection</th>
165
+ <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">NoSQL Injection</th>
166
+ </tr>
167
+ </thead>
168
+ <tbody class="bg-white divide-y divide-gray-200">
169
+ <tr>
170
+ <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">Database Type</td>
171
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">Relational (MySQL, PostgreSQL, etc.)</td>
172
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">NoSQL (MongoDB, CouchDB, etc.)</td>
173
+ </tr>
174
+ <tr>
175
+ <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">Query Syntax</td>
176
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">SQL language with statements</td>
177
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">JSON-like query documents</td>
178
+ </tr>
179
+ <tr>
180
+ <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">Injection Method</td>
181
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">String concatenation in SQL queries</td>
182
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">Operator injection in JSON queries</td>
183
+ </tr>
184
+ <tr>
185
+ <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">Common Attacks</td>
186
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">UNION attacks, stacked queries</td>
187
+ <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">Operator abuse ($where, $ne, $gt)</td>
188
+ </tr>
189
+ </tbody>
190
+ </table>
191
+ </div>
192
+ </div>
193
+ </div>
194
+
195
+ <!-- Interactive Demo Section -->
196
+ <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12">
197
+ <div class="p-6">
198
+ <h2 class="text-2xl font-bold text-gray-800 mb-6">Interactive Demo</h2>
199
+
200
+ <div class="flex border-b border-gray-200 mb-6">
201
+ <button class="tab-btn py-2 px-4 font-medium text-blue-600 border-b-2 border-blue-600" data-tab="sql">SQL Injection</button>
202
+ <button class="tab-btn py-2 px-4 font-medium text-gray-500 hover:text-blue-600" data-tab="nosql">NoSQL Injection</button>
203
+ </div>
204
+
205
+ <div id="sql" class="tab-content active">
206
+ <div class="mb-6">
207
+ <h3 class="text-lg font-semibold text-gray-700 mb-3">Vulnerable Login Form</h3>
208
+ <div class="bg-gray-100 p-4 rounded-lg mb-4">
209
+ <div class="mb-3">
210
+ <label class="block text-gray-700 mb-1">Username:</label>
211
+ <input type="text" id="sql-username" class="w-full px-3 py-2 border rounded" value="admin' --">
212
+ </div>
213
+ <div class="mb-3">
214
+ <label class="block text-gray-700 mb-1">Password:</label>
215
+ <input type="password" id="sql-password" class="w-full px-3 py-2 border rounded" value="anything">
216
+ </div>
217
+ <button onclick="runSqlDemo()" class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">Login</button>
218
+ </div>
219
+ <div class="code-block mb-2" id="sql-query">
220
+ SELECT * FROM users WHERE username = '[username]' AND password = '[password]'
221
+ </div>
222
+ <div id="sql-result" class="hidden mt-4 p-3 bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700">
223
+ <p>Login successful as admin!</p>
224
+ </div>
225
+ </div>
226
+ </div>
227
+
228
+ <div id="nosql" class="tab-content">
229
+ <div class="mb-6">
230
+ <h3 class="text-lg font-semibold text-gray-700 mb-3">Vulnerable Login Form</h3>
231
+ <div class="bg-gray-100 p-4 rounded-lg mb-4">
232
+ <div class="mb-3">
233
+ <label class="block text-gray-700 mb-1">Username:</label>
234
+ <input type="text" id="nosql-username" class="w-full px-3 py-2 border rounded" value="admin">
235
+ </div>
236
+ <div class="mb-3">
237
+ <label class="block text-gray-700 mb-1">Password:</label>
238
+ <input type="password" id="nosql-password" class="w-full px-3 py-2 border rounded" value="{$ne: \"\"}">
239
+ </div>
240
+ <button onclick="runNoSqlDemo()" class="bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700">Login</button>
241
+ </div>
242
+ <div class="code-block mb-2" id="nosql-query">
243
+ db.users.findOne({ username: "[username]", password: "[password]" })
244
+ </div>
245
+ <div id="nosql-result" class="hidden mt-4 p-3 bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700">
246
+ <p>Login successful as admin!</p>
247
+ </div>
248
+ </div>
249
+ </div>
250
+ </div>
251
+ </div>
252
+
253
+ <!-- Prevention Section -->
254
+ <div class="bg-white rounded-xl shadow-md overflow-hidden">
255
+ <div class="p-6">
256
+ <h2 class="text-2xl font-bold text-gray-800 mb-6">Prevention Best Practices</h2>
257
+
258
+ <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
259
+ <div class="bg-green-50 p-4 rounded-lg border border-green-200">
260
+ <div class="flex items-center mb-3">
261
+ <i class="fas fa-shield-alt text-green-500 mr-3"></i>
262
+ <h3 class="text-lg font-semibold text-gray-800">Input Validation</h3>
263
+ </div>
264
+ <p class="text-gray-600">
265
+ Validate all user inputs against strict whitelists of allowed characters and patterns.
266
+ Reject any input that doesn't match expected formats.
267
+ </p>
268
+ </div>
269
+
270
+ <div class="bg-green-50 p-4 rounded-lg border border-green-200">
271
+ <div class="flex items-center mb-3">
272
+ <i class="fas fa-lock text-green-500 mr-3"></i>
273
+ <h3 class="text-lg font-semibold text-gray-800">Parameterized Queries</h3>
274
+ </div>
275
+ <p class="text-gray-600">
276
+ Always use prepared statements or parameterized queries that separate SQL code from data.
277
+ Never concatenate user input directly into queries.
278
+ </p>
279
+ </div>
280
+
281
+ <div class="bg-green-50 p-4 rounded-lg border border-green-200">
282
+ <div class="flex items-center mb-3">
283
+ <i class="fas fa-user-shield text-green-500 mr-3"></i>
284
+ <h3 class="text-lg font-semibold text-gray-800">Least Privilege</h3>
285
+ </div>
286
+ <p class="text-gray
287
+ </html>
prompts.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ SQL Injection and NoSQL Injection are two types of attacks that target the database layer of an application. The main difference between them lies in the type of database they target. SQL Injection SQL Injection occurs when an attacker is able to inject malicious SQL code into a web application's database in order to extract or modify sensitive data. This happens when user input is not properly sanitized or validated, allowing an attacker to inject malicious SQL code. NoSQL Injection NoSQL Injection, on the other hand, targets NoSQL databases, such as MongoDB. It occurs when an attacker is able to inject malicious NoSQL queries or commands into a web application's database. This can happen when user input is not properly validated or sanitized, allowing an attacker to manipulate the NoSQL query. Key differences * Database type: SQL Injection targets relational databases (e.g., MySQL), while NoSQL Injection targets NoSQL databases (e.g., MongoDB). * Query syntax: SQL Injection involves injecting malicious SQL code, while NoSQL Injection involves injecting malicious NoSQL queries or commands. Mitigating SQL Injection in Flask API To mitigate SQL Injection in a Flask API, follow these best practices: 1. Use an ORM (Object-Relational Mapping) tool: Flask-SQLAlchemy is a popular ORM tool for Flask that abstracts the underlying SQL database and provides a safe way to interact with the database. 2. Use parameterized queries: Instead of directly injecting user input into SQL queries, use parameterized queries that separate the query logic from the user input. 3. Validate and sanitize user input: Always validate and sanitize user input to prevent malicious data from entering the database. Generate examples of privilege escalation and executing system commands