在 Ubuntu 16.04 下随机化你的 WiFi MAC 地址

在 Ubuntu 16.04 下随机化你的 WiFi MAC 地址

你的设备的 MAC 地址可以在不同的 WiFi 网络中记录你的活动。这些信息能被共享后出售,用于识别特定的个体。但可以用随机生成的伪 MAC 地址来阻止这一行为。

每一个诸如 WiFi 或者以太网卡这样的网络设备,都有一个叫做 MAC 地址的唯一标识符,如:b4:b6:76:31:8c:ff。这就是你能上网的原因:每当你连上 WiFi,路由器就会用这一地址来向你接受和发送数据,并且用它来区别你和这一网络的其它设备。

这一设计的缺陷在于唯一性,不变的 MAC 地址正好可以用来追踪你。连上了星巴克的 WiFi? 好,注意到了。在伦敦的地铁上? 也记录下来。

如果你曾经在某一个 WiFi 验证页面上输入过你的真实姓名,你就已经把自己和这一 MAC 地址建立了联系。没有仔细阅读许可服务条款、你可以认为,机场的免费 WiFi 正通过出售所谓的 ‘顾客分析数据’(你的个人信息)获利。出售的对象包括酒店,餐饮业,和任何想要了解你的人。

我不想信息被记录,再出售给多家公司,所以我花了几个小时想出了一个解决方案。

 

MAC 地址不一定总是不变的

幸运的是,在不断开网络的情况下,是可以随机生成一个伪 MAC 地址的。

我想随机生成我的 MAC 地址,但是有三个要求:

  1. MAC 地址在不同网络中是不相同的。这意味着,我在星巴克和在伦敦地铁网络中的 MAC 地址是不相同的,这样在不同的服务提供商中就无法将我的活动系起来。
  2. MAC 地址需要经常更换,这样在网络上就没人知道我就是去年在这儿经过了 75 次的那个人。
  3. MAC 地址一天之内应该保持不变。当 MAC 地址更改时,大多数网络都会与你断开连接,然后必须得进入验证页面再次登陆 - 这很烦人。

 

操作网络管理器NetworkManager

我第一次尝试用一个叫做 macchanger 的工具,但是失败了。因为网络管理器NetworkManager会根据它自己的设置恢复默认的 MAC 地址。

我了解到,网络管理器 1.4.1 以上版本可以自动生成随机的 MAC 地址。如果你在使用 Ubuntu 17.04 版本,你可以根据这一配置文件实现这一目的。但这并不能完全符合我的三个要求(你必须在随机random和稳定stable这两个选项之中选择一个,但没有一天之内保持不变这一选项)

因为我使用的是 Ubuntu 16.04,网络管理器版本为 1.2,不能直接使用高版本这一新功能。可能网络管理器有一些随机化方法支持,但我没能成功。所以我编了一个脚本来实现这一目标。

幸运的是,网络管理器 1.2 允许模拟 MAC 地址。你在已连接的网络中可以看见 ‘编辑连接’ 这一选项。

网络管理器也支持钩子处理 —— 任何位于 /etc/NetworkManager/dispatcher.d/pre-up.d/ 的脚本在建立网络连接之前都会被执行。

 

分配随机生成的伪 MAC 地址

我想根据网络 ID 和日期来生成新的随机 MAC 地址。 我们可以使用网络管理器的命令行工具 nmcli 来显示所有可用网络:

  1. <span class="pun">></span><span class="pln"> nmcli connection</span>
  2. <span class="pln">NAME UUID TYPE DEVICE</span>
  3. <span class="typ">Gladstone</span><span class="typ">Guest</span><span class="lit">618545ca</span><span class="pun">-</span><span class="pln">d81a</span><span class="pun">-</span><span class="lit">11e7</span><span class="pun">-</span><span class="pln">a2a4</span><span class="pun">-</span><span class="lit">271245e11a45</span><span class="lit">802</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="pln">wireless wlp1s0</span>
  4. <span class="typ">DoESDinky</span><span class="lit">6e47c080</span><span class="pun">-</span><span class="pln">d81a</span><span class="pun">-</span><span class="lit">11e7</span><span class="pun">-</span><span class="lit">9921</span><span class="pun">-</span><span class="lit">87bc56777256</span><span class="lit">802</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="pln">wireless </span><span class="pun">--</span>
  5. <span class="typ">PublicWiFi</span><span class="lit">79282c10</span><span class="pun">-</span><span class="pln">d81a</span><span class="pun">-</span><span class="lit">11e7</span><span class="pun">-</span><span class="lit">87cb</span><span class="pun">-</span><span class="lit">6341829c2a54</span><span class="lit">802</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="pln">wireless </span><span class="pun">--</span>
  6. <span class="pln">virgintrainswifi </span><span class="lit">7d0c57de</span><span class="pun">-</span><span class="pln">d81a</span><span class="pun">-</span><span class="lit">11e7</span><span class="pun">-</span><span class="lit">9bae</span><span class="pun">-</span><span class="lit">5be89b161d22</span><span class="lit">802</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="pln">wireless </span><span class="pun">--</span>

因为每个网络都有一个唯一标识符(UUID),为了实现我的计划,我将 UUID 和日期拼接在一起,然后使用 MD5 生成 hash 值:

  1. <span class="com">#</span><span class="pln"> eg </span><span class="lit">618545ca</span><span class="pun">-</span><span class="pln">d81a</span><span class="pun">-</span><span class="lit">11e7</span><span class="pun">-</span><span class="pln">a2a4</span><span class="pun">-</span><span class="lit">271245e11a45</span><span class="pun">-</span><span class="lit">2017</span><span class="pun">-</span><span class="lit">12</span><span class="pun">-</span><span class="lit">03</span>
  2. <span class="pun">></span><span class="kwd">echo</span><span class="pun">-</span><span class="pln">n </span><span class="str">"${UUID}-$(date +%F)"</span><span class="pun">|</span><span class="kwd">md5sum</span>
  3. <span class="lit">53594de990e92f9b914a723208f22b3f</span><span class="pun">-</span>

生成的结果可以代替 MAC 地址的最后八个字节。

值得注意的是,最开始的字节 02 代表这个地址是自行指定的。实际上,真实 MAC 地址的前三个字节是由制造商决定的,例如 b4:b6:76 就代表 Intel。

有可能某些路由器会拒绝自己指定的 MAC 地址,但是我还没有遇到过这种情况。

每次连接到一个网络,这一脚本都会用 nmcli 来指定一个随机生成的伪 MAC 地址。

最后,我查看了 ifconfig 的输出结果,我发现 MAC 地址 HWaddr 已经变成了随机生成的地址(模拟 Intel 的),而不是我真实的 MAC 地址。

  1. <span class="pun">></span><span class="kwd">ifconfig</span>
  2. <span class="pln">wlp1s0 </span><span class="typ">Link</span><span class="pln"> encap</span><span class="pun">:</span><span class="typ">Ethernet</span><span class="typ">HWaddr</span><span class="pln"> b4</span><span class="pun">:</span><span class="pln">b6</span><span class="pun">:</span><span class="lit">76</span><span class="pun">:</span><span class="lit">45</span><span class="pun">:</span><span class="lit">64</span><span class="pun">:</span><span class="lit">4d</span>
  3. <span class="pln">inet addr</span><span class="pun">:</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.86</span><span class="typ">Bcast</span><span class="pun">:</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">0.255</span><span class="typ">Mask</span><span class="pun">:</span><span class="lit">255.255</span><span class="pun">.</span><span class="lit">255.0</span>
  4. <span class="pln">inet6 addr</span><span class="pun">:</span><span class="pln"> fe80</span><span class="pun">::</span><span class="lit">648c</span><span class="pun">:</span><span class="pln">aff2</span><span class="pun">:</span><span class="lit">9a9d</span><span class="pun">:</span><span class="lit">764</span><span class="pun">/</span><span class="lit">64</span><span class="typ">Scope</span><span class="pun">:</span><span class="typ">Link</span>
  5. <span class="pln">UP BROADCAST RUNNING MULTICAST MTU</span><span class="pun">:</span><span class="lit">1500</span><span class="typ">Metric</span><span class="pun">:</span><span class="lit">1</span>
  6. <span class="pln">RX packets</span><span class="pun">:</span><span class="lit">12107812</span><span class="pln"> errors</span><span class="pun">:</span><span class="lit">0</span><span class="pln"> dropped</span><span class="pun">:</span><span class="lit">2</span><span class="pln"> overruns</span><span class="pun">:</span><span class="lit">0</span><span class="pln"> frame</span><span class="pun">:</span><span class="lit">0</span>
  7. <span class="pln">TX packets</span><span class="pun">:</span><span class="lit">18332141</span><span class="pln"> errors</span><span class="pun">:</span><span class="lit">0</span><span class="pln"> dropped</span><span class="pun">:</span><span class="lit">0</span><span class="pln"> overruns</span><span class="pun">:</span><span class="lit">0</span><span class="pln"> carrier</span><span class="pun">:</span><span class="lit">0</span>
  8. <span class="pln">collisions</span><span class="pun">:</span><span class="lit">0</span><span class="pln"> txqueuelen</span><span class="pun">:</span><span class="lit">1000</span>
  9. <span class="pln">RX bytes</span><span class="pun">:</span><span class="lit">11627977017</span><span class="pun">(</span><span class="lit">11.6</span><span class="pln"> GB</span><span class="pun">)</span><span class="pln"> TX bytes</span><span class="pun">:</span><span class="lit">20700627733</span><span class="pun">(</span><span class="lit">20.7</span><span class="pln"> GB</span><span class="pun">)</span>

 

脚本

完整的脚本也可以在 Github 上查看

  1. <span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
  2. <span class="com"># /etc/</span><span class="typ">NetworkManager</span><span class="pun">/</span><span class="pln">dispatcher</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">pre</span><span class="pun">-</span><span class="pln">up</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">randomize</span><span class="pun">-</span><span class="pln">mac</span><span class="pun">-</span><span class="pln">addresses</span>
  3. <span class="com">#</span><span class="typ">Configure</span><span class="pln"> every saved </span><span class="typ">WiFi</span><span class="pln"> connection </span><span class="kwd">in</span><span class="typ">NetworkManager</span><span class="kwd">with</span><span class="pln"> a spoofed MAC</span>
  4. <span class="com">#</span><span class="pln"> address</span><span class="pun">,</span><span class="pln"> seeded </span><span class="kwd">from</span><span class="pln"> the UUID of the connection </span><span class="kwd">and</span><span class="pln"> the </span><span class="kwd">date</span><span class="pln"> eg</span><span class="pun">:</span>
  5. <span class="com">#</span><span class="str">'c31bbcc4-d6ad-11e7-9a5a-e7e1491a7e20-2017-11-20'</span>
  6. <span class="com">#</span><span class="typ">This</span><span class="pln"> makes your MAC impossible</span><span class="pun">(?)</span><span class="pln"> to track across </span><span class="typ">WiFi</span><span class="pln"> providers</span><span class="pun">,</span><span class="kwd">and</span>
  7. <span class="com">#</span><span class="kwd">for</span><span class="pln"> one provider to track across days</span><span class="pun">.</span>
  8. <span class="com">#</span><span class="typ">For</span><span class="pln"> craptive portals that authenticate based on MAC</span><span class="pun">,</span><span class="pln"> you might want to</span>
  9. <span class="com">#</span><span class="pln"> automate logging </span><span class="kwd">in</span><span class="pun">:)</span>
  10. <span class="com">#</span><span class="typ">Note</span><span class="pln"> that </span><span class="typ">NetworkManager</span><span class="pun">>=</span><span class="lit">1.4</span><span class="pun">.</span><span class="lit">1</span><span class="pun">(</span><span class="typ">Ubuntu</span><span class="lit">17.04</span><span class="pun">+)</span><span class="pln"> can </span><span class="kwd">do</span><span class="pln"> something similar</span>
  11. <span class="com">#</span><span class="pln"> automatically</span><span class="pun">.</span>
  12. <span class="kwd">export</span><span class="pln"> PATH</span><span class="pun">=</span><span class="pln">$PATH</span><span class="pun">:</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">:/</span><span class="pln">bin</span>
  13. <span class="pln">LOG_FILE</span><span class="pun">=</span><span class="str">/var/</span><span class="pln">log</span><span class="pun">/</span><span class="pln">randomize</span><span class="pun">-</span><span class="pln">mac</span><span class="pun">-</span><span class="pln">addresses</span>
  14. <span class="kwd">echo</span><span class="str">"$(date): $*"</span><span class="pun">></span><span class="pln"> $</span><span class="pun">{</span><span class="pln">LOG_FILE</span><span class="pun">}</span>
  15. <span class="pln">WIFI_UUIDS</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">nmcli </span><span class="pun">--</span><span class="pln">fields type</span><span class="pun">,</span><span class="pln">uuid connection show </span><span class="pun">|</span><span class="kwd">grep</span><span class="lit">802</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="pln">wireless </span><span class="pun">|</span><span class="kwd">cut</span><span class="str">'-d '</span><span class="pun">-</span><span class="pln">f3</span><span class="pun">)</span>
  16. <span class="kwd">for</span><span class="pln"> UUID </span><span class="kwd">in</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">WIFI_UUIDS</span><span class="pun">}</span>
  17. <span class="kwd">do</span>
  18. <span class="pln">UUID_DAILY_HASH</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="kwd">echo</span><span class="str">"${UUID}-$(date +F)"</span><span class="pun">|</span><span class="kwd">md5sum</span><span class="pun">)</span>
  19. <span class="pln">RANDOM_MAC</span><span class="pun">=</span><span class="str">"02:$(echo -n ${UUID_DAILY_HASH} | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4:\5/')"</span>
  20. <span class="pln">CMD</span><span class="pun">=</span><span class="str">"nmcli connection modify ${UUID} wifi.cloned-mac-address ${RANDOM_MAC}"</span>
  21. <span class="kwd">echo</span><span class="str">"$CMD"</span><span class="pun">>></span><span class="pln"> $</span><span class="pun">{</span><span class="pln">LOG_FILE</span><span class="pun">}</span>
  22. <span class="pln">$CMD </span><span class="pun">&</span>
  23. <span class="kwd">done</span>
  24. <span class="pln">wait</span>

更新:使用自己指定的 MAC 地址可以避免和真正的 intel 地址冲突。感谢 @_fink


via: https://www.paulfurley.com/randomize-your-wifi-mac-address-on-ubuntu-1604-xenial/

作者:Paul M Furley 译者:wenwensnow 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐