forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msftidy.rb
executable file
·225 lines (183 loc) · 4.56 KB
/
msftidy.rb
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
#!/usr/bin/env ruby
#
# $Id$
#
# Check (recursively) for style compliance violations and other
# tree inconsistencies.
#
# by jduck
#
##
#
# Supporting sub-routines
#
##
LONG_LINE_LENGTH = 200 # From 100 to 200 which is stupidly long
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
def check_single_file(dparts, fparts, f_rel)
f = (dparts + fparts).join('/')
#puts f
# blacklisted files..
#?
# check svn existence
svn_parts = fparts[0, fparts.length - 1] + [ '.svn', 'text-base' ]
svn_parts << fparts[-1, 1].first + '.svn-base'
svn_path = svn_parts.join('/')
if not File.file?(svn_path)
show_missing(f, 'not in SVN!', false)
else
# check svn properties
svn_parts = fparts[0, fparts.length - 1] + [ '.svn', 'prop-base' ]
svn_parts << fparts[-1, 1].first + '.svn-base'
svn_path = svn_parts.join('/')
props = File.open(svn_path, 'rb').read rescue nil
if props
kw = false
exec = false
mime = false
props.each_line { |ln|
kw = true if ln =~ /svn:keywords/
exec = true if ln =~ /svn:executable/
mime = true if ln =~ /svn:mime-type/
}
show_missing(f, 'missing svn:keywords', kw)
show_missing(f, 'is executable!', (not exec))
show_missing(f, 'has mime-type property!', (not mime))
else
show_missing(f, 'missing svn properties', props)
end
end
# check various properties based on content
content = File.open(f_rel, "rb").read
# check criteria based on whole content
if content =~ / \< Msf::Exploit/
has_rank = false
has_dd = false
has_rank = true if content =~ /Rank =/
has_dd = true if content =~ /DisclosureDate/
show_missing(f, 'missing exploit ranking', has_rank)
show_missing(f, 'missing disclosure date', has_dd)
end
bad_term = true
if content.gsub("\n", "") =~ /stack[[:space:]]+overflow/i
bad_term = false
end
show_missing(f, 'contains "stack overflow"', bad_term)
# check criteria based on individual lines
spaces = 0
bi = []
ll = []
cr = 0
has_rev = false
has_id = false
url_ok = true
nbo = 0 # non-bin open
long_lines = 0
no_stdio = true
in_comment = false
in_literal = false
src_ended = false
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
# 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
if (ln.length > LONG_LINE_LENGTH)
ll << [ idx, ln ]
end
spaces += 1 if ln =~ / $/
if (ln.length > 1) and (ln =~ /^([\t ]*)/) and ($1.include?(' '))
bi << [ idx, ln ]
end
cr += 1 if ln =~ /\r$/
has_id = true if ln =~ /\$Id:.*\$/
has_rev = true if ln =~ /\$Revision:.*\$/
url_ok = false if ln =~ /\.com\/projects\/Framework/
if ln =~ /File\.open/ and ln =~ /[\"\'][arw]/
if not ln =~ /[\"\'][wra]b\+?[\"\']/
nbo += 1
end
end
# The rest of these only count if it's not a comment line
next if ln =~ /[[:space:]]*#/
if ln =~ /\$std(?:out|err)/ or ln =~ /[[:space:]]puts/
no_stdio = false
end
}
# report information for this file
show_count(f, 'spaces at EOL', spaces)
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
if ll.length > 0
puts "%s ... lines longer than #{LONG_LINE_LENGTH} columns: %u" % [f, ll.length]
ll.each { |el|
el[1] = el[1].inspect
puts ' %8d: %s' % el
}
end
show_count(f, 'carriage return EOL', cr)
show_missing(f, 'missing $'+'Id: $', has_id)
show_missing(f, 'missing $'+'Revision: $', has_rev)
show_missing(f, 'incorrect URL to framework site', url_ok)
show_missing(f, 'writes to stdout', no_stdio)
show_count(f, 'File.open without binary mode', nbo)
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)
}