Code Monkey home page Code Monkey logo

abnffuzzer's Introduction

ABNF Fuzzer

This is a Java fuzz testing tool that can find defects in implementations of Augmented Backus-Naur Form (ABNF) rules such as IETF RFCs. ABNF is itself defined in RFCs 5234 and 7405. You can use it to generate random valid inputs for test cases, which can be helpful for finding edge case defects. I wrote it primarily as a way to learn ANTLR. Thanks to @rainerschuster for providing the Anbf.g4 grammar.

For additional documentation including dependency information and Javadoc please see the Maven generated site.

Usage

This tool can be called directly from Java code โ€” such as a JUnit test case โ€” or from the command line. In order to use it you first need a file containing only ABNF rule definitions. Here's a sample simple ABNF rules file.

foo = bar / baz
bar = "Hello"
baz = "World!"

If you're testing an implementation of an IETF RFC you can simply copy and paste the formal rule definitions into a new file; usually they're all contained in a single section near the end of the document. Be sure to check the errata as those correct the rules in some RFCs. You can also try the IETF Author Tools ABNF Tools, although it appears to produce incorrect output for some RFCs so you may need to manually edit the results. You should also manually remove any prose values from the rules; obviously this tool can't interpret prose so it's usually best to replace those with literals.

Options are available to limit the output by excluding certain rules. This can be useful if your application only provides a partial implementation of a particular rule and you don't want to test certain alternate forms. The tool can generate output as either raw bytes (octets), or characters strings encoded using any of the standard Java character sets.

The probability of finding a particular defect with fuzz testing increases with the number of test cases, up to an asymptotic limit. You have to balance that against test execution time. I recommend doing an extended fuzz testing run with thousands or millions of iterations the first time; let it run for hours. Then use a much smaller number of iterations in your automated continuous integration process so that it doesn't cause long delays.

With some complex ABNF files the fuzzer may cause the JVM to run out of memory or stack space due to excessive recursion. If you run into this problem you may need to exclude the problematic rules or manually edit the ABNF file to block certain paths.

JUnit

Call one of the generate methods wherever you need a random String or byte[] value matching a particular ABNF rule. For example let's say you have a class named MyClass containing a method named myMethod which takes a String parameter and returns true if that parameter matches ABNF rule "foo" defined in RFC 99999.

    @Test
    public void testMyMethod() throws IOException {
        File file = new File("rfc99999.txt");
        Fuzzer fuzzer = new Fuzzer(file);
        
        MyClass m = new MyClass();
        for (int i = 0; i < 100; i++) {
            assertTrue(m.myMethod(fuzzer.generateAscii("foo")));
        }
    }

For additional samples see the JUnit test cases in this repository.

Command Line

For testing web services or applications written in other languages this tool can also be called from the command line. Binary jar file releases are available through this repository. By default it reads ABNF rules from stdin, generates a random value matching the ABNF rule named as the last command-line parameter, and writes it to stdout. You need to have the ANTLR Java runtime binaries and Apache Commons CLI in your CLASSPATH. Command-line options are available to control the number of values to generate, character set, input source, ouput destination. Use the -? command-line option for help on those options.

java com.github.nradov.abnffuzzer.Fuzzer -n 1000 -i rfc99999.txt -o testcases.txt foo

abnffuzzer's People

Contributors

dependabot[bot] avatar nradov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

abnffuzzer's Issues

No enum constant com.github.nradov.abnffuzzer.Terminal.Radix.s

Seeing the following issue with https://tools.ietf.org/html/rfc7950

public class YangTest {

    /**
     * Test the {@link UUID#fromString(String)} method.
     * 
     * @throws IOException
     *             if an error occurs while reading a resource
     */
    @Test
    public void testFromFile() throws IOException {

        final Fuzzer f = new Fuzzer(
                YangTest.class.getResourceAsStream("/rfc7950.yang"));
        
    }

}

Results in

Results :

Tests in error:
  testFromFile(com.github.nradov.abnffuzzer.YangTest): No enum constant com.github.nradov.abnffuzzer.Terminal.Radix.s

Tests run: 11, Failures: 0, Errors: 1, Skipped: 0

Could you clarify why its failing please? Included the abnf in attached zip.

rfc7950.yang.zip

Directly parse ABNF rules from IETF RFCs

The Fuzzer class can currently only parse ABNF rules from files that contain only those rules as per the ANTLR grammar. So users who want to test implementations of IETF RFCs first have to manually copy and paste the rules into a separate file.
It would improve usability if this library could take the URI for an RFC as an argument and automatically extract the ABNF rules out of the entire document. However this appears to be a difficult problem in pattern recognition; it's hard to tell whether a particular line contains an ABNF rule, narrative text, or part of the header / footer. The bap aex tool may offer a starting point, however it doesn't appear to produce correct output for some RFCs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.