
Um Fuzzer baseado em gramática com feedback
Nautilus é um fuzzer guiado por cobertura e baseado em gramática. Você pode usá-lo para melhorar sua cobertura de teste e encontrar mais bugs. Ao especificar a gramática de entradas semi-válidas, o Nautilus é capaz de realizar mutações complexas e descobrir casos de teste mais interessantes. Muitas ideias por trás deste fuzzer estão documentadas em um artigo publicado no NDSS 2019.
A versão 2.0 adicionou muitas melhorias a este protótipo inicial e agora é 100% compatível com o AFL++. Além das melhorias gerais de usabilidade, a versão 2.0 inclui muitos novos recursos interessantes:
Você especifica uma gramática usando regras como EXPR -> EXPR + EXPR ou EXPR -> NUM e NUM -> 1. A partir dessas regras, o fuzzer constrói uma árvore. Essa representação interna permite aplicar mutações muito mais complexas do que bytes brutos. Essa árvore é então transformada em uma entrada real para a aplicação alvo. Em gramáticas livres de contexto normais, esse processo é direto: todas as folhas são concatenadas. A árvore à esquerda no exemplo abaixo resultaria na entrada a=1+2 e a da direita em a=1+1+1+2. Para aumentar a expressividade das suas gramáticas, usando o Nautilus você pode fornecer funções python para o processo de unparse para permitir especificações muito mais complexas.
# checkout the git
git clone '[email protected]:nautilus-fuzz/nautilus.git'
cd nautilus
/path/to/AFLplusplus/afl-clang-fast test.c -o test #afl-clang-fast as provided by AFL
# all arguments can also be set using the config.ron file
cargo run --release -- -g grammars/grammar_py_example.py -o /tmp/workdir -- ./test @@
# or if you want to use QEMU mode:
cargo run /path/to/AFLplusplus/afl-qemu-trace -- ./test_bin @@
Aqui, usamos python para gerar uma gramática para entradas semelhantes a xml válidas. Observe o uso de uma regra de script para garantir que as tags de abertura e fechamento correspondam.
#ctx.rule(NONTERM: string, RHS: string|bytes) adds a rule NONTERM->RHS. We can use {NONTERM} in the RHS to request a recursion.
ctx.rule("START","<document>{XML_CONTENT}</document>")
ctx.rule("XML_CONTENT","{XML}{XML_CONTENT}")
ctx.rule("XML_CONTENT","")
#ctx.script(NONTERM:string, RHS: [string]], func) adds a rule NONTERM->func(*RHS).
# In contrast to normal `rule`, RHS is an array of nonterminals.
# It's up to the function to combine the values returned for the NONTERMINALS with any fixed content used.
ctx.script("XML",["TAG","ATTR","XML_CONTENT"], lambda tag,attr,body: b"<%s %s>%s</%s>"%(tag,attr,body,tag) )
ctx.rule("ATTR","foo=bar")
ctx.rule("TAG","some_tag")
ctx.rule("TAG","other_tag")
#sometimes we don't want to explore the set of possible inputs in more detail. For example, if we fuzz a script
#interpreter, we don't want to spend time on fuzzing all different variable names. In such cases we can use Regex
#terminals. Regex terminals are only mutated during generation, but not during normal mutation stages, saving a lot of time.
#The fuzzer still explores different values for the regex, but it won't be able to learn interesting values incrementally.
#Use this when incremantal exploration would most likely waste time.
ctx.regex("TAG","[a-z]+")
Para testar suas gramáticas, você pode usar o gerador:
$ cargo run --bin generator -- -g grammars/grammar_py_exmaple.py -t 100
<document><some_tag foo=bar><other_tag foo=bar><other_tag foo=bar><some_tag foo=bar></some_tag></other_tag><some_tag foo=bar><other_tag foo=bar></other_tag></some_tag><other_tag foo=bar></other_tag><some_tag foo=bar></some_tag></other_tag><other_tag foo=bar></other_tag><some_tag foo=bar></some_tag></some_tag></document>
Você também pode usar o Nautilus em combinação com o AFL. Basta apontar o AFL -o para o mesmo diretório de trabalho (workdir), e o AFL sincronizará com o Nautilus. Observe que isso é unilateral. O AFL importa entradas do Nautilus, mas não o contrário.
#Terminal/Screen 1
./afl-fuzz -Safl -i /tmp/seeds -o /tmp/workdir/ ./test @@
#Terminal/Screen 2
cargo run --release -- -o /tmp/workdir -- ./test @@