예제 #1
0
def change_interpreter(target, interpreter, output=None):
    if not os.path.isfile(target) or not lief.is_elf(target):
        print("Wrong target! ({})".format(target))
        return 1


    if not os.path.isfile(interpreter) or not lief.is_elf(interpreter):
        print("Wrong interpreter! ({})".format(interpreter))
        return 1

    binary = lief.parse(target)
    if not binary.has_interpreter:
        print("The given target doesn't have interpreter!")
        return 1

    binary.interpreter = interpreter

    output_path = output
    if output_path is None:
        output_path = os.path.basename(target)
        output_path += "_updated"

    if os.path.isfile(output_path):
        os.remove(output_path)

    binary.write(output_path)

    # Set as executable
    st = os.stat(output_path)
    os.chmod(output_path, st.st_mode | stat.S_IEXEC)
    return 0
예제 #2
0
파일: utils.py 프로젝트: zha0/checksec.py
def find_libc():
    """Find the fullpath to the libc library with multiple methods"""
    libc_path = None
    try:
        libc_path = find_library_full("c")
    except (FileNotFoundError, AttributeError, RuntimeError):
        # ldconfig is not accessible as user
        # or running on Windows
        # or other errors
        try:
            libc_path = find_libc_ldd()
        except (FileNotFoundError, RuntimeError):
            # test hardcoded paths
            logging.debug("Finding libc path: hardcoded paths")
            for maybe_libc in LIBC_PATH_POSSIBILITIES:
                logging.debug("Testing libc at %s", maybe_libc)
                maybe_libc_path = Path(maybe_libc)
                if maybe_libc_path.exists():
                    # symlink
                    if maybe_libc_path.is_symlink():
                        dst = os.readlink(str(maybe_libc_path))
                        logging.debug("Resolve symlink %s -> %s",
                                      maybe_libc_path, dst)
                        maybe_libc_path = Path(dst)
                if lief.is_elf(str(maybe_libc_path)):
                    libc_path = maybe_libc
                    break
    if libc_path is None:
        raise LibcNotFoundError(
            "Cannot find a suitable libc path on your system")
    logging.debug("Found libc: %s", libc_path)
    return libc_path
예제 #3
0
 def detect_binary_format(input_path):
     if lief.is_macho(input_path):
         return BinaryFormat.MACHO
     elif lief.is_elf(input_path):
         return BinaryFormat.ELF
     else:
         return None
예제 #4
0
def try_patch_file(path, pkgdir):
    if not os.path.isfile(path) or not lief.is_elf(path):
        return
    binary = lief.parse(path)
    if not binary.has_interpreter:
        return
    old_interp = binary.interpreter
    if old_interp.endswith('/ld-linux-x86-64.so.2'):
        if (old_interp == '/lib64/ld-linux-x86-64.so.2'
                or old_interp == '/usr/lib64/ld-linux-x86-64.so.2'
                or old_interp == '/usr/lib/ld-linux-x86-64.so.2'):
            return
        binary.interpreter = '/lib/ld-linux-x86-64.so.2'
    elif old_interp.startswith(pkgdir + '/'):
        binary.interpreter = old_interp[len(pkgdir):]

    if old_interp == binary.interpreter:
        return

    print("Patching .interp for %s to %s" % (path, binary.interpreter))

    os.remove(path)
    binary.write(path)
    st = os.stat(path)
    os.chmod(path, st.st_mode | stat.S_IEXEC)
예제 #5
0
def check(path):
    if lief.is_pe(path):
        print("PE not supported")
        return -1
    if lief.is_macho(path):
        print("Macho not supported")
        return -1
    if lief.is_elf(path):
        return 0
    return -1
예제 #6
0
 def is_elf(self, elf_path):
     return lief.is_elf(elf_path)
예제 #7
0
def is_elf(filepath: Path) -> bool:
    return lief.is_elf(str(filepath))