Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine ifconfig with openssl to change MAC address

I am trying to make a one-line MAC Address spoofer. However, I cannot combine ifconfig with OpenSSL.

My method of doing this is to change the MAC Address to a randomly generated MAC Address using OpenSSL. However, combining the code to change the MAC Address with the random MAC Address creator doesn't seem to work.

openssl rand -hex6 | sed 's/\(..\)/\1:/g;s?.$//

This code creates a random MAC Address

sudo ifconfig en0 ether xyz

This code sets the MAC Address to xyz

Combining these two lines did not change the MAC Address as expected.

sudo ifconfig en0 ether openssl rand -hex6 | sed 's/\(..\)/\1:/g;s?.$//

This code returns a "Invalid Argument"

I expect this to be a formatting issue, but I could not find proper formatting guides online and I am new to bash.

like image 506
yuwe Avatar asked Oct 20 '25 03:10

yuwe


1 Answers

There are a bunch of problems in this code. I think the main thing you are missing is that you need $( ) to capture the output of one command so you can use it as an argument to another command (and you generally want double-quotes around that, to avoid unexpected parsing weirdness). The way you've written it, openssl rand -hex6 isn't treated as a command, just as three more arguments to sudo ifconfig (and it's the output from sudo ifconfig that's piped to sed).

You also need a space in -hex 6, and the end of the sed command is using inconsistent delimiters and is missing a close quote. Also, rather than using a capture group in the first sed command, you can just use & to get the entire matched string (i.e. s/../&:/g).

Here's a corrected version:

sudo ifconfig en0 ether "$(openssl rand -hex 6 | sed 's/../&:/g;s/:$//')"

EDIT: as @Cyrus pointed out in a comment, MAC address space is divided up into globally- vs. locally administered, and uncast vs. multicast (see Wikipedia). Using a multicast address might cause trouble, so it'd really be better to force the second digit to be even. This is a bit tricky, so I'm going to duck the question of how to do it...

like image 186
Gordon Davisson Avatar answered Oct 21 '25 19:10

Gordon Davisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!