Skip to content

Commit

Permalink
error fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Khalid6468 committed Apr 16, 2022
1 parent 3caf10c commit 0d06abe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
24 changes: 13 additions & 11 deletions chainbreaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import string
import uuid

printable_chars = bytes(string.printable, 'ascii')

class Chainbreaker(object):
ATOM_SIZE = 4
KEYCHAIN_SIGNATURE = "kych"
Expand Down Expand Up @@ -73,9 +75,7 @@ def __init__(self, filepath, unlock_password=None, unlock_key=None, unlock_file=
if not self._is_valid_keychain():
self.logger.warning('Keychain signature does not match. are you sure this is a valid keychain file?')

self._unlock_password = unlock_password
if unlock_password is not None:
self._unlock_password = unlock_password.encode()
self.unlock_password = unlock_password
self.unlock_key = unlock_key
self.unlock_file = unlock_file

Expand Down Expand Up @@ -268,7 +268,7 @@ def _get_keyblob_record(self, record_offset):

key_blob_record = _KEY_BLOB(record[:+_KEY_BLOB.STRUCT.size])

if SECURE_STORAGE_GROUP != str(record[key_blob_record.TotalLength + 8:key_blob_record.TotalLength + 8 + 4]):
if SECURE_STORAGE_GROUP != record[key_blob_record.TotalLength + 8:key_blob_record.TotalLength + 8 + 4].decode():
return '', '', '', 1

cipher_len = key_blob_record.TotalLength - key_blob_record.StartCryptoBlob
Expand Down Expand Up @@ -582,6 +582,8 @@ def unlock_password(self):

@unlock_password.setter
def unlock_password(self, unlock_password):
if isinstance(unlock_password, str):
unlock_password = unlock_password.encode()
self._unlock_password = unlock_password

if self._unlock_password:
Expand Down Expand Up @@ -693,10 +695,10 @@ def keyblob_decryption(encryptedblob, iv, dbkey):

# now we handle the unwrapping. we need to take the first 32 bytes,
# and reverse them.
revplain = ''
for i in range(32):
revplain += plain[31 - i]

# revplain = b''
# for i in range(32):
# revplain += plain[31 - i]
revplain = (plain[:32])[::-1]
# now the real key gets found. */
plain = Chainbreaker._kcdecrypt(dbkey, iv, revplain)

Expand Down Expand Up @@ -930,7 +932,7 @@ def decrypt_password(self):
try:
if self.SSGP and self.DBKey:
self._password = Chainbreaker._kcdecrypt(self.DBKey, self.SSGP.IV, self.SSGP.EncryptedPassword)
if not all(c in string.printable for c in self._password):
if not all(c in printable_chars for c in self._password):
self._password = base64.b64encode(self._password)
self.password_b64_encoded = True
self.locked = False
Expand All @@ -943,9 +945,9 @@ def decrypt_password(self):
def get_password_output_str(self):
password = self.Password
if self.password_b64_encoded:
return ' [-] Base64 Encoded Password: {}\n'.format(password)
return ' [-] Base64 Encoded Password: {}\n'.format(password.decode())
else:
return ' [-] Password: {}\n'.format(password)
return ' [-] Password: {}\n'.format(password.decode())

@property
def Password(self):
Expand Down
55 changes: 20 additions & 35 deletions pyDes.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,10 +581,7 @@ def xorstr(self, x, y):
if len(x) != len(y):
raise Exception("string lengths differ %d %d" % (len(x), len(y)))

ret = ''
for i in range(len(x)):
ret += chr(ord(x[i]) ^ ord(y[i]))

ret = bytes(i^j for i,j in zip(x,y))
return ret

def encrypt(self, data, pad=''):
Expand All @@ -607,23 +604,17 @@ def encrypt(self, data, pad=''):
return self.__key3.encrypt(data)

if self.getMode() == CBC:
# CBC updated code
self.__key1.setIV(self.getIV())
self.__key2.setIV(self.getIV())
self.__key3.setIV(self.getIV())
i = 0
retdata = []
while i< len(data):
thisblock = data[i:i + self.block_size]
# could be better
lastblock = self.getIV()
retdata = b""
for i in range(0, len(data), self.block_size):
thisblock = data[i:i+self.block_size]
thisblock = self.xorstr(lastblock, thisblock)
thisblock = self.__key1.encrypt(thisblock)
thisblock = self.__key2.decrypt(thisblock)
thisblock = self.__key3.encrypt(thisblock)
self.__key1.setIV(thisblock)
self.__key2.setIV(thisblock)
self.__key3.setIV(thisblock)
retdata.append(thisblock)
i += self.block_size
return bytes.fromhex('').join(retdata)
lastblock = self.__key3.encrypt(thisblock)
resdata += lastblock
return resdata

raise Exception("Not reached")

Expand All @@ -647,23 +638,17 @@ def decrypt(self, data, pad=''):
return self.__key1.decrypt(data, pad)

if self.getMode() == CBC:
# CBC updated code
self.__key1.setIV(self.getIV())
self.__key2.setIV(self.getIV())
self.__key3.setIV(self.getIV())
i = 0
retdata = []
while i< len(data):
cipherchunk = data[i:i + self.block_size]
# could be better
lastblock = self.getIV()
retdata = b""
for i in range(0, len(data), self.block_size):
cipherchunk = data[i: i+self.block_size]
thisblock = self.__key3.decrypt(cipherchunk)
thisblock = self.__key2.encrypt(thisblock)
thisblock = self.__key1.decrypt(thisblock)
self.__key1.setIV(cipherchunk)
self.__key2.setIV(cipherchunk)
self.__key3.setIV(cipherchunk)
retdata.append(thisblock)
i += self.block_size
return bytes.fromhex('').join(retdata)
retdata += self.xorstr(lastblock, thisblock)
lastblock = cipherchunk
return retdata

raise Exception("Not reached")

Expand Down Expand Up @@ -819,7 +804,7 @@ def __profile__():
# profile.run('__filetest__()')

if __name__ == '__main__':
# __test__()
__test__()
# __fulltest__()
# __filetest__()
__profile__()
# __profile__()

0 comments on commit 0d06abe

Please sign in to comment.