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

Use CLE from angr project to load dynamically linked executable #53

Merged
merged 2 commits into from
Mar 5, 2024
Merged
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
16 changes: 8 additions & 8 deletions rainbow/loaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
import os
from typing import Optional

from .elfloader import elfloader
from .cleloader import cleloader
from .hexloader import hexloader
from .peloader import peloader

LOADERS = {".hex": hexloader, ".elf": elfloader, ".so": elfloader, ".exe": peloader}
LOADERS = {".hex": hexloader, ".elf": cleloader, ".so": cleloader, ".exe": cleloader}


def load_selector(filename, rainbow_instance, typ=None, *args, **kwargs) -> Optional[int]:
if typ is None:
ext = os.path.splitext(filename)[1]
loader = LOADERS[ext]
else:
loader = LOADERS[typ]
"""Select the appropriate loader.

Default to CLE loader if unknown as it has the most chance of succeeding.
"""
typ = typ if typ is not None else os.path.splitext(filename)[1]
loader = LOADERS.get(typ, cleloader)
return loader(filename, rainbow_instance, *args, **kwargs)
45 changes: 45 additions & 0 deletions rainbow/loaders/cleloader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file is part of rainbow
#
# rainbow is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#
# Copyright 2022 A Iooss, ANSSI

import cle


def cleloader(path: str, emu, ld_path=(), verbose=False) -> None:
"""Load binary using CLE

It will try to load their associated libraries and resolves imports.
"""
if verbose:
print(f"[+] Opening {path}")
ld = cle.Loader(path, except_missing_libs=True, ld_path=ld_path)

# Map memory
if verbose:
for obj in ld.all_objects:
print(f"[ ] Mapping at 0x{obj.min_addr:08X}: {obj.binary_basename}")
emu.map_space(ld.min_addr, ld.max_addr, verbose=verbose)
for start_addr, backer in ld.memory.backers():
emu.emu.mem_write(start_addr, bytes(backer))

# Load symbols
func_symbols = [s for s in ld.symbols if s.is_function]
if verbose:
print(f"[+] Loading {len(func_symbols)} functions symbol")
for symbol in func_symbols:
emu.functions[symbol.name] = symbol.rebased_addr
emu.function_names.update({symbol.rebased_addr: symbol.name})
106 changes: 0 additions & 106 deletions rainbow/loaders/elfloader.py

This file was deleted.

54 changes: 0 additions & 54 deletions rainbow/loaders/peloader.py

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
install_requires=[
"unicorn>=2.0.1",
"capstone>=4.0.0",
"lief>=0.10.0",
"cle>=9.2",
"intelhex",
"pygments",
"numpy",
Expand Down