# Randomizing MAC at boot



## athos (Mar 18, 2012)

Hello everyone, 

I've been many time willing to do some kind of little project, and, I thought about this.
A nice C and "low-level" project which may be fun to do.

Any pointers ? 
(avoid xkcd jokes, please xD).


It should (or not) change MAC of defined interfaces (that's to be defined).
Should it be loaded as a kld?

Thank you for your time.


----------



## xibo (Mar 19, 2012)

This thread is about setting MAC addresses from C.
RAND_bytes(3) is supposed to be a good random number generator.

I wouldn't do that with a kernel module though (unless you want to experiment with kernel modules), as bugs in them can be quite bad.


----------



## athos (Mar 19, 2012)

Many thanks xibo!

Since what I really want to do is experiment with the kernel, I think I'll take it the hard way and try to set it up as a kld. Let's see how it goes...

I guess that with the "Designing FreeBSD Rootkits" books, I would have enough information, wouldn't I?


----------



## vermaden (Mar 19, 2012)

This will way You can generate a random MAC address:


```
% env LC_ALL=C tr -c -d '0123456789abcdef' < /dev/urandom | head -c 12 | sed 's!^M$!!;s!\-!!g;s!\.!!g;s!\(..\)!\1:!g;s!:$!!'
f9:5b:7c:dc:c1:b8
```

Put that into startup script before /etc/rc.d/netif to set random MAC for wanted interfaces.


----------



## athos (Mar 19, 2012)

vermaden said:
			
		

> This will way You can generate a random MAC address:
> 
> 
> ```
> ...



I love unix versatility... XD

I was hoping to have to deal with some C coding, and then... BAM you came with a simple solution XD

Thanks for the nice script.


----------



## vermaden (Mar 20, 2012)

athos said:
			
		

> I love unix versatility... XD
> 
> I was hoping to have to deal with some C coding, and then... BAM you came with a simple solution XD
> 
> Thanks for the nice script.



Welcome mate 

Generating the random MAC address is one thing, making it look like a REAL MAC requires something more, a REAL manufacturer prefix, for example check this one: http://standards.ieee.org/develop/regauth/oui/oui.txt

If You want to have a REAL MAC address that is random, take random line from manufacturers list for first 3 octets, then generate the last 3


----------



## athos (Mar 20, 2012)

I know, I know, 

I once made a script that arp-pings the whole network where I was, and by downloading the IEEE OUIDs recognized the vendor 

It was for linux, though. In fact, it downloaded that same file, and by using some grep and sed, it mades the vendors list ^^

Thank you for you time.

That sed command is crazy


----------

