From 4ca3bea0a91176d5fbe24e92e2c2226fd98bc4ea Mon Sep 17 00:00:00 2001 From: van Hauser Date: Wed, 3 Feb 2021 22:55:52 +0100 Subject: [PATCH] add basic dictionary reading --- afl/src/engines/mod.rs | 2 +- afl/src/mutators/mutations.rs | 72 ++++++++++++++++++++++++++++++++ fuzzers/libfuzzer_libpng/test.sh | 2 +- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/afl/src/engines/mod.rs b/afl/src/engines/mod.rs index 1f5fb2c14c..ff1e5f04ba 100644 --- a/afl/src/engines/mod.rs +++ b/afl/src/engines/mod.rs @@ -87,7 +87,7 @@ where if attr.is_file() && attr.len() > 0 { println!("Loading file {:?} ...", &path); - let bytes = std::fs::read(&path)?; + let bytes = fs::read(&path)?; let input = BytesInput::new(bytes); let fitness = self.evaluate_input(&input, engine.executor_mut(), corpus, manager)?; diff --git a/afl/src/mutators/mutations.rs b/afl/src/mutators/mutations.rs index 4113c55400..04f9009184 100644 --- a/afl/src/mutators/mutations.rs +++ b/afl/src/mutators/mutations.rs @@ -6,6 +6,12 @@ use crate::{ AflError, }; +#[cfg(feature = "std")] +use std::{ + fs::File, + io::{BufRead, BufReader}, +}; + const ARITH_MAX: u64 = 35; const INTERESTING_8: [i8; 9] = [-128, -1, 0, 1, 16, 32, 64, 100, 127]; @@ -691,3 +697,69 @@ where Ok(MutationResult::Mutated) } + +/// Read a dictionary file and return the number of entries read +pub fn read_dict_file( + f: &str, + dict : &mut Vec>, +) -> Result { + + let mut entries = 0; + + println!("Loading dictionary {:?} ...", &f); + + let file = File::open(&f)?; // panic if not found + let reader = BufReader::new(file); + + for line in reader.lines() { + let line = line.unwrap(); + let line = line.trim_start().trim_end(); + + // we are only interested in '"..."', not prefixed 'foo = ' + let start = line.chars().nth(0); + if line.len() == 0 || start == Some('#') { continue; } + let pos_quote = match line.find("\"") { + Some(x) => x, + _ => return Err(AflError::IllegalArgument("Illegal line: ".to_owned() + line)), + }; + if line.chars().nth(line.len() - 1) != Some('"') { + return Err(AflError::IllegalArgument("Illegal line: ".to_owned() + line)); + } + + // extract item + let item = match line.get(pos_quote + 1 .. line.len() - 1) { + Some(x) => x, + _ => return Err(AflError::IllegalArgument("Illegal line: ".to_owned() + line)), + }; + + if item.len() == 0 { continue; } + + // decode + // FIXME: TODO + //let item = unescape(item); + + let entry: Vec = item.as_bytes().to_vec(); + dict.push(entry); + + println!("Debug: {:?}", item); + entries += 1; + } + + Ok(entries) + +} + +#[cfg(test)] +mod tests { + use crate::{ + mutators::{read_dict_file}, + }; + + #[test] + fn test_read_dict() { + let mut v : Vec> = Vec::new(); + let res = read_dict_file(&"test.dic".to_string(), &mut v).unwrap(); + #[cfg(feature = "std")] + println!("Dictionary entries: {:?}", res); + } +} diff --git a/fuzzers/libfuzzer_libpng/test.sh b/fuzzers/libfuzzer_libpng/test.sh index a3881da017..c115d14436 100644 --- a/fuzzers/libfuzzer_libpng/test.sh +++ b/fuzzers/libfuzzer_libpng/test.sh @@ -15,4 +15,4 @@ test "$!" -gt 0 && { sleep 20 echo "[+] Done" killall .libfuzzer_test.elf -rm -rf ./.libfuzzer_test.elf \ No newline at end of file +rm -rf ./.libfuzzer_test.elf