|
| 1 | +use core::time::Duration; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use clap::Parser; |
| 5 | +use libafl::{ |
| 6 | + corpus::{InMemoryCorpus, OnDiskCorpus}, |
| 7 | + events::SimpleEventManager, |
| 8 | + executors::{forkserver::ForkserverExecutor, HasObservers}, |
| 9 | + feedback_and_fast, feedback_or, |
| 10 | + feedbacks::{ |
| 11 | + CrashFeedback, MaxMapFeedback, NautilusChunksMetadata, NautilusFeedback, TimeFeedback, |
| 12 | + }, |
| 13 | + fuzzer::{Fuzzer, StdFuzzer}, |
| 14 | + generators::{NautilusContext, NautilusGenerator}, |
| 15 | + inputs::{NautilusInput, NautilusTargetBytesConverter}, |
| 16 | + monitors::SimpleMonitor, |
| 17 | + mutators::{ |
| 18 | + NautilusRandomMutator, NautilusRecursionMutator, NautilusSpliceMutator, |
| 19 | + StdScheduledMutator, Tokens, |
| 20 | + }, |
| 21 | + observers::{CanTrack, HitcountsMapObserver, StdMapObserver, TimeObserver}, |
| 22 | + schedulers::{IndexesLenTimeMinimizerScheduler, QueueScheduler}, |
| 23 | + stages::mutational::StdMutationalStage, |
| 24 | + state::StdState, |
| 25 | + HasMetadata, |
| 26 | +}; |
| 27 | +use libafl_bolts::{ |
| 28 | + current_nanos, |
| 29 | + rands::StdRand, |
| 30 | + shmem::{ShMem, ShMemProvider, UnixShMemProvider}, |
| 31 | + tuples::{tuple_list, Handled}, |
| 32 | + AsSliceMut, Truncate, |
| 33 | +}; |
| 34 | +use nix::sys::signal::Signal; |
| 35 | + |
| 36 | +/// The commandline args this fuzzer accepts |
| 37 | +#[derive(Debug, Parser)] |
| 38 | +#[command( |
| 39 | + name = "forkserver_simple", |
| 40 | + about = "This is a simple example fuzzer to fuzz a executable instrumented by afl-cc, using Nautilus grammar.", |
| 41 | + author = "tokatoka <tokazerkje@outlook.com>, dmnk <domenukk@gmail.com>" |
| 42 | +)] |
| 43 | +struct Opt { |
| 44 | + #[arg( |
| 45 | + help = "The instrumented binary we want to fuzz", |
| 46 | + name = "EXEC", |
| 47 | + required = true |
| 48 | + )] |
| 49 | + executable: String, |
| 50 | + |
| 51 | + #[arg( |
| 52 | + help = "Timeout for each individual execution, in milliseconds", |
| 53 | + short = 't', |
| 54 | + long = "timeout", |
| 55 | + default_value = "1200" |
| 56 | + )] |
| 57 | + timeout: u64, |
| 58 | + |
| 59 | + #[arg( |
| 60 | + help = "If not set, the child's stdout and stderror will be redirected to /dev/null", |
| 61 | + short = 'd', |
| 62 | + long = "debug-child", |
| 63 | + default_value = "false" |
| 64 | + )] |
| 65 | + debug_child: bool, |
| 66 | + |
| 67 | + #[arg( |
| 68 | + help = "Arguments passed to the target", |
| 69 | + name = "arguments", |
| 70 | + num_args(1..), |
| 71 | + allow_hyphen_values = true, |
| 72 | + )] |
| 73 | + arguments: Vec<String>, |
| 74 | + |
| 75 | + #[arg( |
| 76 | + help = "Signal used to stop child", |
| 77 | + short = 's', |
| 78 | + long = "signal", |
| 79 | + value_parser = str::parse::<Signal>, |
| 80 | + default_value = "SIGKILL" |
| 81 | + )] |
| 82 | + signal: Signal, |
| 83 | + |
| 84 | + #[arg(help = "The nautilus grammar file", short)] |
| 85 | + grammar: PathBuf, |
| 86 | +} |
| 87 | + |
| 88 | +#[allow(clippy::similar_names)] |
| 89 | +pub fn main() { |
| 90 | + env_logger::init(); |
| 91 | + const MAP_SIZE: usize = 65536; |
| 92 | + |
| 93 | + let opt = Opt::parse(); |
| 94 | + |
| 95 | + let mut shmem_provider = UnixShMemProvider::new().unwrap(); |
| 96 | + |
| 97 | + // The coverage map shared between observer and executor |
| 98 | + let mut shmem = shmem_provider.new_shmem(MAP_SIZE).unwrap(); |
| 99 | + // let the forkserver know the shmid |
| 100 | + shmem.write_to_env("__AFL_SHM_ID").unwrap(); |
| 101 | + let shmem_buf = shmem.as_slice_mut(); |
| 102 | + |
| 103 | + // Create an observation channel using the signals map |
| 104 | + let edges_observer = unsafe { |
| 105 | + HitcountsMapObserver::new(StdMapObserver::new("shared_mem", shmem_buf)).track_indices() |
| 106 | + }; |
| 107 | + |
| 108 | + // Create an observation channel to keep track of the execution time |
| 109 | + let time_observer = TimeObserver::new("time"); |
| 110 | + |
| 111 | + let context = NautilusContext::from_file(15, opt.grammar); |
| 112 | + |
| 113 | + // Feedback to rate the interestingness of an input |
| 114 | + // This one is composed by two Feedbacks in OR |
| 115 | + let mut feedback = feedback_or!( |
| 116 | + // New maximization map feedback linked to the edges observer and the feedback state |
| 117 | + MaxMapFeedback::new(&edges_observer), |
| 118 | + // Time feedback, this one does not need a feedback state |
| 119 | + TimeFeedback::new(&time_observer), |
| 120 | + // Nautilus context |
| 121 | + NautilusFeedback::new(&context), |
| 122 | + ); |
| 123 | + |
| 124 | + // A feedback to choose if an input is a solution or not |
| 125 | + // We want to do the same crash deduplication that AFL does |
| 126 | + let mut objective = feedback_and_fast!( |
| 127 | + // Must be a crash |
| 128 | + CrashFeedback::new(), |
| 129 | + // Take it only if trigger new coverage over crashes |
| 130 | + // Uses `with_name` to create a different history from the `MaxMapFeedback` in `feedback` above |
| 131 | + MaxMapFeedback::with_name("mapfeedback_metadata_objective", &edges_observer) |
| 132 | + ); |
| 133 | + |
| 134 | + // create a State from scratch |
| 135 | + let mut state = StdState::new( |
| 136 | + // RNG |
| 137 | + StdRand::with_seed(current_nanos()), |
| 138 | + // Corpus that will be evolved, we keep it in memory for performance |
| 139 | + InMemoryCorpus::<NautilusInput>::new(), |
| 140 | + // Corpus in which we store solutions (crashes in this example), |
| 141 | + // on disk so the user can get them after stopping the fuzzer |
| 142 | + OnDiskCorpus::new(PathBuf::from("./crashes")).unwrap(), |
| 143 | + // States of the feedbacks. |
| 144 | + // The feedbacks can report the data that should persist in the State. |
| 145 | + &mut feedback, |
| 146 | + // Same for objective feedbacks |
| 147 | + &mut objective, |
| 148 | + ) |
| 149 | + .unwrap(); |
| 150 | + |
| 151 | + let _ = state.metadata_or_insert_with::<NautilusChunksMetadata>(|| { |
| 152 | + NautilusChunksMetadata::new("/tmp/".into()) |
| 153 | + }); |
| 154 | + |
| 155 | + // The Monitor trait define how the fuzzer stats are reported to the user |
| 156 | + let monitor = SimpleMonitor::new(|s| println!("{s}")); |
| 157 | + |
| 158 | + // The event manager handle the various events generated during the fuzzing loop |
| 159 | + // such as the notification of the addition of a new item to the corpus |
| 160 | + let mut mgr = SimpleEventManager::new(monitor); |
| 161 | + |
| 162 | + // A minimization+queue policy to get testcasess from the corpus |
| 163 | + let scheduler = IndexesLenTimeMinimizerScheduler::new(&edges_observer, QueueScheduler::new()); |
| 164 | + |
| 165 | + // A fuzzer with feedbacks and a corpus scheduler |
| 166 | + let mut fuzzer = StdFuzzer::new(scheduler, feedback, objective); |
| 167 | + |
| 168 | + // If we should debug the child |
| 169 | + let debug_child = opt.debug_child; |
| 170 | + |
| 171 | + // Create the executor for the forkserver |
| 172 | + let args = opt.arguments; |
| 173 | + |
| 174 | + let observer_ref = edges_observer.handle(); |
| 175 | + |
| 176 | + let mut tokens = Tokens::new(); |
| 177 | + let mut executor = ForkserverExecutor::builder() |
| 178 | + .program(opt.executable) |
| 179 | + .debug_child(debug_child) |
| 180 | + .shmem_provider(&mut shmem_provider) |
| 181 | + .autotokens(&mut tokens) |
| 182 | + .parse_afl_cmdline(args) |
| 183 | + .coverage_map_size(MAP_SIZE) |
| 184 | + .timeout(Duration::from_millis(opt.timeout)) |
| 185 | + .kill_signal(opt.signal) |
| 186 | + .target_bytes_converter(NautilusTargetBytesConverter::new(&context)) |
| 187 | + .build(tuple_list!(time_observer, edges_observer)) |
| 188 | + .unwrap(); |
| 189 | + |
| 190 | + if let Some(dynamic_map_size) = executor.coverage_map_size() { |
| 191 | + executor.observers_mut()[&observer_ref] |
| 192 | + .as_mut() |
| 193 | + .truncate(dynamic_map_size); |
| 194 | + } |
| 195 | + |
| 196 | + let mut generator = NautilusGenerator::new(&context); |
| 197 | + |
| 198 | + if state.must_load_initial_inputs() { |
| 199 | + state |
| 200 | + .generate_initial_inputs(&mut fuzzer, &mut executor, &mut generator, &mut mgr, 8) |
| 201 | + .expect("Failed to generate inputs"); |
| 202 | + } |
| 203 | + |
| 204 | + state.add_metadata(tokens); |
| 205 | + |
| 206 | + // Setup a mutational stage with a basic bytes mutator |
| 207 | + let mutator = StdScheduledMutator::with_max_stack_pow( |
| 208 | + tuple_list!( |
| 209 | + NautilusRandomMutator::new(&context), |
| 210 | + NautilusRandomMutator::new(&context), |
| 211 | + NautilusRandomMutator::new(&context), |
| 212 | + NautilusRandomMutator::new(&context), |
| 213 | + NautilusRandomMutator::new(&context), |
| 214 | + NautilusRandomMutator::new(&context), |
| 215 | + NautilusRecursionMutator::new(&context), |
| 216 | + NautilusSpliceMutator::new(&context), |
| 217 | + NautilusSpliceMutator::new(&context), |
| 218 | + NautilusSpliceMutator::new(&context), |
| 219 | + ), |
| 220 | + 2, |
| 221 | + ); |
| 222 | + let mut stages = tuple_list!(StdMutationalStage::new(mutator)); |
| 223 | + |
| 224 | + fuzzer |
| 225 | + .fuzz_loop(&mut stages, &mut executor, &mut state, &mut mgr) |
| 226 | + .expect("Error in the fuzzing loop"); |
| 227 | +} |
0 commit comments