-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathambiguous.py
73 lines (51 loc) · 1.5 KB
/
ambiguous.py
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
#! /usr/bin/env python3
import argparse
import os.path
import sys
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import SymbolTableSection
import dataclasses
VERBOSITY = 0
def dprint(*args, **kwargs):
if VERBOSITY > 0:
print(*args, **kwargs)
def eprint(*args, **kwargs):
print(*args, **kwargs, file=sys.stderr)
FILES: dict[str, str] = {}
import hashlib
# Returns the md5 hash of a bytearray
def getStrHash(byte_array: bytearray) -> str:
return hashlib.md5(byte_array).hexdigest()
def read_file(filepath: str):
filename = os.path.basename(filepath)
with open(filepath, "rb") as f:
elffile = ELFFile(f)
text_name = ".text"
text = elffile.get_section_by_name(text_name)
if text:
hash = getStrHash(text.data())
FILES[filename] = hash
else:
print(f"{filename} has no .text section")
def main():
description = ""
epilog = ""
parser = argparse.ArgumentParser(
description=description,
epilog=epilog,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("files", nargs="+", help="elf files to read.")
args = parser.parse_args()
for elf in args.files:
read_file(elf)
hashes = dict()
for k, v in FILES.items():
if v not in hashes:
hashes[v] = list()
hashes[v].append(k)
for k, v in hashes.items():
if len(v) > 1:
print(v)
if __name__ == "__main__":
main()