AMI BIOS Guard Extractor v3.2

Each input file name is now shown at the top
Each output file now includes the input file name
Applied a few small static analysis code fixes
This commit is contained in:
Plato Mavropoulos 2021-01-19 16:30:19 +02:00
parent e235b7fbc4
commit 70844b0a24

View file

@ -7,7 +7,7 @@ AMI BIOS Guard Extractor
Copyright (C) 2018-2021 Plato Mavropoulos Copyright (C) 2018-2021 Plato Mavropoulos
""" """
print('AMI BIOS Guard Extractor v3.1') print('AMI BIOS Guard Extractor v3.2')
import sys import sys
@ -15,7 +15,7 @@ import sys
sys_ver = sys.version_info sys_ver = sys.version_info
if sys_ver < (3,7) : if sys_ver < (3,7) :
sys.stdout.write('\n\nError: Python >= 3.7 required, not %d.%d!\n' % (sys_ver[0], sys_ver[1])) sys.stdout.write('\n\nError: Python >= 3.7 required, not %d.%d!\n' % (sys_ver[0], sys_ver[1]))
(raw_input if sys_ver[0] <= 2 else input)('\nPress enter to exit') (raw_input if sys_ver[0] <= 2 else input)('\nPress enter to exit') # pylint: disable=E0602
sys.exit(1) sys.exit(1)
import os import os
@ -64,7 +64,7 @@ class PFAT_Header(ctypes.LittleEndianStructure) :
] ]
def pfat_print(self) : def pfat_print(self) :
print('\nPFAT Main Header:\n') print('\n PFAT Main Header:\n')
print(' Size : 0x%X' % self.Size) print(' Size : 0x%X' % self.Size)
print(' Checksum : 0x%0.4X' % self.Checksum) print(' Checksum : 0x%0.4X' % self.Checksum)
print(' Tag : %s' % self.Tag.decode('utf-8')) print(' Tag : %s' % self.Tag.decode('utf-8'))
@ -174,7 +174,7 @@ def get_struct(buffer, start_offset, class_name, param_list = None) :
fit_len = min(len(struct_data), struct_len) fit_len = min(len(struct_data), struct_len)
if (start_offset >= len(buffer)) or (fit_len < struct_len) : if (start_offset >= len(buffer)) or (fit_len < struct_len) :
input('\nError: Offset 0x%X out of bounds at %s, possibly incomplete image!' % (start_offset, class_name.__name__)) input('\n Error: Offset 0x%X out of bounds at %s, possibly incomplete image!' % (start_offset, class_name.__name__))
sys.exit(1) sys.exit(1)
ctypes.memmove(ctypes.addressof(structure), struct_data, fit_len) ctypes.memmove(ctypes.addressof(structure), struct_data, fit_len)
@ -189,20 +189,19 @@ else :
ami_pfat = [] ami_pfat = []
in_path = input('\nEnter the full folder path: ') in_path = input('\nEnter the full folder path: ')
print('\nWorking...') print('\nWorking...')
for root, dirs, files in os.walk(in_path): for root, _, files in os.walk(in_path):
for name in files : for name in files :
ami_pfat.append(os.path.join(root, name)) ami_pfat.append(os.path.join(root, name))
pfat_index = 1 pfat_index = 1
input_name = ''
input_extension = ''
output_path = '' output_path = ''
block_hdr_size = ctypes.sizeof(PFAT_Block_Header) block_hdr_size = ctypes.sizeof(PFAT_Block_Header)
block_rsa_size = ctypes.sizeof(PFAT_Block_RSA) block_rsa_size = ctypes.sizeof(PFAT_Block_RSA)
pfat_pat = re.compile(b'_AMIPFAT.AMI_BIOS_GUARD_FLASH_CONFIGURATIONS', re.DOTALL) pfat_pat = re.compile(b'_AMIPFAT.AMI_BIOS_GUARD_FLASH_CONFIGURATIONS', re.DOTALL)
for input_file in ami_pfat : for input_file in ami_pfat :
input_name,input_extension = os.path.splitext(os.path.basename(input_file))
input_dir = os.path.dirname(os.path.abspath(input_file))
file_data = b'' file_data = b''
final_data = b'' final_data = b''
block_name = '' block_name = ''
@ -214,6 +213,22 @@ for input_file in ami_pfat :
pfat_match = pfat_pat.search(buffer) pfat_match = pfat_pat.search(buffer)
if pfat_index == 1 :
input_name,input_extension = os.path.splitext(os.path.basename(input_file))
input_dir = os.path.dirname(os.path.abspath(input_file))
print('\n*** %s%s' % (input_name, input_extension))
if not pfat_match :
print('\n Error: This is not an AMI BIOS Guard (PFAT) image!')
continue
output_path = os.path.join(input_dir, '%s%s' % (input_name, input_extension) + '_extracted') # Set extraction directory
if os.path.isdir(output_path) : shutil.rmtree(output_path) # Delete any existing extraction directory
os.mkdir(output_path) # Create extraction directory
if not pfat_match : continue if not pfat_match : continue
buffer = buffer[pfat_match.start() - 0x8:] buffer = buffer[pfat_match.start() - 0x8:]
@ -226,14 +241,7 @@ for input_file in ami_pfat :
pfat_hdr.pfat_print() pfat_hdr.pfat_print()
print(' Title : %s' % hdr_data[0]) print(' Title : %s' % hdr_data[0])
if pfat_index == 1 : file_path = os.path.join(output_path, '%s%s -- %d' % (input_name, input_extension, pfat_index))
output_path = os.path.join(input_dir, '%s%s' % (input_name, input_extension) + '_extracted') # Set extraction directory
if os.path.isdir(output_path) : shutil.rmtree(output_path) # Delete any existing extraction directory
os.mkdir(output_path) # Create extraction directory
file_path = os.path.join(output_path, '%d' % pfat_index)
for entry in hdr_data[1:] : for entry in hdr_data[1:] :
entry_data = entry.split(' ') entry_data = entry.split(' ')
@ -272,7 +280,7 @@ for input_file in ami_pfat :
block_script_decomp = BigScript(code_bytes=block_script_data) block_script_decomp = BigScript(code_bytes=block_script_data)
block_script_lines = block_script_decomp.to_string().replace('\t',' ').split('\n') block_script_lines = block_script_decomp.to_string().replace('\t',' ').split('\n')
for line in block_script_lines : for line in block_script_lines :
spacing = ' ' * 12 if line.endswith(('begin','end',':')) else ' ' * 20 spacing = ' ' * 16 if line.endswith(('begin','end',':')) else ' ' * 24
operands = [op for op in line.split(' ') if op != ''] operands = [op for op in line.split(' ') if op != '']
print(spacing + ('{:<12s}' + '{:<11s}' * (len(operands) - 1)).format(*operands)) print(spacing + ('{:<12s}' + '{:<11s}' * (len(operands) - 1)).format(*operands))
elif not is_opcode_div : elif not is_opcode_div :