Skip to main content

A coverage-guided fuzzer for Python and Python extensions.

Project description

Atheris: A Coverage-Guided, Native Python Fuzzer

Atheris is a coverage-guided Python fuzzing engine. It supports fuzzing of Python code, but also native extensions written for CPython. Atheris is based off of libFuzzer. When fuzzing native code, Atheris can be used in combination with Address Sanitizer or Undefined Behavior Sanitizer to catch extra bugs.

Installation Instructions

Atheris supports Linux (32- and 64-bit) and Mac OS X, Python versions 3.6-3.9.

You can install prebuilt versions of Atheris with pip:

pip3 install atheris

These wheels come with a built-in libFuzzer, which is fine for fuzzing Python code. If you plan to fuzz native extensions, you may need to build from source to ensure the libFuzzer version in Atheris matches your Clang version.

Building from Source

Atheris relies on libFuzzer, which is distributed with Clang. If you have a sufficiently new version of clang on your path, installation from source is as simple as:

# Build latest release from source
pip3 install --no-binary atheris atheris
# Build development code from source
git clone https://github.com/google/atheris.git
cd atheris
pip3 install .

If you don't have clang installed or it's too old, you'll need to download and build the latest version of LLVM. Follow the instructions in Installing Against New LLVM below.

Mac

Apple Clang doesn't come with libFuzzer, so you'll need to install a new version of LLVM from head. Follow the instructions in Installing Against New LLVM below.

Installing Against New LLVM

# Building LLVM
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake -DLLVM_ENABLE_PROJECTS='clang;compiler-rt' -G "Unix Makefiles" ../llvm
make -j 10  # This step is very slow

# Installing Atheris
CLANG_BIN="$(pwd)/bin/clang" pip3 install <whatever>

Using Atheris

Example

import atheris

with atheris.instrument_imports():
  import some_library
  import sys

def TestOneInput(data):
  some_library.parse(data)

atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()

When fuzzing Python, Atheris will report a failure if the Python code under test throws an uncaught exception.

Python coverage

Atheris collects Python coverage information by instrumenting bytecode. There are 3 options for adding this instrumentation to the bytecode:

  • You can instrument the libraries you import:

    with atheris.instrument_imports():
      import foo
      from bar import baz
    

    This will cause instrumentation to be added to foo and bar, as well as any libraries they import.

  • Or, you can instrument individual functions:

    @atheris.instrument_func
    def my_function(foo, bar):
      print("instrumented")
    
  • Or finally, you can instrument everything:

    atheris.instrument_all()
    

    Put this right before atheris.Setup(). This will find every Python function currently loaded in the interpreter, and instrument it. This might take a while.

Atheris can additionally instrument regular expression checks, e.g. re.search. To enable this feature, you will need to add: atheris.enabled_hooks.add("RegEx") To your script before your code calls re.compile. Internally this will import the re module and instrument the necessary functions. This is currently an experimental feature.

Why am I getting "No interesting inputs were found"?

You might see this error:

ERROR: no interesting inputs were found. Is the code instrumented for coverage? Exiting.

You'll get this error if the first 2 calls to TestOneInput didn't produce any coverage events. Even if you have instrumented some Python code, this can happen if the instrumentation isn't reached in those first 2 calls. (For example, because you have a nontrivial TestOneInput). You can resolve this by adding an atheris.instrument_func decorator to TestOneInput, using atheris.instrument_all(), or moving your TestOneInput function into an instrumented module.

Visualizing Python code coverage

Examining which lines are executed is helpful for understanding the effectiveness of your fuzzer. Atheris is compatible with coverage.py: you can run your fuzzer using the coverage.py module as you would for any other Python program. Here's an example:

python3 -m coverage run your_fuzzer.py -atheris_runs=10000  # Times to run
python3 -m coverage html
(cd htmlcov && python3 -m http.server 8000)

Coverage reports are only generated when your fuzzer exits gracefully. This happens if:

  • you specify -atheris_runs=<number>, and that many runs have elapsed.
  • your fuzzer exits by Python exception.
  • your fuzzer exits by sys.exit().

No coverage report will be generated if your fuzzer exits due to a crash in native code, or due to libFuzzer's -runs flag (use -atheris_runs). If your fuzzer exits via other methods, such as SIGINT (Ctrl+C), Atheris will attempt to generate a report but may be unable to (depending on your code). For consistent reports, we recommend always using -atheris_runs=<number>.

If you'd like to examine coverage when running with your corpus, you can do that with the following command:

python3 -m coverage run your_fuzzer.py corpus_dir/* -atheris_runs=$(ls corpus_dir | wc -l)

This will cause Atheris to run on each file in <corpus-dir>, then exit. Importantly, if you leave off the -atheris_runs=$(ls corpus_dir | wc -l), no coverage report will be generated.

Using coverage.py will significantly slow down your fuzzer, so only use it for visualizing coverage; don't use it all the time.

Fuzzing Native Extensions

In order for fuzzing native extensions to be effective, your native extensions must be instrumented. See Native Extension Fuzzing for instructions.

Structure-aware Fuzzing

Atheris is based on a coverage-guided mutation-based fuzzer (LibFuzzer). This has the advantage of not requiring any grammar definition for generating inputs, making its setup easier. The disadvantage is that it will be harder for the fuzzer to generate inputs for code that parses complex data types. Often the inputs will be rejected early, resulting in low coverage.

Atheris supports custom mutators (as offered by LibFuzzer) to produce grammar-aware inputs.

Example (Atheris-equivalent of the example in the LibFuzzer docs):

@atheris.instrument_func
def TestOneInput(data):
  try:
    decompressed = zlib.decompress(data)
  except zlib.error:
    return

  if len(decompressed) < 2:
    return

  try:
    if decompressed.decode() == 'FU':
      raise RuntimeError('Boom')
  except UnicodeDecodeError:
    pass

To reach the RuntimeError crash, the fuzzer needs to be able to produce inputs that are valid compressed data and satisfy the checks after decompression. It is very unlikely that Atheris will be able to produce such inputs: mutations on the input data will most probably result in invalid data that will fail at decompression-time.

To overcome this issue, you can define a custom mutator function (equivalent to LLVMFuzzerCustomMutator). This example produces valid compressed data. To enable Atheris to make use of it, pass the custom mutator function to the invocation of atheris.Setup.

def CustomMutator(data, max_size, seed):
  try:
    decompressed = zlib.decompress(data)
  except zlib.error:
    decompressed = b'Hi'
  else:
    decompressed = atheris.Mutate(decompressed, len(decompressed))
  return zlib.compress(decompressed)

atheris.Setup(sys.argv, TestOneInput, custom_mutator=CustomMutator)
atheris.Fuzz()

As seen in the example, the custom mutator may request Atheris to mutate data using atheris.Mutate() (this is equivalent to LLVMFuzzerMutate).

You can experiment with custom_mutator_example.py and see that without the mutator Atheris would not be able to find the crash, while with the mutator this is achieved in a matter of seconds.

$ python3 example_fuzzers/custom_mutator_example.py --no_mutator
[...]
#2      INITED cov: 2 ft: 2 corp: 1/1b exec/s: 0 rss: 37Mb
#524288 pulse  cov: 2 ft: 2 corp: 1/1b lim: 4096 exec/s: 262144 rss: 37Mb
#1048576        pulse  cov: 2 ft: 2 corp: 1/1b lim: 4096 exec/s: 349525 rss: 37Mb
#2097152        pulse  cov: 2 ft: 2 corp: 1/1b lim: 4096 exec/s: 299593 rss: 37Mb
#4194304        pulse  cov: 2 ft: 2 corp: 1/1b lim: 4096 exec/s: 279620 rss: 37Mb
[...]

$ python3 example_fuzzers/custom_mutator_example.py
[...]
INFO: found LLVMFuzzerCustomMutator (0x7f9c989fb0d0). Disabling -len_control by default.
[...]
#2      INITED cov: 2 ft: 2 corp: 1/1b exec/s: 0 rss: 37Mb
#3      NEW    cov: 4 ft: 4 corp: 2/11b lim: 4096 exec/s: 0 rss: 37Mb L: 10/10 MS: 1 Custom-
#12     NEW    cov: 5 ft: 5 corp: 3/21b lim: 4096 exec/s: 0 rss: 37Mb L: 10/10 MS: 7 Custom-CrossOver-Custom-CrossOver-Custom-ChangeBit-Custom-
 === Uncaught Python exception: ===
RuntimeError: Boom
Traceback (most recent call last):
  File "example_fuzzers/custom_mutator_example.py", line 62, in TestOneInput
    raise RuntimeError('Boom')
[...]

Custom crossover functions (equivalent to LLVMFuzzerCustomCrossOver) are also supported. You can pass the custom crossover function to the invocation of atheris.Setup. See its usage in custom_crossover_fuzz_test.py.

Structure-aware Fuzzing with Protocol Buffers

libprotobuf-mutator has bindings to use it together with Atheris to perform structure-aware fuzzing using protocol buffers.

See the documentation for atheris_libprotobuf_mutator.

Integration with OSS-Fuzz

Atheris is fully supported by OSS-Fuzz, Google's continuous fuzzing service for open source projects. For integrating with OSS-Fuzz, please see https://google.github.io/oss-fuzz/getting-started/new-project-guide/python-lang.

API

The atheris module provides three key functions: instrument_imports(), Setup() and Fuzz().

In your source file, import all libraries you wish to fuzz inside a with atheris.instrument_imports():-block, like this:

# library_a will not get instrumented
import library_a

with atheris.instrument_imports():
    # library_b will get instrumented
    import library_b

Generally, it's best to import atheris first and then import all other libraries inside of a with atheris.instrument_imports() block.

Next, define a fuzzer entry point function and pass it to atheris.Setup() along with the fuzzer's arguments (typically sys.argv). Finally, call atheris.Fuzz() to start fuzzing. You must call atheris.Setup() before atheris.Fuzz().

instrument_imports(include=[], exclude=[])

  • include: A list of fully-qualified module names that shall be instrumented.
  • exclude: A list of fully-qualified module names that shall NOT be instrumented.

This should be used together with a with-statement. All modules imported in said statement will be instrumented. However, because Python imports all modules only once, this cannot be used to instrument any previously imported module, including modules required by Atheris. To add coverage to those modules, use instrument_all() instead.

A full list of unsupported modules can be retrieved as follows:

import sys
import atheris
print(sys.modules.keys())

instrument_func(func)

  • func: The function to instrument.

This will instrument the specified Python function and then return func. This is typically used as a decorator, but can be used to instrument individual functions too. Note that the func is instrumented in-place, so this will affect all call points of the function.

This cannot be called on a bound method - call it on the unbound version.

instrument_all()

This will scan over all objects in the interpreter and call instrument_func on every Python function. This works even on core Python interpreter functions, something which instrument_imports cannot do.

This function is experimental.

Setup(args, test_one_input, internal_libfuzzer=None)

  • args: A list of strings: the process arguments to pass to the fuzzer, typically sys.argv. This argument list may be modified in-place, to remove arguments consumed by the fuzzer. See the LibFuzzer docs for a list of such options.
  • test_one_input: your fuzzer's entry point. Must take a single bytes argument. This will be repeatedly invoked with a single bytes container.
  • internal_libfuzzer: Indicates whether libfuzzer will be provided by atheris or by an external library (see native_extension_fuzzing.md). If unspecified, Atheris will determine this automatically. If fuzzing pure Python, leave this as True.

Fuzz()

This starts the fuzzer. You must have called Setup() before calling this function. This function does not return.

In many cases Setup() and Fuzz() could be combined into a single function, but they are separated because you may want the fuzzer to consume the command-line arguments it handles before passing any remaining arguments to another setup function.

FuzzedDataProvider

Often, a bytes object is not convenient input to your code being fuzzed. Similar to libFuzzer, we provide a FuzzedDataProvider to translate these bytes into other input forms.

You can construct the FuzzedDataProvider with:

fdp = atheris.FuzzedDataProvider(input_bytes)

The FuzzedDataProvider then supports the following functions:

def ConsumeBytes(count: int)

Consume count bytes.

def ConsumeUnicode(count: int)

Consume unicode characters. Might contain surrogate pair characters, which according to the specification are invalid in this situation. However, many core software tools (e.g. Windows file paths) support them, so other software often needs to too.

def ConsumeUnicodeNoSurrogates(count: int)

Consume unicode characters, but never generate surrogate pair characters.

def ConsumeString(count: int)

Alias for ConsumeBytes in Python 2, or ConsumeUnicode in Python 3.

def ConsumeInt(int: bytes)

Consume a signed integer of the specified size (when written in two's complement notation).

def ConsumeUInt(int: bytes)

Consume an unsigned integer of the specified size.

def ConsumeIntInRange(min: int, max: int)

Consume an integer in the range [min, max].

def ConsumeIntList(count: int, bytes: int)

Consume a list of count integers of size bytes.

def ConsumeIntListInRange(count: int, min: int, max: int)

Consume a list of count integers in the range [min, max].

def ConsumeFloat()

Consume an arbitrary floating-point value. Might produce weird values like NaN and Inf.

def ConsumeRegularFloat()

Consume an arbitrary numeric floating-point value; never produces a special type like NaN or Inf.

def ConsumeProbability()

Consume a floating-point value in the range [0, 1].

def ConsumeFloatInRange(min: float, max: float)

Consume a floating-point value in the range [min, max].

def ConsumeFloatList(count: int)

Consume a list of count arbitrary floating-point values. Might produce weird values like NaN and Inf.

def ConsumeRegularFloatList(count: int)

Consume a list of count arbitrary numeric floating-point values; never produces special types like NaN or Inf.

def ConsumeProbabilityList(count: int)

Consume a list of count floats in the range [0, 1].

def ConsumeFloatListInRange(count: int, min: float, max: float)

Consume a list of count floats in the range [min, max]

def PickValueInList(l: list)

Given a list, pick a random value

def ConsumeBool()

Consume either True or False.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

atheris-2.0.9.tar.gz (76.9 kB view details)

Uploaded Source

Built Distributions

atheris-2.0.9-cp39-cp39-manylinux2014_x86_64.whl (29.5 MB view details)

Uploaded CPython 3.9

atheris-2.0.9-cp39-cp39-macosx_11_0_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.9macOS 11.0+ x86-64

atheris-2.0.9-cp38-cp38-manylinux2014_x86_64.whl (29.6 MB view details)

Uploaded CPython 3.8

atheris-2.0.9-cp38-cp38-macosx_11_0_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.8macOS 11.0+ x86-64

atheris-2.0.9-cp37-cp37m-manylinux2014_x86_64.whl (29.8 MB view details)

Uploaded CPython 3.7m

atheris-2.0.9-cp37-cp37m-macosx_11_0_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.7mmacOS 11.0+ x86-64

atheris-2.0.9-cp36-cp36m-manylinux2014_x86_64.whl (29.8 MB view details)

Uploaded CPython 3.6m

atheris-2.0.9-cp36-cp36m-macosx_10_9_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.6mmacOS 10.9+ x86-64

File details

Details for the file atheris-2.0.9.tar.gz.

File metadata

  • Download URL: atheris-2.0.9.tar.gz
  • Upload date:
  • Size: 76.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for atheris-2.0.9.tar.gz
Algorithm Hash digest
SHA256 59516866d40ae212f39ab10d880fedd307c44fab1734b2837bf4a67d58c8398d
MD5 3e1a42b8a596cb2080d3deb78eef3104
BLAKE2b-256 69720a5133ad668728e7911403c149b5d5c15eab07820ca1562ad665b9fd975d

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 29.5 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for atheris-2.0.9-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 334a0b46e35221f9b5d8758f6289d80e16c37900802eb9823d97e4a1fc3242db
MD5 b2b01661c74cf3bae91e0fab4c7f3f05
BLAKE2b-256 3d82798b4173e72ad155fd376ee9094e81ae9aa320504f87b0ae2ac4fea26ce4

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp39-cp39-macosx_11_0_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.9, macOS 11.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.5

File hashes

Hashes for atheris-2.0.9-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 a1bc01d2ec51a8f6839ea01ae6ce8fbd14008e5bc50c07625f5ba5c7c7c9efe3
MD5 ab05bc1ff81996df74ac23b6448de0c6
BLAKE2b-256 d264e2c0b7309973365922ec19c61cd3fdeccddfc2d2e2dd569152e5f874e6f3

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 29.6 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for atheris-2.0.9-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 718995677a2e6df5b0fe77bebc2ab719005d85913989b340ec07d0d46cf5aaf5
MD5 886f2cf2d47630109970f2eed1d54332
BLAKE2b-256 1d17a0c43bd4cc3cf5bc5cce02f30a7bf8ed50d74b59434e97f5dc241145af0c

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp38-cp38-macosx_11_0_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp38-cp38-macosx_11_0_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.8, macOS 11.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.5

File hashes

Hashes for atheris-2.0.9-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 cb95e880c51d4863ebd81dc6ea4f15b92c412999b530f3bc3786f87312b02c48
MD5 63e84797b7ad13d624ccc4bff03a447c
BLAKE2b-256 4d8369baa5d87a2686dd8bcbc2cba56ba165f2e974789ca2d8873954e5439a7d

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 29.8 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for atheris-2.0.9-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4513e2e3029d08d5d5a460dc4a6d7b4381481d14504a30795e38db7d3daa7df2
MD5 381de70484d3102a6a1188e4847f64ca
BLAKE2b-256 a9d5f91a881c2322014cca7d28ca06eb43f558a224a59bfcbd5866fb79e95f47

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp37-cp37m-macosx_11_0_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp37-cp37m-macosx_11_0_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.7m, macOS 11.0+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.5

File hashes

Hashes for atheris-2.0.9-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 4a75209672c753f1772b28af9111917593dee066eda294c66f276e883af1a71a
MD5 beaf5739cf0028fe98012851fba91b1b
BLAKE2b-256 15833b1be5b65c395bb1c670bae3823fb2c73f405f11d10b20b3053977193f07

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 29.8 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.52.0 CPython/3.8.6

File hashes

Hashes for atheris-2.0.9-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dce3f949c966472ea870ee735df3bf6c1783811886be226457db58cf35c029a3
MD5 85bcdb4274434499e476f98f4d800ac5
BLAKE2b-256 8d02aa2d0094b446b5076f741b71ba20ae15209f5be48dd1d1b04afc825e686f

See more details on using hashes here.

File details

Details for the file atheris-2.0.9-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: atheris-2.0.9-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.5

File hashes

Hashes for atheris-2.0.9-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e76cc7041b4fdef4669fdfe4c74d1144f12967dc4885e73b7518e3fd3b1d08e3
MD5 96a896805bf521e4dcf5ea919f32529b
BLAKE2b-256 d066d97ae1dae369aaced526eefee813a684c9640b39c6fe35230f93c016ce84

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page