-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyze_crash.sh
More file actions
executable file
·471 lines (403 loc) · 14.8 KB
/
Copy pathanalyze_crash.sh
File metadata and controls
executable file
·471 lines (403 loc) · 14.8 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/bin/bash
# SSV Crash Analysis Script
# Allows re-running specific crashes to debug fuzzing issues
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Function to print colored output
print_color() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Function to list available targets with crashes
list_targets_with_crashes() {
print_color $BLUE "=== Targets with Crash Data ==="
echo
local found_crashes=0
local counter=1
if [[ -d "afl_workdir" ]]; then
for target_dir in afl_workdir/*/; do
if [[ -d "$target_dir" ]]; then
local target_name=$(basename "$target_dir")
local output_dir="${target_dir}output"
# Count crashes from all fuzzer instances (default, master, slave1, etc.)
local total_crashes=0
if [[ -d "$output_dir" ]]; then
for fuzzer_dir in "$output_dir"/*/; do
if [[ -d "$fuzzer_dir" ]]; then
local crash_dir="${fuzzer_dir}crashes"
if [[ -d "$crash_dir" ]]; then
local crash_count=$(find "$crash_dir" -name "id:*" -type f 2>/dev/null | wc -l)
total_crashes=$((total_crashes + crash_count))
fi
fi
done
fi
if [[ $total_crashes -gt 0 ]]; then
printf "%2d) %-40s (%d crashes)\n" $counter "$target_name" $total_crashes
((counter++))
found_crashes=1
fi
fi
done
fi
if [[ $found_crashes -eq 0 ]]; then
print_color $YELLOW "No targets with crashes found."
print_color $BLUE "Run some fuzzing first with: ./run_fuzzer.sh"
return 1
fi
echo
return 0
}
# Function to get target by number
get_target_by_number() {
local choice=$1
local counter=1
if [[ -d "afl_workdir" ]]; then
for target_dir in afl_workdir/*/; do
if [[ -d "$target_dir" ]]; then
local target_name=$(basename "$target_dir")
local output_dir="${target_dir}output"
# Check if target has any crashes from all fuzzer instances
local total_crashes=0
if [[ -d "$output_dir" ]]; then
for fuzzer_dir in "$output_dir"/*/; do
if [[ -d "$fuzzer_dir" ]]; then
local crash_dir="${fuzzer_dir}crashes"
if [[ -d "$crash_dir" ]]; then
local crash_count=$(find "$crash_dir" -name "id:*" -type f 2>/dev/null | wc -l)
total_crashes=$((total_crashes + crash_count))
fi
fi
done
fi
if [[ $total_crashes -gt 0 ]]; then
if [[ $counter -eq $choice ]]; then
echo "$target_name"
return 0
fi
((counter++))
fi
fi
done
fi
return 1
}
# Function to list crashes for a specific target
list_crashes() {
local target=$1
local output_dir="afl_workdir/${target}/output"
if [[ ! -d "$output_dir" ]]; then
print_color $RED "No output directory found for target: $target"
return 1
fi
print_color $GREEN "=== Crashes for $target ==="
echo
# Collect all crashes from all fuzzer instances
local all_crashes=()
local fuzzer_sources=()
for fuzzer_dir in "$output_dir"/*/; do
if [[ -d "$fuzzer_dir" ]]; then
local fuzzer_name=$(basename "$fuzzer_dir")
local crash_dir="${fuzzer_dir}crashes"
if [[ -d "$crash_dir" ]]; then
local fuzzer_crashes=($(find "$crash_dir" -name "id:*" -type f | sort))
for crash_file in "${fuzzer_crashes[@]}"; do
all_crashes+=("$crash_file")
fuzzer_sources+=("$fuzzer_name")
done
fi
fi
done
if [[ ${#all_crashes[@]} -eq 0 ]]; then
print_color $YELLOW "No crashes found for $target"
return 1
fi
# Display crashes with fuzzer source information
local counter=1
for i in "${!all_crashes[@]}"; do
local crash_file="${all_crashes[$i]}"
local fuzzer_source="${fuzzer_sources[$i]}"
local filename=$(basename "$crash_file")
local filesize=$(stat -c%s "$crash_file" 2>/dev/null || echo "unknown")
printf "%2d) %-40s [%s] (%s bytes)\n" $counter "$filename" "$fuzzer_source" "$filesize"
((counter++))
done
echo
print_color $CYAN "Total crashes: ${#all_crashes[@]}"
print_color $BLUE "Crashes found in fuzzer instances: $(printf '%s ' "${fuzzer_sources[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
return 0
}
# Function to analyze a specific crash
analyze_crash() {
local target=$1
local crash_number=$2
local output_dir="afl_workdir/${target}/output"
# Collect all crashes from all fuzzer instances (same logic as list_crashes)
local all_crashes=()
for fuzzer_dir in "$output_dir"/*/; do
if [[ -d "$fuzzer_dir" ]]; then
local crash_dir="${fuzzer_dir}crashes"
if [[ -d "$crash_dir" ]]; then
local fuzzer_crashes=($(find "$crash_dir" -name "id:*" -type f | sort))
for crash_file in "${fuzzer_crashes[@]}"; do
all_crashes+=("$crash_file")
done
fi
fi
done
if [[ $crash_number -lt 1 || $crash_number -gt ${#all_crashes[@]} ]]; then
print_color $RED "Invalid crash number. Please select 1-${#all_crashes[@]}"
return 1
fi
local crash_file="${all_crashes[$((crash_number-1))]}"
local filename=$(basename "$crash_file")
print_color $PURPLE "=== Analyzing Crash: $filename ==="
echo
# Display crash file info
print_color $BLUE "📄 Crash File Information:"
echo " File: $crash_file"
echo " Size: $(stat -c%s "$crash_file") bytes"
echo " Modified: $(stat -c%y "$crash_file")"
echo
# Display hex dump of crash input
print_color $BLUE "🔍 Crash Input (hex dump - first 256 bytes):"
hexdump -C "$crash_file" | head -16
echo
# Display raw bytes if small enough
local filesize=$(stat -c%s "$crash_file")
if [[ $filesize -le 1024 ]]; then
print_color $BLUE "📝 Raw Input (if printable):"
if file "$crash_file" | grep -q "text"; then
cat "$crash_file"
echo
else
print_color $YELLOW " (Binary data - see hex dump above)"
fi
echo
fi
# Check if binary exists
local binary_path="target/debug/${target}"
if [[ ! -f "$binary_path" ]]; then
print_color $YELLOW "⚠️ Binary not found: $binary_path"
print_color $BLUE "Building target first..."
if ! cargo afl build --bin "$target"; then
print_color $RED "✗ Failed to build target"
return 1
fi
print_color $GREEN "✓ Target built successfully"
fi
echo
print_color $PURPLE "🚀 Reproducing Crash..."
print_color $YELLOW "Running: $binary_path < $crash_file"
echo "--- CRASH OUTPUT ---"
# Run the binary with crash input and capture output
if timeout 10s "$binary_path" < "$crash_file" 2>&1; then
print_color $YELLOW "⚠️ Program completed without crashing (timeout or fixed?)"
else
local exit_code=$?
if [[ $exit_code -eq 124 ]]; then
print_color $RED "⚠️ Program timed out after 10 seconds"
else
print_color $RED "💥 Program crashed with exit code: $exit_code"
fi
fi
echo "--- END CRASH OUTPUT ---"
echo
}
# Function to run crash with debugger
debug_crash() {
local target=$1
local crash_number=$2
local output_dir="afl_workdir/${target}/output"
# Collect all crashes from all fuzzer instances (same logic as analyze_crash)
local all_crashes=()
for fuzzer_dir in "$output_dir"/*/; do
if [[ -d "$fuzzer_dir" ]]; then
local crash_dir="${fuzzer_dir}crashes"
if [[ -d "$crash_dir" ]]; then
local fuzzer_crashes=($(find "$crash_dir" -name "id:*" -type f | sort))
for crash_file in "${fuzzer_crashes[@]}"; do
all_crashes+=("$crash_file")
done
fi
fi
done
local crash_file="${all_crashes[$((crash_number-1))]}"
local binary_path="target/debug/${target}"
print_color $PURPLE "🐛 Running crash in GDB debugger..."
print_color $YELLOW "Commands you can use in GDB:"
echo " run < $crash_file # Run the program with crash input"
echo " bt # Show backtrace after crash"
echo " info registers # Show register state"
echo " quit # Exit debugger"
echo
# Check if gdb is available
if ! command -v gdb &> /dev/null; then
print_color $RED "GDB not found. Install with: sudo apt install gdb"
return 1
fi
gdb "$binary_path"
}
# Function to save crash for manual analysis
save_crash_for_analysis() {
local target=$1
local crash_number=$2
local output_dir="afl_workdir/${target}/output"
# Collect all crashes from all fuzzer instances (same logic as analyze_crash)
local all_crashes=()
local fuzzer_sources=()
for fuzzer_dir in "$output_dir"/*/; do
if [[ -d "$fuzzer_dir" ]]; then
local fuzzer_name=$(basename "$fuzzer_dir")
local crash_dir="${fuzzer_dir}crashes"
if [[ -d "$crash_dir" ]]; then
local fuzzer_crashes=($(find "$crash_dir" -name "id:*" -type f | sort))
for crash_file in "${fuzzer_crashes[@]}"; do
all_crashes+=("$crash_file")
fuzzer_sources+=("$fuzzer_name")
done
fi
fi
done
local crash_file="${all_crashes[$((crash_number-1))]}"
local fuzzer_source="${fuzzer_sources[$((crash_number-1))]}"
local filename=$(basename "$crash_file")
# Create analysis directory
local analysis_dir="crash_analysis/${target}"
mkdir -p "$analysis_dir"
local saved_file="${analysis_dir}/${filename}"
cp "$crash_file" "$saved_file"
# Create analysis report
local report_file="${analysis_dir}/${filename}.analysis"
cat > "$report_file" << EOF
Crash Analysis Report
====================
Target: $target
Crash File: $filename
Fuzzer Source: $fuzzer_source (multi-core instance)
Original Location: $crash_file
Analysis Date: $(date)
File Size: $(stat -c%s "$crash_file") bytes
Hex Dump:
$(hexdump -C "$crash_file" | head -32)
Binary Info:
$(file "$crash_file")
To reproduce:
./target/debug/$target < $saved_file
To debug:
gdb ./target/debug/$target
(gdb) run < $saved_file
Note: This crash was found during multi-core fuzzing session.
EOF
print_color $GREEN "✓ Crash saved for analysis:"
print_color $BLUE " Input file: $saved_file"
print_color $BLUE " Report: $report_file"
}
# Main menu function
show_crash_menu() {
local target=$1
while true; do
print_color $BLUE "=== Crash Analysis for $target ==="
echo "1) List all crashes"
echo "2) Analyze specific crash"
echo "3) Debug crash with GDB"
echo "4) Save crash for manual analysis"
echo "5) Back to target selection"
echo
read -p "Choose action (1-5): " action
case $action in
1)
list_crashes "$target"
echo
read -p "Press Enter to continue..."
;;
2)
if list_crashes "$target"; then
echo
read -p "Select crash number to analyze: " crash_num
if [[ "$crash_num" =~ ^[0-9]+$ ]]; then
analyze_crash "$target" "$crash_num"
else
print_color $RED "Invalid number"
fi
fi
echo
read -p "Press Enter to continue..."
;;
3)
if list_crashes "$target"; then
echo
read -p "Select crash number to debug: " crash_num
if [[ "$crash_num" =~ ^[0-9]+$ ]]; then
debug_crash "$target" "$crash_num"
else
print_color $RED "Invalid number"
fi
fi
;;
4)
if list_crashes "$target"; then
echo
read -p "Select crash number to save: " crash_num
if [[ "$crash_num" =~ ^[0-9]+$ ]]; then
save_crash_for_analysis "$target" "$crash_num"
else
print_color $RED "Invalid number"
fi
fi
echo
read -p "Press Enter to continue..."
;;
5)
break
;;
*)
print_color $RED "Invalid action. Please select 1-5."
;;
esac
done
}
# Main script logic
main() {
print_color $CYAN "🔍 SSV Crash Analysis Tool"
echo
# Check if we're in the right directory
if [[ ! -f "Cargo.toml" || ! -d "fuzz" ]]; then
print_color $RED "Error: This script must be run from the ssv-fuzz project root"
exit 1
fi
while true; do
if ! list_targets_with_crashes; then
exit 0
fi
read -p "Select target number (or 'q' to quit): " choice
if [[ "$choice" == "q" || "$choice" == "Q" ]]; then
print_color $GREEN "Goodbye!"
exit 0
fi
if [[ "$choice" =~ ^[0-9]+$ ]]; then
target=$(get_target_by_number "$choice")
if [[ $? -eq 0 ]]; then
show_crash_menu "$target"
else
print_color $RED "Invalid target number"
echo
read -p "Press Enter to continue..."
fi
else
print_color $RED "Invalid input. Enter a number or 'q' to quit."
echo
read -p "Press Enter to continue..."
fi
done
}
# Run main function
main "$@"