-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.sh
More file actions
361 lines (198 loc) · 6.08 KB
/
commands.sh
File metadata and controls
361 lines (198 loc) · 6.08 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env bash
# Lab 27 - Exploit Fix Test
# Commands executed during the lab
# Install Required Tools
sudo apt update
sudo apt install -y python3 python3-pip python3-venv git sqlite3
mkdir -p ~/exploit-fix-lab
cd ~/exploit-fix-lab
python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade pip
pip install flask pytest requests pytest-cov bandit pip-audit
# Create dependency file used later in troubleshooting and CI checks
nano ~/exploit-fix-lab/requirements.txt
# Create a simple web application with a known SQL injection vulnerability
cd ~/exploit-fix-lab
nano vulnerable_app.py
# Quick syntax verification
python3 -m py_compile vulnerable_app.py
echo $?
# Step 2: Create Exploit Script
nano ~/exploit-fix-lab/exploit_test.py
# Syntax verification
python3 -m py_compile exploit_test.py
echo $?
# Terminal 1: Start the vulnerable application
cd ~/exploit-fix-lab
source venv/bin/activate
python3 vulnerable_app.py
# Terminal 2: Run exploit
cd ~/exploit-fix-lab
source venv/bin/activate
python3 exploit_test.py
# Document your findings
nano ~/exploit-fix-lab/exploit_report.md
# Steps to Reproduce
python3 vulnerable_app.py
```
2. Run the exploit test:
```bash
python3 exploit_test.py
```
3. Observe that the following payload bypasses authentication:
```json
{
"username": "admin' OR '1'='1",
"password": "anything"
}
```
4. The application returns a successful login response even though the password is invalid.
## Observed Result
The vulnerable application returned HTTP 200 and granted access to the admin user.
## Root Cause
User input was directly concatenated into the SQL query instead of using parameterized queries.
# Root Cause
cat exploit_report.md
# Steps to Reproduce
python3 vulnerable_app.py
```
2. Run the exploit test:
```bash
python3 exploit_test.py
```
3. Observe that the following payload bypasses authentication:
```json
{
"username": "admin' OR '1'='1",
"password": "anything"
}
```
4. The application returns a successful login response even though the password is invalid.
## Observed Result
The vulnerable application returned HTTP 200 and granted access to the admin user.
## Root Cause
User input was directly concatenated into the SQL query instead of using parameterized queries.
# Step 1: Create Secure Version
nano ~/exploit-fix-lab/secure_app.py
# Syntax verification
python3 -m py_compile secure_app.py
echo $?
# Step 2: Implement Input Validation
python3 - <<'PY'
from secure_app import validate_input
tests = [
("admin", "admin123"),
("ab", "admin123"),
("admin' OR '1'='1", "anything"),
("normal_user", "short"),
]
for username, password in tests:
print((username, password), "=>", validate_input(username, password))
PY
# Stop vulnerable app
^C
# Start secure app
cd ~/exploit-fix-lab
source venv/bin/activate
python3 secure_app.py
# In Terminal 2, test that exploit no longer works
cd ~/exploit-fix-lab
source venv/bin/activate
BASE_URL=http://localhost:5001 python3 exploit_test.py
# Additional realistic direct API verification of the fix
curl -i -X POST http://localhost:5001/login \
-H "Content-Type: application/json" \
-d "{\"username\":\"admin' OR '1'='1\",\"password\":\"anything\"}"
# Step 1: Create Comprehensive Test Suite
nano ~/exploit-fix-lab/test_security.py
# Syntax verification
python3 -m py_compile test_security.py
echo $?
# Run all tests
cd ~/exploit-fix-lab
source venv/bin/activate
pytest test_security.py -v
# Run with coverage report
pytest test_security.py --cov=secure_app --cov-report=html
# Verify coverage artifacts
ls -la htmlcov | head
# Step 3: Create CI/CD Integration Script
nano ~/exploit-fix-lab/run_security_tests.sh
# Security Testing Script for CI/CD Pipeline
chmod +x ~/exploit-fix-lab/run_security_tests.sh
cd ~/exploit-fix-lab
./run_security_tests.sh
# Verify generated CI artifacts
ls -l test-results.xml security-report.json
# Inspect Bandit JSON report
sed -n '1,80p' security-report.json
# Check exploit documentation exists
cd ~/exploit-fix-lab
cat exploit_report.md
# Steps to Reproduce
python3 vulnerable_app.py
```
2. Run the exploit test:
```bash
python3 exploit_test.py
```
3. Observe that the following payload bypasses authentication:
```json
{
"username": "admin' OR '1'='1",
"password": "anything"
}
```
4. The application returns a successful login response even though the password is invalid.
## Observed Result
The vulnerable application returned HTTP 200 and granted access to the admin user.
## Root Cause
User input was directly concatenated into the SQL query instead of using parameterized queries.
# Verify vulnerable app demonstrates the issue
python3 exploit_test.py
# Start the secure app
python3 secure_app.py
# Test secure app rejects SQL injection
curl -i -X POST http://localhost:5001/login \
-H "Content-Type: application/json" \
-d "{\"username\":\"admin' OR '1'='1\",\"password\":\"anything\"}"
# You can also run the exploit script against the secure app
BASE_URL=http://localhost:5001 python3 exploit_test.py
# All tests should pass
cd ~/exploit-fix-lab
source venv/bin/activate
pytest test_security.py -v
# Check test coverage
pytest test_security.py --cov=secure_app
# Verify test results file created
ls -l test-results.xml
# Create Security Checklist
nano ~/exploit-fix-lab/security_checklist.md
# Security Checklist
cat security_checklist.md
# Find process using port
sudo lsof -i :5000
sudo lsof -i :5001
# Kill process
sudo kill -9 8124
sudo kill -9 8397
sudo lsof -i :5000
sudo lsof -i :5001
# Remove database files and reinitialize
cd ~/exploit-fix-lab
rm -f users.db users_secure.db
python3 secure_app.py
# Manual process check
ps aux | grep python
# Re-run automated tests without manually relying on the server
pytest test_security.py -v
# Verify virtual environment is activated
which python3
source ~/exploit-fix-lab/venv/bin/activate
which python3
# Reinstall dependencies
cd ~/exploit-fix-lab
pip install -r requirements.txt
# Final project snapshot
ls -la