2010-11-05 00:05:34 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
|
|
|
# Check (recursively) for style compliance violations and other
|
|
|
|
# tree inconsistencies.
|
|
|
|
#
|
2012-01-10 20:53:20 +00:00
|
|
|
# by jduck and friends
|
2010-11-05 00:05:34 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Supporting sub-routines
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2011-10-16 15:53:19 +00:00
|
|
|
LONG_LINE_LENGTH = 200 # From 100 to 200 which is stupidly long
|
2011-12-12 21:12:01 +00:00
|
|
|
CHECK_OLD_RUBIES = !!ENV['MSF_CHECK_OLD_RUBIES']
|
|
|
|
|
|
|
|
if CHECK_OLD_RUBIES
|
|
|
|
require 'rvm'
|
|
|
|
warn "This is going to take a while, depending on the number of Rubies you have installed."
|
|
|
|
end
|
2011-10-16 15:53:19 +00:00
|
|
|
|
2010-11-05 00:05:34 +00:00
|
|
|
def show_count(f, txt, num)
|
|
|
|
puts "%s ... %s: %u" % [f, txt, num] if num > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_missing(f, txt, val)
|
|
|
|
puts '%s ... %s' % [f, txt] if not val
|
|
|
|
end
|
|
|
|
|
2011-12-12 21:12:01 +00:00
|
|
|
# This check is only enabled if the environment variable MSF_CHECK_OLD_RUBIES is set
|
|
|
|
def test_old_rubies(f_rel)
|
|
|
|
return true unless CHECK_OLD_RUBIES
|
|
|
|
return true unless Object.const_defined? :RVM
|
|
|
|
puts "Checking syntax for #{f_rel}."
|
|
|
|
@rubies ||= RVM.list_strings
|
|
|
|
res = %x{rvm all do ruby -c #{f_rel}}.split("\n").select {|msg| msg =~ /Syntax OK/}
|
|
|
|
@rubies.size == res.size
|
|
|
|
end
|
|
|
|
|
2010-11-05 00:05:34 +00:00
|
|
|
|
|
|
|
def check_single_file(dparts, fparts, f_rel)
|
|
|
|
f = (dparts + fparts).join('/')
|
2011-11-23 00:22:50 +00:00
|
|
|
# puts "Checking: #{f.inspect}"
|
2010-11-05 00:05:34 +00:00
|
|
|
|
2011-11-23 00:22:50 +00:00
|
|
|
# Put some kind of blacklist mechanism here, yaml config would be nice...
|
2010-11-05 00:05:34 +00:00
|
|
|
|
2011-11-23 00:22:50 +00:00
|
|
|
# check for executable
|
|
|
|
f_exec = File.executable?(f_rel)
|
2012-01-30 07:52:49 +00:00
|
|
|
show_missing(f, "WARNING: is executable", !f_exec)
|
2010-11-05 00:05:34 +00:00
|
|
|
|
2011-12-12 21:12:01 +00:00
|
|
|
# check all installed rubies
|
|
|
|
|
|
|
|
old_rubies = test_old_rubies(f_rel)
|
2012-01-30 07:52:49 +00:00
|
|
|
show_missing(f, "ERROR: fails alternate Ruby version check", old_rubies)
|
2011-12-12 21:12:01 +00:00
|
|
|
|
|
|
|
|
2010-11-05 00:05:34 +00:00
|
|
|
# check various properties based on content
|
|
|
|
content = File.open(f_rel, "rb").read
|
|
|
|
|
2010-11-14 19:00:47 +00:00
|
|
|
# check criteria based on whole content
|
2010-11-16 20:17:03 +00:00
|
|
|
if content =~ / \< Msf::Exploit/
|
|
|
|
has_rank = false
|
|
|
|
has_dd = false
|
|
|
|
|
2010-11-14 19:00:47 +00:00
|
|
|
has_rank = true if content =~ /Rank =/
|
2012-02-01 02:49:08 +00:00
|
|
|
has_dd = true if content =~ /DisclosureDate/ or content =~ /Generic Payload Handler/
|
2010-11-16 20:17:03 +00:00
|
|
|
|
2012-01-30 07:52:49 +00:00
|
|
|
show_missing(f, 'ERROR: missing exploit ranking', has_rank)
|
|
|
|
show_missing(f, 'ERROR: missing disclosure date', has_dd)
|
2010-11-14 19:00:47 +00:00
|
|
|
end
|
2010-11-16 20:17:03 +00:00
|
|
|
|
2012-02-05 21:39:36 +00:00
|
|
|
# Check disclosure date format
|
2012-02-21 07:04:05 +00:00
|
|
|
if content =~ /'DisclosureDate' => ['|\"](.+)['|\"]/
|
2012-02-05 21:39:36 +00:00
|
|
|
d = $1 #Captured date
|
|
|
|
# Flag if overall format is wrong
|
|
|
|
if d =~ /^... \d{1,2} \d{4}/
|
|
|
|
# Flag if month format is wrong
|
|
|
|
m = d.split[0]
|
|
|
|
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
|
|
|
|
if months.index(m).nil?
|
|
|
|
show_missing(f, 'WARNING: incorrect disclosure month format', false)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
show_missing(f, 'WARNING: incorrect disclosure date format', false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-15 21:37:34 +00:00
|
|
|
# Check title format
|
|
|
|
if content =~ /'Name'\s+=>\s[\x22\x27](.+)[\x22\x27],\s*$/
|
|
|
|
name = $1
|
|
|
|
words = $1.split
|
|
|
|
[words.first, words.last].each do |word|
|
|
|
|
if word[0,1] =~ /[a-z]/ and word[1,1] !~ /[A-Z0-9]/
|
|
|
|
next if word =~ /php[A-Z]/
|
|
|
|
next if %w{iseemedia activePDF freeFTPd osCommerce myBB}.include? word
|
|
|
|
show_missing(f, "WARNING: bad capitalization in module title: #{word}", false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-05 22:07:55 +00:00
|
|
|
# If an exploit module mentinos the word "stack overflow", chances are they mean "stack buffer overflow".
|
|
|
|
# "stack overflow" means "stack exhaustion". See explanation:
|
|
|
|
# http://blogs.technet.com/b/srd/archive/2009/01/28/stack-overflow-stack-exhaustion-not-the-same-as-stack-buffer-overflow.aspx
|
2010-11-16 20:17:03 +00:00
|
|
|
bad_term = true
|
2012-02-05 22:07:55 +00:00
|
|
|
if content =~ /class Metasploit\d < Msf::Exploit::Remote/ and content.gsub("\n", "") =~ /stack[[:space:]]+overflow/i
|
2010-11-14 19:00:47 +00:00
|
|
|
bad_term = false
|
2012-02-05 23:48:05 +00:00
|
|
|
show_missing(f, 'WARNING: contains "stack overflow" You mean "stack buffer overflow"?', bad_term)
|
|
|
|
elsif content =~ /class Metasploit\d < Msf::Auxiliary/ and content.gsub("\n", "") =~ /stack[[:space:]]+overflow/i
|
|
|
|
bad_term = false
|
|
|
|
show_missing(f, 'WARNING: contains "stack overflow" You mean "stack exhaustion"?', bad_term)
|
2010-11-14 19:00:47 +00:00
|
|
|
end
|
|
|
|
|
2012-02-18 23:29:14 +00:00
|
|
|
# Check function naming style and arg length
|
2012-02-14 03:03:13 +00:00
|
|
|
functions = content.scan(/def (\w+)\(*(.+)\)*/)
|
2012-03-15 22:00:44 +00:00
|
|
|
|
2012-02-14 03:03:13 +00:00
|
|
|
functions.each do |func_name, args|
|
2012-03-15 22:00:44 +00:00
|
|
|
=begin
|
2012-02-18 23:29:14 +00:00
|
|
|
# Check Ruby variable naming style
|
|
|
|
if func_name =~ /[a-z][A-Z]/ or func_name =~ /[A-Z][a-z]/
|
|
|
|
show_missing(f, "WARNING: Poor function naming style for: '#{func_name}'", false)
|
|
|
|
end
|
2012-03-15 22:00:44 +00:00
|
|
|
=end
|
2012-02-18 23:29:14 +00:00
|
|
|
|
|
|
|
# Check argument length
|
2012-02-14 03:03:13 +00:00
|
|
|
args_length = args.split(",").length
|
|
|
|
if args_length > 6
|
|
|
|
show_missing(f, "WARNING: Poorly designed argument list in '#{func_name}'. Try a hash.", false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-15 22:00:44 +00:00
|
|
|
=begin
|
2012-02-18 23:58:36 +00:00
|
|
|
vars = content.scan(/([\x20|\w]+) \= [\'|\"]*\w[\'|\"]*/).flatten
|
2012-02-18 23:29:14 +00:00
|
|
|
vars.each do |v|
|
2012-02-21 17:46:52 +00:00
|
|
|
v = v.strip
|
2012-02-21 07:04:05 +00:00
|
|
|
next if v =~ /^var/ or v =~ /^Rank/
|
2012-02-18 23:29:14 +00:00
|
|
|
if v =~ /[a-z][A-Z]/ or v =~ /[A-Z][a-z]/
|
|
|
|
show_missing(f, "WARNING: Poor variable naming style for: '#{v}'", false)
|
|
|
|
end
|
|
|
|
end
|
2012-03-15 22:00:44 +00:00
|
|
|
=end
|
2012-02-18 23:29:14 +00:00
|
|
|
|
2010-11-14 19:00:47 +00:00
|
|
|
# check criteria based on individual lines
|
2010-11-05 00:05:34 +00:00
|
|
|
spaces = 0
|
|
|
|
bi = []
|
2011-03-04 04:21:16 +00:00
|
|
|
ll = []
|
2012-01-10 20:53:20 +00:00
|
|
|
bc = []
|
2010-11-05 00:05:34 +00:00
|
|
|
cr = 0
|
2010-11-11 22:25:13 +00:00
|
|
|
url_ok = true
|
2010-11-05 00:05:34 +00:00
|
|
|
nbo = 0 # non-bin open
|
2011-02-23 20:20:29 +00:00
|
|
|
long_lines = 0
|
|
|
|
no_stdio = true
|
2010-11-05 00:05:34 +00:00
|
|
|
|
|
|
|
in_comment = false
|
2011-10-23 11:56:07 +00:00
|
|
|
in_literal = false
|
|
|
|
src_ended = false
|
|
|
|
|
2010-11-05 00:05:34 +00:00
|
|
|
idx = 0
|
|
|
|
content.each_line { |ln|
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
# block comment awareness
|
|
|
|
if ln =~ /^=end$/
|
|
|
|
in_comment = false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
in_comment = true if ln =~ /^=begin$/
|
|
|
|
next if in_comment
|
|
|
|
|
2011-10-23 11:56:07 +00:00
|
|
|
# block string awareness (ignore indentation in these)
|
|
|
|
in_literal = false if ln =~ /^EOS$/
|
|
|
|
next if in_literal
|
|
|
|
in_literal = true if ln =~ /\<\<-EOS$/
|
|
|
|
|
|
|
|
# ignore stuff after an __END__ line
|
|
|
|
src_ended = true if ln =~ /^__END__$/
|
|
|
|
next if src_ended
|
|
|
|
|
2012-01-10 20:53:20 +00:00
|
|
|
if ln =~ /[\x00-\x08\x0b\x0c\x0e-\x19\x7f-\xff]/
|
|
|
|
bc << [ idx, ln.inspect]
|
|
|
|
end
|
2011-10-23 11:56:07 +00:00
|
|
|
|
|
|
|
if (ln.length > LONG_LINE_LENGTH)
|
2011-03-04 04:21:16 +00:00
|
|
|
ll << [ idx, ln ]
|
2011-02-23 20:20:29 +00:00
|
|
|
end
|
|
|
|
|
2011-11-20 00:34:31 +00:00
|
|
|
spaces += 1 if ln =~ /[ \t]$/
|
2010-11-05 00:05:34 +00:00
|
|
|
if (ln.length > 1) and (ln =~ /^([\t ]*)/) and ($1.include?(' '))
|
|
|
|
bi << [ idx, ln ]
|
|
|
|
end
|
|
|
|
cr += 1 if ln =~ /\r$/
|
2010-11-11 22:25:13 +00:00
|
|
|
url_ok = false if ln =~ /\.com\/projects\/Framework/
|
2010-11-05 00:05:34 +00:00
|
|
|
if ln =~ /File\.open/ and ln =~ /[\"\'][arw]/
|
2011-11-28 23:52:34 +00:00
|
|
|
if not ln =~ /[\"\'][wra]\+?b\+?[\"\']/
|
2010-11-05 00:05:34 +00:00
|
|
|
nbo += 1
|
|
|
|
end
|
|
|
|
end
|
2011-02-23 20:20:29 +00:00
|
|
|
|
|
|
|
# The rest of these only count if it's not a comment line
|
|
|
|
next if ln =~ /[[:space:]]*#/
|
|
|
|
|
2011-09-18 01:57:51 +00:00
|
|
|
if ln =~ /\$std(?:out|err)/ or ln =~ /[[:space:]]puts/
|
|
|
|
no_stdio = false
|
|
|
|
end
|
2010-11-05 00:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# report information for this file
|
2012-01-30 07:52:49 +00:00
|
|
|
show_count(f, 'WARNING: spaces at EOL', spaces)
|
2010-11-05 00:05:34 +00:00
|
|
|
if bi.length > 0
|
|
|
|
puts '%s ... bad indent: %u' % [f, bi.length]
|
|
|
|
bi.each { |el|
|
|
|
|
el[1] = el[1].inspect
|
|
|
|
puts ' %8d: %s' % el
|
|
|
|
}
|
|
|
|
end
|
2011-10-23 11:56:07 +00:00
|
|
|
|
2011-03-04 04:21:16 +00:00
|
|
|
if ll.length > 0
|
2012-01-30 07:52:49 +00:00
|
|
|
puts "WARNING: %s ... lines longer than #{LONG_LINE_LENGTH} columns: %u" % [f, ll.length]
|
2011-03-04 04:21:16 +00:00
|
|
|
ll.each { |el|
|
|
|
|
el[1] = el[1].inspect
|
|
|
|
puts ' %8d: %s' % el
|
|
|
|
}
|
|
|
|
end
|
2011-10-23 11:56:07 +00:00
|
|
|
|
2012-01-10 20:53:20 +00:00
|
|
|
if bc.length > 0
|
2012-01-30 07:52:49 +00:00
|
|
|
puts "ERROR: %s ... probably has unicode: %u" % [f, bc.length]
|
2012-01-10 20:53:20 +00:00
|
|
|
bc.each { |ec|
|
|
|
|
ec[1] = ec[1].inspect
|
|
|
|
puts ' %8d: %s' % ec
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-01-30 07:52:49 +00:00
|
|
|
show_count(f, 'WARNING: carriage return EOL', cr)
|
|
|
|
show_missing(f, 'WARNING: incorrect URL to framework site', url_ok)
|
|
|
|
show_missing(f, 'ERROR: writes to stdout', no_stdio)
|
|
|
|
show_count(f, 'WARNING: File.open without binary mode', nbo)
|
2010-11-05 00:05:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Main program
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
dirs = ARGV
|
|
|
|
|
|
|
|
if dirs.length < 1
|
|
|
|
$stderr.puts "usage: #{File.basename(__FILE__)} <directory or file>"
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
dirs.each { |dir|
|
|
|
|
# process all args
|
|
|
|
|
|
|
|
f = nil
|
|
|
|
old_dir = nil
|
|
|
|
|
|
|
|
if dir
|
|
|
|
if File.file?(dir)
|
|
|
|
# whoa, a single file!
|
|
|
|
f = File.basename(dir)
|
|
|
|
dir = File.dirname(dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
old_dir = Dir.getwd
|
|
|
|
Dir.chdir(dir)
|
|
|
|
dparts = dir.split('/')
|
|
|
|
else
|
|
|
|
dparts = []
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only one file?
|
|
|
|
if f
|
|
|
|
check_single_file(dparts, [ f ], f)
|
|
|
|
else
|
|
|
|
# Do a recursive check of the specified directory
|
|
|
|
Dir.glob('**/*.rb') { |f|
|
|
|
|
check_single_file(dparts, f.split('/'), f)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(old_dir)
|
|
|
|
}
|