MAC Address Spoofing on OS X/macOS

This can be useful when travelling.

Function

You can put this in your .zshrc/.bashrc/etc.

spoof () {
    CRED='\033[0;31m'
    CBLUE='\033[0;34m'
    getmac () {
        ifconfig "$1" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
    }

    macorig=$(networksetup -getmacaddress "$1" | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
    macprev=$(getmac "$1")
    macrand=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')
    sudo /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -z;
    sudo ifconfig "$1" ether "$macrand"; networksetup -detectnewhardware;
    macnass=$(getmac "$1")
    echo "Original MAC: ${macorig}";
    echo "Previous MAC: ${macprev}";
    echo "Your new MAC: ${CBLUE}${macnass}";

    if [ "${macnass}" == "${macprev}" ] ; then
        echo "${CRED}ATTENTION: NO NEW MAC ASSIGNED DUE TO LAG IN OS X";
    fi
}

Description

spoof <adapter>

The function generates a random MAC address, handles the neccessary steps to put the adapter in its manipulative state, assigns the new address to the selected adapter (i.e. spoof en0 for Airport/Wi-Fi) and prints the newly assigned MAC address on screen.

❯ spoof en0
Original MAC: 0c:b6:8a:4d:07:00
Previous MAC: 0c:0b:1d:19:9b:bb
Your new MAC: 1c:cf:63:59:8c:f7

Helpers

# For OS X 10.10 Yosemite and later
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder; say -v Jill flushed

System Setup

  • OS X 10.11 El Capitan
  • ZSH with .zshrc