Skip to content

Instantly share code, notes, and snippets.

@rskelley9
Last active February 9, 2018 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rskelley9/38fe08be915c75359e9e to your computer and use it in GitHub Desktop.
Save rskelley9/38fe08be915c75359e9e to your computer and use it in GitHub Desktop.
How to change your MAC address with ruby and shell.

Changing Your MAC Address

The following walkthrough will show you how to temporarily change the MAC address of your ethernet/WiFi device. This is useful for MAC spoofing and network security testing.

Notes

  • MAC addresses are 48-bit, hexadecimal uids used for organizing a physical network. They're factory-assigned by the creators of the device's network interface card and are typically coded into the device's firmware. They're generated by encoding a manufacturer's registered id number.

  • Even if you're using a proxy to mask your machine's IP address, a network administrator can still identify your machine by its unique MAC address.

  • Note that your MAC addresses will be reset once your computer reboots. However, you can create an alias and store it in your bash/zsh/etc profile for later use.

Steps

1a.) List all of the active network interfaces on your computer using pcgrep and a regular expression. This makes it easier to select the one you want to change. Note that you can find the static IP Address and the MAC Address of each interface via your Mac's Network Preferences as well.

ifconfig | pcregrep -M -o '^[^\t:]+:([^\n]|\n\t)*status: active'

1b.) To make things easier, you can list only the active network interfaces' names.

ifconfig | pcregrep -M -o '^[^\t:]+:([^\n]|\n\t)*status: active' | egrep -o -m 1 '^[^\t:]+'

2.) Generate a new, unique MAC address using the following Ruby script and then print it out to view it.

ezmac=$(ruby -e 'puts (1..6).map{"%0.2X"%rand(256)}.join(":")')

echo ${ezmac}

3.) Change the appropriate network interface's MAC address to the MAC address you just generated. Below, we're changing the MAC address of en0.

sudo ifconfig en0 ${ezmac}

4.) Verify that the change was made.

ifconfig en0

Troubleshooting

If you get a 'bad value' message or your MAC address isn't being set to the new value you generated try:

# First disassociate from your wireless network
sudo airport -z

# Then turn the network interface controller off
sudo ifconfig en0 down

# Then on again
sudo ifconfig en0 up

# Then change it
sudo ifconfig en0 ether ${ezmac}

If that still doesn't work, you may have to specify another name for your Wi-Fi interface:

# Try using 'ether'
sudo ifconfig en0 ether ${ezmac}

# Try using 'Wi-Fi for OS X prior to 10.8'
sudo ifconfig en0 Wi-Fi ${ezmac}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment