mirror of
https://github.com/KnugiHK/WhatsApp-Chat-Exporter.git
synced 2026-04-24 06:51:39 +00:00
Add support for encrypted iPhone backup
This commit is contained in:
@@ -3,43 +3,115 @@
|
|||||||
import shutil
|
import shutil
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import os
|
import os
|
||||||
|
import getpass
|
||||||
|
try:
|
||||||
|
from iphone_backup_decrypt import EncryptedBackup, RelativePath
|
||||||
|
except:
|
||||||
|
support_encrypted = False
|
||||||
|
else:
|
||||||
|
support_encrypted = True
|
||||||
|
|
||||||
|
def extract_encrypted(base_dir, password):
|
||||||
|
backup = EncryptedBackup(backup_directory=base_dir, passphrase=password)
|
||||||
|
print("Decrypting WhatsApp database...")
|
||||||
|
backup.extract_file(relative_path=RelativePath.WHATSAPP_MESSAGES, output_filename="7c7fba66680ef796b916b067077cc246adacf01d")
|
||||||
|
backup.extract_file(relative_path=RelativePath.WHATSAPP_CONTACTS, output_filename="ContactsV2.sqlite")
|
||||||
|
data = backup.execute_sql("""SELECT count()
|
||||||
|
FROM Files
|
||||||
|
WHERE relativePath
|
||||||
|
LIKE 'Message/Media/%'"""
|
||||||
|
)
|
||||||
|
total_row_number = data[0][0]
|
||||||
|
print(f"Gathering media...(0/{total_row_number})", end="\r")
|
||||||
|
data = backup.execute_sql("""SELECT fileID,
|
||||||
|
relativePath,
|
||||||
|
flags,
|
||||||
|
file
|
||||||
|
FROM Files
|
||||||
|
WHERE relativePath
|
||||||
|
LIKE 'Message/Media/%'"""
|
||||||
|
)
|
||||||
|
if not os.path.isdir("Message"):
|
||||||
|
os.mkdir("Message")
|
||||||
|
if not os.path.isdir("Message/Media"):
|
||||||
|
os.mkdir("Message/Media")
|
||||||
|
i = 0
|
||||||
|
for row in data:
|
||||||
|
destination = row[1]
|
||||||
|
hashes = row[0]
|
||||||
|
folder = hashes[:2]
|
||||||
|
flags = row[2]
|
||||||
|
file = row[3]
|
||||||
|
if flags == 2:
|
||||||
|
try:
|
||||||
|
os.mkdir(destination)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
elif flags == 1:
|
||||||
|
decrypted = backup.decrypt_inner_file(file_id=hashes, file_bplist=file)
|
||||||
|
with open(destination, "wb") as f:
|
||||||
|
f.write(decrypted)
|
||||||
|
i += 1
|
||||||
|
if i % 100 == 0:
|
||||||
|
print(f"Gathering media...({i}/{total_row_number})", end="\r")
|
||||||
|
print(f"Gathering media...({total_row_number}/{total_row_number})", end="\r")
|
||||||
|
|
||||||
|
def is_encrypted(base_dir):
|
||||||
|
with sqlite3.connect(f"{base_dir}/Manifest.db") as f:
|
||||||
|
c = f.cursor()
|
||||||
|
try:
|
||||||
|
c.execute("""SELECT count()
|
||||||
|
FROM Files
|
||||||
|
""")
|
||||||
|
except sqlite3.DatabaseError:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def extract_media(base_dir):
|
def extract_media(base_dir):
|
||||||
with sqlite3.connect(f"{base_dir}/Manifest.db") as manifest:
|
if is_encrypted(base_dir):
|
||||||
c = manifest.cursor()
|
if not support_encrypted:
|
||||||
c.execute("""SELECT count()
|
print("You don't have the dependencies to handle encrypted backup.")
|
||||||
FROM Files
|
print("Read more about how to deal with encrypted backup:")
|
||||||
WHERE relativePath
|
print("https://github.com/KnugiHK/Whatsapp-Chat-Exporter/blob/main/README.md#encrypted-iphone-backup")
|
||||||
LIKE 'Message/Media/%'""")
|
return False
|
||||||
total_row_number = c.fetchone()[0]
|
password = getpass.getpass("Enter the password:")
|
||||||
print(f"Gathering media...(0/{total_row_number})", end="\r")
|
extract_encrypted(base_dir, password)
|
||||||
c.execute("""SELECT fileID,
|
else:
|
||||||
relativePath,
|
with sqlite3.connect(f"{base_dir}/Manifest.db") as manifest:
|
||||||
flags
|
c = manifest.cursor()
|
||||||
FROM Files
|
c.execute("""SELECT count()
|
||||||
WHERE relativePath
|
FROM Files
|
||||||
LIKE 'Message/Media/%'""")
|
WHERE relativePath
|
||||||
row = c.fetchone()
|
LIKE 'Message/Media/%'""")
|
||||||
if not os.path.isdir("Message"):
|
total_row_number = c.fetchone()[0]
|
||||||
os.mkdir("Message")
|
print(f"Gathering media...(0/{total_row_number})", end="\r")
|
||||||
if not os.path.isdir("Message/Media"):
|
c.execute("""SELECT fileID,
|
||||||
os.mkdir("Message/Media")
|
relativePath,
|
||||||
i = 0
|
flags
|
||||||
while row is not None:
|
FROM Files
|
||||||
destination = row[1]
|
WHERE relativePath
|
||||||
hashes = row[0]
|
LIKE 'Message/Media/%'""")
|
||||||
folder = hashes[:2]
|
|
||||||
flags = row[2]
|
|
||||||
if flags == 2:
|
|
||||||
os.mkdir(destination)
|
|
||||||
elif flags == 1:
|
|
||||||
shutil.copyfile(f"{base_dir}/{folder}/{hashes}", destination)
|
|
||||||
i += 1
|
|
||||||
if i % 100 == 0:
|
|
||||||
print(f"Gathering media...({i}/{total_row_number})", end="\r")
|
|
||||||
row = c.fetchone()
|
row = c.fetchone()
|
||||||
print(f"Gathering media...({total_row_number}/{total_row_number})", end="\r")
|
if not os.path.isdir("Message"):
|
||||||
|
os.mkdir("Message")
|
||||||
|
if not os.path.isdir("Message/Media"):
|
||||||
|
os.mkdir("Message/Media")
|
||||||
|
i = 0
|
||||||
|
while row is not None:
|
||||||
|
destination = row[1]
|
||||||
|
hashes = row[0]
|
||||||
|
folder = hashes[:2]
|
||||||
|
flags = row[2]
|
||||||
|
if flags == 2:
|
||||||
|
os.mkdir(destination)
|
||||||
|
elif flags == 1:
|
||||||
|
shutil.copyfile(f"{base_dir}/{folder}/{hashes}", destination)
|
||||||
|
i += 1
|
||||||
|
if i % 100 == 0:
|
||||||
|
print(f"Gathering media...({i}/{total_row_number})", end="\r")
|
||||||
|
row = c.fetchone()
|
||||||
|
print(f"Gathering media...({total_row_number}/{total_row_number})", end="\r")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user