-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.rs
More file actions
426 lines (336 loc) Β· 12.7 KB
/
main.rs
File metadata and controls
426 lines (336 loc) Β· 12.7 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#[cfg(not(feature = "cli"))]
fn main() {}
mod add_seeds;
mod build;
mod clean;
mod coverage;
mod fuzz;
mod minimize;
mod plot;
mod run;
mod triage;
#[cfg(feature = "cli")]
use crate::fuzz::FuzzingConfig;
#[cfg(feature = "cli")]
use anyhow::{anyhow, Context, Result};
#[cfg(feature = "cli")]
use clap::{Args, Parser, Subcommand, ValueEnum};
#[cfg(feature = "cli")]
use std::{fs, path::PathBuf};
pub const DEFAULT_UNMODIFIED_TARGET: &str = "automatically guessed";
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum FuzzingEngines {
All,
AFLPlusPlus,
Honggfuzz,
}
pub const DEFAULT_OUTPUT_DIR: &str = "./output";
pub const DEFAULT_CORPUS_DIR: &str = "{ziggy_output}/{target_name}/corpus/";
pub const DEFAULT_COVERAGE_DIR: &str = "{ziggy_output}/{target_name}/coverage/";
pub const DEFAULT_MINIMIZATION_DIR: &str = "{ziggy_output}/{target_name}/corpus_minimized/";
pub const DEFAULT_PLOT_DIR: &str = "{ziggy_output}/{target_name}/plot/";
pub const DEFAULT_CRASHES_DIR: &str = "{ziggy_output}/{target_name}/crashes/";
pub const DEFAULT_TRIAGE_DIR: &str = "{ziggy_output}/{target_name}/triage/";
#[derive(Parser)]
#[clap(name = "cargo")]
#[clap(bin_name = "cargo")]
pub enum Cargo {
#[clap(subcommand)]
Ziggy(Ziggy),
}
#[derive(Subcommand)]
#[clap(
author,
version,
about = "A multi-fuzzer management utility for all of your Rust fuzzing needs π§βπ€"
)]
pub enum Ziggy {
/// Build the fuzzer and the runner binaries
Build(Build),
/// Fuzz targets using different fuzzers in parallel
Fuzz(Fuzz),
/// Run a specific input or a directory of inputs to analyze backtrace
Run(Run),
/// Minimize the input corpus using the given fuzzing target
Minimize(Minimize),
/// Generate code coverage information using the existing corpus
Cover(Cover),
/// Plot AFL++ data using afl-plot
Plot(Plot),
/// Add seeds to the running AFL++ fuzzers
AddSeeds(AddSeeds),
/// Triage crashes found with casr - currently only works for AFL++
Triage(Triage),
/// Remove generated artifacts from the target directory
Clean(Clean),
}
#[derive(Args)]
pub struct Build {
/// No AFL++ (Fuzz only with honggfuzz)
#[clap(long = "no-afl", action)]
no_afl: bool,
/// No honggfuzz (Fuzz only with AFL++)
#[clap(long = "no-honggfuzz", action)]
no_honggfuzz: bool,
/// Compile in release mode (--release)
#[clap(long = "release", action)]
release: bool,
/// Build with ASAN (nightly only)
#[clap(long = "asan", action)]
asan: bool,
}
#[derive(Args)]
pub struct Fuzz {
/// Target to fuzz
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Shared corpus directory
#[clap(short, long, value_parser, value_name = "DIR", default_value = DEFAULT_CORPUS_DIR)]
corpus: PathBuf,
/// Initial corpus directory (will only be read)
#[clap(short, long, value_parser, value_name = "DIR")]
initial_corpus: Option<PathBuf>,
/// Compile in release mode (--release)
#[clap(long = "release", action)]
release: bool,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
/// Number of concurrent fuzzing jobs
#[clap(short, long, value_name = "NUM", default_value_t = 1)]
jobs: u32,
/// Timeout for a single run
#[clap(short, long, value_name = "SECS")]
timeout: Option<u32>,
/// Memory limit for the fuzz target. (If fuzzing with honggfuzz, a numeric value in MiB must be specified)
#[clap(short, long, value_name = "STRING")]
memory_limit: Option<String>,
/// Perform initial minimization
#[clap(short = 'M', long, action, default_value_t = false)]
minimize: bool,
/// Dictionary file (format:<http://llvm.org/docs/LibFuzzer.html#dictionaries>)
#[clap(short = 'x', long = "dict", value_name = "FILE")]
dictionary: Option<PathBuf>,
/// Maximum length of input
#[clap(short = 'G', long = "maxlength", default_value_t = 1048576)]
max_length: u64,
/// Minimum length of input (AFL++ only)
#[clap(short = 'g', long = "minlength", default_value_t = 1)]
min_length: u64,
/// No AFL++ (Fuzz only with honggfuzz)
#[clap(long = "no-afl", action)]
no_afl: bool,
/// No honggfuzz (Fuzz only with AFL++)
#[clap(long = "no-honggfuzz", action)]
no_honggfuzz: bool,
// This value helps us create a global timer for our display
#[clap(skip = std::time::Instant::now())]
start_time: std::time::Instant,
/// Pass flags to AFL++ directly
#[clap(short, long)]
afl_flags: Vec<String>,
/// AFL++ configuration
#[clap(short = 'C', long, default_value = "generic")]
config: FuzzingConfig,
/// With a coverage worker
#[clap(long)]
coverage_worker: bool,
/// Coverage generation interval in minutes
#[clap(long, default_value = "15")]
coverage_interval: u64,
/// Fuzz an already AFL++ instrumented binary; the ziggy way
#[clap(short, long)]
binary: Option<PathBuf>,
/// Build with ASAN (nightly only)
#[clap(long = "asan", action)]
asan: bool,
/// Foreign fuzzer directories to sync with (AFL++ -F option)
#[clap(long = "foreign-sync", short = 'F', action)]
foreign_sync_dirs: Vec<PathBuf>,
}
#[derive(Args)]
pub struct Run {
/// Target to use
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Input directories and/or files to run
#[clap(short, long, value_name = "DIR", default_value = DEFAULT_CORPUS_DIR)]
inputs: Vec<PathBuf>,
/// Recursively run nested directories for all input directories
#[clap(short, long)]
recursive: bool,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
/// Build with ASAN (nightly only)
#[clap(long = "asan", action)]
asan: bool,
/// Activate these features on the target
#[clap(short = 'F', long, num_args = 0..)]
features: Vec<String>,
/// Stop the run after the first crash is encountered
#[clap(short = 'x', long)]
stop_on_crash: bool,
}
#[derive(Args, Clone)]
pub struct Minimize {
/// Target to use
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Corpus directory to minimize
#[clap(short, long, default_value = DEFAULT_CORPUS_DIR)]
input_corpus: PathBuf,
/// Minimized corpus output directory
#[clap(short, long, default_value = DEFAULT_MINIMIZATION_DIR)]
output_corpus: PathBuf,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
/// Number of concurrent minimizing jobs (AFL++ only)
#[clap(short, long, value_name = "NUM", default_value_t = 1)]
jobs: u32,
/// After how many ms should an input time out.
#[clap(short, long, value_name = "TIMEOUT", default_value_t = 5000)]
timeout: u32,
#[clap(short, long, value_enum, default_value_t = FuzzingEngines::All)]
engine: FuzzingEngines,
}
#[derive(Args)]
pub struct Cover {
/// Target to generate coverage for
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Output directory for code coverage report
#[clap(short, long, value_parser, value_name = "DIR", default_value = DEFAULT_COVERAGE_DIR)]
output: PathBuf,
/// Input corpus directory to run target on
#[clap(short, long, value_parser, value_name = "DIR", default_value = DEFAULT_CORPUS_DIR)]
input: PathBuf,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
/// Source directory of covered code
#[clap(short, long, value_parser, value_name = "DIR")]
source: Option<PathBuf>,
/// Keep coverage data files (WARNING: Do not use if source code has changed)
#[clap(short, long, default_value_t = false)]
keep: bool,
/// Comma separated list of output types. See grcov --help to see supported output types. Default: html
#[clap(short = 't', long)]
output_types: Option<String>,
}
#[derive(Args)]
pub struct Plot {
/// Target to generate plot for
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Name of AFL++ fuzzer to use as data source
#[clap(short, long, value_name = "NAME", default_value = "mainaflfuzzer")]
input: String,
/// Output directory for plot
#[clap(short, long, value_parser, value_name = "DIR", default_value = DEFAULT_PLOT_DIR)]
output: PathBuf,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
}
#[derive(Args)]
pub struct Triage {
/// Target to use
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Triage output directory to be written to (will be overwritten)
#[clap(short, long, value_name = "DIR", default_value = DEFAULT_TRIAGE_DIR)]
output: PathBuf,
/// Number of concurrent fuzzing jobs
#[clap(short, long, value_name = "NUM", default_value_t = 1)]
jobs: u32,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
/// Terminate runner after x seconds
#[clap(short, long, value_name = "SECS")]
timeout: Option<u32>,
/* future feature, wait for casr
/// Crash directory to be sourced from
#[clap(short, long, value_parser, value_name = "DIR", default_value = DEFAULT_CRASHES_DIR)]
input: PathBuf,
*/
}
#[derive(Args)]
pub struct AddSeeds {
/// Target to use
#[clap(value_name = "TARGET", default_value = DEFAULT_UNMODIFIED_TARGET)]
target: String,
/// Seeds directory to be added
#[clap(short, long, value_parser, value_name = "DIR")]
input: PathBuf,
/// Fuzzers output directory
#[clap(
short, long, env = "ZIGGY_OUTPUT", value_parser, value_name = "DIR", default_value = DEFAULT_OUTPUT_DIR
)]
ziggy_output: PathBuf,
}
#[derive(Args)]
pub struct Clean {
/// Arguments passed through to cargo clean, see cargo clean --help
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<std::ffi::OsString>,
}
#[cfg(feature = "cli")]
fn main() -> Result<(), anyhow::Error> {
let Cargo::Ziggy(command) = Cargo::parse();
match command {
Ziggy::Build(args) => args.build().context("Failed to build the fuzzers"),
Ziggy::Fuzz(mut args) => args.fuzz().context("Failure running fuzzers"),
Ziggy::Run(mut args) => args.run().context("Failure running inputs"),
Ziggy::Minimize(mut args) => args.minimize().context("Failure running minimization"),
Ziggy::Cover(mut args) => args
.generate_coverage()
.context("Failure generating coverage"),
Ziggy::Plot(mut args) => args.generate_plot().context("Failure generating plot"),
Ziggy::AddSeeds(args) => args.add_seeds().context("Failure adding seeds to AFL"),
Ziggy::Triage(args) => args.triage().context("Failure triaging with casr"),
Ziggy::Clean(args) => args.clean().context("Failure cleaning build artifacts"),
}
}
pub fn find_target(target: &String) -> Result<String, anyhow::Error> {
// If the target is already set, we're done here
if target != DEFAULT_UNMODIFIED_TARGET {
return Ok(target.into());
}
let new_target_result = guess_target();
new_target_result.context("Target is not obvious")
}
fn guess_target() -> Result<String> {
let metadata = cargo_metadata::MetadataCommand::new().exec()?;
let default_package = metadata.workspace_default_members;
if let Some(package_id) = default_package.first() {
if let Some(package) = metadata.packages.iter().find(|p| p.id == *package_id) {
return Ok(package.name.to_string());
}
}
Err(anyhow!("Please specify a target"))
}
fn target_dir() -> &'static cargo_metadata::camino::Utf8PathBuf {
use std::sync::LazyLock;
static TARGET_DIR: LazyLock<cargo_metadata::camino::Utf8PathBuf> = LazyLock::new(|| {
cargo_metadata::MetadataCommand::new().exec().map_or_else(
|_| cargo_metadata::camino::Utf8PathBuf::from("target"),
|metadata| metadata.target_directory,
)
});
&TARGET_DIR
}