-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfpvm_wrap
More file actions
executable file
·55 lines (44 loc) · 1.42 KB
/
fpvm_wrap
File metadata and controls
executable file
·55 lines (44 loc) · 1.42 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
from lief import *
import argparse
import os
import stat
import re
# Create an argument parser
parser = argparse.ArgumentParser(
prog='fpvm_wrap',
description='Rewrite a set of imported symbols to use <symbol>$fpvm')
parser.add_argument('input')
parser.add_argument('-o', dest='output', default='a.out')
parser.add_argument('-s', dest='symbols', default="", help="comma-seperated list of symbols")
parser.add_argument('-f', '--from-file', help='file which contains a symbol on each line')
args = parser.parse_args()
symbols = set()
for s in args.symbols.split(','):
symbols.add(s)
if args.from_file is not None:
with open(args.from_file) as f:
for line in f:
raw=line.strip()
if re.search("^\s*\#",raw) :
# print('skipping comment ',raw)
continue
match = re.search("^\s*(\S+)",raw)
if (match) :
name = match.group(1)
# print('adding',name)
symbols.add(name)
# Load the input binary
elf = ELF.parse(args.input)
# Iterate over the imported symbols of the elf binary. If the symbol's
# name is in our set of symbols, append $fpvm to it
for s in elf.imported_symbols:
# print('considering ', s)
if s.name in symbols:
print('wrapping ', s)
s.name += "$fpvm"
# Save the modifed binary
elf.write(args.output)
# Do a 'chmod +x'
st = os.stat(args.output)
os.chmod(args.output, st.st_mode | stat.S_IEXEC)