Never been to DZone Snippets before?

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

Mac Wifi MAC spoofing (See related posts)

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 on this post

Caius posts on Apr 22, 2008 at 23:32
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 <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
markpercival posts on Apr 23, 2008 at 07:29
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.

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts