File size: 3,273 Bytes
443f404
 
 
 
 
 
 
 
 
 
 
172247f
 
 
443f404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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. 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()