sh1k4ku's picture
Add Assignment 3 (Q1-Q4 + Q5 coverage-guided fuzzing); relocate all hidden-answer source outside the deployed tree
443f404 verified
Raw History Blame
3.33 kB
import random
MAX_EXECS = 200_000
INTERESTING_VALUES = [0, 1, 0x7F, 0x80, 0xFF]
# -----------------------------------------------------------------------------
# TODO 1 — a mutation operator.
#
# Take an input and return a NEW input that is changed a little. Choose a change
# that can actually steer the target through its checks, and be ready to
# justify your choice in your report. Your mutation does not need to change
# the length of `data` -- the starting input is already long enough for
# anything you'll need to reach.
#
# The placeholder below changes nothing (no-op), so the fuzzer makes no progress.
# Replace it with a real mutation.
# -----------------------------------------------------------------------------
def mutate(data: bytes) -> bytes:
data = bytearray(data)
# example of the shape a mutation takes (not a full solution):
# i = random.randrange(len(data))
# data[i] ^= 1 << random.randrange(8)
# >>> TODO 1: change one small part of `data` here <<<
return bytes(data)
# -----------------------------------------------------------------------------
# TODO 2 — the coverage decision.
#
# `cov` is this run's coverage (cov[k] > 0 means edge k was reached this run).
# `covered` is the set of edge indices reached on ANY previous run.
# Return True if this run reached an edge that is NOT already in `covered`
# (i.e. the input discovered new code and is worth keeping).
#
# The placeholder below always returns False, so nothing is ever kept.
# -----------------------------------------------------------------------------
def is_interesting(cov, covered) -> bool:
# >>> TODO 2: return True if `cov` reached an edge not in `covered` <<<
return False
def fuzz():
log("Starter template running. It will NOT find anything until you complete:")
log(" TODO 1 (mutate): actually change a byte so inputs can pass the checks.")
log(" TODO 2 (is_interesting): return True when a run reaches a NEW edge.")
corpus = [bytes(SEED)]
covered = set()
found = set()
BUGS = {1: "bug1", 2: "bug2"}
def add_edges(cov):
for i in range(len(cov)):
if cov[i] > 0:
covered.add(i)
add_edges(run(SEED)[0])
for execs in range(1, MAX_EXECS + 1):
base = random.choice(corpus)
candidate = mutate(base)
cov, crashed = run(candidate)
if crashed:
for bit, name in BUGS.items():
if (crashed & bit) and bit not in found:
found.add(bit)
save_crash(candidate, name)
log(f"CRASH [{name}] after {execs} executions:", candidate.hex(" "))
keep = is_interesting(cov, covered)
add_edges(cov)
if keep:
corpus.append(candidate)
if found == set(BUGS):
report(execs, len(corpus), covered)
log("Both bugs found after", execs, "executions. Done.")
return
if execs % 500 == 0:
report(execs, len(corpus), covered)
missing = [BUGS[b] for b in BUGS if b not in found]
log("stopped after", MAX_EXECS, "executions -",
f"found {sorted(BUGS[b] for b in found)}, still missing {missing},",
f"coverage stuck at {len(covered)}/{NUM_EDGES}. Complete the TODOs and re-run.")
fuzz()