Last active
May 7, 2023 04:55
-
-
Save IcyApril/56c3fdacb3a640f37c245e5813b98b99 to your computer and use it in GitHub Desktop.
This file contains 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 | |
echo -n Password: | |
read -s password | |
echo | |
hash="$(echo -n $password | openssl sha1)" | |
upperCase="$(echo $hash | tr '[a-z]' '[A-Z]')" | |
prefix="${upperCase:0:5}" | |
response=$(curl -s https://api.pwnedpasswords.com/range/$prefix) | |
while read -r line; do | |
lineOriginal="$prefix$line" | |
if [ "${lineOriginal:0:40}" == "$upperCase" ]; then | |
echo "Password breached." | |
exit 1 | |
fi | |
done <<< "$response" | |
echo "Password not found in breached database." | |
exit 0 |
For fun, a two-line version (after input checking) that uses AWK for the dirty work:
#!/bin/sh
if [ -z "$1" ]
then
echo "Usage: ${0##*/} <password>"
exit 1
fi
HASH="$(printf "$1" | openssl sha1)"
curl -s "https://api.pwnedpasswords.com/range/${HASH:0:5}" |
awk -F":" -v SUFFIX="${HASH:5}" '$1 == toupper(SUFFIX) { print $2 }'
@croose using your version I get the following error Bad substitution
@stephane-chazelas yours worked perfectly - thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are a number of issues with that script:
without
-r
andIFS=
,read
would fail to preserve leading andtrailing space or tab characters in the input or backslashes.
See
https://unix.stackexchange.com/questions/209123/understanding-ifs-read-r-line
For instance, it would say that "test\123" is breached.
with
echo -n
, that would not work properly for passwords like-nenene
, and possibly (depending on the environment) some thatcontain backslashes. See
https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo
leaving a variable unquoted has a very special meaning in
shells like bash. See
https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells
With
echo $password
, $password would undergo split+glob. Sofor instance, it could say that the "***" password is not
breached, because
echo -n $password
would output the list ofnon-hidden files in the current directory.
On my system,
see the leading "(stdin)= " which needs to be removed
tr '[a-z]' '[A-Z]'
only makes sense in the POSIX/C locale.There's not much guarantee what you'll get in other locales.
tr '[:lower:]' '[:upper:]'
ortr abcdef ABCDEF
are better forthat. Recent versions of bash now also have builtin operators
for case conversion (
hash=${hash^^}
)How about: