PiotrMachowski / Xiaomi-cloud-tokens-extractor

This tool/script retrieves tokens for all devices connected to Xiaomi cloud and encryption keys for BLE devices.
MIT License
3.23k stars 333 forks source link

12 bytes encryption key #57

Closed krzakx closed 2 years ago

krzakx commented 2 years ago

Can 12 bytes encryption key be extracted from CN servers by using Xiaomi-cloud-tokens-extractor application?

PiotrMachowski commented 2 years ago

What is it used for?

krzakx commented 2 years ago

Follows: https://custom-components.github.io/ble_monitor/faq#my-sensors-ble-advertisements-are-encrypted-how-can-i-get-the-key

HOW TO GET THE MIBEACON V2/V3 ENCRYPTION KEY Yeelight Remote (YLYK01YL) and dimmers (YLKG07YL and YLKG08YL) use a legacy type of encryption. This MiBeacon V2/V3 encryption key is shorter than the MiBeacon V4/V5 encryption key, as it is a 12 bytes (24 characters) long string.

and if it is about me I am trying extract from Xiaomi Mijia Mi Smart Door Lock Youth Edition MJZNMSQ01YD

or maybe you know reason why I am not getting BLE KEY, see: https://imgbb.com/3v5tLzm

PiotrMachowski commented 2 years ago

I'm not sure if cloud contains this encryption key, but you can try checking it by yourself. You have to modify and run python version of the script:

            print(f"Devices found for server \"{current_server}\":")
            for device in devices["result"]["list"]:
                print_tabbed("---------", 3)
+                print(device)
                if "name" in device:
                    print_entry("NAME", device["name"], 3)
                if "did" in device:
krzakx commented 2 years ago

Thanks for advice. Unfortunately I do not see encryption key.

Devices found for server "cn":
   ---------
{'did': '1012157342', 'token': 'xxxx4ee340e8ff38a059xxxx', 'longitude': '0', 'latitude': '0', 'name': 'Zamek', 'pid': '6', 'localip': '185.239.42.71', 'mac': 'CD:xx:1C:99:XX:xx', 'ssid': '', 'bssid': '', 'parent_id': '', 'parent_model': '', 'show_mode': 1, 'model': 'loock.lock.v6', 'adminFlag': 1, 'shareFlag': 0, 'permitLevel': 16, 'isOnline': False, 'desc': '', 'extra': {'isSetPincode': 0, 'pincodeType': 0, 'fw_version': '2.2.6_1090', 'needVerifyCode': 0, 'isPasswordEncrypt': 0}, 'uid': 193087701, 'pd_id': 1257, 'password': '', 'p2p_id': '', 'rssi': 0, 'family_id': 0, 'reset_flag': 0}
   NAME:     Zamek
   ID:       101215xxxx
   MAC:      CD:xx:1C:99:xx:xx
   IP:       185.239.42.71
   TOKEN:    xxxx4ee340e8ff38a059xxxx
   MODEL:    loock.lock.v6
   ---------
PiotrMachowski commented 2 years ago

I have checked scripts you have linked and this key seems to be extracted directly from device. You can try to make this modification, maybe it will work:

                print_tabbed("---------", 3)
                if "name" in device:
                    print_entry("NAME", device["name"], 3)
                if "did" in device:
                    print_entry("ID", device["did"], 3)
-                    if "blt" in device["did"]:
+                    if True:
                        beaconkey = connector.get_beaconkey(current_server, device["did"])
                        if beaconkey and "result" in beaconkey and "beaconkey" in beaconkey["result"]:
                            print_entry("BLE KEY", beaconkey["result"]["beaconkey"], 3)
                if "mac" in device:
                    print_entry("MAC", device["mac"], 3)
krzakx commented 2 years ago

Again thanks for continuous support. But unfortunately no satisfying results.

{'did': '1012157342', 'token': 'de344ee340e8ff38a0xxxxxx', 'longitude': '0', 'latitude': '0', 'name': 'Zamek', 'pid': '6', 'localip': '185.239.42.71', 'mac': 'CD:BE:1C:99:E4:47', 'ssid': '', 'bssid': '', 'parent_id': '', 'parent_model': '', 'show_mode': 1, 'model': 'loock.lock.v6', 'adminFlag': 1, 'shareFlag': 0, 'permitLevel': 16, 'isOnline': False, 'desc': '', 'extra': {'isSetPincode': 0, 'pincodeType': 0, 'fw_version': '2.2.6_1110', 'needVerifyCode': 0, 'isPasswordEncrypt': 0}, 'uid': 193087701, 'pd_id': 1257, 'password': '', 'p2p_id': '', 'rssi': 0, 'family_id': 0, 'reset_flag': 0}
   NAME:     Zamek
   ID:       101215xxxx
   BLE KEY:  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
   MAC:      CD:xx:1C:99:xx:xx
   IP:       185.239.42.71
   TOKEN:    de344ee340e8ff38a0xxxxxx
   MODEL:    loock.lock.v6
   ---------
PiotrMachowski commented 2 years ago

Ok, the last shot:

                print_tabbed("---------", 3)
                if "name" in device:
                    print_entry("NAME", device["name"], 3)
                if "did" in device:
                    print_entry("ID", device["did"], 3)
-                    if "blt" in device["did"]:
+                    if True:
                        beaconkey = connector.get_beaconkey(current_server, device["did"])
+                        print(beaconkey)
                        if beaconkey and "result" in beaconkey and "beaconkey" in beaconkey["result"]:
                            print_entry("BLE KEY", beaconkey["result"]["beaconkey"], 3)
                if "mac" in device:
                    print_entry("MAC", device["mac"], 3)
krzakx commented 2 years ago

Nope, doesn't work.

Devices found for server "cn":
   ---------
   NAME:     Zamek
   ID:       101215xxxx
{'code': 0, 'message': 'ok', 'result': {'did': '', 'beaconkey': 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF'}}
   BLE KEY:  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
   MAC:      CD:xx:1C:99:xx:xx
   IP:       185.239.42.71
   TOKEN:    de344ee340e8ff38a0xxxxxx
   MODEL:    loock.lock.v6
   ---------

about being connected to MiHome cloud I am ~99% sure I am because I was able to update firmware from 2.2.6._1090 to 2.2.6_1110 (have some minimal doubts as 90% is Chinese and most operation I do intuitive).

Ernst79 commented 2 years ago

I see that you have BLE_KEY = FFFFFFFFFFFFFFFFFFF, I thought this means that the device doesn't require a key. I'll respond to you in your BLE monitor issue how to find out

PiotrMachowski commented 2 years ago

@Ernst79 in the second comment there is a link that contains several method of getting an encryption key. Method 5 seems to be recommended in this case and it looks like it retrieves the key directly from the device

Ernst79 commented 2 years ago

Yes, I know, but we first need to find out that it really uses the old encryption format with 12 bytes. I doubt it is.

krzakx commented 2 years ago

UPDATE! For device I was talking about encryption is in version 4 see https://github.com/custom-components/ble_monitor/issues/667#issuecomment-1030252340

anyway I do still have issues in order to get out BLE KEY :/

rezmus commented 2 years ago

it turned out that this lock use secure ble bind. you can't read beacon key from the cloud, however you can still log it during pairing.

Xeno22 commented 1 year ago

This helped me a lot to retrieve the BLE keys from my ceiling.10 in my LAN: https://python-miio.readthedocs.io/en/latest/index.html#getting-started

But I don't know if the lock is supported.