diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..856b5c2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +__pycache__/ +.vscode/ +cached_results/ +venv/ +modified_binaries/ +old_results/ +/Binalyzer/tests/* +\!/Binalyzer/tests/Makefile +!/Binalyzer/tests/*.c \ No newline at end of file diff --git a/Binalyzer/README.md b/Binalyzer/README.md new file mode 100644 index 0000000..a7e6eee --- /dev/null +++ b/Binalyzer/README.md @@ -0,0 +1,18 @@ +# Running + +To run Binalyzer, you should use Python 3.8 (versions 3.9 - 3.11 should also work, but were not tested; version 3.12 will **NOT** work). + +First, create an environment and install the requirements: + +```bash +$ python3.8 -m venv venv +$ python3.8 -m ensurepip --upgrade +$ python3.8 -m pip install -r requirements.txt +``` + +Afterwards, run the script with any program in your filesystem: + +```bash +$ python3.8 filter.py tests/simple +$ python3.8 filter.py /bin/ls +``` \ No newline at end of file diff --git a/Binalyzer/full_ldd.py b/Binalyzer/full_ldd.py index 3deef44..991dd50 100644 --- a/Binalyzer/full_ldd.py +++ b/Binalyzer/full_ldd.py @@ -71,6 +71,8 @@ def ldpaths(ld_so_conf='/etc/ld.so.conf'): for c in include_files: paths = paths + ldpaths(os.path.realpath(c)) + # add the default lib directory as the previous code does not seem to include it + paths.append("/lib") paths = list(set(paths)) paths.sort() return paths @@ -109,7 +111,7 @@ def dynamic_dt_needed_paths( dt_needed, eclass, paths): return dt_needed_paths -def all_dynamic_dt_needed_paths(f, paths): +def all_dynamic_dt_needed_paths(f, paths, depth = 0): """ Return a dictionary of all the DT_NEEDED => Library Paths for a given ELF file obtained by recursively following linkage. """ @@ -120,8 +122,11 @@ def all_dynamic_dt_needed_paths(f, paths): # This needs to be iterated until we traverse the entire linkage tree dt_needed = readelf.dynamic_dt_needed() dt_needed_paths = dynamic_dt_needed_paths(dt_needed, eclass, paths) + # max depth to prevent infinite loops + if depth > 10: # random max number, increasing to 30 does not seem to make a difference... + return dt_needed_paths for n, lib in dt_needed_paths.items(): - dt_needed_paths = dict(all_dynamic_dt_needed_paths(lib, paths), **dt_needed_paths) + dt_needed_paths = dict(all_dynamic_dt_needed_paths(lib, paths, depth + 1), **dt_needed_paths) except ELFError as ex: sys.stderr.write('ELF error: %s\n' % ex) sys.exit(1) diff --git a/Binalyzer/requirements.txt b/Binalyzer/requirements.txt index 4ae4a5e..ddb66ed 100644 --- a/Binalyzer/requirements.txt +++ b/Binalyzer/requirements.txt @@ -1,4 +1,6 @@ -pyelftools -capstone -lief -angr +pyelftools==0.27 +capstone==4.0.2 +lief==0.10.1 +angr==9.0.4378 +six==1.15.0 +protobuf==3.13.0 diff --git a/Binalyzer/syscalls.py b/Binalyzer/syscalls.py index 93a7d8a..f18769e 100644 --- a/Binalyzer/syscalls.py +++ b/Binalyzer/syscalls.py @@ -141,6 +141,8 @@ def init(fname): with open(fname, 'rb') as f: elf = ELFFile(f) code = elf.get_section_by_name('.text') + if code is None: # what + return [] ops = code.data() addr = code['sh_addr'] md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)