The officially official Devuan Forum!

You are not logged in.

#1 2017-07-02 18:37:16

Registered: 2017-03-16
Posts: 435  

automatic mac address spoofer for wireless network card

If you are paranoid about privacy like me, you will like this little creation of mine.

The script does 4 things:
1. Figures out the name of your wireless network device, kernel module used by your wifi device, and real MAC address
2. Creates a random MAC address, preserving the vendor bits (so that the fake MAC address looks plausible)
3. Create a udev rule to change the real MAC address to the random one
4. "Reboot" the wireless card by unloading and reloading its kernel module, so that you pick up the new MAC address right away


# some variables
user=$(users | xargs -n1 | uniq | grep -v root)
export DISPLAY=:0

wifi_device=$(cat /proc/net/wireless | perl -ne '/(\w+):/ && print $1') # ex: wlan0
kernel_module=$(ls /sys/class/net/$wifi_device/device/driver/module/drivers | perl -ne '/:(\w+)/ && print $1') # ex: ath9k
real_mac=$(grep $wifi_device /etc/udev/rules.d/*persistent-net.rules | perl -ne '/address}==\"([0-9a-f:]+)/ && print $1')

# construct new mac address
vendor_bits=$(cut -d ':' -f 1-3 <<< $real_mac)
random_dec1=$( min=0; max=255; echo $(( min+($RANDOM)%(max-min+1) )) ); random_hex1=$(printf "%02x" $random_dec1)
random_dec2=$( min=0; max=255; echo $(( min+($RANDOM)%(max-min+1) )) ); random_hex2=$(printf "%02x" $random_dec2)
random_dec3=$( min=0; max=255; echo $(( min+($RANDOM)%(max-min+1) )) ); random_hex3=$(printf "%02x" $random_dec3)
spoofed_mac=$(printf "%s:%s:%s:%s" $vendor_bits $random_hex1 $random_hex2 $random_hex3)

# create udev rule
echo "ACTION==\"add\", SUBSYSTEM==\"net\", ATTR{address}==\"$real_mac\", RUN+=\"/sbin/ip link set dev %k address $spoofed_mac\"" >/etc/udev/rules.d/75-mac-spoof.rules
udevadm control --reload
udevadm trigger

# show notification
su $user -c "notify-send -t 5000 -i $icon \"MAC address spoofer\" \"Randomizing wifi MAC address to $spoofed_mac. Please wait...\""

# reload kernel module (has practically same effect as a reboot)
rmmod $kernel_module; sleep 3; modprobe $kernel_module

The script needs to be run as root and the only dependency is the libnotify-bin package so that the script can show a notification. You can check that it worked by running ifconfig before and after the script and noting the MAC address of your wireless network card. Because of the udev rule, the spoofed MAC address will survive reboots.

Set this up as an anacron job every few days to get a new MAC address every so often.

Last edited by GNUser (2017-07-02 21:11:32)


#2 2017-07-05 02:57:14

From: Woodside South Australia
Registered: 2017-06-21
Posts: 72  

Re: automatic mac address spoofer for wireless network card

Thank you.
There is _much_ here that is useful and good smile


#3 2018-01-25 20:42:15

Registered: 2018-01-25
Posts: 192  

Re: automatic mac address spoofer for wireless network card

Thank you for sharing this.  My only question is, how does this differ from macchanger?


Board footer