def unpackCommand(file, outDir): """Extracts the firmware image from the updater executable, unpacks it and extracts it to the specified directory""" print "Reading installer binary" exeFile = pe.readExe(file.read()) print "Decompressing installer data" zipFile = zip.readZip(zip.findZip(exeFile["_winzip_"].tobytes())) print "Reading .dat file" datFile = dat.readDat(zipFile[dat.findDat(zipFile.keys())]) print "Decoding firmware image" firmwareData = fdat.decryptFdat(datFile["FDAT"].tobytes()) print "Extracting updater image" writeFile(outDir, "updater.img", firmwareData.getImg()) print "Decompressing .tar file" tarFile = tar.readTar(firmwareData.getTar()) print "Extracting files" for path, data in tarFile.iteritems(): if not re.search("^\d{4}_([^/]+)_sum/\\1\.sum$", path): if path.startswith("0700_part_image/dev/nflash") and lzpt.isLzpt(data): print "Decompressing file system image " + os.path.basename(path) data = lzpt.readLzpt(data) writeFile(outDir, path, data) print "Done"
def unpackDat(datFile, fdatFile): print('Decrypting firmware image') datContents = dat.readDat(datFile) crypterName, data = fdat.decryptFdat(datContents.firmwareData) shutil.copyfileobj(data, fdatFile) return { 'normalUsbDescriptors': datContents.normalUsbDescriptors, 'updaterUsbDescriptors': datContents.updaterUsbDescriptors, 'isLens': datContents.isLens, 'crypterName': crypterName, }