gazprea-fuzzer-python/gazprea_fuzzer.py
2023-11-18 12:21:52 -07:00

84 lines
2.5 KiB
Python

import json
from xml import etree
from fuzzingbook import Grammars
from fuzzingbook.Grammars import is_valid_grammar
from isla.solver import ISLaSolver
import ast_generator.ast_generator
from ast_parser.ast_solver import AstSolver
from ast_parser.gaz_unparser import AstParser
from ast_generator import gazprea_ast_grammar
import xml.etree.ElementTree as ET
import xml.dom.minidom
import ast_parser
class GazpreaFuzzer(ISLaSolver):
"""Produce Gazprea code"""
def __init__(self,
grammar: Grammars,
start_symbol: str = "<start>",
constraint: str = "",
**kwargs) -> None:
"""
@brief initialize a Gazprea code generator
:param grammar: the grammar from which you would like to generate code
:param start_symbol: the start symbol of the grammar (default "<start>")
:param constraint: any constraints that you would like to impose on the solver
:param kwargs: any extra arguments passed to the ISLaSolver
"""
assert start_symbol in grammar
assert is_valid_grammar(grammar)
super().__init__(grammar, constraint, start_symbol=start_symbol, **kwargs)
def fuzz(self) -> str:
"""Produce the hecking code"""
AST = AstParser(eval(str(self.solve())), from_xml=True)
AstSolver.fix_missing_locations(AST)
AST.unparse()
return AST.input
if __name__ == '__main__':
gen = ast_generator.ast_generator.AstGenerator(gazprea_ast_grammar.GAZPREA_TOP_LEVEL, json.loads('{}'))
with open("debug/test.xml", 'w') as t:
et = gen.generate_ast()
dom = xml.dom.minidom.parseString(ET.tostring(et).decode('utf-8'))
pretty: str = dom.toprettyxml()
repretty = ""
for line in pretty.split('\n'):
if line.startswith("<?xml"):
pass
else:
repretty += (line + '\n')
t.write(repretty)
gen.populate_ast(et)
with open("debug/populated.xml", 'w') as t:
dom = xml.dom.minidom.parseString(ET.tostring(et).decode('utf-8'))
pretty: str = dom.toprettyxml()
repretty = ""
for line in pretty.split('\n'):
if line.startswith("<?xml"):
pass
else:
repretty += (line + '\n')
t.write(repretty)
source = ast_parser.ast_parser.AstParser(et, from_xml=True)
source.unparse()
with open("debug/test.gz", 'w') as t:
t.write(source.input)