forked from instruder/flvfuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flvfuzz.py
294 lines (229 loc) · 7.06 KB
/
flvfuzz.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
287
288
289
290
291
292
293
294
# Multithreaded FLV fuzzer 0.1 by Wojciech Pawlikowski (C) 2011
#
# Todo:
# + write smart fuzzing for different codecs and fuzz only stuff within the frame (i.e. AAC)
# + more deterministic approach
# + multiple samples support
# + simple code coverage which tells the minimal set of samples
# + better multithreading
# + configuration via commandline
#
# Requirements:
# + python 2.7 (because pydbg won't work with later versions)
# + flvlib
# + haxe (in your PATH)
# + pydbg
# + flashplayer.exe in current directory
# + good flv sample called 'sample.flv'
#
# Usage:
# + C:\WORK\flvfuzz.py
from flvlib.constants import TAG_TYPE_AUDIO, TAG_TYPE_VIDEO, TAG_TYPE_SCRIPT, FRAME_TYPE_KEYFRAME
from flvlib.astypes import MalformedFLV, FLVObject
from flvlib.tags import FLV, EndOfFile, AudioTag, VideoTag, ScriptTag
from pydbg import *
from pydbg.defines import *
import os, sys, stat, random, binascii, time, threading
# CHANGE STUFF HERE
fuzz_audiotag = False # fuzz audio ?
fuzz_videotag = True # fuzz video ?
fuzz_keyframe = False # fuzz keyframe ?
fuzz_vector = 20 # bigger = lower number of fuzzed frames
fuzz_factor = 10 # how much data should be fuzzed
fuzz_timeout = 30 # timeout in seconds
fuzz_threads = 20 # number of concurrent threads
# STOP CHANGES HERE
class FuzzGenerator:
def __init__(self, buf):
self.values = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
self.buf = buf
def _fuzz_for_real(self, buf):
test = []
for i in xrange(0, len(buf)):
test += binascii.b2a_hex(buf[i])
for i in xrange(0, fuzz_factor):
test[random.randint(0, len(buf))] = random.choice(self.values)
tmp = ''
for i in test:
tmp += i
buf = binascii.a2b_hex(tmp)
return buf
def fuzz(self, offset, size):
frame_content = self.buf[offset:offset + size]
fuzzed_buf = self._fuzz_for_real(frame_content)
tmp1 = self.buf[:offset]
tmp2 = self.buf[offset + size:]
self.buf = tmp1 + fuzzed_buf + tmp2
return self.buf
class CheckAudioTag(AudioTag):
def parse(self):
parent = self.parent_flv
AudioTag.parse(self)
x = random.randint(0, fuzz_vector)
if fuzz_audiotag and (x == fuzz_vector):
f = FuzzGenerator(parent.buffer)
parent.buffer = f.fuzz(self.offset, self.size)
class CheckVideoTag(VideoTag):
def parse(self):
parent = self.parent_flv
VideoTag.parse(self)
if self.frame_type == FRAME_TYPE_KEYFRAME and not fuzz_keyframe:
return
x = random.randint(0, fuzz_vector)
if fuzz_videotag and (x == fuzz_vector):
f = FuzzGenerator(parent.buffer)
parent.buffer = f.fuzz(self.offset, self.size)
class CheckScriptTag(ScriptTag):
def parse(self):
parent = self.parent_flv
ScriptTag.parse(self)
tag_to_class = {
TAG_TYPE_AUDIO: CheckAudioTag,
TAG_TYPE_VIDEO: CheckVideoTag,
TAG_TYPE_SCRIPT: CheckScriptTag
}
class MyFLV(FLV):
def __init__(self, f):
FLV.__init__(self, f)
try:
st_size = os.stat(self.f.name)[6]
except:
print '[-] Could not stat() original file'
return
self.buffer = self.f.read(st_size)
self.f.seek(0L)
def tag_type_to_class(self, tag_type):
try:
return tag_to_class[tag_type]
except KeyError:
raise MalformedFLV("Invalid tag type: %d", tag_type)
class FuzzThread(threading.Thread):
def __init__(self, test_id):
threading.Thread.__init__(self)
self.keep_file = False
self.test_id = test_id
self.current_testcase = 'sample_%d.flv' % test_id
self.sample_deleted = False
self.lock = threading.Lock()
self.event = threading.Event()
self.dbg = pydbg()
self.dbg.pydbg_log = self.log
self.dbg.set_callback(EXCEPTION_ACCESS_VIOLATION, self.handle_av)
self.dbg.set_callback(USER_CALLBACK_DEBUG_EVENT, self.handle_timeout)
def log(self, msg):
print '[Execute->pydbg]: %s' % msg
def handle_av(self, dbg):
crash_bin = utils.crash_binning.crash_binning()
crash_bin.record_crash(dbg)
print '[*] Test sample: %s generated A/V' % self.current_testcase
print crash_bin.crash_synopsis()
self.keep_file = True
dbg.terminate_process()
return DBG_EXCEPTION_NOT_HANDLED
def handle_timeout(self, dbg):
if time.time() - dbg.start_time > fuzz_timeout:
dbg.terminate_process()
if not self.keep_file:
os.system('del player_%d.swf %s 2> nul' % (self.test_id, self.current_testcase))
self.event.set()
return DBG_CONTINUE
def create_testcase(self):
try:
f = open('sample.flv', 'rb')
except IOError, (errno, strerror):
print '[-] Could not open sample file: %s' % strerror
return
flv = MyFLV(f)
tag_iterator = flv.iter_tags()
try:
while True:
tag = tag_iterator.next()
except MalformedFLV, e:
msg = e[0] % e[1:]
print '[-] Not a valid FLV file: %s' % msg
return
except EndOfFile:
print '[-] Unexpected end-of-file'
return
except StopIteration:
pass
f.close()
try:
f = open(self.current_testcase, 'wb')
except IOError, (errno, strerror):
print '[-] Could not open destination file: %s' % strerror
return
f.write(flv.buffer)
f.close()
def prepare_swf(self):
try:
self.lock.acquire()
fname = 'compile_%d.hxml'% self.test_id
try:
f = open(fname, 'w')
except IOError, (errno, strerror):
print '[-] Could not create compile.hxml file: %s' % strerror
return
f.write('''
-swf player_%d.swf
-main FLVPlayer_%d
''' % (self.test_id, self.test_id))
f.close()
fname = 'FLVPlayer_%d.hx' % self.test_id
try:
f = open(fname, 'w')
except IOError, (errno, strerror):
print '[-] Could not create FLVPlayer_id.hx: %s' % strerror
return
f.write('''
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
class FLVPlayer_%d {
private var mc : flash.display.MovieClip;
private var myVideo : Video;
public function new() {
mc = flash.Lib.current;
myVideo = new Video();
var myNetConnect : NetConnection = new NetConnection();
myNetConnect.connect(null);
var myNetStream : NetStream = new NetStream(myNetConnect);
myVideo.attachNetStream(myNetStream);
mc.addChild(myVideo);
trace(myVideo);
myNetStream.play('%s');
}
public static function main() {
new FLVPlayer_%d();
}
}
''' % (self.test_id, self.current_testcase, self.test_id))
f.close()
os.system('haxe compile_%d.hxml'% self.test_id)
os.unlink('FLVPlayer_%d.hx' % self.test_id)
os.unlink('compile_%d.hxml' % self.test_id)
self.lock.release()
except Exception, e:
print '[-] Unknown error: %s' % e[0]
return
def run(self):
self.create_testcase()
self.prepare_swf()
player_no = 'player_%d.swf' % self.test_id
self.dbg.load('flashplayer.exe', command_line=player_no)
self.dbg.start_time = time.time()
self.dbg.run()
self.event.wait()
def main():
test_id = 0
threads = []
while True:
for i in range(fuzz_threads):
tid = FuzzThread(test_id)
tid.start()
threads.append(tid)
test_id = test_id + 1
for t in threads:
t.join()
if __name__ == '__main__':
main()