On 07/09/14 14:36, cprise wrote:
> As a follow-up to the 'random MAC addresses (wifi)' thread, I was wondering if anyone has tried automating it on Qubes and if so, what some of your experiences are.
I have automated it.
> I have started trying out different approaches, but it appears that the hooks/interfaces needed at this level are undergoing a great deal of flux, and the fact of having wifi live in a vm throws a wrinkle or two in there as well.
>
> My underlying rule is: Since bootup /and/ resume both reset the address to the hardware ID, randomization must be handled during those events to prevent the wifi interface from becoming active with and broadcasting the hardware ID. Handling the bootup part is easy; Suspend is a whole other story.
My approach keeps the random MAC (it does not assign a new MAC, though) after sleep/resume (see below).
> I tried adding a script to /etc/NetworkManager/dispatcher.d in the template used by netvm but this doesn't work in most cases, probably because there is no "pre-up" --it is all "post". NM people decided in their infamous wisdom not to follow the ifup/ifdown scripting model...
http://thread.gmane.org/gmane.linux.network.networkmanager.devel/13766/focus=13846
>
> Its very typical of Gnome/RedHat thinking, IMO. Heaven forbid a distro maintainer write a bad pre-up script and then blame the result on upstream. Actually, I think they get more blame /this/ way.
>
> Anyway, dispatcher.d isn't triggered by entering sleep on our beloved Qubes, though the contents of 'qubes-suspend.service' and '51qubes-suspend-netvm' indicate it is trying to do /something/ about networking interfaces. The question is, what? The vms don't support normal suspend/resume processes themselves, so I don't know that these RPC calls imply.
>
> The score:
> * sleep.d broken, pieces of it still used by Qubes systemd config
> * dispatcher.d doesn't handle pre-up, so it can't change the MAC address (iface is already up by the time the script runs)
> * Need to find the right place to tell netvm to run macchanger and nmcli, but I just found the Qubes suspend-netvm hooks and feel a little confused.
>
My approach is not general, but "works for me". It's a two-part combination of 1) a udev rule, and 2) a script that invokes the macchanger utility.
Adding the following udev rule to the netvm template should work for any Intel WiFi card, and should randomize the MAC before any broadcasts are sent (at least, it runs before NetworkManager starts, and you'll be notified if this is not the case).
/etc/udev/rules.d/51-macchanger.rules:
#---begin
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="iwlwifi", RUN+="/home/user/change-mac.sh wlp0s1"
#---end
(replace wlp0s1 with your card's name; this changes depending on device assignments).
and place the randomization script below in the netvm user's home directory (replace the hwaddr with your static MAC [optional], and pick one of the three macchanger options [or use your own]):
/home/user/change-mac.sh:
#---begin
#!/bin/bash
hwaddr="XX:XX:XX:XX:XX:XX"
iface=$1
(
if [ "$#" != "1" ]
then
echo "mac changer script must be given iface name as argument: $@"
echo "Using default of wlp0s1 instead."
iface="wlp0s1"
fi
IFCONFIG="/usr/sbin/ifconfig"
MACCHANGER="/bin/macchanger"
GREP="/bin/grep"
PS="/bin/ps"
nmisrunning=$($PS auxw | $GREP "NetworkManager" | $GREP -v "grep")
$IFCONFIG $iface down
#pick one of the below macchanger options:
#$MACCHANGER -a $iface #change to another WiFi card
#$MACCHANGER -A $iface #change to any valid MAC address
#$MACCHANGER -r -b $iface #change to any random MAC address
$IFCONFIG $iface up
stillthere=$($IFCONFIG | $GREP "$hwaddr")
if [ "$stillthere" != "" ]
then
$IFCONFIG $iface down
echo "macchanger: Hardware WLAN MAC still in use. I took down $iface to (hopefully) prevent infoleaks, but you never know..."
else
echo "macchanger: Changed the WLAN MAC."
fi
if [ "$nmisrunning" != "" ]
then
echo "macchanger: NetworkManager was already running when we started--infoleak possible."
fi
) >/var/log/macchanger_log
#---end
Andrew