Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import argparse
import sys
from elftools.elf.elffile import ELFFile
from elftools.elf.dynamic import DynamicSegment
def log_error(msg):
sys.stderr.write("[ERROR] {}\n".format(msg))
def log(msg):
sys.stderr.write(msg + "\n")
def unique_or_none(list):
if not list:
return None
assert len(list) == 1
return list[0]
def main():
parser = argparse.ArgumentParser(description="Rewrite portions of .dynstr.")
parser.add_argument("elf_path", metavar="ELF", help="path to the ELF file.")
args = parser.parse_args()
with open(args.elf_path, "rb+") as elf_file:
elf = ELFFile(elf_file)
# Blank dynstr section
dynamic = unique_or_none([segment
for segment
in elf.iter_segments()
if type(segment) is DynamicSegment])
if dynamic is None:
log("Not a dynamic executable")
return 0
address = unique_or_none([tag.entry.d_val
for tag
in dynamic.iter_tags()
if tag.entry.d_tag == "DT_STRTAB"])
offset = None
if address:
offset = unique_or_none(list(elf.address_offsets(address)))
size = unique_or_none([tag.entry.d_val
for tag
in dynamic.iter_tags()
if tag.entry.d_tag == "DT_STRSZ"])
if offset is None or size is None:
log("DT_STRTAB not found")
return 0
elf_file.seek(offset)
original = elf_file.read(size)
new = "\x00" * size
new = new.encode("ascii")
elf_file.seek(offset)
elf_file.write(new)
log("patching dynstr with zeroes")
# Blank strtab section
strtab = unique_or_none([section for section in elf.iter_sections() if section.name == ".strtab"])
offset = strtab.header["sh_offset"]
size = strtab.header["sh_size"]
elf_file.seek(offset)
original = elf_file.read(size)
new = "\x00" * size
new = new.encode("ascii")
elf_file.seek(offset)
elf_file.write(new)
log("patching strtab with zeroes")
return 0
if __name__ == "__main__":
sys.exit(main())