home rss search January 01, 2017

Randomize MAC


How To Randomize MAC address on OpenBSD

Have you ever wanted to get a new IP address lease, but never knew how? Did you every want to keep your machine a little more anonymous by switching ips every once in a while? Have you stopped sharing the latest Linux ISO image hours ago but clients are still hitting your ip? This can all be solved with a new ip lease.

If you want to retrieve a new ip address from your internet service provider through DHCP and your isp binds your ip to your MAC then you need to change your mac address. Not just change it, but randomize it so you can get a new ip whenever you want to. In computer networking a Media Access Control address (MAC address) or Ethernet Hardware Address (EHA) or hardware address or adapter address is a quasi-unique identifier attached to most network adapters (NICs). It is a number that acts like a name for a particular network adapter, so, for example, the network cards (or built-in network adapters) in two different computers will have different names, or MAC addresses, as would an Ethernet adapter and a wireless adapter in the same computer, and as would multiple network cards in a router. However, it is possible to change the MAC address on most of today's hardware, often referred to as MAC spoofing. Wikipedia

We are going to setup a small perl script which will place a new randomized MAC address into the /etc/netstart file. If you want to get a new ip for any reason you now have that choice.

Getting Started

Step 1: is to edit the /etc/netstart file and add the the following two(2) lines "### Change MAC" and "ifconfig em0 lladdr 00:a1:a2:a3:a4:a5" entries to the top on the file. Insert them starting on the fourth(4th) line. Your /etc/netstart should look similar to the example below. Here you can see the first seven(7) lines of our example netstart file.

#!/bin/sh -
#
#       $OpenBSD: netstart,v 1.234 2020/10/10 10:20:30 itajon Exp $
### Change MAC
ifconfig em0 lladdr 00:a1:a2:a3:a4:a5
###
# Strip comments (and leading/trailing whitespace if IFS is set)

Step 2: is to download the following perl script called calomel_mac_randomizer.pl. You can copy and paste it from below since it is small.

#!/usr/bin/perl
#
## calomel_mac_randomizer.pl
#

my $infile = "/etc/netstart";

open F,$infile or die "Could not open $infile ($!)";
undef $/; # Tell perl to slurp whole file in one piece.
$txt = <F>;
close F;

my $macaddress = &rmacaddr;

$txt =~ s/^(ifconfig em0 lladdr ).*/$1$macaddress/m;

open F,">$infile.$^T.$$" or die "Could not create $infile.$^T.$$ ($!)";
print F $txt;
close F;

rename "$infile.$^T.$$", $infile
or die "Error renaming $infile.$^T.$$ to $infile ($!)";

sub rdig{
  my @digits = qw/0 a 1 b 2 c 3 d 4 e 5 f 6 7 8/;
  srand(time ^ $$ ^ unpack "%32L*", `ps -ael | gzip`);
  $digits[int(rand(@digits))];
}

sub rmacaddr{
  join ':',"00",&rdig.&rdig,&rdig.&rdig,&rdig.&rdig,&rdig.&rdig,&rdig.&rdig;
}

Step 3: now, you can run the script and it will open the /etc/netstart file and randomize the mac address. After running the script check the netstart file by grepping for lladdr, for example " grep lladdr /etc/netstart " and see the change to the MAC.

Step 4: To use this script you need to know how your isp will allow you to get a new ip. If you have a cable modem for example you need to reset the cable modem when you reset the MAC address. The reason is the isp links the cable modem's mac to the mac of your network interface. Since we are changing the interface mac the two no longer match. If you have a FIOS fiber link on the other hand, you can simply tear down the interface, randomize the mac and bring the interface back up.

In the script below you may also want to add other duties to complete when the interface is reset. If you use squid you may want randomize the user-agent with the calomel.org squid_ua_random script. Clients who use DynDns or another dynamic dns server will want to update their ip with ddclient too. Examples and ideas are commented out in the script below.

In Conclusion

To make life easier here is a shell script that will tear down the network interface (em0 for this example), flush the routes, wait 45 seconds for the cable modem to initialize and then bring the interface up.

######################################--
### Calomel.org script to randomize the MAC and restart networking
######################################--

####### Power down cable modem and change MAC
clear
echo "____.:| generating random info..."
/your_tools/mac_randomizer.pl

## if you have squid and squid_ua_randomizer use this ##
# /your_tools/squid_ua_randomizer.pl;squid -k reconfigure

####### Tear down the external network interface
echo "____.:| tearing down network interface (em0) and routes..."
ifconfig em0 down
ifconfig em0 delete
route -n flush
arp -ad

####### Countdown to cable modem initialization
echo " "
echo "____.:| ATTENTION: reset cable modem now."
echo "____.:| network initialization in..."
for ((x=45; x > 0; x=x-5))
 do
   echo "$x seconds"
   sleep 5
 done

####### Initialize network, bring up interface and ask for dhcpd info
echo " "
echo "____.:| network initialization started"
echo "____.:| netstart..."
/bin/sh /etc/netstart
sleep 2
echo " "
echo "____.:| ifconfig..."
ifconfig em0 up

####### Network config complete.
echo " "
echo "____.:| network restart completed."
logger network restart completed
ifconfig em0

####### Cleanup (clear anchors and check processor speed)
# echo " "
# echo "____.:| flush the "games" anchor..."
# pfctl -a games -F all
# echo " "
# echo "____.:| cool running performance mode..."
# apm -C

####### ddclient users enable this function
# echo " "
# echo "____.:| ddclient new ip..."
# ddclient -force

####### DONE
echo " "
echo "____.:| Done."

Contact Us RSS Feed Google Site Search