Skip to content

Instantly share code, notes, and snippets.

@willinspire
Created October 24, 2018 17:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willinspire/89135d813deb938a1967ce6f0dda2158 to your computer and use it in GitHub Desktop.
Save willinspire/89135d813deb938a1967ce6f0dda2158 to your computer and use it in GitHub Desktop.
Automatically generate and assign a new MAC address for the local system.
#! /bin/bash
################################################################################
# __
# / _|
# _ __ ___ __ _ ___ ___ _ __ ___ ___ | |_
# | '_ ` _ \ / _` |/ __/ __| '_ \ / _ \ / _ \| _|
# | | | | | | (_| | (__\__ \ |_) | (_) | (_) | |
# |_| |_| |_|\__,_|\___|___/ .__/ \___/ \___/|_|
# | |
# |_|
#
################################################################################
#
## SCRIPT INFO: macspoof.sh
#
# Automatically generate and assign a new MAC address for the local system
#
# Default execution (without optional parameters) will generate a new MAC
# address and assign that new address to the system's local wireless card.
# This will effectively change the local system's hardware identity.
#
## USAGE EXAMPLE:
#
# ./macspoof.sh [OPTIONAL_PARAMETERS]
#
## OPTIONS:
#
# -h|--help Print this help text
# -s|--silent Avoid printing outputs to the terminal
# -m|--mac Output a ramdomly-generated MAC address
# -i|--interface Output the system's wireless interface
# -a|--mac-interface Output a randomly-generated MAC address
# and the system's wireless interface
#
## ATTRIBUTION:
#
# Inspired by @ghzmdr --> ghzmdr.github.io
# View inspiration source here: gist.github.com/ghzmdr/96325a8238380e631c98
#
# Expanded and assembled by h8rt3rmin8r for ResoNova International Consulting
# (resonova.com) for research purposes.
#
# Download the source: http://bit.ly/macspoof-sh
#
################################################################################
# Variable assignments and function declarations
INTERFACE=$(iw dev | awk '$1=="Interface"{print $2}')
ERR_OUT="ERROR: Something went wrong"
VB_SET="/dev/stdout"
MANPAGE="https://pastebin.com/raw/KEE8CZJr"
function macgen() {
# MAC Generator loop
MAC="" # Intentionally blank
for i in {0..5}; do
VAL=$RANDOM; PART=$(printf "%2X" $VAL); MAC+=$(printf "%.2s" $PART)
if [ $i -lt 5 ]; then MAC+=":"; fi
done
}
function macspoof() {
# Generate and apply a new MAC address to the system interface
macgen
# Inform user as to why password is required at this point
echo "Switching local MAC address with sudo..." &>${VB_SET}
sleep 0.6
sudo ip link set dev ${INTERFACE} down
sudo ip link set dev ${INTERFACE} address ${MAC}
sudo ip link set dev ${INTERFACE} up
echo "Done!" &>${VB_SET}; echo &>${VB_SET}
echo "${INTERFACE} has been set to ${MAC}" &>${VB_SET}
exit 0
}
# Validation filter (sanity check 'iw dev' output)
if [[ "$(echo ${INTERFACE} | wc -c)" -gt 8 ]]; then echo ${ERR_OUT}; exit 1; fi
# Optional output and process variations
if [[ ! "x$1" == "x" ]];
then
case "$1" in
-s|-silent|--silent|-q|-quiet|--quiet)
# Optional silent runtime
VB_SET="/dev/null"
;;
-m|-mac|--mac|--mac-address)
# Output random MAC only (for 3rd party use cases)
macgen; echo "${MAC}"; exit 0
;;
-i|-interface|--interface)
# Output interface only (for 3rd party use cases)
echo "${INTERFACE}"; exit 0
;;
-a|-all|--all|-im|-mi|-mac-interface|--mac-interface)
# Output interface and random MAC only (for 3rd party use cases)
macgen; echo "${MAC} -- ${INTERFACE}"; exit 0
;;
-h|-help|--help)
# Print the script help text
echo
curl -s ${MANPAGE}
echo; echo; exit
;;
esac
fi
# LET'S GO!
macspoof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment