09/30/12

Mutating your MAC address

Recently I was at an airport that offered free WiFi for the first 15 minutes, afterwards the system asked you pay. I knew that they must be checking identity via MAC address, so I wrote this little nifty python script that changes it to some random value each time you call it. This would only work on Linux (or maybe a MAC) OS.
I am not suggesting that you violate any laws, I put this up for educational purposes. Use it at your own risk.
For obtaining the MAC address through python, I used synthesizerpatel‘s code snippet. Alternatively the MAC address could have been obtained via a system call ifconfig using “subprocess.Popen”, as described in here.

 #!/usr/bin/python  
   
 import fcntl, socket, struct, random  
 from subprocess import call  
   
 def getHwAddr(ifname):  
   s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)  
   info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))  
   return ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]  
   
   
 a=getHwAddr('wlan0')  
 i=random.randint(2,5)  
 b=a[:3*i]+hex((int(a.split(':')[i])+random.randint(1,255))%255)[2:].zfill(2)+a[(i+1)*3-1:]  
   
 print "Changing from "+a+" to "+b  
 call(["sudo ifconfig wlan0 down"],shell=True)  
 call(["sudo ifconfig wlan0 hw ether "+b],shell=True)  
 call(["sudo ifconfig wlan0 up"],shell=True)