-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmakebin.py
executable file
·49 lines (34 loc) · 1.06 KB
/
makebin.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
#!/usr/bin/env python3
import sys, os, struct
loaderfile = sys.argv[1]
elffile = sys.argv[2]
outfile = sys.argv[3]
data = open(loaderfile,"rb").read()
hdrlen, loaderlen, elflen, arg = struct.unpack(">IIII",data[:16])
if hdrlen < 0x10:
print("ERROR: header length is 0x%x, expected at least 0x10"%hdrlen)
sys.exit(1)
loaderoff = hdrlen
elfoff = loaderoff + loaderlen
elfend = elfoff + elflen
hdr = data[:hdrlen]
loader = data[loaderoff:elfoff]
elf = open(elffile,"rb").read()
if elflen > 0:
print("WARNING: loader already contains ELF, will replace")
elflen = len(elf)
if loaderlen < len(loader):
print("ERROR: loader is larger than its reported length")
sys.exit(1)
if loaderlen > len(loader):
print("Padding loader with 0x%x zeroes"%(loaderlen-len(loader)))
loader += b"\0"*(loaderlen-len(loader))
newdata = struct.pack(">IIII", hdrlen, loaderlen, elflen, 0) + hdr[16:]
newdata += loader
newdata += elf
print("Header: 0x%x bytes"%(hdrlen))
print("Loader: 0x%x bytes"%(loaderlen))
print("ELF: 0x%x bytes"%(elflen))
f = open(outfile,"wb")
f.write(newdata)
f.close()