### Install and Execute PythonFuzz Source: https://github.com/fuzzitdev/pythonfuzz/blob/master/README.md Instructions for installing the PythonFuzz package via pip and executing a fuzzing target script from the command line. ```bash pip install pythonfuzz python examples/htmlparser/fuzz.py ``` -------------------------------- ### Fuzzing Compression Libraries Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Shows an example of fuzzing zlib decompression to find edge cases in compressed data handling. ```APIDOC ## Fuzzing Compression Libraries ### Description Example of fuzzing zlib decompression to find edge cases in compressed data handling. ### Method N/A (Fuzzing function) ### Endpoint N/A ### Parameters N/A ### Request Example ```python import zlib from pythonfuzz.main import PythonFuzz @PythonFuzz def fuzz(buf): try: zlib.decompress(buf) except zlib.error: pass if __name__ == '__main__': fuzz() # Run: python fuzz_zlib.py ./zlib_corpus ``` ### Response N/A (Fuzzing function does not return a response in the traditional sense) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### PythonFuzz Application-Level Bug Detection with JSON Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Demonstrates detecting application-level bugs by implementing custom assertions within a fuzz target. This example checks for round-trip consistency of JSON parsing and re-serialization. ```python from pythonfuzz.main import PythonFuzz import json @PythonFuzz def fuzz(buf): try: string = buf.decode("utf-8") # Parse and re-serialize to check round-trip consistency parsed = json.loads(string) reserialized = json.dumps(parsed) reparsed = json.loads(reserialized) # Application-level check: verify round-trip produces same result if parsed != reparsed: raise AssertionError(f"Round-trip mismatch: {parsed} != {reparsed}") except (UnicodeDecodeError, json.JSONDecodeError): # Expected exceptions for invalid input pass if __name__ == '__main__': fuzz() ``` -------------------------------- ### Fuzzing Python's HTML Parser Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt This example demonstrates how to use PythonFuzz to fuzz the built-in `html.parser.HTMLParser`. It defines a fuzz target function that decodes input bytes to ASCII, feeds them to the HTMLParser, and catches `UnicodeDecodeError`. This setup helps identify unhandled exceptions and edge cases within the HTML parsing logic. ```python try: from html.parser import HTMLParser except ImportError: from HTMLParser import HTMLParser from pythonfuzz.main import PythonFuzz @PythonFuzz def fuzz(buf): try: string = buf.decode("ascii") parser = HTMLParser() parser.feed(string) except UnicodeDecodeError: pass if __name__ == '__main__': fuzz() # Run: python fuzz_html.py ./html_corpus # Output: # #0 READ units: 0 # #394378 NEW cov: 608 corp: 24 exec/s: 1119 rss: 10.73828125 MB # crash was written to crash-dbfa437e5956643645681fe6a3ac76997be0b29a7c7af82d88c8c390f379502d ``` -------------------------------- ### Managing Fuzzing Corpus Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Illustrates how to initialize a Corpus object, load seed directories, generate mutated inputs, and save new interesting test cases. ```python from pythonfuzz.corpus import Corpus corpus = Corpus( dirs=["./corpus", "./seeds"], max_input_size=4096, mutators_filter=None, dict_path="./dict.txt" ) print(f"Corpus length: {corpus.length}") mutated_input = corpus.generate_input() new_input = bytearray(b"interesting test case") corpus.put(new_input) buf = bytearray(b"seed input") mutated = corpus.mutate(buf) ``` -------------------------------- ### PythonFuzz Coverage Tracing Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Illustrates how PythonFuzz uses Python's trace functionality for coverage-guided mutation. It shows how to manually set the trace function and execute code to be traced. ```python from pythonfuzz import tracer import sys # The tracer is automatically configured when using @PythonFuzz # For manual usage: # Set trace function sys.settrace(tracer.trace) # Execute code to trace def target_function(data): if len(data) > 10: process_long(data) else: process_short(data) target_function(b"test") # Get total coverage (number of unique line transitions) coverage = tracer.get_coverage() print(f"Coverage: {coverage} unique transitions") # Coverage is tracked as (previous_line, current_line) pairs per file # Higher coverage numbers indicate more code paths explored ``` -------------------------------- ### CLI Interface Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt PythonFuzz provides a robust command-line interface for executing fuzzing sessions with various configuration flags. ```APIDOC ## CLI Execution ### Description Run the fuzzer directly from the terminal using various flags to control behavior, resource limits, and mutation strategies. ### Common Flags - **--rss-limit-mb** (int) - Memory limit in MB. - **--timeout** (int) - Timeout per test case in seconds. - **--runs** (int) - Number of iterations to perform. - **--dict** (string) - Path to a dictionary file for smarter mutations. - **--mutator-filter** (string) - Filter or exclude specific mutator types. ### Example ```bash python fuzz.py --rss-limit-mb 1024 --timeout 60 ./corpus ``` ``` -------------------------------- ### Using Dictionaries for Smarter Mutations Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Explains how to load and use dictionaries in PythonFuzz to inject domain-specific tokens into test inputs, enhancing the fuzzer's effectiveness. ```python from pythonfuzz.dictionary import Dictionary dictionary = Dictionary() dictionary.load("./html.dict") word = dictionary.get_word() dictionary.load_directory("./dict_samples/") ``` -------------------------------- ### Fuzzing Archive Formats Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Demonstrates fuzz testing of Python's zipfile module to find issues in ZIP archive handling. ```APIDOC ## Fuzzing Archive Formats ### Description Example demonstrating fuzz testing of Python's zipfile module to find issues in ZIP archive handling. ### Method N/A (Fuzzing function) ### Endpoint N/A ### Parameters N/A ### Request Example ```python import io import zipfile from pythonfuzz.main import PythonFuzz try: allowed_exceptions = (zipfile.BadZipFile, zipfile.LargeZipFile) except AttributeError: # Python 2 compatibility allowed_exceptions = (zipfile.BadZipfile, zipfile.LargeZipFile) @PythonFuzz def fuzz(buf): f = io.BytesIO(buf) try: z = zipfile.ZipFile(f) z.testzip() except: pass if __name__ == '__main__': fuzz() # Run: python fuzz_zip.py ./zip_corpus ``` ### Response N/A (Fuzzing function does not return a response in the traditional sense) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Fuzzing Compression Libraries with PythonFuzz Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Shows how to fuzz the zlib decompression library. It catches zlib.error to prevent the fuzzer from crashing on malformed compressed data. ```python import zlib from pythonfuzz.main import PythonFuzz @PythonFuzz def fuzz(buf): try: zlib.decompress(buf) except zlib.error: pass if __name__ == '__main__': fuzz() ``` -------------------------------- ### Fuzzing Archive Formats with PythonFuzz Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Demonstrates fuzz testing of the zipfile module. It includes compatibility logic for different Python versions and handles common ZIP-related exceptions. ```python import io import zipfile from pythonfuzz.main import PythonFuzz try: allowed_exceptions = (zipfile.BadZipFile, zipfile.LargeZipFile) except AttributeError: allowed_exceptions = (zipfile.BadZipfile, zipfile.LargeZipFile) @PythonFuzz def fuzz(buf): f = io.BytesIO(buf) try: z = zipfile.ZipFile(f) z.testzip() except: pass if __name__ == '__main__': fuzz() ``` -------------------------------- ### Dictionary Support Source: https://context7.com/fuzzitdev/pythonfuzz/llms.txt Illustrates how PythonFuzz supports libFuzzer/AFL-style dictionaries for smarter mutations, enabling domain-specific token insertion. ```APIDOC ## Dictionary Support ### Description PythonFuzz supports libFuzzer/AFL-style dictionaries for smarter mutations, allowing domain-specific tokens to be inserted into test inputs. ### Method N/A (Class usage) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from pythonfuzz.dictionary import Dictionary # Create and load dictionary from file dictionary = Dictionary() dictionary.load("./html.dict") # Get random word from dictionary for mutation word = dictionary.get_word() # Dictionary file format (html.dict): # # Comments start with # # tag_html="" # tag_body="" # tag_script="