188 lines
5.5 KiB
Markdown
Raw Normal View History

2020-06-18 10:59:51 -04:00
<!--
2020-12-03 06:29:52 -05:00
Title: How to spoof MAC address and hostname automatically at boot on macOS
Description: Learn how to spoof MAC address and hostname automatically at boot on macOS.
2020-06-18 10:59:51 -04:00
Author: Sun Knudsen <https://github.com/sunknudsen>
Contributors: Sun Knudsen <https://github.com/sunknudsen>
2020-09-10 10:07:07 -04:00
Reviewers:
2020-06-18 10:59:51 -04:00
Publication date: 2020-05-19T00:00:00.000Z
Listed: true
2020-06-18 10:59:51 -04:00
-->
2020-12-03 06:29:52 -05:00
# How to spoof MAC address and hostname automatically at boot on macOS
2020-06-18 10:59:51 -04:00
2021-02-11 17:44:27 -05:00
[![How to spoof MAC address and hostname automatically at boot on macOS](how-to-spoof-mac-address-and-hostname-automatically-at-boot-on-macos.png)](https://www.youtube.com/watch?v=ASXANpr_zX8 "How to spoof MAC address and hostname automatically at boot on macOS")
2020-06-18 10:59:51 -04:00
2021-03-14 10:23:02 -04:00
> Heads-up: unfortunately this guide doesnt work on Macs equipped with T2 chips running macOS Mojave. If thats your case, consider upgrading to Catalina.
> Heads-up: unfortunately this guide doesnt work on Macs equipped with M1 processors (see [issue](https://github.com/sunknudsen/privacy-guides/issues/15)).
2020-06-18 10:59:51 -04:00
2020-08-25 07:40:01 -04:00
## Caveats
- When copy/pasting commands that start with `$`, strip out `$` as this character is not part of the command
- When copy/pasting commands that start with `cat << "EOF"`, select all lines at once (from `cat << "EOF"` to `EOF` inclusively) as they are part of the same (single) command
## Guide
2021-05-21 13:21:41 -04:00
### Step 1: create `/usr/local/sbin` directory
2020-06-18 10:59:51 -04:00
```shell
2020-08-25 07:40:01 -04:00
sudo mkdir -p /usr/local/sbin
2021-01-13 08:49:10 -05:00
sudo chown ${USER}:admin /usr/local/sbin
2020-06-18 10:59:51 -04:00
```
2021-05-21 13:21:41 -04:00
### Step 2: add `/usr/local/sbin` directory to `PATH` environment variable
2020-09-26 07:20:10 -04:00
2021-07-21 19:48:34 -04:00
> Heads-up: run `echo $SHELL` to find default shell.
2020-09-26 07:20:10 -04:00
2021-06-28 10:44:39 -04:00
#### Bash (`/bin/bash`)
2020-09-26 07:20:10 -04:00
```shell
2021-06-28 10:44:39 -04:00
echo 'export PATH=$PATH:/usr/local/sbin' >> ~/.bashrc
2021-04-17 16:33:01 -04:00
source ~/.bashrc
2020-09-26 07:20:10 -04:00
```
2021-06-28 10:44:39 -04:00
#### Z Shell (`/bin/zsh`)
2020-09-26 07:20:10 -04:00
```shell
2021-06-28 10:44:39 -04:00
echo 'export PATH=$PATH:/usr/local/sbin' >> ~/.zshrc
2020-09-26 07:20:10 -04:00
source ~/.zshrc
```
### Step 3: create `spoof.sh` script
2020-06-18 10:59:51 -04:00
```shell
cat << "EOF" > /usr/local/sbin/spoof.sh
#! /bin/sh
set -e
2021-04-17 16:33:15 -04:00
set -o pipefail
2020-06-18 10:59:51 -04:00
export LC_CTYPE=C
2020-09-26 07:20:10 -04:00
basedir=$(dirname "$0")
2020-06-18 10:59:51 -04:00
# Spoof computer name
2021-03-16 14:23:21 -04:00
first_name=$(sed "$(jot -r 1 1 2048)q;d" $basedir/first-names.txt | sed -e 's/[^a-zA-Z]//g')
model_name=$(system_profiler SPHardwareDataType | awk '/Model Name/ {$1=$2=""; print $0}' | sed -e 's/^[ ]*//')
2020-09-10 10:10:51 -04:00
computer_name="$first_names $model_name"
2021-03-16 14:23:21 -04:00
host_name=$(echo $computer_name | sed -e 's///g' | sed -e 's/ /-/g')
2020-06-18 10:59:51 -04:00
sudo scutil --set ComputerName "$computer_name"
sudo scutil --set LocalHostName "$host_name"
sudo scutil --set HostName "$host_name"
2020-09-10 10:10:51 -04:00
printf "%s\n" "Spoofed hostname to $host_name"
2020-06-18 10:59:51 -04:00
# Spoof MAC address of en0 interface
2021-03-16 14:23:21 -04:00
mac_address_prefix=$(sed "$(jot -r 1 1 768)q;d" $basedir/mac-address-prefixes.txt | sed -e 's/[^A-F0-9:]//g')
mac_address_suffix=$(openssl rand -hex 3 | sed 's/\(..\)/\1:/g; s/.$//')
mac_address=$(echo "$mac_address_prefix:$mac_address_suffix" | awk '{print toupper($0)}')
2020-06-18 10:59:51 -04:00
sudo ifconfig en0 ether "$mac_address"
2020-09-10 10:10:51 -04:00
printf "%s\n" "Spoofed MAC address of en0 interface to $mac_address"
2020-06-18 10:59:51 -04:00
EOF
```
2020-09-26 07:20:10 -04:00
### Step 4: make `spoof.sh` executable
2020-06-18 10:59:51 -04:00
```shell
chmod +x /usr/local/sbin/spoof.sh
```
2021-02-23 19:15:46 -05:00
### Step 5: download [first-names.txt](./first-names.txt)
2020-06-18 10:59:51 -04:00
This list includes the top 2048 most popular baby names from the [USA Social Security Administration](https://www.ssa.gov/oact/babynames/limits.html).
```shell
2020-12-03 06:29:52 -05:00
curl -o /usr/local/sbin/first-names.txt https://sunknudsen.com/static/media/privacy-guides/how-to-spoof-mac-address-and-hostname-automatically-at-boot-on-macos/first-names.txt
2020-06-18 10:59:51 -04:00
```
2021-02-23 19:15:46 -05:00
### Step 6: download [mac-address-prefixes.txt](./mac-address-prefixes.txt)
2020-06-18 10:59:51 -04:00
This list includes 768 Apple MAC address prefixes.
```shell
2020-12-03 06:29:52 -05:00
curl -o /usr/local/sbin/mac-address-prefixes.txt https://sunknudsen.com/static/media/privacy-guides/how-to-spoof-mac-address-and-hostname-automatically-at-boot-on-macos/mac-address-prefixes.txt
2020-06-18 10:59:51 -04:00
```
2020-09-26 07:20:10 -04:00
### Step 7: create `local.spoof.plist` launch daemon
2020-06-18 10:59:51 -04:00
This step is responsible for running `spoof.sh` every time your Mac boots.
```shell
2020-08-25 07:40:01 -04:00
cat << "EOF" | sudo tee /Library/LaunchDaemons/local.spoof.plist
2020-06-18 10:59:51 -04:00
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
2020-08-25 07:40:01 -04:00
<dict>
<key>Label</key>
<string>spoof</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/spoof.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
2020-06-18 10:59:51 -04:00
</plist>
EOF
```
2020-09-26 07:20:10 -04:00
### Step 8: reboot and confirm hostname and MAC address have been spoofed
2020-06-18 10:59:51 -04:00
2021-08-26 06:57:51 -04:00
#### Spoofed hostname
2020-12-25 10:08:11 -05:00
```console
2020-08-25 07:40:01 -04:00
$ scutil --get HostName
Gatlins-MacBook-Pro
2021-08-26 06:57:51 -04:00
```
2020-06-18 10:59:51 -04:00
2021-08-26 06:57:51 -04:00
#### Hardware MAC address
```console
2020-08-25 07:40:01 -04:00
$ ifconfig en0 | grep ether | awk '{print $2}'
20:ee:28:31:03:f6
2021-08-26 06:57:51 -04:00
```
#### Hardware MAC address
2020-06-18 10:59:51 -04:00
2021-08-26 06:57:51 -04:00
```console
2020-08-25 07:40:01 -04:00
$ networksetup -listallhardwareports | awk -v RS= '/en0/{print $NF}'
9c:f4:8e:d6:2b:7d
2020-06-18 10:59:51 -04:00
```
2020-10-10 10:43:57 -04:00
“Spoofed hostname” is random and “Spoofed MAC address” doesnt match “Hardware MAC address”?
2020-06-18 10:59:51 -04:00
👍
---
## Want things back the way they were before following this guide? No problem!
2020-08-10 19:35:31 -04:00
### Step 1: set computer name, local hostname and hostname
2020-06-18 10:59:51 -04:00
2020-09-26 07:20:10 -04:00
Replace `John` with a name to your liking.
2020-06-18 10:59:51 -04:00
```shell
2020-09-26 07:20:10 -04:00
sudo scutil --set ComputerName "Johns MacBook Pro"
sudo scutil --set LocalHostName "Johns-MacBook-Pro"
sudo scutil --set HostName "Johns-MacBook-Pro"
2020-06-18 10:59:51 -04:00
```
2020-08-25 07:40:01 -04:00
### Step 2: set MAC address to default value
2020-06-18 10:59:51 -04:00
2020-08-25 07:40:01 -04:00
Given MAC address spoofing is ephemeral, deleting the `/Library/LaunchDaemons/local.spoof.plist` launch daemon and rebooting will reset your MAC address to its default value.
2020-06-18 10:59:51 -04:00
```shell
sudo rm /Library/LaunchDaemons/local.spoof.plist
```
2020-08-25 07:40:01 -04:00
### Step 3: delete script and datasets
2020-06-18 10:59:51 -04:00
```shell
rm /usr/local/sbin/spoof.sh
2020-09-26 07:20:10 -04:00
rm /usr/local/sbin/first-names.txt
rm /usr/local/sbin/mac-address-prefixes.txt
2020-06-18 10:59:51 -04:00
```
2020-12-03 06:29:52 -05:00
👍