• Stars
    star
    298
  • Rank 139,663 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

AFL-based fuzz testing for .NET

SharpFuzz: AFL-based fuzz testing for .NET

NuGet Build Status License

SharpFuzz is a tool that brings the power of afl-fuzz to .NET platform. If you want to learn more about fuzzing, my motivation for writing SharpFuzz, the types of bugs it can find, or the technical details about how the integration with afl-fuzz works, read my blog post SharpFuzz: Bringing the power of afl-fuzz to .NET platform.

Table of contents

CVE

Articles

Trophies

If you find some interesting bugs with SharpFuzz, and are comfortable with sharing them, I would love to add them to this list. Please send me an email, make a pull request for the README file, or file an issue.

Requirements

AFL works on Linux and macOS. If you are using Windows, you can use any Linux distribution that works under the Windows Subsystem for Linux. For native Windows support, you can use libFuzzer instead of AFL.

You will need GNU make and a working compiler (gcc or clang) in order to compile afl-fuzz. You will also need to have the .NET Core 2.1 or greater installed on your machine in order to instrument .NET assemblies with SharpFuzz.

To simplify your fuzzing experience, it's also recommended to install PowerShell.

Installation

You can install afl-fuzz and SharpFuzz.CommandLine global .NET tool by running the following script:

#/bin/sh
set -eux

# Download and extract the latest afl-fuzz source package
wget http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz
tar -xvf afl-latest.tgz

rm afl-latest.tgz
cd afl-2.52b/

# Install afl-fuzz
sudo make install
cd ..
rm -rf afl-2.52b/

# Install SharpFuzz.CommandLine global .NET tool
dotnet tool install --global SharpFuzz.CommandLine

Usage

This tutorial assumes that you are somewhat familiar with afl-fuzz. If you don't know anything about it, you should first read the AFL quick start guide and the afl-fuzz README. If you have enough time, I would also recommend reading Understanding the status screen and Technical whitepaper for afl-fuzz.

As an example, we are going to fuzz Jil, which is a fast JSON serializer and deserializer (see SharpFuzz.Samples for many more examples of complete fuzzing projects).

1. Create a new .NET console project, then add Jil and SharpFuzz packages to it by running the following commands:

dotnet add package Jil
dotnet add package SharpFuzz

2. In your Main function, call SharpFuzz.Fuzzer.OutOfProcess.Run with the function that you want to test as a parameter:

using System;
using System.IO;
using SharpFuzz;

namespace Jil.Fuzz
{
  public class Program
  {
    public static void Main(string[] args)
    {
      Fuzzer.OutOfProcess.Run(stream =>
      {
        try
        {
          using (var reader = new StreamReader(stream))
          {
            JSON.DeserializeDynamic(reader);
          }
        }
        catch (DeserializationException) { }
      });
    }
  }
}

We want to fuzz the deserialization capabilities of Jil, which is why we are calling the JSON.DeserializeDynamic method. The input data will be provided to us via the stream parameter (if the code you are testing takes its input as a string, you can use an additional overload of Fuzzer.OutOfProcess.Run that accepts Action<string>).

If the code passed to Fuzzer.OutOfProcess.Run throws an exception, it will be reported to afl-fuzz as a crash. However, we want to treat only unexpected exceptions as bugs. DeserializationException is what we expect when we encounter an invalid JSON input, which is why we catch it in our example.

3. Create a directory with some test cases (one test is usually more than enough). Test files should contain some input that is accepted by your code as valid, and should also be as small as possible. For example, this is the JSON I'm using for testing JSON deserializers:

{"menu":{"id":1,"val":"X","pop":{"a":[{"click":"Open()"},{"click":"Close()"}]}}}

4. Let's say that your project is called Fuzzing.csproj and that your test cases are in the Testcases directory. Start fuzzing by running the fuzz.ps1 script like this:

pwsh scripts/fuzz.ps1 Jil.Fuzz.csproj -i Testcases

For formats such as HTML, JavaScript, JSON, or SQL, the fuzzing process can be greatly improved with the usage of a dictionary file. AFL comes with bunch of dictionaries, which you can find after installation in /usr/local/share/afl/dictionaries/. With this in mind, we can improve our fuzzing of Jil like this:

pwsh scripts/fuzz.ps1 Jil.Fuzz.csproj -i Testcases \
  -x /usr/local/share/afl/dictionaries/json.dict

5. Sit back and relax! You will often have some useful results within minutes, but sometimes it can take more than a day, so be patient.

The input files responsible for unhandled exceptions will appear in the findings/crashes directory. The total number of unique crashes will be displayed in red on the afl-fuzz status screen.

In practice, the real number of unique exceptions will often be much lower than the reported number, which is why it's usually best to write a small program that just goes through the crashing inputs, runs the fuzzing function on each of them, and saves only the inputs that produce unique stack traces.

Advanced topics

Acknowledgements

More Repositories

1

noise

.NET Standard 1.3 implementation of the Noise Protocol Framework (revision 33 of the spec)
C#
143
star
2

runtastic

Command line tool to archive all your Runtastic activities
Go
131
star
3

zinio

This command line tool removes DRM from magazines in your digital Zinio library
Go
50
star
4

aes-gcm-siv

.NET Core 3.0 implementation of AES-GCM-SIV nonce misuse-resistant authenticated encryption
C#
22
star
5

hkpropel-downloader

Download HKPropel (Human Kinetics) books in EPUB format
C#
21
star
6

future-plc-downloader

This command line tool gives you the option to download Future plc magazine issues from your digital library
Go
17
star
7

alignment-and-pipelining

Benchmarks showing the difference between the naive intrinsics usage and the optimized code that takes advantage of alignment and pipelining
C#
11
star
8

noisesocket

.NET Standard 1.3 implementation of the NoiseSocket Protocol (revision 2 of the spec)
C#
9
star
9

aes-armv8

Accelerated AES computation in pure C# using ARMv8 AES compiler intrinsics
C#
9
star
10

sqlite-vfsdemo

VFS implementation for SQLite that creates the database file if it doesn't exist, and fails otherwise
C
8
star
11

sha256-armv8

Accelerated SHA-256 computation in pure C# using ARMv8 SHA-256 compiler intrinsics
C#
7
star
12

cryptopals-go

Go solutions to the Matasano Crypto Challenges
Go
7
star
13

dark-horse-converter

Converts digital Dark Horse comics into CBZ format
Go
7
star
14

lftp-server

JSON RPC server for LFTP
Go
5
star
15

dark-horse-downloader

This Chrome extension gives you the option to download Dark Horse comic books from your digital library
JavaScript
5
star
16

sharpfuzz-samples

Complete SharpFuzz fuzzing projects for various NuGet packages
C#
4
star
17

protobuf-fuzzers

A collection of structure-aware fuzzers for SharpFuzz using libFuzzer with custom mutations and libprotobuf-mutator
C#
4
star
18

nginx-configuration

Strong nginx configuration for SSL Labs rating A+ and SecurityHeaders.io rating A
Nginx
3
star
19

libfuzzer-dotnet

libFuzzer driver for SharpFuzz
C++
3
star
20

javascript-crypto

Copy and paste friendly JavaScript cryptography using Web Cryptography API
JavaScript
2
star
21

mijailovic.net

My personal blog
SCSS
1
star
22

send-to-aria2

This Firefox extension captures download links and sends them to aria2 server
JavaScript
1
star
23

parsing-algorithms

Various algorithms for parsing mathematical expressions in infix notation
Go
1
star
24

dotfiles

Metalnem's dotfiles
Shell
1
star
25

dropbox

This Go library implements Dropbox API content hash algorithm
Go
1
star
26

hashes

Save hashes of all files in specified directories in SQLite database, and compare two databases against each other to see what's changed
Go
1
star