Giter VIP home page Giter VIP logo

javafuzz's Introduction

fuzzit.dev was acquired by GitLab and the new home for this repo is here

Javafuzz: coverage-guided fuzz testing for Java

Javafuzz is coverage-guided fuzzer for testing Java packages.

Fuzzing for safe languages like nodejs is a powerful strategy for finding bugs like unhandled exceptions, logic bugs, security bugs that arise from both logic bugs and Denial-of-Service caused by hangs and excessive memory usage.

Fuzzing can be seen as a powerful and efficient strategy in real-world software in addition to classic unit-tests.

Usage

Fuzz Target

The first step is to implement the following function (also called a fuzz target):

public class FuzzExample extends AbstractFuzzTarget {
    public void fuzz(byte[] data) {
        try {
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));
        } catch (IOException e) {
            // ignore as we expect this exception
        }
    }
}

Features of the fuzz target:

  • Javafuzz will call the fuzz target in an infinite loop with random data (according to the coverage guided algorithm) passed to buf.
  • The function must catch and ignore any expected only (dont catch Exception) exceptions that arise when passing invalid input to the tested package.
  • The fuzz target must call the test function/library with with the passed buffer or a transformation on the test buffer if the structure is different or from different type.
  • Fuzz functions can also implement application level checks to catch application/logical bugs - For example: decode the buffer with the testable library, encode it again, and check that both results are equal. To communicate the results the result/bug the function should throw an exception.
  • Javafuzz will report any unhandled exceptions as crashes as well as inputs that hit the memory limit specified to javafuzz or hangs/they run more the the specified timeout limit per testcase.

Installing

Add this to your pom.xml

  <dependencies>
    <dependency>
      <groupId>dev.fuzzit.javafuzz</groupId>
      <artifactId>core</artifactId>
      <version>1.23-SNAPSHOT</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

<plugin>
        <plugin>
          <groupId>dev.fuzzit.javafuzz</groupId>
          <artifactId>javafuzz-maven-plugin</artifactId>
          <version>1.22</version>
        </plugin>
</plugins>

Running

The next step is to javafuzz with your fuzz target function

docker run -it maven:3.6.2-jdk-11 /bin/bash
git clone https://github.com/fuzzitdev/javafuzz.git
cd javafuzz
mvn install
cd examples
wget -O jacocoagent.jar https://github.com/fuzzitdev/javafuzz/raw/master/javafuzz-maven-plugin/src/main/resources/jacocoagent-exp.jar
MAVEN_OPTS="-javaagent:jacocoagent.jar" mvn javafuzz:fuzz -DclassName=dev.fuzzit.javafuzz.examples.FuzzYaml
# Output:
#0 READ units: 0
#1 NEW     cov: 61 corp: 0 exec/s: 1 rss: 23.37 MB
#23320 PULSE     cov: 61 corp: 1 exec/s: 10614 rss: 35.3 MB
#96022 NEW     cov: 70 corp: 1 exec/s: 11320 rss: 129.95 MB
#96971 NEW     cov: 78 corp: 2 exec/s: 10784 rss: 129.95 MB
#97046 NEW     cov: 79 corp: 3 exec/s: 9375 rss: 129.95 MB
#97081 NEW     cov: 81 corp: 4 exec/s: 11666 rss: 129.95 MB
#97195 NEW     cov: 93 corp: 5 exec/s: 9500 rss: 129.95 MB
#97216 NEW     cov: 97 corp: 6 exec/s: 10500 rss: 129.95 MB
#97238 NEW     cov: 102 corp: 7 exec/s: 11000 rss: 129.95 MB
#97303 NEW     cov: 108 corp: 8 exec/s: 10833 rss: 129.96 MB
#97857 PULSE     cov: 108 corp: 9 exec/s: 225 rss: 129.96 MB
#97857 PULSE     cov: 108 corp: 9 exec/s: 0 rss: 940.97 MB
#97857 PULSE     cov: 108 corp: 9 exec/s: 0 rss: 1566.01 MB

This example quickly finds an infinite hang which takes all the memory in jpeg-js.

Corpus

Javafuzz will generate and test various inputs in an infinite loop. corpus is optional directory and will be used to save the generated testcases so later runs can be started from the same point and provided as seed corpus.

Javafuzz can also start with an empty directory (i.e no seed corpus) though some valid test-cases in the seed corpus may speed up the fuzzing substantially.

Javafuzz tries to mimic some of the arguments and output style from libFuzzer.

More fuzz targets examples (for real and popular libraries) are located under the examples directory and bugs that were found using those targets are listed in the trophies section.

Coverage

For coverage instrumentation we use JaCoCo library

Other languages

Currently this library also exists for the following languages:

Credits & Acknowledgments

Javafuzz is a port of fuzzitdev/jsfuzz.

Which in turn based based on go-fuzz originally developed by Dmitry Vyukov's. Which is in turn heavily based on Michal Zalewski AFL.

Another solid fuzzing with coverage library for java is JQF but is more focused on semantic fuzzing (i.e structure aware) and thus depends on quickcheck. JavaFuzz does not depends on any framework an focuses on mutations producing buffer array and using coverage to find more bugs.

Contributions

Contributions are welcome!:) There are still a lot of things to improve, and tests and features to add. We will slowly post those in the issues section. Before doing any major contribution please open an issue so we can discuss and help guide the process before any unnecessary work is done.

javafuzz's People

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  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  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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javafuzz's Issues

Java fuzzing is failing due to unknown zest option

When attempting to run our fuzz test suite remotely all of our jobs are failing with the following message:

020/02/25 14:18:11 Running fuzzing with: [-jar zest-cli.jar --exit-on-crash --exact-crash-path=artifact --libfuzzer-compat-output fuzzer -rss_limit_mb=1984 com.github.nitram509.jmacaroons.FuzzTests macaroonSerialization]
FUZZER: Unknown option: '-rss_limit_mb=1984'
FUZZER: Usage: ZestCLI [[-e] [--exact-crash-path=<exactCrashPath>]] [[-e]
FUZZER:                [--exact-crash-path=<exactCrashPath>]] [-hlV]
FUZZER:                [-i=<inputDirectory>] [-o=<outputDirectory>] PACKAGE TEST_CLASS
FUZZER:                TEST_METHOD
FUZZER:       PACKAGE           package containing the fuzzer and all dependencies
FUZZER:       TEST_CLASS        full class name where the fuzz function is located
FUZZER:       TEST_METHOD       fuzz function name
FUZZER:   -e, --exit-on-crash   Exit fuzzer on first crash (default: false)
FUZZER:       --exact-crash-path=<exactCrashPath>
FUZZER:                         exact path for the crash
FUZZER:   -h, --help            Show this help message and exit.
FUZZER:   -i, --input=<inputDirectory>
FUZZER:                         Input directory containing seed test cases (default:
FUZZER:                           none)
FUZZER:   -l, --libfuzzer-compat-output
FUZZER:                         Use libFuzzer compat output instead of AFL like stats
FUZZER:                           screen (default: false)
FUZZER:   -o, --output=<outputDirectory>
FUZZER:                         Output Directory containing results (default:
FUZZER:                           fuzz_results)
FUZZER:   -V, --version         Print version information and exit.
2020/02/25 14:18:12 process finished with error = exit status 2
2020/02/25 14:18:12 Exit Status: 2

I can confirm the suite runs locally, but I'm not sure how to work around this issue using the fuzzit dashboard.

ClassNotFoundException jacoco

I have followed the steps in the readme multiple times but end up getting this exception, am I missing something obvious here?

java.lang.ClassNotFoundException: org.jacoco.agent.rt.RT
	at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
	at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
	at java.base/java.lang.Class.forName0(Native Method)
	at java.base/java.lang.Class.forName(Class.java:315)
	at dev.fuzzit.javafuzz.core.Fuzzer.<init>(Fuzzer.java:25)
	at org.fuzzitdev.javafuzz.maven.FuzzGoal.execute(FuzzGoal.java:62)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:192)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
hello mojo

Byte overflow leading to index-out-of-bounds exception

Consider changing these instances of Byte to Integer:

List<Byte> digits = new ArrayList<Byte>();

and eliminating this cast:

It is easy to overflow a Byte. Moreover, because a Byte is signed, an overflow can result in a negative value and an ArrayIndexOutOfBoundsException on this line:

Such was the case with a corpus I was using.

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.