forked from jcramb/revshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2cc.py
executable file
·46 lines (34 loc) · 1.06 KB
/
bin2cc.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
#!/usr/bin/env python
import sys
if len(sys.argv) < 2:
print("usage: %s <outname> <prefix>:<file> ..." % sys.argv[0])
sys.exit(0)
base = sys.argv[1]
header= '#ifndef %s_h\n' % base
header += '#define %s_h\n\n' % base
src = '#include "%s.h"\n\n' % base
prefixes = []
files = []
for i in range(2, len(sys.argv)):
prefix, infile = sys.argv[i].split(':')
header += 'extern char _%sbuf[];\n' % prefix
header += 'extern int _%sbuf_len;\n\n' % prefix
src += 'char _%sbuf[] = {' % prefix
buf = open(infile, 'rb').read()
for j, b in enumerate(buf):
if j % 12 == 0:
src += '\n '
# ugly but works without future pkg in python2
if (type(b) == str):
src += '0x%02x, ' % ord(b)
else:
src += '0x%02x, ' % b
src += '\n};\n\n'
src += 'int _%sbuf_len = sizeof(_%sbuf);\n\n' % (prefix, prefix)
header += '#endif'
with open('%s.h' % base, 'w') as f:
print('PY %s.h' % base)
f.write(header)
with open('%s.cc' % base, 'w') as f:
print('PY %s.cc' % base)
f.write(src)