forked from MarioVilas/shellcode_tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbin2py.py
executable file
·286 lines (236 loc) · 9.14 KB
/
bin2py.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env python
# Convert binary files to data embedded in Python sources
# by Mario Vilas (mvilas at gmail.com)
# Copyright (c) 2009-2011, Mario Vilas
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import glob
import string
import base64
import zlib
import gzip
try:
import cStringIO as StringIO
except ImportError:
import StringIO
helpMessage = '''Convert binary files to data embedded in Python sources
by Mario Vilas (mvilas at gmail.com)
Usage:
./%s {input} [switches]
Where "input" is a glob-style filename list, and "switches" can be a combination of the following:
-a Append output instead of overwriting
-b Encode with base64
-d Add python code to decode the string
-g Compress with gzip
-o filename Specify a single output file
-x Convert to hexadecimal, instead of using repr()
-z Compress with zlib'''
def showHelp(e = None):
if e: print "Error: %s\n" % e
print helpMessage % os.path.basename(sys.argv[0])
def parseCommandLine():
argv = []
for i in range(1, len(sys.argv)):
token = sys.argv[i]
if token[0] in ('-', '/'):
for c in token[1:]:
argv.append('-' + c)
else:
argv.append(token)
if not argv: raise Exception, "no parameters supplied"
input = []
output = []
appendflag = False
hexaflag = False
zlibflag = False
gzipflag = False
base64flag = False
decoderflag = False
i = 0
while i < len(argv):
token = argv[i]
if token[0] == '-':
s = token[1].lower()
if s == 'o': # output filename
if output: raise Exception, "inconsistent switch: %s" % s
i += 1
if i >= len(argv): raise Exception, "expected parameter after switch -%s" % s
output_filename = argv[i]
if output_filename[0] == '-': raise Exception, "unexpected argument: %r" % token
output_filename = os.path.abspath(output_filename)
output.append(output_filename)
elif s == 'a': # append output
appendflag = True
elif s == 'b': # base64 encoding
base64flag = True
elif s == 'd': # write decoder
decoderflag = True
elif s == 'g': # gzip compression
if zlibflag: raise Exception, "inconsistent switch -%s" % s
gzipflag = True
elif s == 'x': # use hexadecimal
hexaflag = True
elif s == 'z': # zlib compression
if gzipflag: raise Exception, "inconsistent switch -%s" % s
zlibflag = True
else:
raise Exception, "unknown switch -%s" % s
else: # input filename list
filelist = glob.glob(token)
if not filelist: raise Exception, "can't find file(s): %r" % token
input += filelist
i += 1
if decoderflag and not (gzipflag or zlibflag or base64flag): decoderflag = False
return input, output, appendflag, hexaflag, zlibflag, gzipflag, base64flag, decoderflag
def gzipCompress(data):
sf = StringIO.StringIO()
gf = gzip.GzipFile('', 'wb', 9, sf)
gf.write(data)
gf.close()
sf.seek(0)
data = sf.read()
sf.close()
return data
def sanitizeVariableName(dirty):
valid = string.digits
valid += string.letters
valid += '_'
clean = ''
for c in dirty:
if not c in valid:
c = '_'
clean += c
return clean
def hexData(data, varname = 'document', align = 32):
hexdata = '%s = ""\n' % varname
if align > 0:
r = range(0, len(data), align)
else:
r = range(0, len(data))
for i in r:
hexdata += '%s += "' % varname
for c in data[i:i+align]:
hexdata += r'\x%s' % hex(ord(c))[2:].zfill(2)
hexdata += '"\n'
return hexdata
def reprData(data, varname = 'document', max = 32):
reprdata = '%s = %r\n' % (varname, '')
prefix = '%s += ' % varname
suffix = '\n'
if max > 0:
for i in range(0, len(data), max):
reprdata += prefix + repr(data[i:i+max]) + suffix
else:
reprdata += prefix + repr(data) + suffix
return reprdata
def putDecoder(zlibflag, gzipflag, base64flag):
code = ''
if zlibflag:
code += 'import zlib\n'
if gzipflag:
code += 'import gzip\n'
code += 'import StringIO\n'
if base64flag:
code += 'import base64\n'
code += '\n'
code += 'def decode(data):\n'
if base64flag:
code += ' data = base64.decodestring(data)\n'
if zlibflag:
code += ' data = zlib.decompress(data)\n'
elif gzipflag:
code += ' sf = StringIO.StringIO()\n'
code += ' sf.write(data)\n'
code += ' sf.seek(0)\n'
code += ' gf = gzip.GzipFile("", "rb", 9, sf)\n'
code += ' data = gf.read()\n'
code += ' gf.close()\n'
code += ' sf.close()\n'
code += ' return data\n'
code += '\n'
return code
def main():
try:
input, output, appendflag, hexaflag, zlibflag, gzipflag, base64flag, decoderflag = parseCommandLine()
except Exception, e:
showHelp(e)
return
single_output = bool(output)
if single_output:
if appendflag:
ofile = open(output[0], 'a')
else:
ofile = open(output[0], 'w')
if decoderflag:
ofile.write(putDecoder(zlibflag, gzipflag, base64flag))
count = 0
for filename in input:
filename = os.path.abspath(filename)
if filename in output: continue
data = open(filename, 'rb').read()
if zlibflag:
data = zlib.compress(data, 9)
elif gzipflag:
data = gzipCompress(data)
if single_output:
varname = os.path.splitext(os.path.basename(filename))[0]
varname = sanitizeVariableName(varname)
else:
varname = 'document'
if base64flag:
data = '%s = %r\n' % (varname, base64.encodestring(data))
elif hexaflag:
data = hexData(data, varname)
else:
data = reprData(data, varname)
if decoderflag:
data += '%s = decode(%s)\n' % (varname, varname)
if single_output:
data += '\n'
ofile.write(data)
else:
filename += '.py'
if decoderflag:
data = putDecoder(zlibflag, gzipflag, base64flag) + data
if appendflag:
open(filename, 'a').write(data)
else:
open(filename, 'w').write(data)
output.append(filename)
count += 1
if single_output:
ofile.close()
if count == 1:
print "1 file processed."
else:
print "%i files processed." % count
if __name__ == '__main__':
try:
import psyco
psyco.bind(main)
except ImportError:
pass
main()