Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binalyzer fix #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
__pycache__/
.vscode/
cached_results/
venv/
modified_binaries/
old_results/
/Binalyzer/tests/*
\!/Binalyzer/tests/Makefile
!/Binalyzer/tests/*.c
18 changes: 18 additions & 0 deletions Binalyzer/README.md
Original file line number Diff line number Diff line change
@@ -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
```
9 changes: 7 additions & 2 deletions Binalyzer/full_ldd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions Binalyzer/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Binalyzer/syscalls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down