Skip to content

Instantly share code, notes, and snippets.

@jdh747
Created April 12, 2019 23:53
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 jdh747/908110e353ff582071f533af2c909c4d to your computer and use it in GitHub Desktop.
Save jdh747/908110e353ff582071f533af2c909c4d to your computer and use it in GitHub Desktop.
Utility for modifying MAC addresses on Mac ;)
#!/bin/sh
set -e
get_mac_address() {
echo $(ifconfig en0 ether | grep ether | rev | cut -d' ' -f2 | rev)
}
get_ip_address() {
echo $(ifconfig en0 inet | grep inet | cut -d' ' -f2)
}
set_mac_address() {
sudo ifconfig en0 ether $1
}
set_branch() {
echo "Updating mac address: $(get_mac_address) -> $1"
set_mac_address $1
echo "Done. New MAC: $(get_mac_address)"
}
randomise_branch() {
echo "Current MAC: $(get_mac_address)"
NEW_MAC=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')
set_mac_address ${NEW_MAC}
echo "Done. New MAC: $(get_mac_address)"
}
select_branch() {
IP_ADDRESS=$(get_ip_address)
DEVICES=$(sudo nmap -sn ${IP_ADDRESS}/24 | awk '/Nmap/{ip=$NF;next} /MAC/{printf "%s|%s|", ip,$3} /MAC/{for(i=4;i<=NF;i++) printf "%s_", $i; printf ";"} ')
# This part is needlessly clunky as, for whatever reason, the version of bash/sh on MAC misbehaves with arrays -- counts and index iteration didn't work properly
i=0
IFS=";" read -ra DEVICE_ARRAY <<< ${DEVICES}
for DEV in ${DEVICE_ARRAY[@]}; do
i=$((i+1)) && printf "\t%s) %s\n" "${i}" "${DEV}"
done
read -p "Select a device to spoof: " selected
NEW_MAC=$(echo ${DEVICE_ARRAY} | cut -d' ' -f${selected} | cut -d \| -f2)
echo "Spoofing MAC address: $(get_mac_address) -> ${NEW_MAC}"
set_mac_address $NEW_MAC
echo "Done. New MAC: $(get_mac_address)"
}
case $1 in
--set)
set_branch $2
;;
--randomise)
randomise_branch
;;
--select)
select_branch
;;
*)
echo "Unknown or no option given"
exit 64 # User error code
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment