-
Notifications
You must be signed in to change notification settings - Fork 16
/
EyeWitness.rb
executable file
·1590 lines (1333 loc) · 54.8 KB
/
EyeWitness.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
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# This is a port of EyeWitness to Ruby, using a new screenshot engine
# This gem import checker is from Robin Woods (@digininja). Thanks for
# the help and showing me how you check for it. Works awesome :)
begin
require 'cgi'
require 'ipaddr'
require 'net/http'
require 'net/https'
require 'netaddr'
require 'nokogiri'
require 'optparse'
require 'ostruct'
require 'pp'
require 'selenium-webdriver'
require 'socket'
require 'timeout'
require 'uri'
require 'similar_text'
rescue LoadError => e
# Trying to catch errors and let user know which gem caused it
if e.to_s =~ /cannot load such file -- (.*)/
missing_gem = $1
puts "\nError: #{missing_gem} gem not installed\n"
puts "\t use: \"bundle install\" to install all required gems or \"gem install netaddr\" to install individually\n\n"
exit
else
puts "There was an error loading the gems:"
puts
puts e.to_s
exit
end
end
# Change timeout in Net::HTTP
module Net
class HTTP
alias old_initialize initialize
def initialize(*args)
old_initialize(*args)
@read_timeout = 10 # 10 seconds
end
end
end
class CliParser
def self.parse(args)
# Used for a hash-like data structure
options = OpenStruct.new
# Set the default values
options.file_name = nil
options.nessus_xml = nil
options.nmap_xml = nil
options.single_website = nil
options.create_targets = nil
options.skip_sort = false
options.timeout = 10
options.jitter = nil
options.dir_name = "none"
options.results_number = 25
options.ua_name = nil
options.localscan = nil
options.rid_dns = false
options.proxy_ip = nil
options.proxy_port = nil
options.redirection = nil
options.auth_user = nil
options.auth_pass = nil
# Check for config's file existance, and if present, read in its values
begin
File.open("eyewitness.config", "r") do |config_file|
config_file.each_line do |config_line|
if config_line.split("=")[0].downcase == "timeout"
options.timeout = config_line.split("=")[1].gsub('\n', '').to_i
elsif config_line.split("=")[0].downcase == "useragent"
options.ua_name = config_line.split("=")[1].gsub('\n', '')
elsif config_line.split("=")[0].downcase == "jitter"
options.jitter = config_line.split("=")[1].gsub('\n', '').to_i
elsif config_line.split("=")[0].downcase == "results"
options.results_number = config_line.split("=")[1].gsub('\n', '').to_i
elsif config_line.split("=")[0].downcase == "nodns"
options.rid_dns = config_line.split("=")[1].gsub('\n', '')
elsif config_line.split("=")[0].downcase == "proxy_ip"
options.proxy_ip = config_line.split("=")[1].gsub('\n', '')
elsif config_line.split("=")[0].downcase == "proxy_port"
options.proxy_port = config_line.split("=")[1].gsub('\n', '').to_i
elsif config_line.split("=")[0].downcase == "redirects"
options.redirection = config_line.split("=")[1].gsub('\n', '')
else
# Do nothing, since we don't care about anything else in the file
end # End if statement for reading key => values from the config file
end # End looping over each line
end # End of the file open
rescue Errno::ENOENT
# just do nothing, since no config file is present
end
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: [options]"
# Grouped for EyeWitness Input functions
opts.on("-f", "--filename file.txt", "File containing URLs to screenshot") do |in_filename|
options.file_name = in_filename
end
opts.on("--nessus file.nessus", "Nessus .nessus file output") do |nessus_xml|
options.nessus_xml = nessus_xml
end
opts.on("--nmap file.xml", "Nmap XML file output") do |nmap_xml|
options.nmap_xml = nmap_xml
end
opts.on("-s", "--single URL", "Single URL to screenshot") do |single_url|
options.single_website = single_url
end
opts.on("--skip-sort", "Do not group similar pages together") do |skip_sorter|
options.skip_sort = true
end
opts.on("--no-dns", "Parse nmap XML and only use IP address,",
"instead of DNS for web server") do |rid_dns|
options.rid_dns = true
end
opts.on("--createtargets Filename", "Create a file containing web servers",
"from nmap or nessus output.") do |target_make|
options.create_targets = target_make
end
opts.on("--redirects", "Show web redirections in the report.\n\n") do |redir|
options.redirection = true
end
# Authentication Settings for EyeWitness
opts.on("--username jsmith", "Username for basic/digest authentication.") do |the_user|
options.auth_user = the_user
end
opts.on("--password password123", "Password for basic/digest authentication.\n\n") do |the_pass|
options.auth_pass = the_pass
end
# Proxy Settings for EyeWitness
opts.on("--proxyip 127.0.0.1", "IP address of web proxy proxy.") do |prox_ip|
options.proxy_ip = prox_ip
end
opts.on("--proxyport 8080", Integer, "Port number of web proxy.\n\n") do |prox_port_num|
options.proxy_port = prox_port_num
end
# Timing options
opts.on("-t", "--timeout 7", Integer, "Maximum number of seconds to wait for",
"server headers (timeout of 10 seconds for screenshot).") do |max_timeout|
options.timeout = max_timeout
end
opts.on("--jitter 15", Integer, "Number of seconds to use as a base to",
"randomly deviate from when making requests.\n\n") do |jit_num|
options.jitter = jit_num
end
# Report output options
opts.on("-d Directory Name", "Name of directory for EyeWitness report.") do |d_name|
options.dir_name = d_name
end
opts.on("--results 25", Integer, "Number of URLs displayed per page within",
"the EyeWitness report.\n\n") do |res_num|
options.results_number = res_num
end
# Useragent Options
opts.on("--useragent Mozilla/4.0", "User agent to use when requesting all",
"websites with EyeWitness.") do |ua_string|
options.ua_name = ua_string
end
# Local Scanning Options
opts.on("--localscan 192.168.1.0/24", "CIDR notation of IP range to scan.\n\n")\
do |scan_range|
options.localscan = scan_range
end
# Show help and command line flags
opts.on_tail('-h', '--help', '-?', 'Show this message') do
puts opts
exit
end
end # end of opt_parser
begin
opt_parser.parse!(args)
if options.single_website.nil? && options.file_name.nil? && options.nessus_xml.nil? && options.nmap_xml.nil? && options.create_targets.nil? && options.localscan.nil?
puts "[*] Error: You need to provide EyeWitness a valid command!"
puts "[*] Error: Use --help to show usage options!\n\n"
exit
end # end if statement checking to make sure you gave eyewitness a valid command
if (!options.proxy_ip.nil? && options.proxy_port.nil?) || (options.proxy_ip.nil? && !options.proxy_port.nil?)
puts "[*] Error: When using a proxy, you must provide both the IP and port to use!"
puts "[*] Error: Please restart Eyewitness!\n\n"
exit
end # End if statement if using proxy and gave IP but not port, or vice versa
return options
rescue OptionParser::InvalidOption
puts "[*] Error: Invalid command line option provided!"
puts "[*] Error: Please restart EyeWitness!\n\n"
exit
end # End of try catch for invalid option
end # End of self.parse
end # End cli_parser class
class NessusParser < Nokogiri::XML::SAX::Document
def initialize
@system_name = nil
@port_number = nil
@service_name = nil
@plugin_name = nil
@get_text = false
@web_services = ['www', 'http?', 'https?']
@url_list = []
end
def start_element name, attrs = []
@attrs = attrs
# Get the IP or name of the system scanned
if name == "ReportHost"
@attrs.each do |key, value|
if key == "name"
@system_name = value
end
end
end
# Grab the port number, service name, and plugin name
if name == "ReportItem"
@attrs.each do |key, value|
if key == "port"
@port_number = value
end
if key == "svc_name"
@service_name = value
end
if key == "pluginName"
value = value.downcase
@web_services.each do |web_svc|
if (@service_name.include? web_svc and value.include? "service detection")
@plugin_name = value
else
@get_plug_out = false
end
end
end
end # End of Report Items iterator
end # End of Report Item If statement
if name == "plugin_output"
if !@plugin_name.nil?
@get_text = true
end
end
end # End of start_element function
def characters string
if @get_text and !string.empty?
@plugin_output = string.gsub('\n', '')
@get_text = false
end
end # End of characters function
def end_element name
if (name == "plugin_output" and !@plugin_output.nil?)
if (@plugin_output.include? 'TLS' or @plugin_output.include? 'SSL')
@final_url = "https://#{@system_name}:#{@port_number}"
if !@url_list.include? @final_url
@url_list << @final_url
end
else
@final_url = "http://#{@system_name}:#{@port_number}"
if !@url_list.include? @final_url
@url_list << @final_url
end
end
end
if name == "ReportItem"
@plugin_output = nil
@port_number = nil
@service_name = nil
@plugin_name = nil
@get_text = false
end
if name == "ReportHost"
@system_name = nil
end
end # End of end_element function
def url_get()
return @url_list
end
end # End of nessus parsing class
class NmapParser < Nokogiri::XML::SAX::Document
def initialize
@ip_address = nil
@hostname = nil
@potential_port = nil
@final_port_number = nil
@port_state = nil
@protocol = nil
@tunnel = nil
@final_url = nil
@url_array = []
end
def ip_only()
@nodns = true
end
def start_element name, attrs = []
@attrs = attrs
# Find IP addresses of all machines
if name == "address"
@attrs.each do |key, value|
if key == "addr"
if @ip_address == nil
@ip_address = value
end
end
end
end
if name == "hostname"
@hostname = nil
@attrs.each do |key, value|
if key == "name"
@hostname = value
end
end
end
if name == "port"
@attrs.each do |key, value|
if key == "portid"
@potential_port = value
end
end
end
# Find port state
if name == "state"
@attrs.each do |key, value|
if key == "state"
if value == "open"
@port_state = "open"
else
@port_state = "closed"
end
end
end
end
# Find port "name"
if name == "service"
@attrs.each do |key, value|
if key == "name"
if value.include? "https"
@protocol = "https://"
@final_port_number = @potential_port
elsif value.include? "http"
@protocol = "http://"
@final_port_number = @potential_port
# This is needed for port 8081
elsif value.include? "blackice"
@protocol = "http://"
@final_port_number = @potential_port
end
end
if key == "tunnel"
if value.include? "ssl"
@tunnel = "ssl"
end
end
end # end attrs iterator
if @protocol == "https://" || @tunnel == "ssl"
@protocol = "https://"
if @hostname.nil? && @port_state == "open" || @nodns == true
@final_url = "#{@protocol}#{@ip_address}:#{@final_port_number}"
if !@url_array.include? @final_url
@url_array << @final_url
else
end
elsif @port_state == "open"
@final_url = "#{@protocol}#{@hostname}:#{@final_port_number}"
if !@url_array.include? @final_url
@url_array << @final_url
else
@final_url = "#{@protocol}#{@ip_address}:#{@final_port_number}"
if !@url_array.include? @final_url
@url_array << @final_url
else
end
end
else
end
elsif @protocol == "http://"
if @hostname.nil? && @port_state == "open" || @nodns == true
@final_url = "#{@protocol}#{@ip_address}:#{@final_port_number}"
if !@url_array.include? @final_url
@url_array << @final_url
else
end
elsif @port_state == "open"
@final_url = "#{@protocol}#{@hostname}:#{@final_port_number}"
if !@url_array.include? @final_url
@url_array << @final_url
else
@final_url = "#{@protocol}#{@ip_address}:#{@final_port_number}"
if !@url_array.include? @final_url
@url_array << @final_url
else
end
end
else
end #End of if statement printing valid servers
end # End if statement looking at protocol and tunnel
end # End of if statement for the element starting with the name "service"
end # End of start_element function
def end_element name
if name == "host"
@ip_address = nil
@hostname = nil
end
if name == "service"
@potential_port = nil
@final_port_number = nil
@port_state = nil
@protocol = nil
@tunnel = nil
@final_url = nil
end
end # End of end_element function
def url_get()
return @url_array
end
end # End of nmap parsing class
def capture_screenshot(sel_driver, output_path, url_to_grab, cli_object)
# do a "try catch" for timeout issues
begin
# Function used to capture screenshots with selenium
if ((!cli_object.auth_user.nil?) and (!cli_object.auth_pass.nil?))
orig_name = url_to_grab
url_to_grab = url_to_grab.split('://')[0] + "://" + cli_object.auth_user + ":" + cli_object.auth_pass + "@" + url_to_grab.split('://')[1]
else
end
sel_driver.get url_to_grab
# Rename for reporting purposes. This isn't efficient, but then again, this app could be done better
# I'll want to likely refactor the entire source code and work on making it more efficient at some point.
if ((!cli_object.auth_user.nil?) and (!cli_object.auth_pass.nil?))
url_to_grab = orig_name
end
screenshot_name = url_to_grab.gsub('://', '.').gsub('/', '.').gsub(':', '.')
sourcecode_name = "#{screenshot_name}.txt"
screenshot_name = "#{screenshot_name}.png"
screen_cap_path = File.join(output_path, 'screens', screenshot_name)
source_code_path = File.join(output_path, 'source', sourcecode_name)
sel_driver.save_screenshot(screen_cap_path)
File.open("#{source_code_path}", 'w') do |write_sourcecode|
write_sourcecode.write(sel_driver.page_source)
end
title_tag = sel_driver.title
# Try to close pop up boxes here
popup = sel_driver.switch_to.alert
popup.dismiss
return sel_driver.page_source, title_tag, source_code_path
rescue Timeout::Error
puts "[*] Error: Request Timed out for screenshot..."
blank_page_source = "TIMEOUTERROR"
no_title = "Timeout Error"
return blank_page_source, no_title, source_code_path
rescue Errno::ECONNREFUSED
blank_page_source = "CONNREFUSED"
no_title = "Connection Refused or URL Skipped"
return blank_page_source, no_title, source_code_path
rescue Selenium::WebDriver::Error::UnknownError
blank_page_source = "POSSIBLEXML"
no_title = "Bad response, or possible XML"
return blank_page_source, no_title, source_code_path
rescue Selenium::WebDriver::Error::UnhandledAlertError
blank_page_source = "POSSIBLEXML"
no_title = "Bad response, or possible XML"
return blank_page_source, no_title, source_code_path
rescue Selenium::WebDriver::Error::NoAlertPresentError
return sel_driver.page_source, title_tag, source_code_path
rescue NameError
blank_page_source = "POSSIBLEXML"
no_title = "Bad response, or possible XML"
return blank_page_source, no_title, source_code_path
rescue
blank_page_source = "UNKNOWNERROR"
no_title = "Unknown error when connecting to web server"
return blank_page_source, no_title, source_code_path
end
end
def default_creds(source_code_path, full_file_path)
# This function parses the signatures file, and compares it with the source code
# of the site connected to, and determines if there is a match
creds_path = File.join("#{full_file_path}", "signatures.txt")
# Create the blank variable which will store the web page's source code
page_content = ''
begin
# Open the page, and read the source code into the variable
File.open("#{source_code_path}", "r") do |source_code|
source_code.each_line do |source_line|
page_content += source_line
end
end
rescue Errno::ENOENT
puts "[*] WARNING source code file not found!"
puts "[*] Skipping credential check..."
return nil
end # End try catch
begin
File.open("#{creds_path}", "r") do |signature_file|
signature_file.each_line do |signature|
signature_delimeted = signature.split('|')[0]
default_creds = signature.split('|')[1]
# Values for signatures not found
all_signatures = signature_delimeted.split(';')
page_content = page_content.downcase
signature_not_present = false
all_signatures.each do |individual_signature|
individual_signature = individual_signature.downcase
if !page_content.include? "#{individual_signature}"
signature_not_present = true
end
end
if signature_not_present
else
return default_creds
end
end
end
rescue Errno::ENOENT
puts "[*] WARNING Default credentials file not in same directory as EyeWitness!"
puts "[*] Skipping credential check..."
end # End try catch
return nil
end #End of default creds function
def fetch(uri_str, url_list, limit = 10)
# This checks up to 10 redirects. If it keeps going further, change the limit value
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
uri = URI.parse(uri_str)
if uri_str.start_with?("http://")
# code came from - http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
elsif uri_str.start_with?("https://")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
end
response = http.request(request)
case response
when Net::HTTPSuccess
url_list.push("<b>#{uri_str}</b> <- Final URL<br>")
when Net::HTTPRedirection
url_list.push("<b>#{uri_str}</b> redirects to...<br>")
uri = URI.parse(uri_str)
base_url = "#{uri.scheme}://#{uri.host}"
new_url = URI.parse(response.header['location'])
if (new_url.relative?)
new_url = base_url + response.header['location']
fetch(new_url, url_list, limit - 1)
else
fetch(response['location'], url_list, limit - 1)
end
else
response.error!
end
end
def file_names(url_given)
# Create the names of the screenshot and source code file used in the report
url_given.gsub('\n', '')
pic_name = url_given
source_name = url_given
source_name = source_name.gsub('://', '.').gsub('/', '.').gsub(':', '.')
pic_name = "#{source_name}.png"
source_name = "#{source_name}.txt"
return url_given, source_name, pic_name
end # End of file_names function
def folder_out(dir_name, full_path)
# Create the CSS file for the report, and remove the extra 4 spaces
css_file = 'img {
max-width: 100%;
height: auto;
}
#screenshot{
overflow: auto;
max-width: 850px;
max-height: 550px;
}'.gsub(' ', '')
# Check to see if the directory name is null or not
if dir_name == "none"
# Get the Time and Date for creating the folder structure
date_time = Time.new
current_date = "#{date_time.month}#{date_time.day}#{date_time.year}"
current_time = "#{date_time.hour}#{date_time.min}#{date_time.sec}"
output_folder_name = "#{current_date}_#{current_time}"
else
output_folder_name = dir_name
end
output_folder_name = File.join(output_folder_name)
# Check to see if output folder starts with C:\ or /
if (output_folder_name.start_with?("C:\\") or output_folder_name.start_with?("/"))
if File.directory?(output_folder_name)
puts "[*] ERROR: Folder specified already exists!"
puts "[*] ERROR: Please provide a new directory to write to!"
exit
else
# Create the paths for making the directories (valid for Win and nix)
source_out_folder_name = File.join("#{output_folder_name}", "source")
screen_out_folder_name = File.join("#{output_folder_name}", "screens")
end
else
output_folder_name = File.join(full_path, output_folder_name)
source_out_folder_name = File.join("#{output_folder_name}", "source")
screen_out_folder_name = File.join("#{output_folder_name}", "screens")
end
# Actually create the directories now
Dir.mkdir(output_folder_name)
Dir.mkdir(source_out_folder_name)
Dir.mkdir(screen_out_folder_name)
# Write the css file out
File.open("#{output_folder_name}/style.css", 'w') do |stylesheet|
stylesheet.puts css_file
end
# Get the time for the top of the report
date_time = Time.new
current_date = "#{date_time.month}/#{date_time.day}/#{date_time.year}"
current_time = "#{date_time.hour}:#{date_time.min}:#{date_time.sec}"
return output_folder_name, current_date, current_time
end # End of folder_out function
def html_encode(dangerous_data)
# html encode data so we can't execute malicious scripts
encoded = CGI::escapeHTML(dangerous_data)
return encoded
end
def logistics(url_file)
# This is basically a single function designed to parse a text file
# and verify that each url starts with http or https
file_urls = []
num_urls = 0
begin
File.open(url_file, "r").each do |url|
if url == "" || url == "\n" || url == "\r"
else
url = url.strip
if !url.start_with?('http://') && !url.start_with?('https://')
url = "http://#{url}"
end
file_urls << url
num_urls += 1
end
end
rescue Errno::ENOENT
puts "[*] Error: File not valid, or not found."
puts "[*] Error: Please rerun and provide a valid file!"
abort
rescue Errno::EISDIR
puts "[*] Error: You provided a directory instead of a file!"
puts "[*] Error: Please rerun and provide a valid input file containing URLS!\n\n"
abort
end
return file_urls, num_urls
end # End of logistics function
def page_tracker(number_urls, max_links_per_page, total_num_pages, report_table_data, out_rep_folder, day_of_report, time_of_report)
# Used to track the number of pages that is needed
if number_urls == max_links_per_page
if total_num_pages == 1
# Close out the html and write it to disk
report_table_data += "</table>\n"
# Get path to where the report will be written, and write it out
report_html = File.join(out_rep_folder, "report.html")
File.open(report_html, 'w') do |first_report_page|
first_report_page.write(report_table_data)
end # End of report writeout
total_num_pages += 1
report_table_data = web_report_header(day_of_report, time_of_report)
number_urls = 0
else
report_table_data += "</table>\n"
multi_page_reporthtml = File.join(out_rep_folder, "report_page#{total_num_pages}.html")
File.open(multi_page_reporthtml, 'w') do |report_page_out|
report_page_out.write(report_table_data)
end
#Reset URL counter
total_num_pages += 1
report_table_data = web_report_header(day_of_report, time_of_report)
number_urls = 0
end # End of page counter if statement
end # End if statement if page url counter matches max per page
return number_urls, total_num_pages, report_table_data
end # End of page_tracker function
def request_comparison(original_content, new_content, max_difference)
# compares the two requests and determines if it is above the "threshold"
original_request_length = original_content.length
new_request_length = new_content.length
if new_request_length > original_request_length
a, b = new_request_length, orig_request_length
total_difference = a - b
if total_difference > max_difference
return false, total_difference
else
return true, nil
end
else
total_difference = orig_request_length - new_request_length
if total_difference > max_difference
return False, total_difference
else
return True, "None"
end
end # End of if statement determing size of requests and performing math
end # end of request comparison function
def scanner(cidr_range, tool_path)
# Used to port scan a provided cidr range
ports = [80, 443, 8080, 8443]
# Live webservers
live_webservers = []
# port scanning code taken from
# http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open
port_open = false
# Create scanner output path
scanner_output_path = File.join("#{tool_path}", "scanneroutput.txt")
net1 = NetAddr::CIDR.create(cidr_range)
begin
net1.enumerate.each do |scan_ip|
ports.each do |scan_port|
begin
Timeout.timeout(5) do
begin
puts "[*] Attempting to connect to #{scan_ip}:#{scan_port}..."
s = TCPSocket.new(scan_ip, scan_port)
s.close
# Determine if we need to put http or https in front based off of port number
if scan_port == 443 or scan_port == 8443
live_webservers << "https://#{scan_ip}:#{scan_port}"
puts "[*][*] #{scan_ip} looks to be listening on #{scan_port}."
else
live_webservers << "http://#{scan_ip}:#{scan_port}"
puts "[*][*] #{scan_ip} looks to be listening on #{scan_port}."
end
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
puts "[*] Error: Unable to connect to host or network (#{scan_ip}:#{scan_port})"
end
end
rescue Timeout::Error
end
end # End port iterator
end # End of ip iterator
rescue Interrupt
puts "[*][*] You just rage quit the scanner!"
server_out_file = File.join(tool_path, "scanneroutput.txt")
File.open(server_out_file, 'w') do |srv_out|
live_webservers.each do |web_srv|
srv_out.write("#{web_srv}\n")
end
end
puts "[*][*] Wrote out the results to scanneroutput.txt"
puts "[*][*] The pool on the roof must have a leak..."
end
server_out_file = File.join(tool_path, "scanneroutput.txt")
File.open(server_out_file, 'w') do |srv_out|
live_webservers.each do |web_srv|
srv_out.write("#{web_srv}\n")
end
end
end # End of scanner function
def selenium_driver(possible_user_agent, possible_proxy_ip, possible_proxy_port)
# Other drivers are available as well
#http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver.html#for-class_method
profile = Selenium::WebDriver::Firefox::Profile.new
if !possible_user_agent.nil?
profile['general.useragent.override'] = "#{possible_user_agent}"
end
if !possible_proxy_ip.nil? && !possible_proxy_port.nil?
profile['network.proxy.type'] = 1
profile['network.proxy.http'] = possible_proxy_ip
profile['network.proxy.http_port'] = possible_proxy_port
profile['network.proxy.ssl'] = possible_proxy_ip
profile['network.proxy.ssl_port'] = possible_proxy_port
end
driver = Selenium::WebDriver.for :firefox, :profile => profile
return driver
end
def single_page_report(report_source, full_report_path)
# The end of the html for a single paged report
report_source += "</table>\n</body>\n</html>"
report_file = File.join(full_report_path, "report.html")
File.open(report_file, 'w') do |report_done|
report_done.write(report_source)
end
end # End single page report function
def source_header_grab(url_to_head, total_timeout, trace_redirect)
invalid_ssl = false
# All of this code basically grabs the server headers and source code of the
# provided URL
# Code for timeout - http://stackoverflow.com/questions/8014291/making-http-head-request-with-timeout-in-ruby
uri = URI.parse("#{url_to_head}")
if url_to_head.start_with?("http://")
# code came from - http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = total_timeout
request = Net::HTTP::Get.new(uri.request_uri)
elsif url_to_head.start_with?("https://")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.read_timeout = total_timeout
request = Net::HTTP::Get.new(uri.request_uri)
else
puts "[*] Error: Error with URL, please investigate!"
exit
end # end if statement for starting with http:// or https://
if trace_redirect
# Array containing redirected urls
all_redirects = []
fetch(url_to_head, all_redirects)
end # End trace redirect if statement
begin
# actually make the request
response = http.request(request)
rescue OpenSSL::SSL::SSLError
invalid_ssl = true
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
begin
response = http.request(request)
rescue OpenSSL::SSL::SSLError
puts "[*] Error: SSL Error connecting to #{url_to_head}"
response = "SSLERROR"
rescue Errno::ECONNRESET
response = "CONNECTIONDENIED"
end
rescue Timeout::Error
response = "TIMEDOUT"
rescue Errno::ECONNREFUSED
response = "CONNECTIONDENIED"
rescue Errno::ECONNRESET
response = "CONNECTIONDENIED"
rescue SocketError
response = "BADURL"
rescue
response = "UNKNOWNERROR"
end
return response, invalid_ssl, all_redirects
end # End header_grab function
def table_maker(web_report_html, website_url, possible_creds, page_header_source, source_code_name,
screenshot_name, length_difference, iwitness_dir, output_report_path, potential_blank, bad_ssl,
page_title, redirect_list, check_redirs)
web_report_html += "<tr>\n<td><div style=\"display: inline-block; width: 300px; word-wrap:break-word\">\n"
web_report_html += "<a href=\"#{website_url}\" target=\"_blank\">#{website_url}</a><br>"
# If there's any creds identified by EyeWitness, add them to the report
if !possible_creds.nil?
encoded_creds = html_encode(possible_creds)
web_report_html += "<br><b>Default credentials:</b> #{encoded_creds} <br>"
end
screenshot_path = File.join(output_report_path, "screens", screenshot_name)
# If EyeWitness encountered any of the identified errors, add it to the report
if page_header_source == "CONNECTIONDENIED"
web_report_html += "CONNECTION REFUSED FROM SERVER!</div></td><td> Connection Refused from server!</td></tr>"
elsif page_header_source == "TIMEDOUT"
web_report_html += "Connection to web server timed out!</div></td><td> Connection to web server timed out!</td></tr>"
elsif page_header_source == "UNKNOWNERROR"
web_report_html += "Unknown error when connecting to web server!</div></td><td> Unknown error when connecting to web server. Please contact developer and give him details (like the URL) to investigate this!</td></tr>"
elsif page_header_source == "BADURL"
web_report_html += "Potentially unable to resolve URL!</div></td><td> Potentially unable to resolve URL!</td></tr>"
elsif page_header_source == "SSLERROR"
web_report_html += "SSL Error when connecting to website! </div></td><td> SSL Error when connecting to website!</td></tr>"
else
full_source_path = File.join(output_report_path, "source", source_code_name)
encoded_title_header = html_encode("Page Title")
begin