def test_expected(self): for file_name in elf_ph_expected: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() PhClass = Elf32ProgramHeader header.fromdata(data) elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) PhClass = Elf64ProgramHeader else: raise "Problem" self.assertEqual(header.e_phnum, len(elf_ph_expected[file_name])) entsize = header.e_phentsize wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() for i in range(header.e_phnum): ph_expect = elf_ph_expected[file_name][i] ph_data = f.get_data(header.e_phoff + (i * entsize), entsize) ph = PhClass(header.ident.endianess) ph.fromdata(ph_data, hdr) self.assertEqual(ph.todata(), ph_data) self.assertEqual(ph.p_type, ph_expect[0]) self.assertEqual(ph.p_offset, ph_expect[1]) self.assertEqual(ph.p_vaddr, ph_expect[2]) self.assertEqual(ph.p_paddr, ph_expect[3]) self.assertEqual(ph.p_filesz, ph_expect[4]) self.assertEqual(ph.p_memsz, ph_expect[5]) self.assertEqual(ph.p_flags, ph_expect[6]) self.assertEqual(ph.p_align, ph_expect[7])
def test_expected(self): for file_name in elf_ph_expected: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() PhClass = Elf32ProgramHeader header.fromdata(data) elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) PhClass = Elf64ProgramHeader else: raise "Problem" self.assertEqual(header.e_phnum, len(elf_ph_expected[file_name])) entsize = header.e_phentsize wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() for i in range(header.e_phnum): ph_expect = elf_ph_expected[file_name][i] ph_data = f.get_data(header.e_phoff + (i * entsize), entsize) ph = PhClass(header.ident.endianess) ph.fromdata(ph_data, hdr) self.assertEqual(ph.todata(), ph_data) self.assertEqual(ph.p_type, ph_expect[0]) self.assertEqual(ph.p_offset, ph_expect[1]) self.assertEqual(ph.p_vaddr, ph_expect[2]) self.assertEqual(ph.p_paddr, ph_expect[3]) self.assertEqual(ph.p_filesz, ph_expect[4]) self.assertEqual(ph.p_memsz, ph_expect[5]) self.assertEqual(ph.p_flags, ph_expect[6]) self.assertEqual(ph.p_align, ph_expect[7])
def setUp(self): self.elf_headers = {} for file_name in elf_files: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() header.fromdata(data) elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) else: raise "Problem" self.elf_headers[file_name] = (header, data) self.bogus_data_size = f.get_data(0, Elf32Header.size() - 1)
def setUp(self): self.elf_headers = {} for file_name in elf_files: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() header.fromdata(data) elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) else: raise "Problem" self.elf_headers[file_name] = (header, data) self.bogus_data_size = f.get_data(0, Elf32Header.size() - 1)
def test_expected(self): for file_name in elf_sh_expected: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() ShClass = Elf32SectionHeader header.fromdata(data) elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) ShClass = Elf64SectionHeader else: raise "Problem" self.assertEqual(header.e_shnum, len(elf_sh_expected[file_name])) entsize = header.e_shentsize for i in range(header.e_shnum): sh_expect = elf_sh_expected[file_name][i] sh_data = f.get_data(header.e_shoff + (i * entsize), entsize) sh = ShClass(header.ident.endianess) sh.fromdata(sh_data, hdr) self.assertEqual(sh.todata(), sh_data) self.assertEqual(sh.sh_name, sh_expect[0]) self.assertEqual(sh.sh_type, sh_expect[1]) self.assertEqual(sh.sh_addr, sh_expect[2]) self.assertEqual(sh.sh_offset, sh_expect[3]) self.assertEqual(sh.sh_size, sh_expect[4]) self.assertEqual(sh.sh_entsize, sh_expect[5]) self.assertEqual(sh.sh_flags, sh_expect[6]) self.assertEqual(sh.sh_link, sh_expect[7]) self.assertEqual(sh.sh_info, sh_expect[8]) self.assertEqual(sh.sh_addralign, sh_expect[9])
def test_expected(self): for file_name in elf_sh_expected: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() ShClass = Elf32SectionHeader header.fromdata(data) elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) ShClass = Elf64SectionHeader else: raise "Problem" self.assertEqual(header.e_shnum, len(elf_sh_expected[file_name])) entsize = header.e_shentsize for i in range(header.e_shnum): sh_expect = elf_sh_expected[file_name][i] sh_data = f.get_data(header.e_shoff + (i * entsize), entsize) sh = ShClass(header.ident.endianess) sh.fromdata(sh_data, hdr) self.assertEqual(sh.todata(), sh_data) self.assertEqual(sh.sh_name, sh_expect[0]) self.assertEqual(sh.sh_type, sh_expect[1]) self.assertEqual(sh.sh_addr, sh_expect[2]) self.assertEqual(sh.sh_offset, sh_expect[3]) self.assertEqual(sh.sh_size, sh_expect[4]) self.assertEqual(sh.sh_entsize, sh_expect[5]) self.assertEqual(sh.sh_flags, sh_expect[6]) self.assertEqual(sh.sh_link, sh_expect[7]) self.assertEqual(sh.sh_info, sh_expect[8]) self.assertEqual(sh.sh_addralign, sh_expect[9])
def test_against_readelf(self): if not have_readelf: return for file_name in elf_sh_files: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() lines = os.popen("readelf -S %s" % file_name).readlines() good_lines = [] if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() ShClass = Elf32SectionHeader header.fromdata(data) got_start = False for line in lines: if got_start: if line.strip().startswith("Key to Flags"): break good_lines.append(line) else: # Looking for start if line.strip().startswith("[Nr]"): got_start = True elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) ShClass = Elf64SectionHeader got_start = False for line in lines: if got_start: if line.strip().startswith("Key"): break good_lines.append(line) else: # Looking for start if line.strip().startswith("Siz"): got_start = True merged_lines = [] i = 0 while i < len(good_lines): merged_lines.append(good_lines[i] + good_lines[i+1]) i += 2 good_lines = merged_lines good_lines = [line[25:] for line in good_lines] entsize = header.e_shentsize for i in range(header.e_shnum): sh_data = f.get_data(header.e_shoff + (i * entsize), entsize) sh = ShClass(header.ident.endianess) sh.fromdata(sh_data, hdr) out = StringIO.StringIO() sh.output(out) out.getvalue() self.assertEqual(out.getvalue(), good_lines[i])
def test_against_readelf(self): if not have_readelf: return for file_name in elf_ph_expected: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() lines = os.popen("readelf -l %s" % file_name).readlines() good_lines = [] if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() PhClass = Elf32ProgramHeader header.fromdata(data) got_start = False for line in lines: if got_start: if line.strip() == "": break good_lines.append(line) else: # Looking for start if line.startswith(" Type"): got_start = True elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) PhClass = Elf64ProgramHeader got_start = False for line in lines: if got_start: if line.strip() == "": break if not line.strip().startswith("["): good_lines.append(line) else: # Looking for start if line.strip().startswith("FileSiz"): got_start = True merged_lines = [] i = 0 while i < len(good_lines): merged_lines.append(good_lines[i] + good_lines[i+1]) i += 2 good_lines = merged_lines entsize = header.e_phentsize for i in range(header.e_phnum): ph_data = f.get_data(header.e_phoff + (i * entsize), entsize) ph = PhClass(header.ident.endianess) ph.fromdata(ph_data, hdr) out = StringIO.StringIO() ph.output(out) out.getvalue() self.assertEqual(out.getvalue(), good_lines[i])
def test_against_readelf(self): if not have_readelf: return for file_name in elf_sh_files: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() lines = os.popen("readelf -S %s" % file_name).readlines() good_lines = [] if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() ShClass = Elf32SectionHeader header.fromdata(data) got_start = False for line in lines: if got_start: if line.strip().startswith("Key to Flags"): break good_lines.append(line) else: # Looking for start if line.strip().startswith("[Nr]"): got_start = True elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) ShClass = Elf64SectionHeader got_start = False for line in lines: if got_start: if line.strip().startswith("Key"): break good_lines.append(line) else: # Looking for start if line.strip().startswith("Siz"): got_start = True merged_lines = [] i = 0 while i < len(good_lines): merged_lines.append(good_lines[i] + good_lines[i + 1]) i += 2 good_lines = merged_lines good_lines = [line[25:] for line in good_lines] entsize = header.e_shentsize for i in range(header.e_shnum): sh_data = f.get_data(header.e_shoff + (i * entsize), entsize) sh = ShClass(header.ident.endianess) sh.fromdata(sh_data, hdr) out = StringIO.StringIO() sh.output(out) out.getvalue() self.assertEqual(out.getvalue(), good_lines[i])
def test_against_readelf(self): if not have_readelf: return for file_name in elf_ph_expected: f = File(file_name, "rb") ident = ElfIdentification() ident.fromdata(f.get_data(0, 16)) wordsize = ident.wordsize hdr = ELF_HEADER_CLASSES[wordsize]() lines = os.popen("readelf -l %s" % file_name).readlines() good_lines = [] if ident.wordsize == 32: data = f.get_data(0, Elf32Header.size()) header = Elf32Header() PhClass = Elf32ProgramHeader header.fromdata(data) got_start = False for line in lines: if got_start: if line.strip() == "": break good_lines.append(line) else: # Looking for start if line.startswith(" Type"): got_start = True elif ident.wordsize == 64: data = f.get_data(0, Elf64Header.size()) header = Elf64Header() header.fromdata(data) PhClass = Elf64ProgramHeader got_start = False for line in lines: if got_start: if line.strip() == "": break if not line.strip().startswith("["): good_lines.append(line) else: # Looking for start if line.strip().startswith("FileSiz"): got_start = True merged_lines = [] i = 0 while i < len(good_lines): merged_lines.append(good_lines[i] + good_lines[i + 1]) i += 2 good_lines = merged_lines entsize = header.e_phentsize for i in range(header.e_phnum): ph_data = f.get_data(header.e_phoff + (i * entsize), entsize) ph = PhClass(header.ident.endianess) ph.fromdata(ph_data, hdr) out = StringIO.StringIO() ph.output(out) out.getvalue() self.assertEqual(out.getvalue(), good_lines[i])