Skip to content

Instantly share code, notes, and snippets.

@topin89
Last active November 27, 2021 17:30
Show Gist options
  • Save topin89/b109342b135649bf8c2e405a6ae79a39 to your computer and use it in GitHub Desktop.
Save topin89/b109342b135649bf8c2e405a6ae79a39 to your computer and use it in GitHub Desktop.
Extract files from LG Backup .lbf file
# For modern LG backup files use a script from here: https://github.com/Mysak0CZ/LBFtool
# with details from there https://forum.xda-developers.com/android/general/tool-lg-restore-com-lge-bnr-lbf-file-t4053579
# Fair warning, this script was designed for really old version of LGBackup and won't work on with new devices
# So, if you don't get what you wanted, try to use every data carving tools you can get
#
# Also, good person with nickname SEVENTY ONE recommends ExtractJPEG( https://www.gunamoi.com.au/soft/extractjpeg/download.html )
# To, well, extract JPG Images from a backup
# 0. You will need 7-zip context menu "Extract here".
# 1. Edit filename to your backup file
# 2. Run this script from same folder
# 3. Select all *.brokenscript and choose 7-zip > Extract here
# 4. On overwrite request, choose either "Yes to all" or "No to all", id doesn't matter
# 5. Ta-da! You just got your files.
# 6. Most .db files are in fact SQLite DBs, you can open 'em with SQLiteStudio
# 7. Sms/sms.vmsg you can read here: http://sms-vmsg.org/sms-reader-app/free-app/
# 8. Guess you can import SMS and MMS to your phone with this app (I don't know, It was friend's backup) https://play.google.com/store/apps/details?id=com.aoe.vmgconverter&hl=ru
# 9. All else, I don't know
# 10. May google be with you
import mmap
index=0
filename=f"LGBackup_xxxxxx.lbf"
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def find_in_file(f,signature, startpos):
prev=b''
chunk_size=2**20
for i,cur in enumerate(read_in_chunks(f,chunk_size)):
searchee=prev+cur
pos=searchee.find(signature)
if pos>=0:
return i*chunk_size+pos-len(prev)+startpos
prev=cur
else:
return -1
with open(filename,"rb") as backup:
starpos=0
while True:
pos=find_in_file(backup, b'PK\x03\x04',starpos)
if pos<0:
break
print(backup.tell())
backup.seek(pos)
print(backup.tell())
index+=1
filename=f"{index}.brokenzip"
with open(filename,"wb") as nextfile:
for chunk in read_in_chunks(backup,2**20):
nextfile.write(chunk)
starpos=pos+1
backup.seek(starpos)
print(backup.tell())
print()
# For modern LG backup files use a script from here: https://github.com/Mysak0CZ/LBFtool
# with details from there https://forum.xda-developers.com/android/general/tool-lg-restore-com-lge-bnr-lbf-file-t4053579
# Fair warning, this script was designed for really old version of LGBackup and won't work on with new devices
# So, if you don't get what you wanted, try to use every data carving tools you can get
#
# Also, good person with nickname SEVENTY ONE recommends ExtractJPEG( https://www.gunamoi.com.au/soft/extractjpeg/download.html )
# To, well, extract JPG Images from a backup
# 0. You will need 7-zip.
# 1. Find 7z.exe in a 7-zip folder in Program Files (or whenever you install it) and copy it to a folder with the script
# 2. Edit filename to your backup file
# 3. Run this script from same folder
# 4. Wait a bit (maybe a lot). New subfolder "restored" should appear.
# 5. Ta-da! You just got your files.
# 6. Most .db files are in fact SQLite DBs, you can open 'em with SQLiteStudio
# 7. Sms/sms.vmsg you can read here: http://sms-vmsg.org/sms-reader-app/free-app/
# 8. Guess you can import SMS and MMS to your phone with this app (I don't know, It was friend's backup) https://play.google.com/store/apps/details?id=com.aoe.vmgconverter&hl=ru
# 9. All else, I don't know
# 10. May google be with you
import mmap
from os import system, remove
filename=f"LGBackup_xxxxxx.lbf"
filename_output=f"one_and_only.brokenzip"
def read_in_chunks(file_object, chunk_size=1024):
"""Lazy function (generator) to read a file piece by piece.
Default chunk size: 1k."""
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
def find_in_file(f,signature, startpos):
prev=b''
chunk_size=2**20
for i,cur in enumerate(read_in_chunks(f,chunk_size)):
searchee=prev+cur
pos=searchee.find(signature)
if pos>=0:
return i*chunk_size+pos-len(prev)+startpos
prev=cur
else:
return -1
def unpack_brokenzip(filename_output):
system(f"7z x -aou -orestored {filename_output}")
with open(filename,"rb") as backup:
starpos=0
while True:
pos=find_in_file(backup, b'PK\x03\x04',starpos)
if pos<0:
break
backup.seek(pos)
print(backup.tell())
with open(filename_output,"wb") as nextfile:
for chunk in read_in_chunks(backup,2**20):
nextfile.write(chunk)
unpack_brokenzip(filename_output)
starpos=pos+1
backup.seek(starpos)
remove(filename_output)
# For modern LG backup files use a script from here: https://github.com/Mysak0CZ/LBFtool
# with details from there https://forum.xda-developers.com/android/general/tool-lg-restore-com-lge-bnr-lbf-file-t4053579
filename="LGBackup_xxxxxx.lbf"
filename_out="LGBackup_xxxxxx_headers_only.lbf"
CHUNK_LENGTH=4096
HEADER=b'PK\x03\x04'
with open(filename,"rb") as backup, open(filename_out,"wb") as newfile:
window=bytearray(backup.read(CHUNK_LENGTH*2))
while window:
header_pos=window.find(HEADER)
if header_pos > -1:
newfile.write(b'0'*header_pos+HEADER)
del window[:header_pos+4]
elif len(window)>CHUNK_LENGTH:
newfile.write(b'0'*CHUNK_LENGTH)
del window[:CHUNK_LENGTH]
else:
newfile.write(b'0'*len(window))
del window[:]
window+=bytearray(backup.read(CHUNK_LENGTH))
@Mysak0CZ
Copy link

Hello, @topin89
Recently I've done a bit of digging into lbf files and here are my findings (including extract tool), if you are interested.
https://forum.xda-developers.com/android/general/tool-lg-restore-com-lge-bnr-lbf-file-t4053579

@topin89
Copy link
Author

topin89 commented Feb 26, 2020

Hello, @Mysak0CZ

Holy cow!
Well, I don't really interested in development, but I'm sure as hell I'll link give a link in all files in this gist! I also recommend to make a gist for your script. At least three persons got me directly from here, not from the XDA thread. Repo would be even better.

Edit: "HP" Replaced with "LG". I guess I'm actually tired now.
My script, I did it to restore notes of my friend from some LG notepad app. I didn't even have an LG phone.

How do you get that header is "AES/ECB/PKCS5Padding + SHA256"? What tool do you use? That's what I'm really interested in. To be clear, I have zero crypto-related reverse engineering experience.

Also, link to technical part goes to https://www.amazon.com/dp/1012097587 (Technical Reports, Part 4 by Miami Conservancy District ) and most likely wrong.
Edit: so, random "funny link", XDA? Hilarious.

@tazik561
Copy link

tazik561 commented Nov 2, 2020

I download zip file and extract theme. After extracting i copied my backup into same folder :

tazik@mx-linux:~/recoverlg
$ ls
lbf_extractor.py  lbf_extractor_unpacker.py  LGBackup_201102.lbf  zip_anonymizer.py
tazik@mx-linux:~/recoverlg

I changed filename=f"LGBackup_201102.lbf" in lbf_extractor.py and run it but i got

tazik@mx-linux:~/recoverlg
$ python lbf_extractor.py 
  File "lbf_extractor.py", line 26
    filename=f"LGBackup_201102.lbf"
                                  ^
SyntaxError: invalid syntax
tazik@mx-linux:~/recoverlg

How can i run ?

@topin89
Copy link
Author

topin89 commented Nov 2, 2020

Try python3 lbf_extractor.py. Default python is still python2 on most Linux distros.
Also, unless your phone is really old, I suggest https://github.com/Mysak0CZ/LBFtool
The author actually used debugger to get lbf structure and it should extract all files, not just zip archives.

@woodywood25
Copy link

Hi,
I have a backup file of my phone in LBF format. Could someone help me write my photos?
I can pay.

I do not speak English. I use a translator.

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment