Last active
November 24, 2025 16:20
-
-
Save jayarc/645a1d72aeee84f824348a705c517aec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Script to decode a single base64-encoded file | |
| # Usage: ./decode_file.sh <file_path> | |
| if [ $# -eq 0 ]; then | |
| echo "Error: No file specified" | |
| echo "Usage: $0 <file_path>" | |
| exit 1 | |
| fi | |
| encoded_file="$1" | |
| # Check if file exists | |
| if [ ! -f "$encoded_file" ]; then | |
| echo "Error: File '$encoded_file' does not exist" | |
| exit 1 | |
| fi | |
| echo "Starting file decoding..." | |
| echo "File: $encoded_file" | |
| echo "----------------------------------------" | |
| # Function to check if a file contains valid JSON | |
| is_valid_json() { | |
| local file="$1" | |
| if [ -f "$file" ] && python3 -m json.tool "$file" >/dev/null 2>&1; then | |
| return 0 | |
| else | |
| return 1 | |
| fi | |
| } | |
| # Function to try base64 decoding with validation | |
| try_base64_decode() { | |
| local input_file="$1" | |
| local output_file="$2" | |
| local level="$3" | |
| if base64 -d "$input_file" > "$output_file" 2>/dev/null; then | |
| if is_valid_json "$output_file"; then | |
| echo "✓ Valid JSON found at $level decode level" | |
| return 0 | |
| else | |
| # Check if it looks like more base64 | |
| if head -c 100 "$output_file" | grep -q "^[A-Za-z0-9+/]*$"; then | |
| echo "→ Appears to be more base64 at $level decode level" | |
| return 1 # Not final result, but might be valid for next level | |
| else | |
| echo "→ Invalid JSON at $level decode level" | |
| return 2 # Failed | |
| fi | |
| fi | |
| else | |
| echo "→ Base64 decode failed at $level decode level" | |
| return 2 # Failed | |
| fi | |
| } | |
| # Extract directory and filename | |
| file_dir=$(dirname "$encoded_file") | |
| file_name=$(basename "$encoded_file") | |
| file_base="${file_name%.*}" | |
| file_ext="${file_name##*.}" | |
| # Generate output filename: original-decoded.json | |
| if [ "$file_base" = "$file_name" ]; then | |
| # No extension | |
| decoded_file="$file_dir/${file_base}-decoded" | |
| else | |
| decoded_file="$file_dir/${file_base}-decoded.${file_ext}" | |
| fi | |
| # Check if decoded file already exists | |
| if [ -f "$decoded_file" ]; then | |
| echo "⏭ Decoded file already exists: $decoded_file" | |
| echo "Delete it first if you want to re-decode." | |
| exit 0 | |
| fi | |
| # Show file size and first few characters for debugging | |
| file_size=$(wc -c < "$encoded_file") | |
| echo "File size: $file_size bytes" | |
| # Show first 50 characters for debugging | |
| first_chars=$(head -c 50 "$encoded_file") | |
| echo "First 50 chars: $first_chars" | |
| decode_success=false | |
| # Method 1: Check if it's already valid JSON | |
| if is_valid_json "$encoded_file"; then | |
| echo "✓ File is already valid JSON (no decoding needed)" | |
| cp "$encoded_file" "$decoded_file" | |
| decode_success=true | |
| else | |
| # Method 2: Try progressive base64 decoding | |
| # Use a unique temp file name based on the file path | |
| temp_id=$(echo "$encoded_file" | tr '/' '_' | tr ' ' '_') | |
| temp1="/tmp/decode_${temp_id}_1" | |
| temp2="/tmp/decode_${temp_id}_2" | |
| temp3="/tmp/decode_${temp_id}_3" | |
| # First decode | |
| if try_base64_decode "$encoded_file" "$temp1" "first"; then | |
| decode_success=true | |
| cp "$temp1" "$decoded_file" | |
| else | |
| decode_result=$? | |
| if [ $decode_result -eq 1 ]; then | |
| # Second decode | |
| if try_base64_decode "$temp1" "$temp2" "second"; then | |
| decode_success=true | |
| cp "$temp2" "$decoded_file" | |
| else | |
| decode_result=$? | |
| if [ $decode_result -eq 1 ]; then | |
| # Third decode | |
| if try_base64_decode "$temp2" "$temp3" "third"; then | |
| decode_success=true | |
| cp "$temp3" "$decoded_file" | |
| fi | |
| fi | |
| fi | |
| fi | |
| fi | |
| # Clean up temp files | |
| rm -f "$temp1" "$temp2" "$temp3" | |
| fi | |
| if [ "$decode_success" = true ]; then | |
| echo "✓ Successfully decoded file" | |
| echo "Output: $decoded_file" | |
| exit 0 | |
| else | |
| echo "✗ Failed to decode file (all methods failed)" | |
| exit 1 | |
| fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment