File: elf_bin2lib.py

package info (click to toggle)
lief 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 16,036 kB
  • sloc: cpp: 76,013; python: 6,167; ansic: 3,355; pascal: 404; sh: 98; makefile: 32
file content (34 lines) | stat: -rw-r--r-- 968 bytes parent folder | download
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
#!/usr/bin/env python
import argparse
import lief
import sys

lief.Logger.set_level(lief.LOGGING_LEVEL.INFO)


def bin2lib(binary, address, output, name=""):
    if not binary.is_pie:
        print("It only works with PIE binaries")
        sys.exit(1)

    function = binary.add_exported_function(address, name)
    print("Function created:")
    print(function)
    binary.write(output)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="")

    parser.add_argument("--name", "-n", default="", help="Name of the function to create")
    parser.add_argument("--output", "-o", default="libfoo.so", help="Output name. (Default: %(default)")

    parser.add_argument("binary", help="The target binary")
    parser.add_argument("address", type=lambda e : int(e, 0), help="Address of the function to export")


    args = parser.parse_args()
    binary = lief.parse(args.binary)
    bin2lib(binary, args.address, args.output, name=args.name)