Skip to content

Instantly share code, notes, and snippets.

@aayla-secura
Created April 23, 2021 01:11
Show Gist options
  • Save aayla-secura/483276b4a886a28825f4353f3279b5ee to your computer and use it in GitHub Desktop.
Save aayla-secura/483276b4a886a28825f4353f3279b5ee to your computer and use it in GitHub Desktop.
Generate a hashcat rule file to prepend {username}: to every password
#!/bin/bash
usage() {
cat <<EOF
Takes a list of usernames a writes a rule file to prepend each of these to each password canditate.
${BASH_SOURCE[0]} <options> <username or file> [<username or file> ...]
Options:
-@ Also take the base username if full one is an email
-t Test the rules when done. Requires an output file
-s CHAR Append the given character (or string) after the username. Default is :
-S CHAR Split each input at the given character. Default is newline.
If the input file contains passwords too, set this to the separator
-o FILE Save output to FILE. Default is stdout
EOF
exit 1
}
print_array() {
local IFS=$'\n' arr="${1}[*]" elem="$2"
echo "${!arr}"
}
in_array() {
local IFS=$'\n' arr="${1}[*]" elem="$2"
print_array "$arr" | fgrep -qx "$elem"
}
add_uname() {
local uname="${1%%${IN_SEP}*}"
if ! in_array USERNAMES "$uname" ; then
USERNAMES+=("$uname")
fi
if [ $DO_SPLIT -eq 1 -a "${uname/@/}" != "$uname" ] ; then
add_uname "${uname%@*}"
fi
}
add_unames_from_file() {
local fname="$1" line
while IFS= read line ; do
add_uname "$line"
done < "$fname"
}
process_unames() {
local a
for a in "$@" ; do
if [ -f "$a" ] ; then
add_unames_from_file "$a"
else
add_uname "$a"
fi
done
}
output_rules() {
local u
print_array USERNAMES | sed "s/\$/${OUT_SEP}/" | rev \
| sed 's/./ ^\0/g;s/^ //' > "$OUTPUT"
}
USERNAMES=()
ARGS=()
DO_SPLIT=0
DO_TEST=0
IN_SEP=$'\n'
OUT_SEP=:
OUTPUT=/dev/stdout
while [ $# -gt 0 ] ; do
case "$1" in
-@)
DO_SPLIT=1
;;
-t)
DO_TEST=1
;;
-o)
OUTPUT="$2"
shift
;;
-s)
OUT_SEP="$2"
shift
;;
-S)
IN_SEP="$2"
shift
;;
-*)
usage
;;
*)
ARGS+=("$1")
;;
esac
shift
done
process_unames "${ARGS[@]}"
output_rules
echo "Give this rule file as the first rule file to hashcat" >&2
if [ $DO_TEST -eq 1 -a -f "$OUTPUT" ] ; then
echo -e "Testing:\n" >&2
temp="$(mktemp)"
echo test > "$temp"
hashcat --stdout -r "$OUTPUT" "$temp"
rm "$temp"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment