Using Fuzzingbook Code in your own Programs

This notebook has instructions on how to use the fuzzingbook code in your own programs.

Can I import the code for my own Python projects?

Yes, you can! (If you like Python, that is.) We provide a fuzzingbook Python package that you can install using the pip package manager:

$ pip install fuzzingbook

Once this is installed, you can import individual classes, constants, or functions from each notebook using

>>> from fuzzingbook.<notebook> import <identifier>

where <identifier> is the name of the class, constant, or function to use, and <notebook> is the name of the respective notebook. (If you read this at fuzzingbook.org, then the notebook name is the identifier preceding ".html" in the URL).

Here is an example importing RandomFuzzer from the chapter on fuzzers, whose notebook name is Fuzzer:

>>> from fuzzingbook.Fuzzer import RandomFuzzer
>>> f = RandomFuzzer()
>>> f.fuzz()
'!7#%"*#0=)$;%6*;>638:*>80"=</>(/*:-(2<4 !:5*6856&?""11<7+%<%7,4.8,*+&,,$,."5%<%76< -5'

The "Synopsis" section at the beginning of a chapter gives a short survey on useful code features you can use.

Can I use the code from within a Jupyter notebook?

Yes, you can! You would first install the fuzzingbook package (as above); you can then access all code right from your notebook.

Another way to use the code is to import the notebooks directly. Download the notebooks from the menu. Then, add your own notebooks into the same folder. After importing fuzzingbook_utils, you can then simply import the code from other notebooks, just as our own notebooks do.

Here is again the above example, importing RandomFuzzer from the chapter on fuzzers – but now from a notebook:

In [1]:
import fuzzingbook_utils
In [2]:
from Fuzzer import RandomFuzzer
In [3]:
f = RandomFuzzer()
f.fuzz()
Out[3]:
'!7#%"*#0=)$;%6*;>638:*>80"=</>(/*:-(2<4 !:5*6856&?""11<7+%<%7,4.8,*+&,,$,."5%<%76< -5'

If you'd like to share your notebook, let us know; we can integrate it in the repository or even in the book.

Can I just run the Python code? I mean, without notebooks?

Yes, you can! (Although we'd always recommend executing the notebooks instead – they have all the text, and far richer inputs and outputs.) You can download the code as Python programs; simply select "Resources $\rightarrow$ Download Code" for one chapter or "Resources $\rightarrow$ All Code" for all chapters. These code files can be executed, yielding (hopefully) the same results as the notebooks.

The code files can also be edited if you wish, but (a) they are very obviously generated from notebooks, (b) therefore not much fun to work with, and (c) if you fix any errors, you'll have to back-propagate them to the notebook before you can make a pull request. Use code files only under severely constrained circumstances.

Which other packages do I need to use the Python modules?

We have attempted to limit the dependencies to a minimum (sometimes using ugly hacks). Generally speaking, if you encounter that a module X is not found, just do pip install X. Most notebooks (notably, the generic fuzzers) only need modules that are part of the standard Python library.

For a full list of dependencies, see the configuration files within the binder folder in the project repository; these list all Linux and Python packages required.