Last active
March 25, 2018 22:18
-
-
Save vgf89/d2c83288bb902b51d32dc019f64d94bb to your computer and use it in GitHub Desktop.
Added support for filetype 0x04, made extraction more restrictive so it only extract files it can find script in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import re | |
def main(): | |
path = "pzz_files" | |
os.chdir(path) | |
directory = os.fsencode(".") | |
for file in os.listdir(directory): | |
filename = os.fsdecode(file) | |
if filename.endswith(".pzz"): | |
extract_script(filename) | |
def extract_script(filename): | |
f = open(filename, "rb") | |
b = f.read() | |
f.close() | |
# If the file doesn't appear to have any script, just ignore it | |
pattern = re.compile("[-0-9]+,[-0-9]+,[-0-9]+,[-0-9]+,[-0-9]+,") | |
matches = pattern.findall(str(b)) | |
if(matches == []): | |
return | |
if (b[0] == 0x3): | |
""" | |
byte 0x04: size of data | |
byte 0x08: size of the script | |
byte 0x0C: size of data | |
""" | |
# Calculate beginning and ending sectors of script | |
header_sectors = 1 + b[0x04] | |
script_sectors = b[0x08] | |
script_start = header_sectors*2048 | |
script_end = script_start + script_sectors*2048 | |
if (script_sectors == 0): | |
return | |
# Extract script, destroy trailing zeros | |
script_bytes = bytearray(b[script_start:script_end]) | |
while (len(script_bytes) > 0 and script_bytes[-1] == 0): | |
script_bytes = script_bytes[:-1] | |
# Write to file script-filename.pzz.txt | |
of = open("script-" + filename + ".txt", "wb") | |
of.write(script_bytes) | |
print("type 0x03 extracted: " + filename) | |
if (b[0] == 0x4): | |
""" | |
byte 0x04: size of data | |
byte 0x08: size of data | |
byte 0x0C: size of data | |
byte 0x10: size of the script | |
""" | |
# Calculate beginning and ending sectors of script | |
header_sectors = 1 + b[0x04] + b[0x08] + b[0x0C] | |
if b[0x0D]: | |
header_sectors += b[0x07] + b[0x0F] | |
script_sectors = b[0x10] | |
script_start = header_sectors*2048 | |
script_end = script_start + script_sectors*2048 | |
if (script_sectors == 0): | |
return | |
# Extract script, destroy trailing zeros | |
script_bytes = bytearray(b[script_start:script_end]) | |
while (len(script_bytes) > 0 and script_bytes[-1] == 0): | |
script_bytes = script_bytes[:-1] | |
# Write to file script-filename.pzz.txt | |
of = open("script-" + filename + ".txt", "wb") | |
of.write(script_bytes) | |
print("type 0x04 extracted: " + filename) | |
elif (b[0] == 0x5): | |
""" | |
byte 0x04: size of data | |
byte 0x08: size of data | |
byte 0x0C: size of the script | |
byte 0x10: size of data | |
byte 0x14: size of data | |
""" | |
# Calculate beginning and ending sectors of script | |
header_sectors = 1 + b[0x04] + b[0x08] | |
script_sectors = b[0x0C] | |
script_start = header_sectors*2048 | |
script_end = script_start + script_sectors*2048 | |
if (script_sectors == 0): | |
return | |
# Extract script, destroy trailing zeros | |
script_bytes = bytearray(b[script_start:script_end]) | |
while (len(script_bytes) > 0 and script_bytes[-1] == 0): | |
script_bytes = script_bytes[:-1] | |
# Write to file script-filename.pzz.txt | |
of = open("script-" + filename + ".txt", "wb") | |
of.write(script_bytes) | |
print("type 0x05 extracted: " + filename) | |
else: | |
return | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
def main(): | |
path = "pzz_files" | |
os.chdir(path) | |
directory = os.fsencode(".") | |
for file in os.listdir(directory): | |
filename = os.fsdecode(file) | |
if filename.endswith(".pzz"): | |
inject_script(filename) | |
def inject_script(filename): | |
f = open(filename, "rb") | |
b = f.read() | |
f.close() | |
if (b[0] == 0x3): | |
""" | |
byte 0x04: size of data | |
byte 0x08: size of the script | |
byte 0x0C: size of data | |
""" | |
# Calculate beginning and ending bytes of each section | |
header_sectors = 1 + b[0x04] | |
header_size = header_sectors * 2048 # Sectors are 2048 bytes each | |
script_sectors = b[0x08] | |
script_size = script_sectors * 2048 | |
footer_sectors = b[0x0C] | |
footer_size = footer_sectors * 2048 | |
if (script_sectors == 0): | |
return | |
# Extract sections | |
header = bytearray(b[:header_size]) | |
footer = b[header_size + script_size:] | |
# grab script file if there is one | |
try: | |
script_file = open("script-" + filename + ".txt", "rb") | |
except: | |
return | |
script = bytearray(script_file.read()) | |
# Pad script up to correct size | |
size_diff = script_sectors * 2048 - len(script) | |
script += b"\0" * (size_diff) | |
# Reconstruct and write pzz file | |
pzz = header + script + footer | |
of = open(filename, "wb") | |
of.write(pzz) | |
print("type 0x03 injected: " + filename) | |
elif (b[0] == 0x4): | |
""" | |
byte 0x04: size of data | |
byte 0x08: size of data | |
byte 0x0C: size of data | |
byte 0x10: size of the script | |
""" | |
if (b[0x13] == 0x80): | |
return | |
# Calculate beginning and ending bytes of each section | |
header_sectors = 1 + b[0x04] + b[0x08] + b[0x0C] | |
if b[0x0D]: | |
header_sectors += b[0x07] + b[0x0F] | |
header_size = header_sectors * 2048 # Sectors are 2048 bytes each | |
script_sectors = b[0x10] | |
script_size = script_sectors * 2048 | |
if (script_sectors == 0): | |
return | |
# Extract sections | |
header = bytearray(b[0:header_size]) | |
# grab script file | |
try: | |
script_file = open("script-" + filename + ".txt", "rb") | |
except: | |
return | |
script = bytearray(script_file.read()) | |
# Pad script up to correct size | |
size_diff = script_size - len(script) | |
script += b"\0" * (size_diff) | |
# Reconstruct and write pzz file | |
pzz = header + script | |
of = open(filename, "wb") | |
of.write(pzz) | |
print("type 0x04 injected: " + filename) | |
elif (b[0] == 0x5): | |
""" | |
bytes 0x04, 0x08: data section sizes | |
byte 0x0C: size of script | |
bytes 0x10, 0x14: data section sizes | |
""" | |
# Calculate beginning and ending bytes of each section | |
header_sectors = 1 + b[0x04] + b[0x08] | |
header_size = header_sectors * 2048 # Sectors are 2048 bytes each | |
script_sectors = b[0x0C] | |
script_size = script_sectors * 2048 | |
footer_sectors = b[0x10] + b[0x14] | |
footer_size = footer_sectors * 2048 | |
if (script_sectors == 0): | |
return | |
# Extract sections | |
header = bytearray(b[0:header_size]) | |
footer = b[header_size + script_size:] | |
# grab script file | |
try: | |
script_file = open("script-" + filename + ".txt", "rb") | |
except: | |
return | |
script = bytearray(script_file.read()) | |
# Pad script up to correct size | |
size_diff = script_sectors * 2048 - len(script) | |
script += b"\0" * (size_diff) | |
# Reconstruct and write pzz file | |
pzz = header + script + footer | |
of = open(filename, "wb") | |
of.write(pzz) | |
print("type 0x05 injected: " + filename) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment