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

WIP: Extract Android backups, yield devices instead of just echoing #80

Merged
merged 3 commits into from
Oct 1, 2017

Conversation

rytilahti
Copy link
Owner

@rytilahti rytilahti commented Sep 17, 2017

Instead of requiring using Android backup extractor tool, this PR makes it possible to pass an Android backup (and a password if used) for the token extraction.

This moves the functionality towards automatic config generation out of backups.
The extraction depends on code which is not available in pypi, and requires some
changes to be useful for us: bluec0re/android-backup-tools#1

This is a WIP PR, let's hope the author of android-backup-tools agrees with the changes & inclusion to the pypi.

To test it out:

pip install git+https://github.com/rytilahti/android-backup-tools.git@sanitize_for_lib_use
pip install git+https://github.com/rytilahti/python-mirobo.git@extract_android_backups
miio-extract-tokens --password 1234 backup.ab


for dev in devices:
if dev.ip or dump_all:
click.echo("%s\n\tModel: %s\n\tIP address: %s\n\tToken: %s\n\tMAC: %s" % (dev.name, dev.model, dev.ip, dev.token, dev.mac))

Choose a reason for hiding this comment

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

line too long (135 > 79 characters)

try:
db = tar.extractfile(DBFILE)
except KeyError as ex:
click.echo("Unable to extract the device database file %s: %s" % (DBFILE, ex))

Choose a reason for hiding this comment

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

line too long (94 > 79 characters)

else:
click.echo("Error, unknown database type!")


@click.command()
@click.argument('backup')
@click.option('--write-to-disk', type=click.File('wb'), help='writes sqlite3 db to a file for debugging')
@click.option('--password', type=str, help='password if the android database is encrypted')

Choose a reason for hiding this comment

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

line too long (91 > 79 characters)

self.db.seek(0) # go to the beginning
click.echo("Saving db to %s" % fp)
fp.write(self.db.read())
config = DeviceConfig(name=name, ip=ip, mac=mac, model=model, token=token)

Choose a reason for hiding this comment

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

line too long (86 > 79 characters)

if ip or self.dump_all:
click.echo("%s\n\tModel: %s\n\tIP address: %s\n\tToken: %s\n\tMAC: %s" % (name, model, ip, token, mac))

config = DeviceConfig(name=name, mac=mac, ip=ip, model=model, token=token)

Choose a reason for hiding this comment

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

line too long (86 > 79 characters)

@@ -4,11 +4,19 @@
import sqlite3
from Crypto.Cipher import AES
from pprint import pformat as pf
import attr

@attr.s

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is one of the lint errors I would like to see fixed.

@coveralls
Copy link

coveralls commented Sep 17, 2017

Coverage Status

Coverage decreased (-0.2%) to 32.582% when pulling 0777489 on extract_android_backups into e8ed75a on master.

@@ -4,11 +4,19 @@
import sqlite3
from Crypto.Cipher import AES
from pprint import pformat as pf
import attr

@attr.s
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is one of the lint errors I would like to see fixed.


def read_tokens(self, db):
self.db = db
print("reading database from %s" % db)
Copy link
Collaborator

Choose a reason for hiding this comment

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

print vs. click.echo

Copy link
Owner Author

Choose a reason for hiding this comment

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

Changed to use logger.

"""Reads device information out from an sqlite3 DB.
If the given file is a .tar file, the file will be extracted
and the database automatically located (out of Android backups).
If the given file is an .ab file, it will be extracted
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the given file is an Android backup (*.ab) the database will be extracted automatically.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks, I also added a note about iOS DBs, but I hope this extraction part will be just a temporary solution and that the DB code can be used for configuration handling for the unified cli tool.

try:
db = tar.extractfile(DBFILE)
except KeyError as ex:
click.echo("Unable to extract the database file %s: %s" % (DBFILE, ex))

Choose a reason for hiding this comment

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

line too long (87 > 79 characters)

@coveralls
Copy link

coveralls commented Sep 20, 2017

Coverage Status

Coverage decreased (-0.2%) to 32.549% when pulling c185c15 on extract_android_backups into e8ed75a on master.

@rytilahti
Copy link
Owner Author

rytilahti commented Sep 22, 2017

I think this is safe to be merged. Hound being hound and all with its too narrow line length limits, still waiting to see if the android-backup-tools author will give it a go, or if there is a need to find another solution/make a fork :-)


if self.encryption == EncryptionType.AES256:
if password is None:
raise Exception("Password need to be provided to extract encrypted archives")

Choose a reason for hiding this comment

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

line too long (93 > 79 characters)

binascii.b2a_hex(master_salt).upper() + b"\n" + \
str(rounds).encode() + b"\n" + \
binascii.b2a_hex(user_iv).upper() + b"\n" + \
binascii.b2a_hex(master_enc).upper() + b"\n" + enc

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

enc = binascii.b2a_hex(user_salt).upper() + b"\n" + \
binascii.b2a_hex(master_salt).upper() + b"\n" + \
str(rounds).encode() + b"\n" + \
binascii.b2a_hex(user_iv).upper() + b"\n" + \

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent


enc = binascii.b2a_hex(user_salt).upper() + b"\n" + \
binascii.b2a_hex(master_salt).upper() + b"\n" + \
str(rounds).encode() + b"\n" + \

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

master_enc = cipher.encrypt(master_dec)

enc = binascii.b2a_hex(user_salt).upper() + b"\n" + \
binascii.b2a_hex(master_salt).upper() + b"\n" + \

Choose a reason for hiding this comment

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

continuation line over-indented for visual indent

user_key = PBKDF2(password,
user_salt, dkLen=256//8, count=rounds)

master_dec = b"\x10" + master_iv + b"\x20" + master_key + b"\x20" + master_ck

Choose a reason for hiding this comment

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

line too long (85 > 79 characters)

This moves the functionality towards automatic config generation out of backups.
The extraction depends on code which is not available in pypi, and requires some
changes to be useful for us: bluec0re/android-backup-tools#1
@rytilahti rytilahti force-pushed the extract_android_backups branch from 9574fcf to 341e709 Compare October 1, 2017 17:06
@rytilahti
Copy link
Owner Author

Forked the modified version from the android-backup-tools, ignoring the style problems for now.

@rytilahti rytilahti merged commit e6e72e0 into master Oct 1, 2017
@rytilahti rytilahti deleted the extract_android_backups branch October 1, 2017 17:08
@coveralls
Copy link

coveralls commented Oct 1, 2017

Coverage Status

Coverage decreased (-2.6%) to 30.198% when pulling 341e709 on extract_android_backups into a6a8ecd on master.

@coveralls
Copy link

coveralls commented Oct 1, 2017

Coverage Status

Coverage decreased (-2.6%) to 30.198% when pulling 341e709 on extract_android_backups into a6a8ecd on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants