Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created January 19, 2018 02:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcph4/51b0884c8cca76b879d364a33f8da934 to your computer and use it in GitHub Desktop.
Save jmcph4/51b0884c8cca76b879d364a33f8da934 to your computer and use it in GitHub Desktop.
Full, working code for "Writing a Simple Fuzzer in Python" at https://jmcph4.github.com/2018/01/19/writing-a-simple-fuzzer-in-python
import sys
from mph.program import Program
from fuzzbang.alphanumericfuzzer import AlphaNumericFuzzer
PATH_TO_NAME = "" # fill this in yourself
def run(string):
"""
Sends the provided string to the `name` program and runs it with that
input. Returns the return value `name` gives us
"""
prog = Program(PATH_TO_NAME, [])
prog.append_string_stdin(string)
prog.exec()
return prog.retval
def generate_input(n):
"""
Returns an alphanumeric string with a length no greater than n.
"""
fuzzer = AlphaNumericFuzzer(0, n)
return f.generate()
if _name__ == "__main__":
# usage
if len(sys.argv) != 3:
print("usage: python3 fuzztut.py num_cases max_length")
exit(1)
# command-line arguments
num_cases = int(sys.argv[1]) # number of test cases to run
max_length = int(sys.argv[2]) # maximum length of each string
results = [] # list for storing the result of each test
# main loop
for i in range(num_cases):
input = generate_input() # generate input string
return_value = run(input) # run name with our input
# save test results to our global results list
test_result = {}
test_result["num"] = i
test_result["input"] = input
test_result["output"] = return_value
results.append(test_result)
# print summary
for test in results:
print("Case #{:d}:".format(test["num"]))
print(" IN: " + test["input"])
print(" OUT: {:4d}".format(test["output"]))
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment