forked from googleprojectzero/domato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
executable file
·473 lines (421 loc) · 15.2 KB
/
generator.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# Domato - main generator script
# -------------------------------
#
# Written and maintained by Ivan Fratric <[email protected]>
#
# Copyright 2017 Google Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import os
import re
import random
import sys
from grammar import Grammar
_N_MAIN_LINES = 1000
_N_EVENTHANDLER_LINES = 500
_N_ADDITIONAL_HTMLVARS = 5
# A map from tag name to corresponding type for HTML tags
_HTML_TYPES = {
'a': 'HTMLAnchorElement',
'abbr': 'HTMLUnknownElement',
'acronym': 'HTMLUnknownElement',
'address': 'HTMLUnknownElement',
'applet': 'HTMLUnknownElement',
'area': 'HTMLAreaElement',
'article': 'HTMLUnknownElement',
'aside': 'HTMLUnknownElement',
'audio': 'HTMLAudioElement',
'b': 'HTMLUnknownElement',
'base': 'HTMLBaseElement',
'basefont': 'HTMLUnknownElement',
'bdi': 'HTMLUnknownElement',
'bdo': 'HTMLUnknownElement',
'bgsound': 'HTMLUnknownElement',
'big': 'HTMLUnknownElement',
'blockquote': 'HTMLUnknownElement',
'br': 'HTMLBRElement',
'button': 'HTMLButtonElement',
'canvas': 'HTMLCanvasElement',
'caption': 'HTMLTableCaptionElement',
'center': 'HTMLUnknownElement',
'cite': 'HTMLUnknownElement',
'code': 'HTMLUnknownElement',
'col': 'HTMLTableColElement',
'colgroup': 'HTMLUnknownElement',
'command': 'HTMLUnknownElement',
'content': 'HTMLContentElement',
'data': 'HTMLDataElement',
'datalist': 'HTMLDataListElement',
'dd': 'HTMLUnknownElement',
'del': 'HTMLModElement',
'details': 'HTMLDetailsElement',
'dfn': 'HTMLUnknownElement',
'dialog': 'HTMLDialogElement',
'dir': 'HTMLDirectoryElement',
'div': 'HTMLDivElement',
'dl': 'HTMLDListElement',
'dt': 'HTMLUnknownElement',
'em': 'HTMLUnknownElement',
'embed': 'HTMLEmbedElement',
'fieldset': 'HTMLFieldSetElement',
'figcaption': 'HTMLUnknownElement',
'figure': 'HTMLUnknownElement',
'font': 'HTMLFontElement',
'footer': 'HTMLUnknownElement',
'form': 'HTMLFormElement',
'frame': 'HTMLFrameElement',
'frameset': 'HTMLFrameSetElement',
'h1': 'HTMLHeadingElement',
'h2': 'HTMLHeadingElement',
'h3': 'HTMLHeadingElement',
'h4': 'HTMLHeadingElement',
'h5': 'HTMLHeadingElement',
'h6': 'HTMLHeadingElement',
'header': 'HTMLUnknownElement',
'hgroup': 'HTMLUnknownElement',
'hr': 'HTMLHRElement',
'i': 'HTMLUnknownElement',
'iframe': 'HTMLIFrameElement',
'image': 'HTMLImageElement',
'img': 'HTMLImageElement',
'input': 'HTMLInputElement',
'ins': 'HTMLModElement',
'isindex': 'HTMLUnknownElement',
'kbd': 'HTMLUnknownElement',
'keygen': 'HTMLKeygenElement',
'label': 'HTMLLabelElement',
'layer': 'HTMLUnknownElement',
'legend': 'HTMLLegendElement',
'li': 'HTMLLIElement',
'link': 'HTMLLinkElement',
'listing': 'HTMLUnknownElement',
'main': 'HTMLUnknownElement',
'map': 'HTMLMapElement',
'mark': 'HTMLUnknownElement',
'marquee': 'HTMLMarqueeElement',
'menu': 'HTMLMenuElement',
'menuitem': 'HTMLMenuItemElement',
'meta': 'HTMLMetaElement',
'meter': 'HTMLMeterElement',
'nav': 'HTMLUnknownElement',
'nobr': 'HTMLUnknownElement',
'noembed': 'HTMLUnknownElement',
'noframes': 'HTMLUnknownElement',
'nolayer': 'HTMLUnknownElement',
'noscript': 'HTMLUnknownElement',
'object': 'HTMLObjectElement',
'ol': 'HTMLOListElement',
'optgroup': 'HTMLOptGroupElement',
'option': 'HTMLOptionElement',
'output': 'HTMLOutputElement',
'p': 'HTMLParagraphElement',
'param': 'HTMLParamElement',
'picture': 'HTMLPictureElement',
'plaintext': 'HTMLUnknownElement',
'pre': 'HTMLPreElement',
'progress': 'HTMLProgressElement',
'q': 'HTMLQuoteElement',
'rp': 'HTMLUnknownElement',
'rt': 'HTMLUnknownElement',
'ruby': 'HTMLUnknownElement',
's': 'HTMLUnknownElement',
'samp': 'HTMLUnknownElement',
'section': 'HTMLUnknownElement',
'select': 'HTMLSelectElement',
'shadow': 'HTMLShadowElement',
'small': 'HTMLUnknownElement',
'source': 'HTMLSourceElement',
'span': 'HTMLSpanElement',
'strike': 'HTMLUnknownElement',
'strong': 'HTMLUnknownElement',
'style': 'HTMLStyleElement',
'sub': 'HTMLUnknownElement',
'summary': 'HTMLUnknownElement',
'sup': 'HTMLUnknownElement',
'table': 'HTMLTableElement',
'tbody': 'HTMLTableSectionElement',
'td': 'HTMLUnknownElement',
'template': 'HTMLTemplateElement',
'textarea': 'HTMLTextAreaElement',
'tfoot': 'HTMLTableSectionElement',
'th': 'HTMLTableCellElement',
'thead': 'HTMLTableSectionElement',
'time': 'HTMLTimeElement',
'title': 'HTMLTitleElement',
'tr': 'HTMLTableRowElement',
'track': 'HTMLTrackElement',
'tt': 'HTMLUnknownElement',
'u': 'HTMLUnknownElement',
'ul': 'HTMLUListElement',
'var': 'HTMLUnknownElement',
'video': 'HTMLVideoElement',
'wbr': 'HTMLUnknownElement',
'xmp': 'HTMLUnknownElement'
}
# A map from tag name to corresponding type for SVG tags
_SVG_TYPES = {
'a': 'SVGAElement',
'altGlyph': 'SVGElement',
'altGlyphDef': 'SVGElement',
'altGlyphItem': 'SVGElement',
'animate': 'SVGAnimateElement',
'animateColor': 'SVGElement',
'animateMotion': 'SVGAnimateMotionElement',
'animateTransform': 'SVGAnimateTransformElement',
'circle': 'SVGCircleElement',
'clipPath': 'SVGClipPathElement',
'color-profile': 'SVGElement',
'cursor': 'SVGCursorElement',
'defs': 'SVGDefsElement',
'desc': 'SVGDescElement',
'discard': 'SVGDiscardElement',
'ellipse': 'SVGEllipseElement',
'feBlend': 'SVGFEBlendElement',
'feColorMatrix': 'SVGFEColorMatrixElement',
'feComponentTransfer': 'SVGFEComponentTransferElement',
'feComposite': 'SVGFECompositeElement',
'feConvolveMatrix': 'SVGFEConvolveMatrixElement',
'feDiffuseLighting': 'SVGFEDiffuseLightingElement',
'feDisplacementMap': 'SVGFEDisplacementMapElement',
'feDistantLight': 'SVGFEDistantLightElement',
'feDropShadow': 'SVGFEDropShadowElement',
'feFlood': 'SVGFEFloodElement',
'feFuncA': 'SVGFEFuncAElement',
'feFuncB': 'SVGFEFuncBElement',
'feFuncG': 'SVGFEFuncGElement',
'feFuncR': 'SVGFEFuncRElement',
'feGaussianBlur': 'SVGFEGaussianBlurElement',
'feImage': 'SVGFEImageElement',
'feMerge': 'SVGFEMergeElement',
'feMergeNode': 'SVGFEMergeNodeElement',
'feMorphology': 'SVGFEMorphologyElement',
'feOffset': 'SVGFEOffsetElement',
'fePointLight': 'SVGFEPointLightElement',
'feSpecularLighting': 'SVGFESpecularLightingElement',
'feSpotLight': 'SVGFESpotLightElement',
'feTile': 'SVGFETileElement',
'feTurbulence': 'SVGFETurbulenceElement',
'filter': 'SVGFilterElement',
'font': 'SVGElement',
'font-face': 'SVGElement',
'font-face-format': 'SVGElement',
'font-face-name': 'SVGElement',
'font-face-src': 'SVGElement',
'font-face-uri': 'SVGElement',
'foreignObject': 'SVGForeignObjectElement',
'g': 'SVGGElement',
'glyph': 'SVGElement',
'glyphRef': 'SVGElement',
'hatch': 'SVGElement',
'hatchpath': 'SVGElement',
'hkern': 'SVGElement',
'image': 'SVGImageElement',
'line': 'SVGLineElement',
'linearGradient': 'SVGLinearGradientElement',
'marker': 'SVGMarkerElement',
'mask': 'SVGMaskElement',
'mesh': 'SVGElement',
'meshgradient': 'SVGElement',
'meshpatch': 'SVGElement',
'meshrow': 'SVGElement',
'metadata': 'SVGMetadataElement',
'missing-glyph': 'SVGElement',
'mpath': 'SVGMPathElement',
'path': 'SVGPathElement',
'pattern': 'SVGPatternElement',
'polygon': 'SVGPolygonElement',
'polyline': 'SVGPolylineElement',
'radialGradient': 'SVGRadialGradientElement',
'rect': 'SVGRectElement',
'set': 'SVGSetElement',
'svg': 'SVGSVGElement',
'solidcolor': 'SVGElement',
'stop': 'SVGStopElement',
'switch': 'SVGSwitchElement',
'symbol': 'SVGSymbolElement',
'text': 'SVGTextElement',
'textPath': 'SVGTextPathElement',
'title': 'SVGTitleElement',
'tref': 'SVGElement',
'tspan': 'SVGTSpanElement',
'unknown': 'SVGElement',
'use': 'SVGUseElement',
'view': 'SVGViewElement',
'vkern': 'SVGElement'
}
def generate_html_elements(ctx, n):
for i in range(n):
tag = random.choice(list(_HTML_TYPES))
tagtype = _HTML_TYPES[tag]
ctx['htmlvarctr'] += 1
varname = 'htmlvar%05d' % ctx['htmlvarctr']
ctx['htmlvars'].append({'name': varname, 'type': tagtype})
ctx['htmlvargen'] += '/* newvar{' + varname + ':' + tagtype + '} */ var ' + varname + ' = document.createElement(\"' + tag + '\"); //' + tagtype + '\n'
def add_html_ids(matchobj, ctx):
tagname = matchobj.group(0)[1:-1]
if tagname in _HTML_TYPES:
ctx['htmlvarctr'] += 1
varname = 'htmlvar%05d' % ctx['htmlvarctr']
ctx['htmlvars'].append({'name': varname, 'type': _HTML_TYPES[tagname]})
ctx['htmlvargen'] += '/* newvar{' + varname + ':' + _HTML_TYPES[tagname] + '} */ var ' + varname + ' = document.getElementById(\"' + varname + '\"); //' + _HTML_TYPES[tagname] + '\n'
return matchobj.group(0) + 'id=\"' + varname + '\" '
elif tagname in _SVG_TYPES:
ctx['svgvarctr'] += 1
varname = 'svgvar%05d' % ctx['svgvarctr']
ctx['htmlvars'].append({'name': varname, 'type': _SVG_TYPES[tagname]})
ctx['htmlvargen'] += '/* newvar{' + varname + ':' + _SVG_TYPES[tagname] + '} */ var ' + varname + ' = document.getElementById(\"' + varname + '\"); //' + _SVG_TYPES[tagname] + '\n'
return matchobj.group(0) + 'id=\"' + varname + '\" '
else:
return matchobj.group(0)
def generate_function_body(jsgrammar, htmlctx, num_lines):
js = ''
js += 'var fuzzervars = {};\n\n'
js += "SetVariable(fuzzervars, window, 'Window');\nSetVariable(fuzzervars, document, 'Document');\nSetVariable(fuzzervars, document.body.firstChild, 'Element');\n\n"
js += '//beginjs\n'
js += htmlctx['htmlvargen']
js += jsgrammar._generate_code(num_lines, htmlctx['htmlvars'])
js += '\n//endjs\n'
js += 'var fuzzervars = {};\nfreememory()\n'
return js
def check_grammar(grammar):
"""Checks if grammar has errors and if so outputs them.
Args:
grammar: The grammar to check.
"""
for rule in grammar._all_rules:
for part in rule['parts']:
if part['type'] == 'text':
continue
tagname = part['tagname']
# print tagname
if tagname not in grammar._creators:
print('No creators for type ' + tagname)
def generate_new_sample(template, htmlgrammar, cssgrammar, jsgrammar):
"""Parses grammar rules from string.
Args:
template: A template string.
htmlgrammar: Grammar for generating HTML code.
cssgrammar: Grammar for generating CSS code.
jsgrammar: Grammar for generating JS code.
Returns:
A string containing sample data.
"""
result = template
css = cssgrammar.generate_symbol('rules')
html = htmlgrammar.generate_symbol('bodyelements')
htmlctx = {
'htmlvars': [],
'htmlvarctr': 0,
'svgvarctr': 0,
'htmlvargen': ''
}
html = re.sub(
r'<[a-zA-Z0-9_-]+ ',
lambda match: add_html_ids(match, htmlctx),
html
)
generate_html_elements(htmlctx, _N_ADDITIONAL_HTMLVARS)
result = result.replace('<cssfuzzer>', css)
result = result.replace('<htmlfuzzer>', html)
handlers = False
while '<jsfuzzer>' in result:
numlines = _N_MAIN_LINES
if handlers:
numlines = _N_EVENTHANDLER_LINES
else:
handlers = True
result = result.replace(
'<jsfuzzer>',
generate_function_body(jsgrammar, htmlctx, numlines),
1
)
return result
def generate_samples(grammar_dir, outfiles):
"""Generates a set of samples and writes them to the output files.
Args:
grammar_dir: directory to load grammar files from.
outfiles: A list of output filenames.
"""
f = open(os.path.join(grammar_dir, 'template.html'))
template = f.read()
f.close()
htmlgrammar = Grammar()
err = htmlgrammar.parse_from_file(os.path.join(grammar_dir, 'html.txt'))
# CheckGrammar(htmlgrammar)
if err > 0:
print('There were errors parsing grammar')
return
cssgrammar = Grammar()
err = cssgrammar.parse_from_file(os.path.join(grammar_dir, 'css.txt'))
# CheckGrammar(cssgrammar)
if err > 0:
print('There were errors parsing grammar')
return
jsgrammar = Grammar()
err = jsgrammar.parse_from_file(os.path.join(grammar_dir, 'js.txt'))
# CheckGrammar(jsgrammar)
if err > 0:
print('There were errors parsing grammar')
return
# JS and HTML grammar need access to CSS grammar.
# Add it as import
htmlgrammar.add_import('cssgrammar', cssgrammar)
jsgrammar.add_import('cssgrammar', cssgrammar)
for outfile in outfiles:
result = generate_new_sample(template, htmlgrammar, cssgrammar,
jsgrammar)
if result is not None:
print('Writing a sample to ' + outfile)
try:
f = open(outfile, 'w')
f.write(result)
f.close()
except IOError:
print('Error writing to output')
def get_option(option_name):
for i in range(len(sys.argv)):
if (sys.argv[i] == option_name) and ((i + 1) < len(sys.argv)):
return sys.argv[i + 1]
elif sys.argv[i].startswith(option_name + '='):
return sys.argv[i][len(option_name) + 1:]
return None
def main():
fuzzer_dir = os.path.dirname(__file__)
multiple_samples = False
for a in sys.argv:
if a.startswith('--output_dir='):
multiple_samples = True
if '--output_dir' in sys.argv:
multiple_samples = True
if multiple_samples:
print('Running on ClusterFuzz')
out_dir = get_option('--output_dir')
nsamples = int(get_option('--no_of_files'))
print('Output directory: ' + out_dir)
print('Number of samples: ' + str(nsamples))
if not os.path.exists(out_dir):
os.mkdir(out_dir)
outfiles = []
for i in range(nsamples):
outfiles.append(os.path.join(out_dir, 'fuzz-' + str(i).zfill(5) + '.html'))
generate_samples(fuzzer_dir, outfiles)
elif len(sys.argv) > 1:
outfile = sys.argv[1]
generate_samples(fuzzer_dir, [outfile])
else:
print('Arguments missing')
print("Usage:")
print("\tpython generator.py <output file>")
print("\tpython generator.py --output_dir <output directory> --no_of_files <number of output files>")
if __name__ == '__main__':
main()