-
Notifications
You must be signed in to change notification settings - Fork 17
/
web_header_scan.rb
executable file
·377 lines (316 loc) · 10.5 KB
/
web_header_scan.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
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
#!/usr/bin/env ruby
# == Synopsis
# This script is designed to automate retrieval of headers from a list of web servers
#
#Input is a file with a list of HTTP servers to be reviewed
#
# == Pre-Requisites
#
#
# == ToDo
#
# * Add logging
# * Add rtf and html reports
# * Add TRACE and OPTIONS checks
#
# == Author
# Author:: Rory McCune
# Copyright:: Copyright (c) 2014 Rory Mccune
# License:: GPLv3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# == Options
# -h, --help Displays help message
# -v, --version Display the version, then exit
# -f, --file File containing servers to scan
# --reportPrefix Prefix for report files (Default is icmp_recon)
# --textReport Create a CSV report of the results
# --hmtlReport Create an HTML report of the results
# --rtfReport Create an RTF report of the results
#
#
# == Usage
#
#
class HTTPScan
VERSION = '0.0.1'
def initialize(hosts)
begin
require 'httparty'
rescue LoadError
puts "Requires httparty - try gem install httparty"
exit
end
#require 'logger'
#clean up the file so that it's easier to use uri
hosts.collect! do |address|
unless address =~ /^http/
if address.split(':')[1] == '443'
address = 'https://' + address
else
address = 'http://' + address
end
end
address
end
@hosts = hosts
end
def header_scan
@headers = Hash.new
@hosts.each do |host|
begin
#resp = HTTParty.get(host, {:no_follow => true, :verify => false})
resp = HTTParty.get(host, {:verify => false})
rescue HTTParty::RedirectionTooDeep => e
puts 'too deep on ' + host
next
rescue Timeout::Error
puts "Timeout Error on " + host
next
rescue Exception => e
puts "Error on " + host
puts e.to_s
next
end
@headers[host] = resp.headers
end
end
def excel_report(report_file_base)
begin
require 'resolv'
require 'rubyXL'
require 'uri'
rescue LoadError
puts 'The excel report requires rubyXL'
puts 'try gem install rubyXL'
exit
end
workbook = RubyXL::Workbook.new
info_sheet = workbook.worksheets[0]
info_sheet.sheet_name = "Server Information Headers"
info_sheet.add_cell(0,0,"IP Address")
info_sheet.add_cell(0,1,"Hostname")
info_sheet.add_cell(0,2,"port")
info_sheet.add_cell(0,3,"Server")
info_sheet.add_cell(0,4,"X-Powered-By")
info_sheet.add_cell(0,5,"X-AspNet-Version")
info_sheet.add_cell(0,6,"X-AspNetmvc-Version")
security_sheet = workbook.add_worksheet('Security Headers')
security_sheet.sheet_name = "Server Security Headers"
security_sheet.add_cell(0,0,"IP Address")
security_sheet.add_cell(0,1,"Hostname")
security_sheet.add_cell(0,2,"port")
security_sheet.add_cell(0,3,"X-XSS-Protection")
security_sheet.add_cell(0,4,"Strict-Transport-Security")
security_sheet.add_cell(0,5,"X-Content-Type-Options")
security_sheet.add_cell(0,6,"Cache-Control")
security_sheet.add_cell(0,7,"Content-Security-Policy")
security_sheet.add_cell(0,8,"X-Frame-Options")
row_count = 1
@headers.each do |host, headers|
url = URI.parse(host)
if url.host =~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/
ip_address = url.host
else
ip_address = Resolv.getaddress(url.host)
end
info_sheet.add_cell(row_count,0,ip_address)
if ip_address == url.host
info_sheet.add_cell(row_count,1,"Unknown")
else
info_sheet.add_cell(row_count,1,url.host)
end
info_sheet.add_cell(row_count,2,url.port.to_s)
info_sheet.add_cell(row_count,3,headers['server'])
info_sheet.add_cell(row_count,4,headers['x-powered-by'])
info_sheet.add_cell(row_count,5,headers['x-aspnet-version'])
info_sheet.add_cell(row_count,6,headers['x-aspnetmvc-version'])
security_sheet.add_cell(row_count,0,ip_address)
if ip_address == url.host
security_sheet.add_cell(row_count,1,"Unknown")
else
security_sheet.add_cell(row_count,1,url.host)
end
security_sheet.add_cell(row_count,2,url.port.to_s)
security_sheet.add_cell(row_count,3,headers['x-xss-protection'])
security_sheet.add_cell(row_count,4,headers['strict-transport-security'])
security_sheet.add_cell(row_count,5,headers['x-content-type-options'])
security_sheet.add_cell(row_count,6,headers['cache-control'])
security_sheet.add_cell(row_count,7,headers['content-security-policy'])
security_sheet.add_cell(row_count,8,headers['x-frame-options'])
row_count = row_count + 1
end
workbook.write(report_file_base + '_headers.xlsx')
end
def text_report(report_file_base)
puts "starting report"
header_report_file = File.new(report_file_base + '.txt' , 'a+')
server_report_file = File.new(report_file_base + '-servers.txt','a+')
header_report_file.puts "Header Report"
header_report_file.puts "-------------\n"
sorted_headers = @headers.sort_by {|address,find| address.split('.').map{ |digits| digits.to_i}}
sorted_headers.each do |host, headers|
header_report_file.puts host
server_report_file.print host + ', '
header_report_file.puts "----------------"
headers.each do |key,val|
header_report_file.puts key + " : " + val
if key =~ /server/
server_report_file.print val
end
end
header_report_file.puts "\n\n------------\n\n"
server_report_file.print "\n"
end
end
def html_report
end
def rtf_report(report_file_base)
begin
require 'rtf'
require 'uri'
rescue LoadError
puts "RTF reports need the rtf gem, gem install 'rtf' should do it"
exit
end
document = RTF::Document.new(RTF::Font.new(RTF::Font::ROMAN, 'Arial'))
document.paragraph << "Web Server Headers"
methods = Hash.new
servers = Hash.new
sorted_headers = @headers.sort_by {|address,find| address.split('.').map{ |digits| digits.to_i}}
sorted_headers.each do |host, headers|
headers.each do |key, val|
if key.downcase == 'allow'
methods[host] = val
elsif key.downcase == 'server'
servers[host] = val
end
end
end
header_table = document.table(@headers.length + 1, 2,3000,5000)
header_table.border_width = 5
header_table[0][0] << 'IP Address'
header_table[0][1] << "Web Server Headers"
row = 1
sorted_headers = @headers.sort_by {|address,find| address.split('.').map{ |digits| digits.to_i}}
sorted_headers.each do |host, headers|
header_table[row][0] << host
headers.each do |key,val|
header_table[row][1] << key + ' : ' + val
header_table[row][1].line_break
end
row = row + 1
end
document.paragraph << "Web Server Methods"
methods_table = document.table(methods.length + 1, 2,3000,5000)
methods_table.border_width = 5
methods_table[0][0] << 'IP Address'
methods_table[0][1] << "Methods Supported"
mrow = 1
methods.each do |host, methods|
methods_table[mrow][0] << host
methods_table[mrow][1] << methods
mrow = mrow + 1
end
document.paragraph << "Web Server Software"
servers_table = document.table(servers.length + 1,2,3000,5000)
servers_table.border_width = 5
servers_table[0][0] << 'IP Address'
servers_table[0][1] << 'server header'
srow = 1
servers.each do |host, server|
servers_table[srow][0] << host
servers_table[srow][1] << server
srow = srow + 1
end
rtf_report_file = File.open(report_file_base + '.rtf', 'a+') do |file|
file.write(document.to_rtf)
end
end
end
if __FILE__ == $0
require 'optparse'
require 'ostruct'
options = OpenStruct.new
options.input_file = ''
options.report_file_base = 'rep_header_scan'
options.text_report = false
options.html_report = false
options.rtf_report = false
options.excel_report = false
opts = OptionParser.new do |opts|
opts.banner = "HTTP Scanner #{HTTPScan::VERSION}"
opts.on("-f", "--file [FILE]", "Input File with list of servers") do |file|
options.input_file = file
end
opts.on("--textReport", "Create a text Report") do |textrep|
options.text_report = true
end
opts.on("--htmlReport", "Create an HTML Report") do |htmlrep|
options.html_report = true
end
opts.on("--rtfReport", "Create an RTF Report") do |rtfrep|
options.rtf_report = true
end
opts.on("--excelReport", "Create an Excel Report") do |excelrep|
options.excel_report = true
end
opts.on("--reportPrefix [REPREF]", "Prefix for report files") do |repref|
options.report_file_base = repref
end
opts.on("-h", "--help", "-?", "--?", "Get Help") do |help|
puts opts
exit
end
opts.on("-v", "--version", "Get Version") do |ver|
puts "Web Header Tool #{HeaderScan::VERSION}"
exit
end
end
opts.parse!(ARGV)
unless options.input_file.length > 1
puts "You need to specify an input file name"
puts opts
exit
end
unless options.rtf_report || options.text_report || options.html_report || options.excel_report
puts "no reporting specified"
puts "you need to use one of --textReport, --htmlReport, --excelReport or --rtfReport"
puts opts
exit
end
begin
input_hosts = File.open(options.input_file, 'r').readlines
input_hosts.each {|host| host.chomp!}
rescue Exception => e
puts "couldn't open the file supplied, check permissions/spelling?"
puts e
exit
end
scan = HTTPScan.new(input_hosts)
scan.header_scan
if options.text_report
scan.text_report(options.report_file_base)
end
if options.html_report
scan.html_report
end
if options.rtf_report
scan.rtf_report(options.report_file_base)
end
if options.excel_report
scan.excel_report(options.report_file_base)
end
end