Skip to content

Commit aa10810

Browse files
committed
tools: add db cleaner
1 parent 09121e9 commit aa10810

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

tools/clean.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
#
3+
# Wifite2 Cracked Database Cleaner
4+
# Removes duplicate entries from cracked.json based on BSSID
5+
#
6+
# Usage: ./clean.sh [cracked.json]
7+
#
8+
9+
set -e
10+
11+
# Colors for output
12+
RED='\033[0;31m'
13+
GREEN='\033[0;32m'
14+
YELLOW='\033[1;33m'
15+
BLUE='\033[0;34m'
16+
NC='\033[0m' # No Color
17+
18+
# Default file location (relative to tools directory)
19+
CRACKED_FILE="${1:-../cracked.json}"
20+
21+
# Check if file exists
22+
if [ ! -f "$CRACKED_FILE" ]; then
23+
echo -e "${RED}[!]${NC} Error: File '$CRACKED_FILE' not found"
24+
echo -e "${BLUE}[?]${NC} Usage: $0 [cracked.json]"
25+
exit 1
26+
fi
27+
28+
# Check if jq is installed
29+
if ! command -v jq &> /dev/null; then
30+
echo -e "${RED}[!]${NC} Error: 'jq' is required but not installed"
31+
echo -e "${BLUE}[?]${NC} Install with: sudo apt install jq"
32+
exit 1
33+
fi
34+
35+
# Validate JSON format
36+
if ! jq empty "$CRACKED_FILE" 2>/dev/null; then
37+
echo -e "${RED}[!]${NC} Error: '$CRACKED_FILE' is not valid JSON"
38+
exit 1
39+
fi
40+
41+
# Create backup
42+
BACKUP_FILE="${CRACKED_FILE}.backup.$(date +%Y%m%d_%H%M%S)"
43+
cp "$CRACKED_FILE" "$BACKUP_FILE"
44+
echo -e "${GREEN}[+]${NC} Created backup: $BACKUP_FILE"
45+
46+
# Count original entries
47+
ORIGINAL_COUNT=$(jq 'length' "$CRACKED_FILE")
48+
echo -e "${BLUE}[*]${NC} Original entries: $ORIGINAL_COUNT"
49+
50+
# Remove duplicates based on BSSID (keep the most recent entry)
51+
# Sort by date descending, then use unique_by to keep first occurrence (most recent)
52+
jq 'sort_by(.date) | reverse | unique_by(.bssid)' "$CRACKED_FILE" > "${CRACKED_FILE}.tmp"
53+
54+
# Count cleaned entries
55+
CLEANED_COUNT=$(jq 'length' "${CRACKED_FILE}.tmp")
56+
REMOVED_COUNT=$((ORIGINAL_COUNT - CLEANED_COUNT))
57+
58+
# Show results
59+
echo -e "${BLUE}[*]${NC} Cleaned entries: $CLEANED_COUNT"
60+
61+
if [ $REMOVED_COUNT -gt 0 ]; then
62+
echo -e "${YELLOW}[!]${NC} Removed $REMOVED_COUNT duplicate(s)"
63+
64+
# Show which BSSIDs had duplicates
65+
echo -e "${BLUE}[*]${NC} Duplicate BSSIDs removed:"
66+
jq -r '.[].bssid' "$CRACKED_FILE" | sort | uniq -d | while read -r bssid; do
67+
ESSID=$(jq -r ".[] | select(.bssid == \"$bssid\") | .essid" "$CRACKED_FILE" | head -1)
68+
echo -e " ${YELLOW}${NC} $bssid ($ESSID)"
69+
done
70+
71+
# Replace original file
72+
mv "${CRACKED_FILE}.tmp" "$CRACKED_FILE"
73+
echo -e "${GREEN}[+]${NC} Successfully cleaned $CRACKED_FILE"
74+
echo -e "${GREEN}[+]${NC} Kept most recent entry for each BSSID"
75+
else
76+
echo -e "${GREEN}[+]${NC} No duplicates found - file is already clean"
77+
rm "${CRACKED_FILE}.tmp"
78+
fi
79+
80+
echo -e "${BLUE}[*]${NC} Done!"

0 commit comments

Comments
 (0)