-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgetgasbytes.py
61 lines (50 loc) · 1.44 KB
/
getgasbytes.py
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
56
57
58
59
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import SymbolTableSection
def get_gas(buff):
if not buff: return buff;
cnt = 12;
ret = "";
for x in range(0, len(buff), cnt):
data = buff[x:];
if len(data) >= cnt:
data = data[:cnt];
cstr = ".byte ";
lst = [];
for b in data:
lst.append("0x%.02x" % ord(b));
ret += cstr + ", ".join(lst) + "\n";
ret = ret[:-1];
return ret;
f = open("./test64", "rb");
elffile = ELFFile(f);
symtab = elffile.get_section_by_name('.symtab')
testarm64 = symtab.get_symbol_by_name("testarm64")[0].entry.st_value;
testarm64_end = symtab.get_symbol_by_name("testarm64_end")[0].entry.st_value;
buff = open("./test64", "rb").read();
buff = buff[testarm64:testarm64_end];
buff = get_gas(buff);
prefix = """
.syntax unified
.global export_kill
export_kill:
mov r1, kill_end - kill
str r1, [r0]
adr r0, kill
bx lr
.global kill
kill:
push {r1-r12, lr}
mov r12, r7
mov r7, 0x25
svc 0
//if all went fine we are now aarch64
"""
suffix = """
pop {r1-r12, pc}
kill_end:
""";
with open("./switchasm.S", "wb") as f:
f.write(prefix);
f.write(buff);
f.write(suffix);
f.flush();