Skip to content

Commit

Permalink
Merge pull request #6 from bluec0re/bluec0re-patch-1
Browse files Browse the repository at this point in the history
Added travis-ci & fixed python 2 & 3 compat issues
  • Loading branch information
bluec0re authored Jun 6, 2018
2 parents b8f8f58 + 3c7c745 commit 313db58
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: python
python:
- "3.6"
- "2.7"
install:
- pip install . pycrypto
script:
- python -mandroid_backup.tests
18 changes: 11 additions & 7 deletions android_backup/android_backup.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import getpass
import binascii
import sys

try:
from Crypto.Cipher import AES
Expand Down Expand Up @@ -60,7 +61,7 @@ def read(self, n=0):
self._buffer = bytearray()

self.pos += len(data)
return data
return bytes(data)

def tell(self):
return self.pos
Expand Down Expand Up @@ -184,7 +185,7 @@ def _decrypt(self, fp, password=None):
cipher = AES.new(user_key,
mode=AES.MODE_CBC,
IV=iv)
master_key = list(cipher.decrypt(master_key))
master_key = bytearray(cipher.decrypt(master_key))
# format: <len IV: 1 byte><IV: n bytes><len key: 1 byte><key: m bytes><len checksum: 1 byte><checksum: k bytes>
# get IV
l = master_key.pop(0)
Expand Down Expand Up @@ -216,21 +217,21 @@ def _decrypt(self, fp, password=None):
if self.stream:
# decryption transformer for Proxy class
def decrypt(data):
data = cipher.decrypt(data)
data = bytearray(cipher.decrypt(data))

if fp.tell() - off >= length:
# check padding (PKCS#7)
pad = data[-1]
assert data.endswith(bytes([pad] * pad))
assert data.endswith(bytearray([pad] * pad)), "Expected {!r} got {!r}".format(bytearray([pad] * pad), data[-pad:])
data = data[:-pad]

return data

return Proxy(decrypt, fp, cipher.block_size)
else:
data = cipher.decrypt(fp.read())
data = bytearray(cipher.decrypt(fp.read()))
pad = data[-1]
assert data.endswith(bytes([pad] * pad))
assert data.endswith(bytearray([pad] * pad)), "Expected {!r} got {!r}".format(bytearray([pad] * pad), data[-pad:])
data = data[:-pad]
return io.BytesIO(data)

Expand All @@ -243,12 +244,15 @@ def encode_utf8(mk):
"""
utf8mk = mk.decode('raw_unicode_escape')
utf8mk = list(utf8mk)
to_char = chr
if sys.version_info[0] < 3:
to_char = unichr
for i in range(len(utf8mk)):
c = ord(utf8mk[i])
# fix java encoding (add 0xFF00 to non ascii chars)
if 0x7f < c < 0x100:
c += 0xff00
utf8mk[i] = chr(c)
utf8mk[i] = to_char(c)
return ''.join(utf8mk).encode('utf-8')

def _encrypt(self, dec, password=None):
Expand Down
31 changes: 17 additions & 14 deletions android_backup/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class UnpackTest(unittest.TestCase):
# t = time.time() - self.startTime
# print("%s: %.3f" % (self.id(), t))

if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
assertRaisesRegex = unittest.TestCase.assertRaisesRegexp

def test_compressed_stream(self):
with AndroidBackup(io.BytesIO(TEST_DATA_NONENC)) as ab:
self.assertEqual(ab.version, 3)
Expand Down Expand Up @@ -72,22 +75,22 @@ def test_encrypted_nonstream(self):


TEST_MEMBERS = pickle.loads(base64.b64decode("""
gANdcQAoY3RhcmZpbGUKVGFySW5mbwpxASmBcQJOfXEDKFgEAAAAbmFtZXEEWCkAAABhcHBzL2V1
gAJdcQAoY3RhcmZpbGUKVGFySW5mbwpxASmBcQJOfXEDKFgEAAAAbmFtZXEEWCkAAABhcHBzL2V1
LmJsdWVjMHJlLmFuZHJvaWQtYmFja3VwL19tYW5pZmVzdHEFWAQAAABtb2RlcQZNpAFYAwAAAHVp
ZHEHTegDWAMAAABnaWRxCEtkWAQAAABzaXplcQlNuwVYBQAAAG10aW1lcQpKqUjwWVgGAAAAY2hr
c3VtcQtN3SBYBAAAAHR5cGVxDEMBMHENWAgAAABsaW5rbmFtZXEOWAAAAABxD1gFAAAAdW5hbWVx
EFgIAAAAYmx1ZWMwcmVxEVgFAAAAZ25hb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AAAZGV2bWlub3JxGUsA
WAYAAABvZmZzZXRxGksAWAsAAABvZmZzZXRfZGF0YXEbTQACWAsAAABwYXhfaGVhZGVyc3EcfXEd
WAYAAABzcGFyc2VxHk51hnEfYmgBKYFxIE59cSEoaARYLgAAAGFwcHMvZXUuYmx1ZWMwcmUuYW5k
cm9pZC1iYWNrdXAvci9zZXR0aW5ncy5jZmdxImgGTaQBaAdN6ANoCEtkaAlLCGgKSg9J8FloC02K
ImgMaBFoEmgTaBRYCAAAAGJsdWVjMHJlcSNoFlgFAAAAdXNlcnNxJGgYSwBoGUsAaBpNAAhoG00A
CmgcfXElaB5OdYZxJmJoASmBcSdOfXEoKGgEWCkAAABhcHBzL2V1LmJsdWVjMHJlLmFuZHJvaWQt
YmFja3VwL2RiL2Zvby5kYnEpaAZNpAFoB03oA2gIS2RoCU0AIGgKSrpJ8FloC01LIGgMaBFoEmgT
aBRYCAAAAGJsdWVjMHJlcSpoFlgFAAAAdXNlcnNxK2gYSwBoGUsAaBpNAAxoG00ADmgcfXEsaB5O
dYZxLWJoASmBcS5OfXEvKGgEWCoAAABhcHBzL2V1LmJsdWVjMHJlLmFuZHJvaWQtYmFja3VwL3Nw
L2Zvby54bWxxMGgGTaQBaAdN6ANoCEtkaAlLNmgKSuBJ8FloC035IGgMaBFoEmgTaBRYCAAAAGJs
dWVjMHJlcTFoFlgFAAAAdXNlcnNxMmgYSwBoGUsAaBpNAC5oG00AMGgcfXEzaB5OdYZxNGJlLg==
"""))
TEST_MEMBERS_NAMES = list(map(lambda f: f.name, TEST_MEMBERS))

Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from setuptools import setup
import sys

deps = []
if sys.version_info[:2] < (3,4):
deps.append('enum34')

setup(
name='android_backup',
Expand Down Expand Up @@ -26,4 +31,5 @@
'android-backup-pack=android_backup.pack:main',
],
},
install_requires=deps,
)

0 comments on commit 313db58

Please sign in to comment.