DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Mac Wifi MAC Spoofing

04.21.2008
| 3899 views |
  • submit to reddit
        A certain unnamed national provider of bagels has seen fit to limit free wifi during lunch hours. They do this based on MAC address. I never liked my MAC address anyways.


Update -- My previous code had two considerable bugs when I posted it. Caius Durling (http://caius.name/) made a considerable improvement with his own script. I've deleted my old code and just placed his here.
#!/usr/bin/env ruby
# 
# USAGE: Turn off wireless, run this, then turn it back on.
# ./random_mac.rb <nothing> # =>  random mac address
# ./random_mac.rb --reset # =>  original mac address

# Set this to your Original MAC Address
original_mac = ""

# Just a little warning
puts "Warning: You haven't set your original Mac Address in the script yet." if original_mac.empty?

# Generates a 2 digit code
def generate_pair
  a = rand(255).to_s(16)
  return (a.length < 2 ? generate_pair : a)
end

# Sets the mac address to the passed value
def set_mac_address( mac_addr )
  raise "MacAddressFormatError" if mac_addr.scan(/^(?:[\w\d]{2}\:){5}[\w\d]{2}$/).empty?
  puts "Old MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}"
  `sudo ifconfig en1 ether #{mac_addr}`
  puts "New MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}"
end



case ARGV.first
when "--reset"
  # Try to reset to old mac address
  if original_mac.empty?
    puts "ERROR - You need to set the old mac address in the script"
    exit(1)
  end
  puts "Resetting to old mac address"
  set_mac_address( original_mac )
  
when nil
  # Generate & set a random mac address
  random_mac = (0..5).map { |x| generate_pair }.join(":")
  set_mac_address( random_mac )
  
when "--help"
  # Show some help I guess
  puts [
    "USAGE: Turn off wireless, run this, then turn it back on.",
    "./random_mac.rb <nothing> # =>  random mac address",
    "./random_mac.rb --reset # =>  original mac address"
    ].join("\n")
  
else
  # Who knows?
  puts "Unknown Flag, try --help"
  exit(2)
end
    

Comments

Snippets Manager replied on Wed, 2008/02/13 - 5:12pm

by the way, on new unibody macbook and macbook pro, you need to Disassociate from any network, before changing the mac address. This post explains it: http://forums.macrumors.com/showthread.php?t=642690#12 1- disconnect airport /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -z 2- then use again this command ifconfig en1 lladdr 00:23:12:xx:xx:xx

Snippets Manager replied on Wed, 2008/02/13 - 5:12pm

This script works great. but one thin, you have to remember that the first and second bit of the most significant byte have special meanings. I mean the second digit from left to right: 0x:00:00:00:00:00 the first bit of that digit defines if your nic is unicast or multicast, and the second defines if your nic is locally administered or globally unique. you should always set it to unicast/globally unique to make it easy just set the left most 2 digits to 00. here is more info: http://en.wikipedia.org/wiki/MAC_address I changed the code to look like: # Generate & set a random mac address random_mac = (0..4).map { |x| generate_pair }.join(":") set_mac_address( "00:#{random_mac}" ) the final code will look like: #!/usr/bin/env ruby # # USAGE: Turn off wireless, run this, then turn it back on. # ./random_mac.rb # => random mac address # ./random_mac.rb --reset # => original mac address # Set this to your Original MAC Address original_mac = "" # Just a little warning puts "Warning: You haven't set your original Mac Address in the script yet." if original_mac.empty? # Generates a 2 digit code def generate_pair a = rand(255).to_s(16) return (a.length < 2 ? generate_pair : a) end # Sets the mac address to the passed value def set_mac_address( mac_addr ) raise "MacAddressFormatError" if mac_addr.scan(/^(?:[\w\d]{2}\:){5}[\w\d]{2}$/).empty? puts "Old MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}" puts "Going to set as: #{mac_addr}" `sudo ifconfig en1 ether #{mac_addr}` puts "New MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}" end case ARGV.first when "--reset" # Try to reset to old mac address if original_mac.empty? puts "ERROR - You need to set the old mac address in the script" exit(1) end puts "Resetting to old mac address" set_mac_address( original_mac ) when nil # Generate & set a random mac address random_mac = (0..4).map { |x| generate_pair }.join(":") set_mac_address( "00:#{random_mac}" ) when "--help" # Show some help I guess puts [ "USAGE: Turn off wireless, run this, then turn it back on.", "./random_mac.rb # => random mac address", "./random_mac.rb --reset # => original mac address" ].join("\n") else # Who knows? puts "Unknown Flag, try --help" exit(2) end Thanks to original writer for this nice script.

Snippets Manager replied on Tue, 2008/02/19 - 3:47am

Great script! Your're right, I wrote the script and then added the random MAC address after testing it on my machine. One quick note, if you want to reset your MAC address back to the original, this should be done automatically on reboot in case you forget to do it when you use your script. Thanks, this will be going in my personal script repo, and I'm going to update the main snippet on the page so that my old broken script no longer shows.

Snippets Manager replied on Wed, 2006/08/16 - 11:06am

There is a couple of bugs in your script, you generate a malformed MAC address firstly due to missing out the last colon. Line #3/4 should in fact read random_mac = "#{rand(255).to_s(16)}:#{rand(255).to_s(16)}:#{rand(255).to_s(16)}:\ #{rand(255).to_s(16)}:#{rand(255).to_s(16)}:#{rand(255).to_s(16)}" to fix that. There is then the problem that you sometimes (~10% of the time in testing) generate a single digit fragment in the MAC address, which also makes ifconfig throw a wobbly and refuse to set the new address. See below for the fix. Long story short, I rewrote the script to fix the bug and also add the facility to reset your mac address to an "original" one manually embedded in the script. Because I need my original MAC address at home due to having filtering on my own wireless network. #!/usr/bin/env ruby # # USAGE: Turn off wireless, run this, then turn it back on. # ./random_mac.rb # => random mac address # ./random_mac.rb --reset # => original mac address # Set this to your Original MAC Address original_mac = "" # Just a little warning puts "Warning: You haven't set your original Mac Address in the script yet." if original_mac.empty? # Generates a 2 digit code def generate_pair a = rand(255).to_s(16) return (a.length < 2 ? generate_pair : a) end # Sets the mac address to the passed value def set_mac_address( mac_addr ) raise "MacAddressFormatError" if mac_addr.scan(/^(?:[\w\d]{2}\:){5}[\w\d]{2}$/).empty? puts "Old MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}" `sudo ifconfig en1 ether #{mac_addr}` puts "New MAC address: #{`ifconfig en1 | grep ether`.match(/ether\s(.+)/)[1]}" end case ARGV.first when "--reset" # Try to reset to old mac address if original_mac.empty? puts "ERROR - You need to set the old mac address in the script" exit(1) end puts "Resetting to old mac address" set_mac_address( original_mac ) when nil # Generate & set a random mac address random_mac = (0..5).map { |x| generate_pair }.join(":") set_mac_address( random_mac ) when "--help" # Show some help I guess puts [ "USAGE: Turn off wireless, run this, then turn it back on.", "./random_mac.rb # => random mac address", "./random_mac.rb --reset # => original mac address" ].join("\n") else # Who knows? puts "Unknown Flag, try --help" exit(2) end