Jump to content
Not connected, Your IP: 3.129.42.198
Omniferum

Script to generate/install a random MAC address

Recommended Posts

C'est moi.

 

Anyway, I got bored one day and cobbled together a batch script that will generate a different valid MAC address for each physical network adapter you have, inject it, and then reset the adapters so the changes take place. So basically you run this and you'll have a brand spanking new MAC and reconnected to your network in under 5 seconds.

 

The only actual reason I wrote this script is because I travel a lot and it was just a way to get around hotspots with MAC restrictions, but this felt like a place that might appreciate it.

 

This should function with any OS language

 

 

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS
 
::Generate and implement a random MAC address
FOR /F "tokens=1" %%a IN ('wmic nic where physicaladapter^=true get deviceid ^| findstr [0-9]') DO (
CALL :MAC
FOR %%b IN (0 00 000) DO (
REG QUERY HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\%%b%%a >NUL 2>NUL && REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\%%b%%a /v NetworkAddress /t REG_SZ /d !MAC! /f >NUL 2>NUL
)
)
 
::Disable power saving mode for network adapters
FOR /F "tokens=1" %%a IN ('wmic nic where physicaladapter^=true get deviceid ^| findstr [0-9]') DO (
FOR %%b IN (0 00 000) DO (
REG QUERY HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\%%b%%a >NUL 2>NUL && REG ADD HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\%%b%%a /v PnPCapabilities /t REG_DWORD /d 24 /f >NUL 2>NUL
)
)
 
::Reset NIC adapters so the new MAC address is implemented and the power saving mode is disabled.
FOR /F "tokens=2 delims=, skip=2" %%a IN ('"wmic nic where (netconnectionid like '%%') get netconnectionid,netconnectionstatus /format:csv"') DO (
netsh interface set interface name="%%a" disable >NUL 2>NUL
netsh interface set interface name="%%a" enable >NUL 2>NUL
)
 
GOTO :EOF
:MAC
::Generates semi-random value of a length according to the "if !COUNT!" line, minus one, and from the characters in the GEN variable
SET COUNT=0
SET GEN=ABCDEF0123456789
SET GEN2=26AE
SET MAC=
:MACLOOP
SET /a COUNT+=1
SET RND=%random%
::%%n, where the value of n is the number of characters in the GEN variable minus one. So if you have 15 characters in GEN, set the number as 14
SET /A RND=RND%%16
SET RNDGEN=!GEN:~%RND%,1!
SET /A RND2=RND%%4
SET RNDGEN2=!GEN2:~%RND2%,1!
IF "!COUNT!" EQU "2" (SET MAC=!MAC!!RNDGEN2!) ELSE (SET MAC=!MAC!!RNDGEN!)
IF !COUNT! LEQ 11 GOTO MACLOOP

Share this post


Link to post

If you are looking for something similar for Linux (Ubuntu), there is a nice HowTo at:
https://help.riseup.net/en/security/network-security/mac-address

 

It should run with the latest Ubuntu too.

 

I´ll paste it here too, if the website comes unavailable.

 

Open a terminal and run:

 

sudo apt-get install macchanger 

 

It’s suggested to take note of your current MAC address at this point, to verify that it is being changed by your scripts later on. To do this for a wired (ethernet) connection, type:
 

 

macchanger eth0 

 

to do the same for a wireless adapter, type:
 

 

macchanger wlan0 

 



The output will look something like this:
 

 

Current MAC: 00:0c:1d:47:a4:0c (Mettler & Fuchs Ag) 

 

00:0c:1d:47:a4:0c is the MAC address in this instance, always in the form of XX:XX:XX:XX:XX:XX. The text in the parentheses will vary.
Create the file /etc/init/macchanger.conf. This can be done by typing:
 

 

sudo nano /etc/init/macchanger.conf
 

 

Paste the following lines into it and save the file (Ctrl+X will save and close in nano):

 

# macchanger - set MAC addresses
#
# Set the MAC addresses for the network interfaces.

description "change mac addresses"

start on starting network-manager

pre-start script
/usr/bin/macchanger -A wlan0
/usr/bin/macchanger -A eth0
/usr/bin/macchanger -A wmaster0
/usr/bin/macchanger -A pan0
#/usr/bin/logger wlan0 `/usr/bin/macchanger -s wlan0`
#/usr/bin/logger eth0 `/usr/bin/macchanger -s eth0`
end script 

 

This script runs after the network manager service starts through the Ubuntu Upstart daemon system then, using the -A switch, creates a random vendor-identified MAC address.
The program macchanger can generate various kinds of addresses—this method may look strange to active network monitoring, but passive network monitoring and background tracking will likely not notice. This method should work best for most people.

 

Create the file /etc/network/if-post-down.d/random-mac and paste the following lines into it:
 

 

#!/bin/sh

MACCHANGER=/usr/bin/macchanger

[ "$IFACE" != "lo" ] || exit 0

# Bring down interface (for wireless cards that are up to scan for networks), change MAC address to a random vendor address, bring up the #interface
/sbin/ifconfig "$IFACE" down
macchanger -A "$IFACE"

 

This script changes the MAC address again when the network is disconnected. It’s possible if the network is never properly brought down and one never reboots that one can use the same address more than once.
Make the random-mac script executable by typing:

 

sudo chmod +x /etc/network/if-post-down.d/random-mac 

 

Restart Network Manager to take effect:

 

sudo service network-manager restart 

 

Your computer will now automatically create (without your intervention or notification) a new random MAC address for every physical network adapter you have (wired and WIFI) and reduce your trackability on public networks!

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Security Check
    Play CAPTCHA Audio
    Refresh Image

×
×
  • Create New...