diff --git a/elfesteem/elf_init.py b/elfesteem/elf_init.py index b6d1699..9cf1be8 100644 --- a/elfesteem/elf_init.py +++ b/elfesteem/elf_init.py @@ -247,9 +247,6 @@ def add_name(self, name): def mod_name(self, idx, name): name = name_to_bytes(name) n = self.content[idx:self.content.find(data_null, idx)] - data = self.content - if type(data) != str: data = data.pack() - data = data[:idx]+name+data[idx+len(n):] dif = len(name) - len(n) if dif != 0: for sh in self.parent.shlist: diff --git a/elfesteem/macho/init.py b/elfesteem/macho/init.py index 37d61dd..c7d18c3 100755 --- a/elfesteem/macho/init.py +++ b/elfesteem/macho/init.py @@ -476,7 +476,7 @@ def add(self, *args, **kargs): if 'content' in kargs : nwsh = Section(parent=sectionHeader(parent=self.load), content=kargs['content']) - if not nwlc.segname==None: + if not nwlc.segname is None: nwsh.parent.segname = nwlc.segname self.add(nwlc) self.add(nwsh) diff --git a/elfesteem/macho/sections.py b/elfesteem/macho/sections.py index 8eb45b8..ba3bbae 100755 --- a/elfesteem/macho/sections.py +++ b/elfesteem/macho/sections.py @@ -1,6 +1,6 @@ import struct from elfesteem.macho.common import * -from elfesteem.cstruct import Constants, CBase, CData, CString, CArray, CStructWithStrTable +from elfesteem.cstruct import Constants, CBase, CString, CArray, CStructWithStrTable from elfesteem.strpatchwork import StrPatchwork import sys @@ -1092,9 +1092,6 @@ def add_name(self, name): def mod_name(self, idx, name): name = name_to_bytes(name) n = self.content[idx:self.content.find(data_null,idx)] - data = self.content - if type(data) != str: data = data.pack() - data = data[:idx]+name+data[idx+len(n):] dif = len(name) - len(n) if dif != 0: for sh in self.parent.shlist: diff --git a/elfesteem/pe.py b/elfesteem/pe.py index 100e086..fcaf5f4 100755 --- a/elfesteem/pe.py +++ b/elfesteem/pe.py @@ -2,7 +2,7 @@ from elfesteem.cstruct import Constants, CBase, CString, CStruct, CArray from elfesteem.cstruct import data_null, data_empty -from elfesteem.cstruct import bytes_to_name, name_to_bytes +from elfesteem.cstruct import bytes_to_name from elfesteem.strpatchwork import StrPatchwork import struct import logging diff --git a/elfesteem/rprc.py b/elfesteem/rprc.py index 36c49e3..9eb9171 100644 --- a/elfesteem/rprc.py +++ b/elfesteem/rprc.py @@ -20,7 +20,7 @@ # is used, e.g. a value of 'version' greater than 2 in the header. import struct -from elfesteem.cstruct import CBase, CData, CStruct, data_null, data_empty +from elfesteem.cstruct import CData, CStruct, data_null, data_empty from elfesteem.strpatchwork import StrPatchwork # Section types diff --git a/examples/otool.py b/examples/otool.py index 30ea670..18f1a0f 100755 --- a/examples/otool.py +++ b/examples/otool.py @@ -1,12 +1,10 @@ #! /usr/bin/env python import sys, os -import time import platform sys.path.insert(1, os.path.abspath(sys.path[0]+'/..')) from elfesteem import macho_init, macho -from elfesteem.cstruct import data_null, CBase def print_header(e, **fargs): print("Mach header") diff --git a/examples/readpe.py b/examples/readpe.py index 6965e1f..2558953 100755 --- a/examples/readpe.py +++ b/examples/readpe.py @@ -7,7 +7,7 @@ sys.path.insert(1, os.path.abspath(sys.path[0]+'/..')) from elfesteem import pe_init, pe -import pprint, struct +import struct def test_rebuild(e): bin = str(e) diff --git a/tests/test_all.py b/tests/test_all.py index 6f66252..c63f32b 100755 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -75,6 +75,12 @@ def test_MD5(assertion): hashlib.md5(struct.pack('BBBB',116,111,116,111)).hexdigest(), 'MD5') +def open_read(f): + fd = open(f, 'rb') + data = fd.read() + fd.close() + return data + if __name__ == "__main__": exit_value = 0 print_colored.bold('test_MD5') diff --git a/tests/test_elf_manipulation.py b/tests/test_elf_manipulation.py index 16aeb58..4668b0a 100644 --- a/tests/test_elf_manipulation.py +++ b/tests/test_elf_manipulation.py @@ -3,7 +3,7 @@ import os __dir__ = os.path.dirname(__file__) -from test_all import run_tests, assertion, hashlib +from test_all import run_tests, assertion, hashlib, open_read from elfesteem.strpatchwork import StrPatchwork from elfesteem.elf_init import ELF, log from elfesteem import elf @@ -34,7 +34,7 @@ def test_ELF_empty(assertion): def test_ELF_invalid(assertion): try: - e = ELF(open(__dir__+'/binary_input/README.txt', 'rb').read()) + e = ELF(open_read(__dir__+'/binary_input/README.txt')) assertion(0,1, 'Not an ELF') except ValueError: pass @@ -56,7 +56,7 @@ def test_ELF_creation(assertion): def test_ELF_small32(assertion): global log_history - elf_small = open(__dir__+'/binary_input/elf_small.out', 'rb').read() + elf_small = open_read(__dir__+'/binary_input/elf_small.out') assertion('d5284d5f438e25ef5502a0c1de97d84f', hashlib.md5(elf_small).hexdigest(), 'Reading elf_small.out') @@ -153,7 +153,7 @@ def test_ELF_small32(assertion): 'Find pattern (not existing)') def test_ELF_small64(assertion): - elf64_small = open(__dir__+'/binary_input/elf64_small.out', 'rb').read() + elf64_small = open_read(__dir__+'/binary_input/elf64_small.out') assertion('dc21d928bb6a3a0fa59b17fafe803d50', hashlib.md5(elf64_small).hexdigest(), 'Reading elf64_small.out') @@ -177,7 +177,7 @@ def test_ELF_small64(assertion): 'Display Reloc Table (elf64)') def test_ELF_group(assertion): - elf_group = open(__dir__+'/binary_input/elf_cpp.o', 'rb').read() + elf_group = open_read(__dir__+'/binary_input/elf_cpp.o') assertion('57fed5de9474bc0600173a1db5ee6327', hashlib.md5(elf_group).hexdigest(), 'Reading elf_cpp.o') @@ -193,7 +193,7 @@ def test_ELF_group(assertion): 'Display Group Section') def test_ELF_TMP320C6x(assertion): - elf_tmp320c6x = open(__dir__+'/binary_input/notle-tesla-dsp.xe64T', 'rb').read() + elf_tmp320c6x = open_read(__dir__+'/binary_input/notle-tesla-dsp.xe64T') assertion('fb83ed8d809f394e70f5d84d0c8e593f', hashlib.md5(elf_tmp320c6x).hexdigest(), 'Reading notle-tesla-dsp.xe64T') @@ -281,7 +281,7 @@ def test_ELF_wordsize_endianess(assertion): def test_ELF_tiny84(assertion): global log_history - elf_tiny = open(__dir__+'/binary_input/tiny84.bin', 'rb').read() + elf_tiny = open_read(__dir__+'/binary_input/tiny84.bin') assertion('90f9fa06566389883d82b9cda016b10d', hashlib.md5(elf_tiny).hexdigest(), 'Reading tiny84') @@ -297,7 +297,7 @@ def test_ELF_tiny84(assertion): def test_ELF_tiny76(assertion): global log_history - elf_tiny = open(__dir__+'/binary_input/tiny76.bin', 'rb').read() + elf_tiny = open_read(__dir__+'/binary_input/tiny76.bin') assertion('3a5753c93c492d2d1d3fc6c227baec7a', hashlib.md5(elf_tiny).hexdigest(), 'Reading tiny76') @@ -313,7 +313,7 @@ def test_ELF_tiny76(assertion): def test_ELF_tiny64(assertion): global log_history - elf_tiny = open(__dir__+'/binary_input/tiny64.bin', 'rb').read() + elf_tiny = open_read(__dir__+'/binary_input/tiny64.bin') assertion('0dd8a6325f7cf633ed8c527add5dc634', hashlib.md5(elf_tiny).hexdigest(), 'Reading tiny64') @@ -330,7 +330,7 @@ def test_ELF_tiny64(assertion): def test_ELF_tiny52(assertion): global log_history - elf_tiny = open(__dir__+'/binary_input/tiny52.bin', 'rb').read() + elf_tiny = open_read(__dir__+'/binary_input/tiny52.bin') assertion('18ddd4966cb003b80862735d19ddbeb7', hashlib.md5(elf_tiny).hexdigest(), 'Reading tiny52') @@ -348,7 +348,7 @@ def test_ELF_tiny52(assertion): def test_ELF_tiny45(assertion): global log_history - elf_tiny = open(__dir__+'/binary_input/tiny45.bin', 'rb').read() + elf_tiny = open_read(__dir__+'/binary_input/tiny45.bin') assertion('44023f74799f2e009a1400c74de50cdd', hashlib.md5(elf_tiny).hexdigest(), 'Reading tiny45') diff --git a/tests/test_macho_manipulation.py b/tests/test_macho_manipulation.py index 4f99938..b7672f4 100644 --- a/tests/test_macho_manipulation.py +++ b/tests/test_macho_manipulation.py @@ -4,7 +4,7 @@ __dir__ = os.path.dirname(__file__) __dir__ += '/binary_input/macho/' -from test_all import run_tests, assertion, hashlib +from test_all import run_tests, assertion, hashlib, open_read import struct from elfesteem.macho import MACHO, log from elfesteem import macho @@ -164,7 +164,7 @@ def test_MACHO_load_commands(assertion): def test_MACHO_macho32_obj(assertion): global log_history # Parsing and modifying files - macho_32 = open(__dir__+'macho_32.o', 'rb').read() + macho_32 = open_read(__dir__+'macho_32.o') macho_32_hash = hashlib.md5(macho_32).hexdigest() e = MACHO(macho_32) d = e.pack() @@ -215,7 +215,7 @@ def test_MACHO_macho32_obj(assertion): def test_MACHO_macho32_exe(assertion): global log_history - macho_32 = open(__dir__+'macho_32.out', 'rb').read() + macho_32 = open_read(__dir__+'macho_32.out') macho_32_hash = hashlib.md5(macho_32).hexdigest() e = MACHO(macho_32) d = e.pack() @@ -238,7 +238,7 @@ def test_MACHO_macho32_exe(assertion): log_history = [] def test_MACHO_macho64_obj(assertion): - macho_64 = open(__dir__+'macho_64.o', 'rb').read() + macho_64 = open_read(__dir__+'macho_64.o') macho_64_hash = hashlib.md5(macho_64).hexdigest() e = MACHO(macho_64) d = e.pack() @@ -247,7 +247,7 @@ def test_MACHO_macho64_obj(assertion): 'Packing after reading 64-bit Mach-O object') def test_MACHO_macho64_exe(assertion): - macho_64 = open(__dir__+'macho_64.out', 'rb').read() + macho_64 = open_read(__dir__+'macho_64.out') macho_64_hash = hashlib.md5(macho_64).hexdigest() e = MACHO(macho_64) d = e.pack() @@ -257,7 +257,7 @@ def test_MACHO_macho64_exe(assertion): def test_MACHO_fat(assertion): global log_history - macho_fat = open(__dir__+'macho_fat.out', 'rb').read() + macho_fat = open_read(__dir__+'macho_fat.out') macho_fat_hash = hashlib.md5(macho_fat).hexdigest() e = MACHO(macho_fat) d = e.pack() @@ -283,7 +283,7 @@ def test_MACHO_fat(assertion): log_history = [] def test_MACHO_virt(assertion): - macho_32 = open(__dir__+'macho_32.out', 'rb').read() + macho_32 = open_read(__dir__+'macho_32.out') e = MACHO(macho_32) d = e.virt[0x1f9c:0x1fae] assertion('structure definie\0', @@ -307,7 +307,7 @@ def test_MACHO_virt(assertion): 'Adding a section (32 bits)') def test_MACHO_bin_sh(assertion): - macho_bin = open(__dir__+'sh', 'rb').read() + macho_bin = open_read(__dir__+'sh') e = MACHO(macho_bin) macho_bin_hash = hashlib.md5(macho_bin).hexdigest() d = e.pack() @@ -317,7 +317,7 @@ def test_MACHO_bin_sh(assertion): def test_MACHO_lib_dns(assertion): global log_history - macho_lib = open(__dir__+'libdns_services.dylib', 'rb').read() + macho_lib = open_read(__dir__+'libdns_services.dylib') e = MACHO(macho_lib) assertion(e.entrypoint, -1, 'No entrypoint in a Mach-O library') @@ -336,7 +336,7 @@ def test_MACHO_lib_dns(assertion): 'Otool-like output including LC_SOURCE_VERSION') def test_MACHO_lib_ecpg(assertion): - macho_lib = open(__dir__+'libecpg.6.5.dylib', 'rb').read() + macho_lib = open_read(__dir__+'libecpg.6.5.dylib') e = MACHO(macho_lib) macho_lib_hash = hashlib.md5(macho_lib).hexdigest() d = e.pack() @@ -356,7 +356,7 @@ def test_MACHO_lib_ecpg(assertion): 'Display symbol with N_STAB type') def test_MACHO_lib_ATcommand(assertion): - macho_lib = open(__dir__+'libATCommandStudioDynamic.dylib', 'rb').read() + macho_lib = open_read(__dir__+'libATCommandStudioDynamic.dylib') e = MACHO(macho_lib) macho_lib_hash = hashlib.md5(macho_lib).hexdigest() d = e.pack() @@ -377,7 +377,7 @@ def test_MACHO_lib_ATcommand(assertion): 'dyldinfo-like output for dyld opcodes (libATCommand...)') def test_MACHO_lib_system(assertion): - macho_lib = open(__dir__+'libSystem.B.dylib', 'rb').read() + macho_lib = open_read(__dir__+'libSystem.B.dylib') e = MACHO(macho_lib) macho_lib_hash = hashlib.md5(macho_lib).hexdigest() d = e.pack() @@ -398,7 +398,7 @@ def test_MACHO_lib_system(assertion): 'dyldinfo-like output for rebase opcodes (libSystem)') def test_MACHO_lib_tls(assertion): - macho_lib = open(__dir__+'libcoretls.dylib', 'rb').read() + macho_lib = open_read(__dir__+'libcoretls.dylib') e = MACHO(macho_lib) macho_lib_hash = hashlib.md5(macho_lib).hexdigest() d = e.pack() @@ -414,7 +414,7 @@ def test_MACHO_lib_tls(assertion): def test_MACHO_app_OSXII(assertion): global log_history - macho_app = open(__dir__+'OSXII', 'rb').read() + macho_app = open_read(__dir__+'OSXII') e = MACHO(macho_app) assertion([('warn', ('parse_dynamic_symbols() can only be used with x86 architectures, not %s', 18), {})], log_history, @@ -432,7 +432,7 @@ def test_MACHO_app_OSXII(assertion): def test_MACHO_app_MTR(assertion): global log_history - macho_app = open(__dir__+'MacTheRipper', 'rb').read() + macho_app = open_read(__dir__+'MacTheRipper') e = MACHO(macho_app) assertion([('warn', ('parse_dynamic_symbols() can only be used with x86 architectures, not %s', 18), {})], log_history, @@ -452,7 +452,7 @@ def test_MACHO_app_MTR(assertion): def test_MACHO_exe_SH3D(assertion): global log_history - macho_app = open(__dir__+'SweetHome3D', 'rb').read() + macho_app = open_read(__dir__+'SweetHome3D') e = MACHO(macho_app) assertion([('warn', ('parse_dynamic_symbols() can only be used with x86 architectures, not %s', 18), {})], log_history, @@ -470,7 +470,7 @@ def test_MACHO_exe_SH3D(assertion): def test_MACHO_lib_print(assertion): global log_history - macho_32be = open(__dir__+'libPrintServiceQuota.1.dylib', 'rb').read() + macho_32be = open_read(__dir__+'libPrintServiceQuota.1.dylib') e = MACHO(macho_32be) assertion([('warn', ('parse_dynamic_symbols() can only be used with x86 architectures, not %s', 18), {})], log_history, @@ -488,7 +488,7 @@ def test_MACHO_lib_print(assertion): def test_MACHO_ios_decibels(assertion): global log_history - macho_ios = open(__dir__+'Decibels', 'rb').read() + macho_ios = open_read(__dir__+'Decibels') e = MACHO(macho_ios) assertion([('warn', ('Some encrypted text is not parsed with the section headers of LC_SEGMENT(__TEXT)',), {}), ('warn', ('parse_dynamic_symbols() can only be used with x86 architectures, not %s', 12), {}), @@ -511,7 +511,7 @@ def test_MACHO_ios_decibels(assertion): def test_MACHO_ios_lyonmetro(assertion): global log_history - macho_ios = open(__dir__+'LyonMetro', 'rb').read() + macho_ios = open_read(__dir__+'LyonMetro') e = MACHO(macho_ios) assertion([('warn', ('Some encrypted text is not parsed with the section headers of LC_SEGMENT(__TEXT)',), {}), ('warn', ('parse_dynamic_symbols() can only be used with x86 architectures, not %s', 12), {}), @@ -533,7 +533,7 @@ def test_MACHO_ios_lyonmetro(assertion): def test_MACHO_obj_telephony(assertion): global log_history - macho_linkopt = open(__dir__+'TelephonyUtil.o', 'rb').read() + macho_linkopt = open_read(__dir__+'TelephonyUtil.o') macho_linkopt_hash = hashlib.md5(macho_linkopt).hexdigest() e = MACHO(macho_linkopt) assertion([('warn', ('Part of the file was not parsed: %d bytes', 6), {})], @@ -551,7 +551,7 @@ def test_MACHO_obj_telephony(assertion): def test_MACHO_loader_lc_build_version(assertion): global log_history - macho_lcbuild = open(__dir__+'macho_lcbuild.out', 'rb').read() + macho_lcbuild = open_read(__dir__+'macho_lcbuild.out') macho_lcbuild_hash = hashlib.md5(macho_lcbuild).hexdigest() e = MACHO(macho_lcbuild) d = e.pack() @@ -565,7 +565,7 @@ def test_MACHO_loader_lc_build_version(assertion): def test_MACHO_prebind_32(assertion): global log_history - macho_32 = open(__dir__+'macho_32.out', 'rb').read() + macho_32 = open_read(__dir__+'macho_32.out') e = MACHO(macho_32) e.add(macho.LoadCommand(sex='<',wsize=32,cmd=0)) d = e.pack() @@ -607,7 +607,7 @@ def test_MACHO_prebind_32(assertion): def test_MACHO_prebind_64(assertion): global log_history - macho_64 = open(__dir__+'macho_64.out', 'rb').read() + macho_64 = open_read(__dir__+'macho_64.out') e = MACHO(macho_64) d = e.virt[0x100000f50:0x100000f62] assertion('structure definie\0', @@ -642,7 +642,7 @@ def test_MACHO_unixthread_32(assertion): # The function changeMainToUnixThread migrates a Mach-O binary for # recent MacOSX (using a LC_MAIN loader) to a Mac-O binary for older # versions of MacOSX (10.7 and older, using a LC_UNIXTHREAD loader). - macho_32 = open(__dir__+'macho_32.out', 'rb').read() + macho_32 = open_read(__dir__+'macho_32.out') e = MACHO(macho_32) changeMainToUnixThread(e) d = e.pack() @@ -656,7 +656,7 @@ def test_MACHO_unixthread_32(assertion): 'Migrating from LC_MAIN to LC_UNIXTHREAD with new segment (32 bits)') def test_MACHO_unixthread_64(assertion): - macho_64 = open(__dir__+'macho_64.out', 'rb').read() + macho_64 = open_read(__dir__+'macho_64.out') e = MACHO(macho_64) changeMainToUnixThread(e) d = e.pack() @@ -670,7 +670,7 @@ def test_MACHO_unixthread_64(assertion): 'Migrating from LC_MAIN to LC_UNIXTHREAD with new segment (64 bits)') def test_MACHO_changeUUID(assertion): - macho_64 = open(__dir__+'macho_64.out', 'rb').read() + macho_64 = open_read(__dir__+'macho_64.out') e = MACHO(macho_64) e.changeUUID("2A0405CF8B1F3502A605695A54C407BB") uuid_pos, = e.load.getpos(macho.LC_UUID) @@ -695,7 +695,7 @@ def test_MACHO_changeUUID(assertion): 'set UUID (pack)') def test_MACHO_extend_segment(assertion): - macho_64 = open(__dir__+'macho_64.out', 'rb').read() + macho_64 = open_read(__dir__+'macho_64.out') e = MACHO(macho_64) for l in e.load: if getattr(l,'segname',None) == "__LINKEDIT": break diff --git a/tests/test_minidump_manipulation.py b/tests/test_minidump_manipulation.py index f2c6da3..def9680 100644 --- a/tests/test_minidump_manipulation.py +++ b/tests/test_minidump_manipulation.py @@ -3,11 +3,11 @@ import os __dir__ = os.path.dirname(__file__) -from test_all import run_tests, assertion, hashlib +from test_all import run_tests, assertion, hashlib, open_read from elfesteem.minidump_init import Minidump def test_MD_windows(assertion): - md = open(__dir__+'/binary_input/windows.dmp', 'rb').read() + md = open_read(__dir__+'/binary_input/windows.dmp') assertion('82a09a9d801bddd1dc94dfb9ba6eddf0', hashlib.md5(md).hexdigest(), 'Reading windows.dmp') @@ -18,7 +18,7 @@ def test_MD_windows(assertion): 'Displaying the content of windows.dmp') def test_MD_i386(assertion): - md = open(__dir__+'/binary_input/minidump-i386.dmp', 'rb').read() + md = open_read(__dir__+'/binary_input/minidump-i386.dmp') assertion('0f2ee1a0a2e6351e64929197c07679e6', hashlib.md5(md).hexdigest(), 'Reading minidump-i386.dmp') @@ -29,7 +29,7 @@ def test_MD_i386(assertion): 'Displaying the content of minidump-i386.dmp') def test_MD_x86_64(assertion): - md = open(__dir__+'/binary_input/minidump-x86_64.dmp', 'rb').read() + md = open_read(__dir__+'/binary_input/minidump-x86_64.dmp') assertion('ecde7af61615e05ffcde1f064c1a22f8', hashlib.md5(md).hexdigest(), 'Reading minidump-x86_64.dmp') diff --git a/tests/test_pe_manipulation.py b/tests/test_pe_manipulation.py index a47abfa..fe7c0df 100644 --- a/tests/test_pe_manipulation.py +++ b/tests/test_pe_manipulation.py @@ -3,7 +3,7 @@ import os __dir__ = os.path.dirname(__file__) -from test_all import run_tests, assertion, hashlib +from test_all import run_tests, assertion, hashlib, open_read from elfesteem.pe_init import log, PE, COFF, Coff from elfesteem.strpatchwork import StrPatchwork from elfesteem import pe @@ -50,7 +50,7 @@ def test_PE_empty64(assertion): def test_PE_manipulate(assertion): global log_history - pe_mingw = open(__dir__+'/binary_input/pe_mingw.exe', 'rb').read() + pe_mingw = open_read(__dir__+'/binary_input/pe_mingw.exe') e = PE(pe_mingw) # Packed file is not identical :-( # Are missing: @@ -274,7 +274,7 @@ def test_PE_manipulate(assertion): def test_PE_dll(assertion): global log_history # Small DLL created with Visual Studio - dll_vstudio = open(__dir__+'/binary_input/pe_vstudio.dll', 'rb').read() + dll_vstudio = open_read(__dir__+'/binary_input/pe_vstudio.dll') e = PE(dll_vstudio) d = e.pack() assertion('19028e1a1bde785fb4a58aeacf56007b', @@ -306,51 +306,51 @@ def test_PE_dll(assertion): def test_PE_ange(assertion): global log_history # Parse some ill-formed PE made by Ange Albertini - e = PE(open(__dir__+'/binary_input/Ange/resourceloop.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/resourceloop.exe')) assertion([('warn', ('Resource tree too deep',), {})]*212, log_history, 'Ange/resourceloop.exe (logs)') log_history = [] - e = PE(open(__dir__+'/binary_input/Ange/namedresource.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/namedresource.exe')) assertion([], log_history, 'Ange/namedresource.exe (logs)') - e = PE(open(__dir__+'/binary_input/Ange/weirdsord.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/weirdsord.exe')) assertion([('warn', ('Section %d offset %#x not aligned to %#x', 0, 513, 16384), {}), ('warn', ('Section %d size %#x not aligned to %#x', 0, 270, 16384), {})], log_history, 'Ange/weirdsord.exe (logs)') log_history = [] - e = PE(open(__dir__+'/binary_input/Ange/nosectionW7.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/nosectionW7.exe')) assertion([('warn', ('Number of rva %d does not match sizeofoptionalheader %d', 16, 0), {})], log_history, 'Ange/nosectionW7.exe (logs)') log_history = [] - e = PE(open(__dir__+'/binary_input/Ange/imports_relocW7.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/imports_relocW7.exe')) assertion([], log_history, 'Ange/imports_relocW7.exe (logs)') - e = PE(open(__dir__+'/binary_input/Ange/imports_tinyXP.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/imports_tinyXP.exe')) assertion([], log_history, 'Ange/imports_tinyXP.exe (logs)') - e = PE(open(__dir__+'/binary_input/Ange/bottomsecttbl.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/bottomsecttbl.exe')) assertion([('warn', ('Number of rva %d does not match sizeofoptionalheader %d', 16, 696), {})], log_history, 'Ange/bottomsecttbl.exe (logs)') log_history = [] - e = PE(open(__dir__+'/binary_input/Ange/delayfake.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/delayfake.exe')) assertion([], log_history, 'Ange/delayfake.exe (logs)') - e = PE(open(__dir__+'/binary_input/Ange/exportobf.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/exportobf.exe')) assertion([], log_history, 'Ange/exportobf.exe (logs)') - e = PE(open(__dir__+'/binary_input/Ange/dllbound-ld.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/dllbound-ld.exe')) assertion([], log_history, 'Ange/dllbound-ld.exe (logs)') - e = PE(open(__dir__+'/binary_input/Ange/d_tiny.dll', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/d_tiny.dll')) assertion([('warn', ('Opthdr magic %#x', 31074), {}), ('warn', ('Number of rva %d does not match sizeofoptionalheader %d', 0, 13864), {}), ('warn', ('Windows 8 needs at least 13 directories, %d found', 0), {}), @@ -359,18 +359,18 @@ def test_PE_ange(assertion): log_history, 'Ange/d_tiny.dll (logs)') log_history = [] - e = PE(open(__dir__+'/binary_input/Ange/dllfw.dll', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/dllfw.dll')) assertion([], log_history, 'Ange/dllfw.dll (logs)') - e = PE(open(__dir__+'/binary_input/Ange/tinydllXP.dll', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/tinydllXP.dll')) assertion([('warn', ('Number of rva %d does not match sizeofoptionalheader %d', 0, 0), {}), ('warn', ('Windows 8 needs at least 13 directories, %d found', 0), {}), ('warn', ('File too short for StrTable 0x55 != 0xc258016a',), {})], log_history, 'Ange/tinydllXP.dll (logs)') log_history = [] - e = PE(open(__dir__+'/binary_input/Ange/resourceloop.exe', 'rb').read()) + e = PE(open_read(__dir__+'/binary_input/Ange/resourceloop.exe')) log_history = [] d = e.DirRes.display().encode('latin1') assertion('98701be30b09759a64340e5245e48195', @@ -399,13 +399,13 @@ def test_COFF_invalid(assertion): # Now, we parse COFF files try: # Not COFF: OptHdr size too big - e = Coff(open(__dir__+'/binary_input/README.txt', 'rb').read()) + e = Coff(open_read(__dir__+'/binary_input/README.txt')) assertion(0,1, 'Not COFF') except ValueError: pass def test_COFF_valid(assertion): - obj_mingw = open(__dir__+'/binary_input/coff_mingw.obj', 'rb').read() + obj_mingw = open_read(__dir__+'/binary_input/coff_mingw.obj') try: e = PE(obj_mingw) assertion(0,1, 'Not PE') @@ -420,7 +420,7 @@ def test_COFF_valid(assertion): assertion(None, d, 'No virt for .obj') def test_COFF_tms320(assertion): - out_tms320 = open(__dir__+'/binary_input/C28346_Load_Program_to_Flash.out', 'rb').read() + out_tms320 = open_read(__dir__+'/binary_input/C28346_Load_Program_to_Flash.out') e = Coff(out_tms320) d = e.SHList.display().encode('latin1') assertion('a63cf686186105b83e49509f213b20ea', @@ -429,27 +429,27 @@ def test_COFF_tms320(assertion): def test_COFF_ckermit(assertion): # C-Kermit binary for OSF1 - out_osf1 = open(__dir__+'/binary_input/cku200.dec-osf-1.3a', 'rb').read() + out_osf1 = open_read(__dir__+'/binary_input/cku200.dec-osf-1.3a') e = Coff(out_osf1) d = repr(e.OSF1Symbols).encode('latin1') assertion('c7df867846612e6fc1c52a8042f706cc', hashlib.md5(d).hexdigest(), 'Display OSF/1 Symbols') # C-Kermit binary for Clipper CLIX - e = Coff(open(__dir__+'/binary_input/cku196.clix-3.1', 'rb').read()) + e = Coff(open_read(__dir__+'/binary_input/cku196.clix-3.1')) # C-Kermit binary for Apollo - e = Coff(open(__dir__+'/binary_input/cku193a05.apollo-sr10-s5r3', 'rb').read()) + e = Coff(open_read(__dir__+'/binary_input/cku193a05.apollo-sr10-s5r3')) # C-Kermit XCOFF32 binary for AIX - e = Coff(open(__dir__+'/binary_input/cku190.rs6aix32c-3.2.4', 'rb').read()) + e = Coff(open_read(__dir__+'/binary_input/cku190.rs6aix32c-3.2.4')) # C-Kermit eCOFF32 binary for MIPS, big endian - e = Coff(open(__dir__+'/binary_input/cku192.irix40', 'rb').read()) + e = Coff(open_read(__dir__+'/binary_input/cku192.irix40')) # C-Kermit eCOFF32 binary for MIPS, little endian - e = Coff(open(__dir__+'/binary_input/cku192.ultrix43c-mips3', 'rb').read()) + e = Coff(open_read(__dir__+'/binary_input/cku192.ultrix43c-mips3')) def test_COFF_invalidity(assertion): global log_history # Some various ways for a COFF to be detected as invalid - obj_mingw = open(__dir__+'/binary_input/coff_mingw.obj', 'rb').read() + obj_mingw = open_read(__dir__+'/binary_input/coff_mingw.obj') obj_mingw = StrPatchwork(obj_mingw) e = COFF(obj_mingw) try: diff --git a/tests/test_rprc_manipulation.py b/tests/test_rprc_manipulation.py index c9fa575..8d2b37c 100644 --- a/tests/test_rprc_manipulation.py +++ b/tests/test_rprc_manipulation.py @@ -3,7 +3,7 @@ import os __dir__ = os.path.dirname(__file__) -from test_all import run_tests, assertion, hashlib +from test_all import run_tests, assertion, hashlib, open_read from elfesteem.rprc import RPRC def test_RPRC_empty(assertion): @@ -18,7 +18,7 @@ def test_RPRC_empty(assertion): 'Creation of a standard empty RPRC; fix point') def test_RPRC_ducati(assertion): - rprc_m3 = open(__dir__+'/binary_input/ducati-m3_p768.bin', 'rb').read() + rprc_m3 = open_read(__dir__+'/binary_input/ducati-m3_p768.bin') assertion('d31c5887b98b37f949da3570b8688983', hashlib.md5(rprc_m3).hexdigest(), 'Reading ducati-m3_p768.bin') @@ -77,7 +77,7 @@ def test_RPRC_ducati(assertion): def test_RPRC_invalid(assertion): try: - e = RPRC(open(__dir__+'/binary_input/README.txt', 'rb').read()) + e = RPRC(open_read(__dir__+'/binary_input/README.txt')) assertion(0,1, 'Not an RPRC') except ValueError: pass