forked from stephenfewer/grinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrinder.rb
More file actions
255 lines (198 loc) · 7.84 KB
/
Copy pathgrinder.rb
File metadata and controls
255 lines (198 loc) · 7.84 KB
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
#
# Copyright (c) 2012, Stephen Fewer of Harmony Security (www.harmonysecurity.com)
# Licensed under a 3 clause BSD license (Please see LICENSE.txt)
# Source code located at https://github.com/stephenfewer/grinder
#
$:.unshift( '.' )
require 'core/configuration'
require 'core/logging'
require 'digest/sha2'
class Grinder
BROWSER_CLASS_IE = '.\browser\internetexplorer.rb'
BROWSER_CLASS_CM = '.\browser\chrome.rb'
BROWSER_CLASS_FF = '.\browser\firefox.rb'
BROWSER_CLASS_SF = '.\browser\safari.rb'
#BROWSER_CLASS_OP = '.\browser\opera.rb'
def initialize( arguments )
@browser_type = nil
@browser_class = nil
@config_file = 'config'
@fuzzer = nil
@verbose = true
use_browser = lambda do | browser |
browser = browser.upcase
if( browser == 'IE' or browser == 'INTERNETEXPLORER' )
@browser_type = 'IE'
@browser_class = BROWSER_CLASS_IE
elsif( browser == 'CM' or browser == 'CHROME' )
@browser_type = 'CM'
@browser_class = BROWSER_CLASS_CM
elsif( browser == 'FF' or browser == 'FIREFOX' )
@browser_type = 'FF'
@browser_class = BROWSER_CLASS_FF
elsif( browser == 'SF' or browser == 'SAFARI' )
@browser_type = 'SF'
@browser_class = BROWSER_CLASS_SF
#elsif( browser == 'OP' or browser == 'OPERA' )
# @browser_type = 'OP'
# @browser_class = BROWSER_CLASS_OP
end
end
arguments.each do | arg |
if( arg.include?( '--quiet' ) )
@verbose = false
elsif( arg.include?( '--config=' ) )
@config_file = arg[9,arg.length]
elsif( arg.include?( '--fuzzer=' ) )
@fuzzer = arg[9,arg.length]
elsif( arg.include?( '--browser=' ) )
use_browser.call( arg[10,arg.length] )
else
use_browser.call( arg )
end
end
end
def systest
if( not config_test() )
return false
end
# this is kinda hacky but it should prevent people from trying to run the node incorectly.
if( not ::File.exist?( ".\\grinder.rb" ) )
print_error( "Error, you are not running this node from the \\grinder\\node\\ directory. You need to change the working directory (cd path\\to\\grinder\\node\\) and try again." )
return false
end
check_dll = lambda do | dllname, dlltitle |
root = "c:\\windows"
if( ENV.include?( 'SystemRoot' ) )
root = ENV[ 'SystemRoot' ]
end
sysdir = 'system32'
if( ::Dir.exist?( "#{root}\\syswow64\\" ) )
sysdir = 'syswow64'
end
dllpath = "#{root}\\#{sysdir}\\#{dllname}"
if( not ::File.exist?( dllpath ) )
begin
::File.open( ".\\data\\#{dllname}", 'rb' ) do | dll_src |
::File.open( dllpath, 'wb' ) do | dll_dst |
dll_dst.write( dll_src.read( dll_src.stat.size ) )
print_status( "Created the grinder #{dlltitle} DLL '#{dllpath}'." )
end
end
rescue
print_error( "Error, the grinder #{dlltitle} DLL '#{dllpath}' does not exist. Please manually copy this file from the \\grinder\\node\\data\\ directory." )
return false
end
end
::File.open( ".\\data\\#{dllname}", 'rb' ) do | dll_src |
::File.open( dllpath, 'rb' ) do | dll_dst |
gl1 = dll_src.read( dll_src.stat.size )
gl2 = dll_dst.read( dll_dst.stat.size )
if( ::Digest::SHA256.digest( gl1 ) != ::Digest::SHA256.digest( gl2 ) )
print_warning( "Warning, the grinder #{dlltitle} DLL '#{dllpath}' does not match the one from the \\grinder\\node\\data\\ directory. Please make sure you are using the latest one." )
end
end
end
return true
end
if( not check_dll.call( 'grinder_logger.dll', 'logger' ) )
return false
end
# sf: we are not using heaphook yet so this stays commented out for now.
#if( not check_dll.call( 'grinder_heaphook.dll', 'heap hook' ) )
# return false
#end
return true
end
def run
if( not config_init( @config_file ) )
print_error( "Failed to load the config file '#{@config_file}'." )
return false
end
print_status( "Using the config file '#{@config_file}'..." )
if( not systest() )
print_error( "System test failed! Please ensure this Grinder node is installed correctly." )
return false
end
if( not @browser_type or not @browser_class )
print_error( "No suitable browser was specified, unable to continue." )
return false
end
print_status( "Bringing up Grinder node '#{$grinder_node}'..." )
continue_pid = ::Process.spawn( ".\\data\\continue.exe" )
print_status( "Started the Grinder continue process #{continue_pid}" )
server_reset = 0
$server_pid = nil
$debugger_pid = nil
while( true )
kill_thread = nil
if( not $server_pid )
$server_pid = ::Process.spawn( "ruby -I. .\\core\\server.rb --config=#{@config_file} --browser=#{@browser_type} #{ ( @fuzzer ? '--fuzzer='+@fuzzer : '' ) } #{ ( not @verbose ? '--quiet' : '' ) }" )
sleep( 2 )
print_status( "Started the Grinder server process #{$server_pid}" )
server_reset = 12
end
$debugger_pid = ::Process.spawn( "ruby -I. #{@browser_class} --config=#{@config_file} --path=/grinder #{ ( not @verbose ? '--quiet' : '' ) }" )
print_status( "Started the Grinder debugger process #{$debugger_pid}" )
if( $debugger_pid and $debugger_restart_minutes )
# start a thread to wait N minutes before killing the debugger process. We do this so we start
# afresh exery N minutes. This help avoid running a target browser for hours without generating
# a crash and a browser memory leak gobbeling up a grinder nodes memory (or the browser being
# deadlocked for some reason).
kill_thread = ::Thread.new do
sleep( $debugger_restart_minutes * 60 )
print_status( "Killing the debugger process #{$debugger_pid} after #{$debugger_restart_minutes} minutes." )
begin
::Process.kill( "KILL", $debugger_pid )
rescue ::Errno::ESRCH, Errno::ECHILD
end
if( server_reset <= 0 )
# every X times kill/restart the web server as their seems to be a memory leak (Due to event handles on Windows 2008)...
print_status( "Killing the server process #{$server_pid}." )
begin
::Process.kill( "KILL", $server_pid )
::Process.wait( $server_pid )
rescue ::Errno::ESRCH, Errno::ECHILD
end
$server_pid = nil
else
server_reset -= 1
end
end
end
# block for the debugger to either exit due to a crash or to be killed by the above kill_thread
begin
::Process.wait( $debugger_pid )
rescue ::Errno::ESRCH, Errno::ECHILD
end
$debugger_pid = nil
# if the kill_thread is still alive by here we kill it
if( kill_thread and kill_thread.alive? )
kill_thread.kill
end
end
return true
end
end
if( $0 == __FILE__ )
verbose = true
if( ARGV.include?( '--help' ) or ARGV.include?( '-h' ) or ARGV.include?( '/h' ) or ARGV.include?( '/help' ) )
print_simple( "Usage: >ruby.exe grinder.rb [options] [browser]" )
print_simple( " --config=ConfigFile.rb Specify an alternative config file to use for this node." )
print_simple( " --fuzzer=FuzzerToUse Specify a single fuzzer to use with this node." )
print_simple( " --browser=BrowserToFuzz Specify the browser to fuzz (e.g. IE, CM, FF, SF)" )
print_simple( " --quiet Don't print any status/error messages to stdout" )
::Kernel.exit( true )
elsif( ARGV.include?( '--version' ) or ARGV.include?( '-v' ) or ARGV.include?( '/v' ) or ARGV.include?( '/version' ) )
print_simple( "Version #{$version_major}.#{$version_minor}#{$version_dev ? '-Dev' : '' }" )
::Kernel.exit( true )
elsif( ARGV.include?( '--quiet' ) or ARGV.include?( '-q' ) or ARGV.include?( '/q' ) or ARGV.include?( '/quiet' ) )
verbose = false
end
print_init( 'GRINDER', verbose )
print_status( "Starting at #{::Time.new.strftime( "%Y-%m-%d %H:%M:%S" )}" )
grinder = Grinder.new( ARGV )
success = grinder.run
print_status( "Finished at #{::Time.new.strftime( "%Y-%m-%d %H:%M:%S" )}" )
::Kernel::exit( success )
end