-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
winafl-cmin.py
777 lines (666 loc) · 28.2 KB
/
winafl-cmin.py
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#
# american fuzzy lop - corpus minimization tool
# ----------------------------------------
#
# Original code written by Michal Zalewski <[email protected]>
#
# Windows fork written by Axel "0vercl0k" Souchet <[email protected]>
#
# Copyright 2017 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
from __future__ import print_function
import argparse
import collections
import logging
import multiprocessing
import os
import shutil
import subprocess
import sys
import time
import re
from textwrap import dedent, wrap
nul = open(os.devnull, 'wb')
AFLShowMapResult = collections.namedtuple(
'AFLShowMapResult', [
'returncode', 'path', 'filesize', 'tuples'
]
)
class AFLShowMapWorker(object):
'''This class abstracts away the interaction with afl-showmap.exe
and plays nice with the multiprocessing module'''
def __init__(self, args):
self.args = args
@staticmethod
def _to_showmap_options(args, trace_name = '-'):
'''Takes the argparse namespace, and convert it to the list of options used
to invoke afl-showmap.exe'''
r = [
'afl-showmap.exe', '-o', trace_name, '-m', args.memory_limit
]
if os.getenv('AFL_NO_SINKHOLE') is None:
r.append('-q')
if args.time_limit > 0:
r.extend(['-t', '%d' % args.time_limit])
else:
r.extend(['-t', 'none'])
if args.static_instr:
r.append('-Y')
else:
r.extend(['-D', args.dynamorio_dir])
r.append('--')
r.extend(['-target_module', args.target_module])
if args.target_method is None:
r.extend(['-target_offset', '0x%x' % args.target_offset])
else:
r.extend(['-target_method', args.target_method])
r.extend(['-nargs', '%d' % args.nargs])
r.extend(['-covtype', args.covtype])
if args.call_convention is not None:
r.extend(['-call_convention', args.call_convention])
for mod in args.coverage_modules:
r.extend(['-coverage_module', mod])
if args.edges_only:
r.append('-e')
r.append('--')
r.extend(args.target_cmdline)
return r
def __call__(self, input_file):
'''Runs afl-showmap.exe on a specific target and extracts
the tuples from the generated trace'''
current_process = multiprocessing.current_process()
fileread = None
if self.args.file_read is not None:
# It means that the target expects to have '@@' replaced with a
# constant path file. First step, is to copy the input to
# this location
fileread = self.args.file_read.replace(
'@@', current_process.name
)
if os.path.isfile(fileread):
os.remove(fileread)
shutil.copyfile(input_file, fileread)
trace_name = 'worker.%d.trace.bin' % current_process.pid
opts = AFLShowMapWorker._to_showmap_options(self.args, trace_name)
# If we have a '@@' marker in the command line, it has to be replaced
# by an actual file. It is either directly the input testcase, or the
# file specified by the -f option.
if opts.count('@@') > 0:
# TODO(0vercl0k): handle inputs via stdin as opposed to '@@' & -f
idx = opts.index('@@')
opts[idx] = input_file if fileread is None else fileread
# Make sure there isn't a trace that hasn't been properly cleaned
if os.path.isfile(trace_name):
os.remove(trace_name)
p = subprocess.Popen(opts, close_fds = True)
p.wait()
if fileread is not None:
# Clean it up
os.remove(fileread)
# Read the trace file and populate the tuple store
tuples = {}
if os.path.isfile(trace_name):
with open(trace_name, 'r') as f:
for line in f.readlines():
tuple_id, hitcount = map(int, line.split(':', 1))
tuples[tuple_id] = hitcount
# Clean it up
os.remove(trace_name)
return AFLShowMapResult(
p.returncode, input_file,
os.path.getsize(input_file), tuples
)
def target_offset(opt):
'''Validates that the target_offset is actually an integer, else
raises an ArgumentTypeError exception back to the argparse parser.'''
try:
return int(opt, 0)
except ValueError:
raise argparse.ArgumentTypeError('must be an integer')
def memory_limit(opt):
'''Validates that the -m parameter is properly formated, else
raises an ArgumentTypeError exception back to the argparse parser.'''
if re.match(r'^\d+[TGkM]?$', opt) or opt == 'none':
return opt
raise argparse.ArgumentTypeError('must be an integer followed by either: '
'T, G, M, k or nothing; or none')
def setup_argparse():
'''Sets up the argparse configuration.'''
parser = argparse.ArgumentParser(
formatter_class = argparse.RawDescriptionHelpFormatter,
epilog = '\n'.join(wrap(dedent(
'''
Examples of use:
* Typical use
winafl-cmin.py -D D:\\DRIO\\bin32 -t 100000 -i in -o minset -covtype edge -coverage_module m.dll -target_module test.exe -target_method fuzz -nargs 2 -- test.exe @@
* Dry-run, keep crashes only with 4 workers with a working directory:
winafl-cmin.py -C --dry-run -w 4 --working-dir D:\\dir -D D:\\DRIO\\bin32 -t 10000 -i in -i C:\\fuzz\\in -o out_mini -covtype edge -coverage_module m.dll -target_module test.exe -target_method fuzz -nargs 2 -- test.exe @@
* Read from specific file
winafl-cmin.py -D D:\\DRIO\\bin32 -t 100000 -i in -o minset -f foo.ext -covtype edge -coverage_module m.dll -target_module test.exe -target_method fuzz -nargs 2 -- test.exe @@
* Read from specific file with pattern
winafl-cmin.py -D D:\\DRIO\\bin32 -t 100000 -i in -o minset -f prefix-@@-foo.ext -covtype edge -coverage_module m.dll -target_module test.exe -target_method fuzz -nargs 2 -- test.exe @@
* Typical use with static instrumentation
winafl-cmin.py -Y -t 100000 -i in -o minset -- test.instr.exe @@
'''
), 100, replace_whitespace = False))
)
group = parser.add_argument_group('basic parameters')
group.add_argument(
'-i', '--input', action = 'append', required = True,
metavar = 'dir', help = 'input directory with the starting corpus.'
' Multiple input directories are supported'
)
group.add_argument(
'-o', '--output', required = True,
metavar = 'dir', help = 'output directory for minimized files'
)
group.add_argument(
'--crash-dir', required=False,
metavar='dir', help='output directory for crashing files'
)
group.add_argument(
'--hang-dir', required=False,
metavar='dir', help='output directory for hanging files'
)
group.add_argument(
'-n', '--dry-run', action = 'store_true', default = False,
help = 'do not really populate the output directory'
)
group.add_argument(
'--working-dir', default = os.getcwd(),
metavar = 'dir', help = 'directory containing afl-showmap.exe,'
'winafl.dll, the target binary, etc.'
)
group.add_argument(
'-v', '--verbose', action = 'store_const',
default = logging.INFO, const = logging.DEBUG
)
group = parser.add_argument_group('instrumentation settings')
instr_type = group.add_mutually_exclusive_group(required = True)
instr_type.add_argument(
'-Y', '--static-instr', action = 'store_true',
help = 'use the static instrumentation mode'
)
instr_type.add_argument(
'-D', '--dynamorio_dir',
metavar = 'dir', help = 'directory containing DynamoRIO binaries (drrun, drconfig)'
)
group.add_argument(
'-covtype', choices = ('edge', 'bb'), default = 'bb',
help = 'the type of coverage being recorded (defaults to bb)'
)
group.add_argument(
'-call_convention', choices = ('stdcall', 'fastcall', 'thiscall', 'ms64'),
default = 'stdcall', help = 'the calling convention of the target_method'
)
group.add_argument(
'-coverage_module', dest = 'coverage_modules', default = None,
action = 'append', metavar = 'module', help = 'module for which to record coverage.'
' Multiple module flags are supported'
)
group.add_argument(
'-target_module', default = None, metavar = 'module',
help = 'module which contains the target function to be fuzzed'
)
group.add_argument(
'-nargs', type = int, default = None, metavar = 'nargs',
help = 'number of arguments the fuzzed method takes. This is used to save/restore'
' the arguments between runs'
)
group = group.add_mutually_exclusive_group()
group.add_argument(
'-target_method', default = None, metavar = 'method',
help = 'name of the method to fuzz in persistent mode.'
' A symbol for the method needs to be exported for this to work'
)
group.add_argument(
'-target_offset', default = None, type = target_offset, metavar = 'rva offset',
help = 'offset of the method to fuzz from the start of the module'
)
group = parser.add_argument_group('execution control settings')
group.add_argument(
'-t', '--time-limit', type = int, default = 0,
metavar = 'msec', help = 'timeout for each run (none)'
)
group.add_argument(
'-m', '--memory-limit', default = 'none', type = memory_limit,
metavar = 'megs', help = 'memory limit for child process'
)
# Note(0vercl0k): If you use -f, which means you want the input file at
# a specific location (and a specific name), we have to force the pool
# process to contain only a single worker as there is a unique location
# specified by -f.. unless you provide a pattern with @@ like
# c:\dir\prefix@@suffix where @@ will be replaced with a unique identifier.
group.add_argument(
'-f', '--file-read', default = None,
metavar = 'file', help = 'location read by the fuzzed program. '
'Usage of @@ is encouraged to keep parallelization possible'
)
group = parser.add_argument_group('minimization settings')
group.add_argument(
'-C', '--crash-only', action = 'store_true', default = False,
help = 'keep crashing inputs in output directory, reject everything else'
)
group.add_argument(
'-e', '--edges-only', action = 'store_true', default = False,
help = 'solve for edge coverage only, ignore hit counts'
)
group.add_argument(
'-w', '--workers', type = int, default = multiprocessing.cpu_count(),
metavar = 'n', help = 'The number of worker processes (default: cpu count)'
)
group.add_argument(
'--skip-dry-run', action = 'store_true', default = False,
help = 'Skip the dry-run step even if it failed'
)
parser.add_argument(
'target_cmdline', nargs = argparse.REMAINDER,
help = 'target command line'
)
return parser.parse_args()
def validate_args(args):
'''Validate command-line arguments'''
# Validate that the first argument is an executable
if not os.path.isfile(args.target_cmdline[0]):
logging.error(
'[!] The target command line\'s first argument needs to'
' be an existing executable file.'
)
return False
# If we are not seeing the '@@' marker somewhere and that we are not
# specifying an input file with -f, then it means something is wrong
if args.file_read is None and '@@' not in args.target_cmdline:
logging.error(
'[!] The target command line needs to include the "@@" marker'
' or -f to specify the input file.'
)
return False
# Another sanity check on the root of output directory
if os.path.isdir(os.path.split(args.output)[0]) is False:
logging.error(
'[!] The output directory %r is not a directory', args.output
)
return False
# Another sanity check on the root of crash directory
if args.crash_dir and os.path.isdir(os.path.split(args.crash_dir)[0]) is False:
logging.error(
'[!] The output crash directory %r is not a directory', args.crash_dir
)
return False
# Another sanity check on the root of hang directory
if args.hang_dir and os.path.isdir(os.path.split(args.hang_dir)[0]) is False:
logging.error(
'[!] The output hangs directory %r is not a directory', args.hang_dir
)
return False
if os.path.isdir(args.working_dir) is False:
logging.error(
'[!] The working directory %r is not a directory', args.working_dir
)
return False
# Regardless of DRIO being used or not, we need afl-showmap.exe
afl_showmap_path = os.path.join(args.working_dir, 'afl-showmap.exe')
if not os.path.isfile(afl_showmap_path):
logging.error('[!] afl-showmap.exe need to be in %s.', args.working_dir)
return False
# Make sure the output directory doesn't exist yet
if os.path.isabs(args.output):
output_dir_path = args.output
else:
output_dir_path = os.path.join(args.working_dir, args.output)
if args.dry_run is False and os.path.isdir(output_dir_path):
logging.error(
'[!] %s already exists, please remove it to avoid data loss.',
args.output
)
return False
if not args.static_instr:
# Make sure we have all the arguments we need
if len(args.coverage_modules) == 0:
logging.error(
'[!] -coverage_module is a required option to use'
'the dynamic instrumentation'
)
return False
if None in [args.target_module, args.nargs]:
logging.error(
'[!] , -target_module and -nargs are required'
' options to use the dynamic instrumentation mode.'
)
return False
if args.target_method is None and args.target_offset is None:
logging.error(
'[!] -target_method or -target_offset is required to use the'
' dynamic instrumentation mode'
)
return False
# If we are using DRIO, one of the thing we need is the DRIO client
winafl_path = os.path.join(args.working_dir, 'winafl.dll')
if not os.path.isfile(winafl_path):
logging.error(
'[!] winafl.dll needs to be in %s.', args.working_dir
)
return False
if args.file_read is not None and '@@' not in args.file_read:
# When a particular input file is specified, first
# check if the file already exists, because we don't want to overwrite
# a potentially interesting test case.
if os.path.isabs(args.file_read):
file_read_path = args.file_read
else:
file_read_path = os.path.join(args.working_dir, args.file_read)
if os.path.isfile(file_read_path):
logging.error(
'[!] %s already exists, please remove it to avoid data loss.',
args.file_read
)
return False
for i in args.input:
if os.path.isabs(i):
dir_path = i
else:
dir_path = os.path.join(args.working_dir, i)
if not os.path.isdir(dir_path):
logging.error(
'[!] Specified input directory "%s" does not exist',
i
)
return False
return True
def target_dry_run(args, test_input):
logging.info('[*] Testing the target binary...')
f = AFLShowMapWorker(args)
results = list(map(f, (test_input, test_input)))
if results[0] != results[1]:
logging.error('[!] Dry-run failed, 2 executions resulted differently:')
logging.error(
' Tuples matching? %r',
results[0].tuples == results[1].tuples
)
logging.error(
' Return codes matching? %r',
results[0].returncode == results[1].returncode
)
if not args.skip_dry_run:
return False
logging.info('[+] OK, %d tuples recorded.', len(results[0].tuples))
return True
def run_all_inputs(args, inputs):
nprocesses = args.workers
if args.file_read is not None and '@@' not in args.file_read:
# If you are providing -f, but doesn't specify '@@' in the command line
# of the target, it might be a sign that you are doing something wrong.
if '@@' not in args.target_cmdline:
logging.warn(
"[ ] You specified the -f option without using '@@' in your "
"command line, this does not sound right."
)
logging.info('[+] Worker pool size: 1 (because no "@@" in the -f option)..')
# Keep in mind that if you want the input files to be named and placed
# by your liking by specifying -f path/foo.ext we have to set the pool
# size to 1 in order to make it work.
nprocesses = 1
# Record stats about the original input set.
# The aggregated size of every input files.
totalsize = 0
# The size of the input set.
inputs_len = len(inputs)
logging.info(
'[+] Found %d test cases across: %s.',
inputs_len, ', '.join(args.input)
)
logging.info('[*] Instantiating %d worker processes.', nprocesses)
p = multiprocessing.Pool(processes = nprocesses)
# This tracks every unique tuples and their popularities
uniq_tuples = collections.Counter()
# This will associate a tuple with the currently fittest file exercising
# this particular tuple.
candidates = {}
# This tracks every files that triggered a hang.
hang_files = []
# This tracks every files that triggered a crash.
crash_files = []
# This tracks every files that generated an empty set of tuples.
# You might get those if the dynamically instrumented target module doesn't
# get hit because the testcase is so malformed that it won't pass a
# function called before for example; hence will return an empty tuple set.
empty_tuple_files = []
# The default return code is no crash.
wanted_returncode = 0
if args.crash_only:
logging.info('[+] Crash only mode enabled.')
wanted_returncode = 2
# Counter tracking how many files we have been through already.
i = 1
for result in p.imap_unordered(
AFLShowMapWorker(args),
inputs
):
print('\rProcessing file %d/%d...' % (i, inputs_len), end=' ')
i += 1
# If the set of tuples is empty, something weird happened
if len(result.tuples) == 0:
logging.debug(
'[x] The input file %s generated an empty set of tuples,'
' skipping it (ret = %d).',
result.path, result.returncode
)
empty_tuple_files.append(result.path)
continue
if result.returncode != wanted_returncode:
if result.returncode == 1:
hang_files.append(result.path)
# If the mode crash only is enabled, we track the non-crashing
# test cases in the same tuple.
if (result.returncode == 2 and args.crash_only is False) or \
(result.returncode == 0 and args.crash_only):
crash_files.append(result.path)
if args.crash_only is False:
logging.debug(
'[x] The input file %s triggered a %s, skipping it.',
result.path,
'hang' if result.returncode == 1 else 'crash'
)
else:
logging.debug(
'[x] The input file %s triggered a %s, skipping it.',
result.path,
'hang' if result.returncode == 1 else 'non crash'
)
continue
totalsize += result.filesize
# Generate the list of unique tuples while processing the results,
# also keep track of their popularities.
uniq_tuples.update(result.tuples.keys())
# Keep an updated dictionary mapping a tuple to the fittest file
# of all the paths.
for tuple_id, tuple_hitcount in result.tuples.items():
fileinfo = {
'size' : result.filesize,
'path' : result.path,
'tuples' : result.tuples,
'hitcount' : tuple_hitcount
}
if tuple_id in candidates:
candidate = candidates[tuple_id]
# If the candidate has a higher hitcount, we keep it.
if tuple_hitcount > candidate['hitcount']:
candidates[tuple_id] = fileinfo
elif tuple_hitcount == candidate['hitcount']:
# If the candidate has the same hitcount, but a
# smaller size, we keep it.
if result.filesize < candidate['size']:
candidates[tuple_id] = fileinfo
elif result.filesize == candidate['size']:
# If the candidate has the same hitcount and
# size, but exercise a bigger number of tuples,
# we keep it.
if len(result.tuples) > len(candidate['tuples']):
candidate[tuple_id] = fileinfo
else:
candidates[tuple_id] = fileinfo
p.close()
len_crash_files, len_hang_files, len_empty_tuple_files = map(
len, (crash_files, hang_files, empty_tuple_files)
)
effective_len = len(inputs) - (
len_crash_files + len_hang_files + len_empty_tuple_files
)
print()
logging.info(
'[+] Found %d unique tuples across %d files',
len(uniq_tuples), effective_len
)
if len_hang_files > 0:
logging.info(' - %d files triggered a hang', len_hang_files)
for hang_file in hang_files:
logging.debug(' - %s generated a hang', hang_file)
if len_crash_files > 0:
logging.info(
' - %d files %s a crash',
len_crash_files,
'did not trigger' if args.crash_only else 'triggered'
)
for crash_file in crash_files:
logging.debug(' - %s generated a crash', crash_file)
if len_empty_tuple_files > 0:
logging.info(
' - %d files resulted in an empty tuple set',
len_empty_tuple_files
)
for empty_tuple_file in empty_tuple_files:
logging.debug(' - %s generated an empty tuple', empty_tuple_file)
return uniq_tuples, candidates, effective_len, totalsize, crash_files, hang_files
def find_best_candidates(uniq_tuples, candidates):
# Using the same strategy than in afl-cmin, quoting lcamtuf:
# '''
# The "best" part is understood simply as the smallest input that
# includes a particular tuple in its trace. Empirical evidence
# suggests that this produces smaller datasets than more involved
# algorithms that could be still pulled off in a shell script.
# '''
minset = []
minsetsize = 0
remaining_tuples = list(uniq_tuples)
len_uniq_tuples = len(uniq_tuples)
for tuple_ in uniq_tuples:
if tuple_ not in remaining_tuples:
# It means we already deleted this tuple, as it was exercised
# as part of another test case.
continue
# Pick the current best file candidate for this tuple.
candidate = candidates[tuple_]
# Remove the other tuples also exercised by the candidate
# from the remaining_tuples list.
for tuple_exercised in candidate['tuples']:
# Remove the tuples exercised if we have not
# removed them already from the
# remaining_tuples list.
if tuple_exercised in remaining_tuples:
remaining_tuples.remove(tuple_exercised)
# Keep track of the final minset and its size.
minset.append(candidate['path'])
minsetsize += candidate['size']
# We are now done with this tuple, we can get rid of it.
del candidates[tuple_]
print('\rProcessing tuple %d/%d...' % (
len_uniq_tuples - len(remaining_tuples),
len_uniq_tuples
), end=' ')
# If we don't have any more tuples left, we are done.
if len(remaining_tuples) == 0:
break
return minset, minsetsize
def do_unique_copy(filepaths, dest_dir):
os.mkdir(dest_dir)
num_digits = len(str(len(filepaths)-1))
for i, fpath in enumerate(filepaths):
filename = os.path.basename(fpath)
dest_path = os.path.join(dest_dir, 'id_' + str(i).zfill(num_digits) + "_" + filename)
shutil.copy(fpath, dest_path)
def main(argc, argv):
print('corpus minimization tool for WinAFL by <[email protected]>')
print('Based on WinAFL by <[email protected]>')
print('Based on AFL by <[email protected]>')
logging.basicConfig(
filename = 'winafl-cmin.log',
level = logging.DEBUG,
format = '%(asctime)s [%(levelname)-5.5s] [%(funcName)s] %(message)s'
)
args = setup_argparse()
cli_handler = logging.StreamHandler(sys.stdout)
cli_handler.setLevel(args.verbose)
logging.getLogger().addHandler(cli_handler)
# Interestingly enough, if the user uses '.. -- target.exe -option foo ..'
# argparse will add '--' in the target_cmdline option, so we need to
# strip it off manually here.
if args.target_cmdline[0] == '--':
del args.target_cmdline[0]
logging.debug(
'[+] winafl-cmin launched with the following arguments: %s',
' '.join(sys.argv)
)
if not validate_args(args):
return 1
os.chdir(args.working_dir)
logging.info('[+] CWD changed to %s.', args.working_dir)
if args.static_instr is True:
logging.info('[+] Dynamorio-less mode is enabled.')
# Go get all the input files we want to have a look at
logging.debug(
'Inspecting the following directories: %s',
', '.join(args.input)
)
inputs = []
for path in args.input:
for root, dirs, files in os.walk(path):
for file_ in files:
inputs.append(os.path.join(root, file_))
if not inputs:
logging.error(' Input directories do not contain any files!')
return 1
# Do a dry run with the first file in the set
if not target_dry_run(args, inputs[0]):
return 1
t0 = time.time()
uniq_tuples, candidates, effective_len, totalsize, crash_files, hang_files = run_all_inputs(args, inputs)
logging.info('[*] Finding best candidates for each tuple...')
minset, minsetsize = find_best_candidates(uniq_tuples, candidates)
print()
logging.info('[+] Original set was composed of %d files', len(inputs))
logging.info(
'[+] Effective set was composed of %d files (total size %d MB).',
effective_len, (totalsize / 1024) / 1024
)
logging.info(
'[+] Narrowed down to %d files (total size %d MB).',
len(minset), (minsetsize / 1024) / 1024
)
if args.dry_run is False:
logging.info(
'[*] Saving the minset in %s...', os.path.abspath(args.output)
)
do_unique_copy(minset, args.output)
if args.crash_dir and crash_files:
logging.info(
'[+] Saving %d crashing files to %s',
len(crash_files), args.crash_dir
)
do_unique_copy(crash_files, args.crash_dir)
if args.hang_dir and hang_files:
logging.info(
'[+] Saving %d hanging files to %s',
len(hang_files), args.hang_dir
)
do_unique_copy(hang_files, args.hang_dir)
logging.info('[+] Time elapsed: %d seconds', time.time() - t0)
return 0
if __name__ == '__main__':
sys.exit(main(len(sys.argv), sys.argv))