Skip to content

Instantly share code, notes, and snippets.

@maalos
Last active October 4, 2023 06:09
Show Gist options
  • Save maalos/e5f5fd012b46e35670fed93aac537709 to your computer and use it in GitHub Desktop.
Save maalos/e5f5fd012b46e35670fed93aac537709 to your computer and use it in GitHub Desktop.
SImple tool for unpacking and repacking firmware files with automatic squashfs decompression allowing for quick and easy embedded filesystem modifications
#!/usr/bin/env python3
import sys
import os
def usage():
print("Usage: firmware-tool <(u)npack/(p)ack> <binary file/unpacked directory> [output filename]")
exit(0)
if len(sys.argv) < 3:
usage
if sys.argv[1] not in ["u", "p"]:
usage
if len(sys.argv) < 4:
outputFirmware = "packed_firmware.bin"
else:
outputFirmware = sys.argv[3]
os.system(f"cp {sys.argv[2]} {outputFirmware}")
sqfsPath = "unpacked/squashfs"
class FirmwarePart:
def __init__(self, name, offset, size, forceOffset = False, forceSize = False):
self.name = name
self.offset = offset
self.size = size
self.forceOffset = forceOffset
self.forceSize = forceSize
"""
SPECIFY YOUR FIRMWARE PARTS HERE
using the output of `binwalk firmware.bin`,
with the format FirmwarePart("part name", offset in bytes, size in bytes, whether to force the part offset, whether to force the part size),
currently set for the firmware of 3G-6408n_v1.19.bin
"""
firmware_parts = [
FirmwarePart("misc-header-data", 0, 11288),
FirmwarePart("lzma-misc-data", 11288, (1114112 - 11288)),
FirmwarePart("squashfs", 1114112, 4353626),
FirmwarePart("misc-footer-data", 5467738, (5468162 - 5467738))
]
if sys.argv[1] == "u":
firmwareFile = open(sys.argv[2], "rb")
os.system("mkdir -v unpacked")
for part in firmware_parts:
outfile = open("unpacked/" + part.name + ".bin", "wb")
firmwareFile.seek(part.offset, 0)
data = firmwareFile.read(part.size)
outfile.write(data)
outfile.close()
print(f"Wrote {part.size} bytes to unpacked/{part.name}.bin")
if part.name == "squashfs":
if os.system(f"unsquashfs -d {sqfsPath} {sqfsPath}.bin"):
print(f"Extracted {sqfsPath}.bin into {sqfsPath}/")
os.remove(f"{sqfsPath}.bin")
if sys.argv[1] == "p":
firmwareFile = open(outputFirmware, "wb")
for part in firmware_parts:
if part.name == "squashfs":
if os.path.exists(f"{sqfsPath}.bin"):
print(f"File {sqfsPath}.bin already exists, removing in order to avoid problems")
os.remove(f"{sqfsPath}.bin")
os.system(f"mksquashfs {sqfsPath}/* {sqfsPath}.bin -comp lzma -no-xattrs -no-sparse -no-progress -quiet")
print(f"Repacking {part.name} from {sys.argv[2]}/", end="")
if part.forceOffset:
firmwareFile.seek(part.offset, 0)
print(f", with forced offset set to {part.offset}", end="")
unpackedPart = open(sys.argv[2] + "/" + part.name + ".bin", "rb")
if part.forceSize:
firmwareFile.write(unpackedPart.read(part.size))
print(f", with forced size set to {part.size}", end="")
else:
firmwareFile.write(unpackedPart.read())
print()
unpackedPart.close()
@maalos
Copy link
Author

maalos commented Sep 29, 2023

shit, binwalk gives wrong size data, gotta fix that

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