Module: Fuzz

Extended by:
LogMethods, Sys::SysMethods
Defined in:
lib/fuzz/fzzr.rb,
lib/fuzz/log.rb,
lib/fuzz/fuzz.rb,
lib/fuzz/screen.rb,
lib/fuzz/system.rb,
lib/fuzz/console.rb,
lib/fuzz/options.rb,
lib/fuzz/version.rb

Overview


version.rb - TAOX11 fuzz checker

Author: Martin Corino

Copyright © Remedy IT Expertise BV


Defined Under Namespace

Modules: Console, Fzzr, LogMethods, Sys Classes: DirObject, FileObject, Reporter, Screen

Constant Summary collapse

FUZZ_ROOT =
self.root_path
FUZZRC =
'.fuzzrc'
FUZZRC_GLOBAL =
File.expand_path(File.join(ENV['HOME'] || ENV['HOMEPATH'] || '~', FUZZRC))
OPTIONS =
OpenStruct.new
FUZZ_VERSION_MAJOR =
0
FUZZ_VERSION_MINOR =
9
FUZZ_VERSION_RELEASE =
11
FUZZ_VERSION =
"#{FUZZ_VERSION_MAJOR}.#{FUZZ_VERSION_MINOR}.#{FUZZ_VERSION_RELEASE}"
"Copyright (c) 2012-#{Time.now.year} Remedy IT Expertise BV, The Netherlands".freeze

Class Method Summary collapse

Methods included from Sys::SysMethods

chmod, cp, has_ansi?, in_dir, mswin?, mv

Methods included from LogMethods

log, log_error, log_fatal, log_info, log_warning, show_error, show_msg, show_warning, silent?, verbose?, verbosity

Class Method Details

.apply_fix?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/fuzz/fuzz.rb', line 136

def self.apply_fix?
  options.apply_fix || false
end

.excluded?(object) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/fuzz/fuzz.rb', line 148

def self.excluded?(object)
  excludes.any? { |excl| (object.fullpath =~ /#{excl}/) }
end

.excludesObject



144
145
146
# File 'lib/fuzz/fuzz.rb', line 144

def self.excludes
  options.config[:excludes] || []
end

.follow_symlink?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/fuzz/fuzz.rb', line 140

def self.follow_symlink?
  options.config[:follow_symlink] || false
end

.fuzzersObject

fuzzer registration



71
72
73
# File 'lib/fuzz/fuzz.rb', line 71

def fuzzers
  @fuzzers ||= {}
end

.fzzr_excluded?(fzzr) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/fuzz/fuzz.rb', line 85

def fzzr_excluded?(fzzr)
  options.config[:fzzr_excludes].include?(fzzr.fuzz_id.to_sym)
end

.fzzr_included?(fzzr) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/fuzz/fuzz.rb', line 89

def fzzr_included?(fzzr)
  !fzzr_excluded?(fzzr)
end

.get_fzzr(id) ⇒ Object



81
82
83
# File 'lib/fuzz/fuzz.rb', line 81

def get_fzzr(id)
  fuzzers[id]
end

.handle_object(object) ⇒ Object



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
# File 'lib/fuzz/fuzz.rb', line 312

def self.handle_object(object)
  log_verbose(%Q{Handling #{object}})
  fzzrs = select_fzzrs(object)
  no_fixes_allowed = false
  rc = fzzrs.inject(true) do |result, fzzr|
    log_verbose(%Q{+ Running fuzzer #{fzzr.fuzz_id}})
    begin
      if fzzr.run(object, options.apply_fix)
        result
      else
        log_verbose(%Q{+ Error from fuzzer #{fzzr.fuzz_id}})
        false
      end
    rescue
      log_error(%Q{EXCEPTION CAUGHT running fuzzer #{fzzr.fuzz_id} on #{object} - #{$!}\n#{$!.backtrace.join("\n")}})
      no_fixes_allowed = true
      break ## immediately stop handling this object, rc will remain false
    end
  end
  unless no_fixes_allowed
    if Fuzz.apply_fix? && object.changed?
      rc = update_file_object(object) && rc
    end
  end
  rc ? true : false
end

.includesObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/fuzz/fuzz.rb', line 56

def includes
  unless @include_re
    @include_re = []
    # @include_re << "\\.(#{Fuzz::FileObject.extensions.join('|')})$"
    # @include_re << "#{Fuzz::FileObject.filenames.join('|')}$"
    @include_re << "\\.(#{options.config[:exts].join('|')})$"
    @include_re << "#{options.config[:filenames].join('|')}$"
  end
  @include_re
end

.init_optparserObject

parse commandline arguments



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
# File 'lib/fuzz/fuzz.rb', line 155

def self.init_optparser
  script_name = File.basename($0)
  if not script_name =~ /fuzz/
    script_name = "ruby "+$0
  end

  options.optparser = opts = OptionParser.new
  opts.banner = "Usage: #{script_name} [options] [glob [glob]]\n\n"
  opts.separator "\n--- [General options] ---\n\n"
  opts.on('-t', '--filetype', '=EXT', String,
          'Defines an alternative filetype to search and scan. Can be specified multiple times.',
          "Default: #{Fuzz::FileObject.extensions.join('|')}") { |v|
            (options.user_config[:exts] ||= []) << v
          }
  opts.on('-f', '--file', '=NAME', String,
          'Defines an alternative filename to search and scan. Can be specified multiple times.',
          "Default: #{Fuzz::FileObject.filenames.join('|')}") { |v|
            (options.user_config[:filenames] ||= []) << v
          }
  opts.on('-a', '--add-files',
          'Add custom filenames and/or filetype extensions to default list instead of replacing defaults.',
          'Default: false') { |v|
            options.user_config[:add_files] = true
          }
  opts.on('-S', '--no-symlinks',
          'Do not follow symlinks.',
          'Default: follow symlinks') { |v|
            options.user_config[:follow_symlink] = false
          }
  opts.on('-P', '--fzzr-path', '=PATH',
          'Adds search path for Fuzzers.',
          "Default: loaded from ~/#{FUZZRC} and/or ./#{FUZZRC}") { |v|
            (options.user_config[:fzzr_paths] ||= []) << v.to_s
          }
  opts.on('-B', '--blacklist', '=FZZRID',
          'Adds Fuzzer ID to list of fuzzers to exclude from Fuzz check.',
          'Default: none') { |v|
            (options.user_config[:fzzr_excludes] ||= []) << v.to_sym
          }
  opts.on('-X', '--exclude', '=MASK',
          'Adds path mask (regular expression) to list to exclude from Fuzz check.',
          'Default: none') { |v|
            (options.user_config[:excludes] ||= []) << v
          }
  opts.on('-c', '--config', '=FUZZRC',
          'Load config from FUZZRC file.',
          "Default:  ~/#{FUZZRC} and/or ./#{FUZZRC}") { |v|
    options.add_config(v)
  }
  opts.on('--write-config', '=[FUZZRC]',
          'Write config to file and exit.',
          "Default: ./#{FUZZRC}") { |v|
    options.user_config.save(String === v ? v : FUZZRC)
    exit
  }
  opts.on('--show-config',
          'Display config settings and exit.') { |v|
    options.load_config
    puts YAML.dump(options.config.__send__ :table)
    exit
  }

  opts.separator ''
  opts.on('-o', '--output', '=FILE', String,
          'Specifies filename to write Fuzz messages to.',
          'Default: stderr') { |v|
            options.output = v
          }
  opts.on('-p', '--apply-fix',
          'Apply fixes (if any) for Fuzz errors.',
          'Default: false') { |v|
            options.apply_fix = true
          }
  opts.on('-n', '--no-recurse',
          'Prevents directory recursion in file selection.',
          'Default: recurse') { |v|
            options.recurse = false
          }
  opts.on('-v', '--verbose',
          'Run with increased verbosity level. Repeat to increase more.',
          'Default: 1') { |v| options.verbose += 1 }

  opts.separator ''
  opts.on('-L', '--list',
          'List available Fuzzers and exit.') {
    options.load_config
    load_fuzzers
    puts "TAOX11 fuzz checker #{FUZZ_VERSION_MAJOR}.#{FUZZ_VERSION_MINOR}.#{FUZZ_VERSION_RELEASE}"
    puts FUZZ_COPYRIGHT
    puts('%-30s %s' % %w{Fuzzer Description})
    puts(('-' * 30)+' '+('-' * 48))
    fuzzers.values.each { |fzzr| puts('%-30s %s' % [fzzr.fuzz_id, fzzr.description]) }
    puts
    exit
  }

  opts.separator ""
  opts.on('-V', '--version',
          'Show version information and exit.') {
    puts "TAOX11 fuzz checker #{FUZZ_VERSION_MAJOR}.#{FUZZ_VERSION_MINOR}.#{FUZZ_VERSION_RELEASE}"
    puts FUZZ_COPYRIGHT
    exit
  }
  opts.on('-h', '--help',
          'Show this help message.') {
    options.load_config
    load_fuzzers
    puts opts;
    puts;
    exit
  }

  opts.separator "\n--- [Fuzzer options] ---\n\n"
end

.iterate_paths(paths) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/fuzz/fuzz.rb', line 339

def self.iterate_paths(paths)
  paths.inject(true) do |result, path|
    if File.readable?(path) && (!File.symlink?(path) || follow_symlink?)
      if File.directory?(path)
        rc = handle_object(dirobj = Fuzz::DirObject.new(path))
        log_verbose(%Q{Iterating #{path}})
        if options.recurse && !excluded?(dirobj)
          rc = iterate_paths(Dir.glob(File.join(path, '*'))) && rc
        end
        rc
      elsif File.file?(path)
        handle_object(Fuzz::FileObject.new(path))
      else
        true
      end
    else
      log_warning(File.readable?(path) ? %Q{Cannot read #{path}} : %Q{Cannot follow symlink #{path}})
      false
    end && result
  end
end

.load_configObject



52
53
54
# File 'lib/fuzz/fuzz.rb', line 52

def load_config
  options.load_config
end

.load_fuzzersObject

load fuzzers



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fuzz/fuzz.rb', line 96

def load_fuzzers
  # standard fuzzers included in Gem
  unless loaded_fzzr_paths.include?(_p = File.join(FUZZ_ROOT, 'fuzzers'))
    Dir.glob(File.join(_p, '*.rb')).each do |fnm|
      require fnm
    end
    loaded_fzzr_paths << _p
  end
  # configured fuzzers
  options.config[:fzzr_paths].each do |fzzrpath|
    unless loaded_fzzr_paths.include?(_p = File.expand_path(fzzrpath))
      Dir.glob(File.join(_p, '*.rb')).each do |fnm|
        require fnm
      end
      loaded_fzzr_paths << _p
    end
  end
end

.log_verbose(msg) ⇒ Object

Backwards compatibility



124
125
126
# File 'lib/fuzz/fuzz.rb', line 124

def self.log_verbose(msg)
  log_info(msg) if verbose?
end

.optionsObject



44
45
46
# File 'lib/fuzz/fuzz.rb', line 44

def options
  Fuzz::OPTIONS
end

.parse_args(argv) ⇒ Object



270
271
272
# File 'lib/fuzz/fuzz.rb', line 270

def self.parse_args(argv)
  options.optparser.parse!(argv)
end

.register_fzzr(fzzr) ⇒ Object



75
76
77
78
79
# File 'lib/fuzz/fuzz.rb', line 75

def register_fzzr(fzzr)
  raise "Duplicate fuzzer registration: #{fzzr.fuzz_id}" if fuzzers.has_key?(fzzr.fuzz_id)
  fuzzers[fzzr.fuzz_id] = fzzr
  fzzr.setup(options.optparser) if options.optparser && fzzr.respond_to?(:setup) && fzzr_included?(fzzr)
end

.reporterObject



35
36
37
# File 'lib/fuzz/fuzz.rb', line 35

def reporter
  @reporter ||= Fuzz::Reporter.new
end

.resetObject



48
49
50
# File 'lib/fuzz/fuzz.rb', line 48

def reset
  options.reset
end

.root_pathObject



22
23
24
25
26
# File 'lib/fuzz/fuzz.rb', line 22

def self.root_path
  f = File.expand_path(__FILE__)
  f = File.expand_path(File.readlink(f)) if File.symlink?(f)
  File.dirname(f)
end

.runObject



384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/fuzz/fuzz.rb', line 384

def self.run
  init_optparser

  # parse arguments
  parse_args(ARGV)

  # load config (if any)
  options.load_config

  # load fuzzers
  load_fuzzers

  run_fzzrs(ARGV)
end

.run_fzzrs(argv) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/fuzz/fuzz.rb', line 361

def self.run_fzzrs(argv)
  options.config[:exts].concat(Fuzz::FileObject.extensions) if options.config[:exts].empty? || options.config[:add_files]
  options.config[:filenames].concat(Fuzz::FileObject.filenames) if options.config[:filenames].empty? || options.config[:add_files]

  options.config[:exts].uniq!
  options.config[:filenames].uniq!

  f_close_output = false
  if String === options.output
    options.output = File.open(options.output, 'w')
    f_close_output = true
  end
  begin
    # determin files/paths to test
    paths = argv.collect { |a| Dir.glob(a) }.flatten.uniq
    paths = Dir.glob('*') if paths.empty?
    # scan all determined objects
    return iterate_paths(paths)
  ensure
    options.output.close if f_close_output
  end
end

.select_fzzrs(object) ⇒ Object



274
275
276
# File 'lib/fuzz/fuzz.rb', line 274

def self.select_fzzrs(object)
  fuzzers.values.collect { |fzzr| (fzzr_included?(fzzr) && fzzr.applies_to?(object)) ? fzzr : nil }.compact
end

.set_reporter(rep) ⇒ Object Also known as: reporter=



39
40
41
# File 'lib/fuzz/fuzz.rb', line 39

def set_reporter(rep)
  @reporter = rep
end

.update_file_object(fo) ⇒ Object



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
# File 'lib/fuzz/fuzz.rb', line 278

def self.update_file_object(fo)
  if File.writable?(fo.fullpath)
    log_verbose(%Q{Updating #{fo}...})
    ftmp = Tempfile.new(fo.name)
    log_verbose(%Q{+ Writing temp file #{ftmp.path}...})
    fo.lines.each { |ln| ftmp.print ln }
    ftmp.close(false) # close but do NOT unlink
    log_verbose(%Q{+ Replacing #{fo} with #{ftmp.path}})
    # create temporary backup
    ftmp2 = Tempfile.new(fo.name)
    ftmp2_name = ftmp2.path.dup
    ftmp2.close(true)
    mv(fo.fullpath, ftmp2_name)
    # replace original
    begin
      mv(ftmp.path, fo.fullpath)
      # preserve file mode
      chmod(File.lstat(ftmp2_name).mode, fo.fullpath)
    rescue
      log_error(%Q{FAILED updating #{fo}: #{$!}})
      # restore backup
      mv(ftmp2_name, fo.fullpath)
      raise
    end
    # remove backup
    File.unlink(ftmp2_name)
    log_verbose(%Q{Finished updating #{fo}.})
    return true
  else
    log_error(%Q{NO_ACCESS - cannot update #{fo}})
    return false
  end
end

.verbosityObject

Option methods



132
133
134
# File 'lib/fuzz/fuzz.rb', line 132

def self.verbosity
  options.verbose
end