Perl script to randomize MAC address
I wrote this quick&dirty Perl script to randomize MAC address. Tested on BT4R2, works fine.
Code:
#!/usr/bin/perl
# MAC ADDRESS RANDOMIZER
# by m0skit0 (acidkaos@hotmail.com)
# 16/03/2011
use strict;
# GLOBALS
my $device;
sub check_args()
{
if ($#ARGV != 0)
{
print "Usage: perl mac_addr_randomize <device>\n";
exit;
}
$device = $ARGV[0];
}
sub generate_random_mac()
{
my $mac = "";
for(my $i = 0; $i < 6; $i++)
{
$mac = $mac.sprintf("%02x", int(rand(256))).":";
}
chop($mac);
return $mac;
}
sub main()
{
check_args();
my $new_mac = generate_random_mac();
print $new_mac."\n";
system("ifconfig", $device, "down");
system("macchanger", "-m", $new_mac, $device);
system("ifconfig", $device, "up");
return;
}
main();
The script automates ifconfig down and up process, but you can comment this lines if you want to do it manually.
PS: if you're on a DHCP network, you'll need to send a new DHCPREQUEST to fill the kernel routing table.
PS: I'm still learning Perl, so be kind xD
PS: please if this does not fit here, my apologies! :p
Re: Perl script to randomize MAC address
Sorry maybe im missing something, besides ifup/ifdown, doesn't 'macchanger -r' set a fully random MAC and 'macchanger -A' sets a random hardware vendor MAC. Might be more elegant to throw a 4 line bash script together.
Either way nice bit of code, i just try not to reinvent the wheel :p
Re: Perl script to randomize MAC address
This is a pretty creative way to do it though. Thanks for sharing.
@leg3nd I agree - My logon script looks like something like this
Code:
interface="wlan0" #or whatever
mac_temp=/tmp/tmp
ifconfig $interface down
/usr/bin/notify-send "Randomly Spoofing MAC Address"
macchanger -r $interface > $mac_temp
mac="$(cat $mac_temp)"
/usr/bin/notify-send "$mac"
ifconfig $interface up
Quote:
Originally Posted by
leg3nd
Sorry maybe im missing something, besides ifup/ifdown, doesn't 'macchanger -r' set a fully random MAC and 'macchanger -A' sets a random hardware vendor MAC. Might be more elegant to throw a 4 line bash script together.
Either way nice bit of code, i just try not to reinvent the wheel :p
Re: Perl script to randomize MAC address
@leg3nd: you're totally right, i didnt know macchanger had such option (which is obvious now that you mention it :p).
Anyway just sharing it, it's useful for practicing Perl :p