Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs for token extraction for Apple users #460

Merged
merged 2 commits into from
Jan 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 49 additions & 6 deletions docs/discovery.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,35 @@ please continue to :ref:`token_extraction`.
Apple
~~~~~

.. TODO::
This part of documentation needs your help!
Please consider submitting a pull request to update this.
Create a new unencrypted iOS backup to your computer.
To do that you've to follow these steps:

See https://github.com/jghaanstra/com.xiaomi-miio/blob/master/docs/obtain_token_mirobot_new.md#ios-users
for instructions how to extract a database from the app.
- Connect your iOS device to the computer
- Open iTunes
- Click on your iOS device (sidebar left or icon on top navigation bar)
- In the Summary view check the following settings
- Automatically Back Up: ``This Computer``
- **Disable** ``Encrypt iPhone backup``
- Click ``Back Up Now``

.. _token_extraction:
When the backup is finished, download `iBackup Viewer <https://www.imactools.com/iphonebackupviewer/>`_ and follow these steps:

- Open iBackup Viewer
- Click on your newly created backup
- Click on the ``Raw Files`` icon (looks like a file tree)
- On the left column, search for ``AppDomain-com.xiaomi.mihome`` and select it
- Click on the search icon in the header
- Enter ``_mihome`` in the search field
- Select the ``Documents/0123456789_mihome.sqlite`` file (the one with the number prefixed)
- Click ``Export -> Selected…`` in the header and store the file

Now you've exported the SQLite database to your Mac and you can extract the tokens.

.. note::

See also `jghaanstra's obtain token docs <https://github.com/jghaanstra/com.xiaomi-miio/blob/master/docs/obtain_token.md#ios-users>`_ for alternative ways.

.. _token_extraction:

Extracting tokens
-----------------
Expand Down Expand Up @@ -217,6 +237,29 @@ to the tool with ``--password <password>``.
Token: 476e6b70343055483XXX
MAC: 28:6C:07:XX:XX:XX

Extracting tokens manually
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the automatic way using miio-extract-tokens not working in some cases, or is this just for completeness?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't work for my sqlite database and therefor I've added this part. There was a TypeError exception:

(homeassistant) root@rpi:/opt/homeassistant# miio-extract-tokens /tmp/1672094281_mihome.sqlite
INFO:miio.extract_tokens:Reading database from /tmp/1672094281_mihome.sqlite
INFO:miio.extract_tokens:Reading tokens from Apple DB
Traceback (most recent call last):
  File "/opt/homeassistant/homeassistant/bin/miio-extract-tokens", line 11, in <module>
    sys.exit(main())
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 155, in main
    devices = list(reader.read_tokens(backup))
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 113, in read_tokens
    yield from self.read_apple()
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 74, in read_apple
    token = BackupDatabaseReader.decrypt_ztoken(dev['ZTOKEN'])
  File "/opt/homeassistant/homeassistant/lib/python3.5/site-packages/miio/extract_tokens.py", line 50, in decrypt_ztoken
    if len(ztoken) <= 32:
TypeError: object of type 'NoneType' has no len()

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should obviously be fixed. Could you try with --dump-raw to see what it outputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should obviously be fixed. Could you try with --dump-raw to see what it outputs?

Sorry busy again :) I can test it tonight… let me open an issue for that!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Let's merge this now, it's a good idea still have the manual way documented, too :-)

--------------------------

Run the following SQLite command:

.. code-block:: bash

sqlite3 <path of *_mihome.sqlite database> "select ZNAME,ZLOCALIP,ZTOKEN from ZDEVICE"

You should get a list which looks like this:

.. code-block:: text

Device 1|x.x.x.x|0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
Device 2|x.x.x.x|0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
Device 3|x.x.x.x|0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef

These are your device names, IP addresses and tokens. However, the tokens are encrypted and you need to decrypt them.
The command for decrypting the token manually is:

.. code-block:: bash

echo '0: <YOUR 32 CHARACTER TOKEN>' | xxd -r -p | openssl enc -d -aes-128-ecb -nopad -nosalt -K 00000000000000000000000000000000

Environment variables for command-line tools
============================================
Expand Down