#!/bin/bash
echo "Script for educational purposes only. Only use on networks you are authorized to access."
echo -e "Do you want to continue? (y/n)"
read answer
if [ "$answer" = "y" ]; then
    echo "Continuing."
else
    exit 1
fi
sudo find  /var/cache/shill -name default.profile -exec cp {} /tmp/ \;
sudo chmod 644 /tmp/default.profile
echo "Passwords found in shill profile: "
DEBUG=${DEBUG:-0}
PASSWD_FILE=/tmp/default.profile

if [ "$DEBUG" -eq 1 ]; then
    grep -E 'Name=|Passphrase=' "$PASSWD_FILE"
fi

grep -E 'Name=|Passphrase=' "$PASSWD_FILE" | while IFS= read -r line; do
    if echo "$line" | grep -q '^Name='; then
        echo "$line"
        continue
    fi

    passphrase="${line#*=}"
    if echo "$passphrase" | grep -q '^rot47:'; then
        encoded="${passphrase#rot47:}"
        # Old ChromeOS format: WiFi.Passphrase=rot47:<encoded>
        decoded="$(echo "$encoded" | tr '!-~' 'P-~!-O')"
        echo "WiFi.Passphrase=$decoded" | grep --color '.*'
    else
        # Newer ChromeOS format: WiFi.Passphrase is stored directly.
        echo "$line" | grep --color '.*'
    fi
done
