Skip to content

Instantly share code, notes, and snippets.

@time-wcrp
Last active March 4, 2021 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save time-wcrp/4e759ee975baf7873e128fe7597876f4 to your computer and use it in GitHub Desktop.
Save time-wcrp/4e759ee975baf7873e128fe7597876f4 to your computer and use it in GitHub Desktop.
Anonymous for MacOS (Modify: Mac Address & Computer Name)
OUTPUT_MAC_ADDRESS=""
OUTPUT_COMPUTER_NAME=""
function IsExistingInterface() {
OUTPUT=$(echo $(ifconfig $1 2>&1))
if [[ ! "$OUTPUT" == *"does not exist"* ]]; then
true
else
false
fi
}
function GetDefaultMacAddress() {
echo $(networksetup -getmacaddress $1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
}
function GetCurrentMacAddress() {
echo $(ifconfig $1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
}
function RandomMacAddress() {
echo $(hexdump -n 6 -ve '1/1 "%.2x "' /dev/random | awk -v a="2,6,a,e" -v r="$RANDOM" 'BEGIN{srand(r);}NR==1{split(a,b,",");r=int(rand()*4+1);printf "%s%s:%s:%s:%s:%s:%s\n",substr($1,0,1),b[r],$2,$3,$4,$5,$6}')
}
function ChangeMacAddress() {
if IsExistingInterface $1; then
IS_SET=false
if [[ "$2" == "random" ]]; then
NEW_MAC_ADDRESS=$(RandomMacAddress)
else
IS_SET=true
NEW_MAC_ADDRESS=$2
fi
DEFAULT_MAC_ADDRESS=$(GetDefaultMacAddress $1)
CURRENT_MAC_ADDRESS=$(GetCurrentMacAddress $1)
echo -e "Default Mac Address:\t$DEFAULT_MAC_ADDRESS"
echo -e "Current Mac Address:\t$CURRENT_MAC_ADDRESS"
echo -e "New Mac Address:\t$NEW_MAC_ADDRESS"
COUNTER=0
while [[ "$CURRENT_MAC_ADDRESS" != "$NEW_MAC_ADDRESS" ]]; do
if [[ $(echo $(networksetup -getairportpower $1 2>&1)) == *"On"* ]] && [[ "$1" == "en0" ]]; then
networksetup -setairportpower $1 off
fi
if [[ "$1" == "en0" ]]; then
networksetup -setairportpower $1 on
fi
ifconfig $1 ether $NEW_MAC_ADDRESS
sleep 0.5
CURRENT_MAC_ADDRESS=$(GetCurrentMacAddress $1)
if [[ "$1" == "en0" ]]; then
networksetup -setairportpower $1 off
fi
sleep 0.5
echo -n "*"
let COUNTER=COUNTER+1
if [ $(expr $COUNTER % 3) == 0 ]; then
if [ ! "$IS_SET" = true ]; then
NEW_MAC_ADDRESS=$(RandomMacAddress)
fi
echo -e "\nNew Mac Address:\t$NEW_MAC_ADDRESS"
fi
done
if [ $1 == "en0" ]; then
networksetup -setairportpower $1 on
fi
echo -e "\nMac Address:\t\t$NEW_MAC_ADDRESS"
OUTPUT_MAC_ADDRESS=$NEW_MAC_ADDRESS
else
echo -e "The interface $1 does not exist."
fi
}
function RandomComputerName() {
echo $(cat /dev/urandom | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 33)
}
function ChangeComputerName() {
echo -e "Current Computer Name:\t$(scutil --get ComputerName)"
echo -e "Current Host Name:\t$(scutil --get HostName)"
echo -e "Current Localhost Name:\t$(scutil --get LocalHostName)"
if [[ "$1" == "random" ]]; then
COMPUTER_NAME=$(RandomComputerName)
else
COMPUTER_NAME=$1
fi
scutil --set ComputerName $COMPUTER_NAME
scutil --set HostName $COMPUTER_NAME
scutil --set LocalHostName $COMPUTER_NAME
echo -e "Computer Name:\t\t$(scutil --get ComputerName)"
echo -e "Host Name:\t\t$(scutil --get HostName)"
echo -e "Localhost Name:\t\t$(scutil --get LocalHostName)"
OUTPUT_COMPUTER_NAME=$COMPUTER_NAME
}
function _usage() {
cat <<EOF
Usage: anonymous <[options]>
Options:
-i --interface Set network interface for change mac address (en0, en1)
-m --mac-address Set new or random mac address (random, 00:00:00:00:00:00)
-c --computer-name Set new or random computer name (random, NewComputerName)
-s --set Set compile value
-q --qrcode Show QR code and detail
-p --print Show detail
--verbose Show verbose
--help Show help
EOF
}
[ $# = 0 ] && _usage " >>>>>>>> no options given "
OPT_ENABLED_VERBOSE=false
OPT_ENABLED_SET=false
OPT_INTERFACE=""
OPT_ENABLED_MAC_ADDRESS=false
OPT_MAC_ADDRESS=""
OPT_ENABLED_COMPUTER_NAME=false
OPT_COMPUTER_NAME=""
OPT_ENABLED_QRCODE=false
OPT_ENABLED_PRINT=false
while getopts ":i:m:c:-:pqs" OPTION; do
if [ "$OPT_ENABLED_VERBOSE" = true ]; then
echo -e "$OPTION - $OPTARG\n"
fi
case "$OPTION" in
i) OPT_INTERFACE="$OPTARG" ;;
m)
OPT_ENABLED_MAC_ADDRESS=true
OPT_MAC_ADDRESS="$OPTARG"
;;
c)
OPT_ENABLED_COMPUTER_NAME=true
OPT_COMPUTER_NAME="$OPTARG"
;;
s) OPT_ENABLED_SET=true ;;
q) OPT_ENABLED_QRCODE=true ;;
p) OPT_ENABLED_PRINT=true ;;
-)
[ $OPTIND -ge 1 ] && optind=$(expr $OPTIND - 1) || optind=$OPTIND
eval OPTION="\$$optind"
OPTARG=$(echo $OPTION | cut -d'=' -f2)
OPTION=$(echo $OPTION | cut -d'=' -f1)
if [ "$OPT_ENABLED_VERBOSE" = true ]; then
echo -e "$OPTION - $OPTARG\n"
fi
case $OPTION in
--interface) OPT_INTERFACE="$OPTARG" ;;
--mac-address)
OPT_ENABLED_MAC_ADDRESS=true
OPT_MAC_ADDRESS="$OPTARG"
;;
--computer-name)
OPT_ENABLED_COMPUTER_NAME=true
OPT_COMPUTER_NAME="$OPTARG"
;;
--set) OPT_ENABLED_SET=true ;;
--qrcode) OPT_ENABLED_QRCODE=true ;;
--verbose) OPT_ENABLED_VERBOSE=true ;;
--print) OPT_ENABLED_PRINT=true ;;
--help) _usage ;;
esac
OPTIND=1
shift
;;
esac
done
if [ "$OPT_ENABLED_VERBOSE" = true ]; then
echo "OPT_ENABLED_VERBOSE: "$OPT_ENABLED_VERBOSE
echo "OPT_ENABLED_SET: "$OPT_ENABLED_SET
echo "OPT_INTERFACE: "$OPT_INTERFACE
echo "OPT_ENABLED_MAC_ADDRESS: "$OPT_ENABLED_MAC_ADDRESS
echo "OPT_MAC_ADDRESS: "$OPT_MAC_ADDRESS
echo "OPT_ENABLED_COMPUTER_NAME: "$OPT_ENABLED_COMPUTER_NAME
echo "OPT_COMPUTER_NAME: "$OPT_COMPUTER_NAME
echo "OPT_ENABLED_QRCODE: "$OPT_ENABLED_QRCODE
echo "OPT_ENABLED_PRINT: "$OPT_ENABLED_PRINT
fi
LINE="========================================================"
if [ "$OPT_ENABLED_MAC_ADDRESS" = true ]; then
if [ "$OPT_ENABLED_SET" = true ] && [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
if [ ! -z "$OPT_INTERFACE" ] && [ ! -z "$OPT_MAC_ADDRESS" ] && [ "$OPT_ENABLED_SET" = true ]; then
echo -e "$LINE"
ChangeMacAddress "$OPT_INTERFACE" "$OPT_MAC_ADDRESS"
fi
if [ ! "$OPT_ENABLED_SET" = true ]; then
OUTPUT_MAC_ADDRESS=$(RandomMacAddress)
fi
if [ "$OPT_ENABLED_QRCODE" = true ]; then
OPT_ENABLED_PRINT=true
echo -e "$LINE"
if ! [ -x "$(command -v qrencode)" ]; then
echo "Error: qrencode is not installed."
else
qrencode -t UTF8 $OUTPUT_MAC_ADDRESS
fi
fi
if [ "$OPT_ENABLED_PRINT" = true ]; then
echo -e "Mac Address:\t\t$OUTPUT_MAC_ADDRESS (${#OUTPUT_MAC_ADDRESS})"
fi
if [ "$OPT_ENABLED_QRCODE" = true ]; then
echo -e "$LINE"
fi
fi
if [ "$OPT_ENABLED_COMPUTER_NAME" = true ]; then
if [ "$OPT_ENABLED_SET" = true ] && [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
if [ ! -z "$OPT_COMPUTER_NAME" ] && [ "$OPT_ENABLED_SET" = true ]; then
echo -e "$LINE"
ChangeComputerName "$OPT_COMPUTER_NAME"
fi
if [ ! "$OPT_ENABLED_SET" = true ]; then
OUTPUT_COMPUTER_NAME=$(RandomComputerName)
fi
if [ "$OPT_ENABLED_QRCODE" = true ]; then
OPT_ENABLED_PRINT=true
echo -e "$LINE"
if ! [ -x "$(command -v qrencode)" ]; then
echo "Error: qrencode is not installed."
else
qrencode -t UTF8 $OUTPUT_COMPUTER_NAME
fi
fi
if [ "$OPT_ENABLED_PRINT" = true ]; then
echo -e "Computer Name:\t\t$OUTPUT_COMPUTER_NAME (${#OUTPUT_COMPUTER_NAME})"
fi
if [ "$OPT_ENABLED_QRCODE" = true ]; then
echo -e "$LINE"
fi
fi
if [ "$OPT_ENABLED_VERBOSE" = true ]; then
echo "OPT_ENABLED_VERBOSE: "$OPT_ENABLED_VERBOSE
echo "OPT_ENABLED_SET: "$OPT_ENABLED_SET
echo "OPT_INTERFACE: "$OPT_INTERFACE
echo "OPT_ENABLED_MAC_ADDRESS: "$OPT_ENABLED_MAC_ADDRESS
echo "OPT_MAC_ADDRESS: "$OPT_MAC_ADDRESS
echo "OPT_ENABLED_COMPUTER_NAME: "$OPT_ENABLED_COMPUTER_NAME
echo "OPT_COMPUTER_NAME: "$OPT_COMPUTER_NAME
echo "OPT_ENABLED_QRCODE: "$OPT_ENABLED_QRCODE
echo "OPT_ENABLED_PRINT: "$OPT_ENABLED_PRINT
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment