-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.rb
executable file
·438 lines (389 loc) · 9.14 KB
/
webserver.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
#!/usr/bin/env ruby
# Name: parsec webserver (Explorer Parser)
# Version: 0.1.9
# Release: 1
# License: CC-BA (Creative Commons By Attribution)
# http://creativecommons.org/licenses/by/4.0/legalcode
# Group: System
# Source: N/A
# URL: http://lateralblast.com.au/
# Distribution: UNIX
# Vendor: Lateral Blast
# Packager: Richard Spindler <richard@lateralblast.com.au>
# Description: Ruby script for processing explorer
# Load required gems
require 'rubygems'
require 'pathname'
require 'etc'
require 'date'
require 'socket'
def install_gem(gem_name)
puts "Information:\tInstalling #{gem_name}"
%x[gem install --no-document #{gem_name}]
Gem.clear_paths
return
end
begin
require 'sinatra'
rescue LoadError
install_gem("sinatra")
end
begin
require 'fileutils'
rescue LoadError
install_gem("fileutils")
end
begin
require 'hex_string'
rescue LoadError
install_gem("hex_string")
end
begin
require 'unpack'
rescue LoadError
install_gem("unpack")
end
begin
require 'enumerate'
rescue LoadError
install_gem("enumerate")
end
begin
require 'iconv'
rescue LoadError
install_gem("iconv")
end
begin
require 'bcrypt'
rescue LoadError
install_gem("bcrypt")
end
begin
require 'fileutils'
rescue LoadError
install_gem("fileutils")
end
# Some webserver defaults
default_bind = "127.0.0.1"
default_exceptions = false
default_port = "9494"
default_sessions = "true"
default_errors = "false"
enable_ssl = true
enable_auth = false
enable_upload = false
ssl_certificate = "ssl/cert.crt"
ssl_key = "ssl/pkey.pem"
$ssl_password = "123456"
# Get front end IP
frontend_ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
frontend_ip = frontend_ip.ip_address
# If we are on AWS set default bind to front-end IP
uname_r = %x[uname -r].chomp
if uname_r.match(/amzn/)
default_bind = frontend_ip
end
# Only allow uploads if we has authentication
if !enable_auth == true
enable_upload = false
end
set :port, default_port
set :bind, default_bind
set :sessions, default_sessions
set :dump_errors, default_errors
set :show_exceptions, default_exceptions
# Load methods
if Dir.exist?("./methods")
file_list = Dir.entries("./methods")
for file in file_list
if file =~ /rb$/
require "./methods/#{file}"
end
end
end
# SSL config
if enable_ssl == true
require 'webrick/ssl'
require 'webrick/https'
if !File.directory?($ssl_dir)
puts "Information: Creating "+$ssl_dir
Dir.mkdir($ssl_dir)
end
if !File.exist?(ssl_certificate) or !File.exist?(ssl_key)
%x[openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout #{ssl_key} -out #{ssl_certificate}]
end
set :ssl_certificate, ssl_certificate
set :ssl_key, ssl_key
module Sinatra
class Application
def self.run!
certificate_content = File.open(ssl_certificate).read
key_content = File.open(ssl_key).read
server_options = {
:Host => bind,
:Port => port,
:SSLEnable => true,
:SSLCertificate => OpenSSL::X509::Certificate.new(certificate_content),
:SSLPrivateKey => OpenSSL::PKey::RSA.new(key_content,$ssl_password)
}
Rack::Handler::WEBrick.run self, server_options do |server|
[:INT, :TERM].each { |sig| trap(sig) { server.stop } }
server.threaded = settings.threaded if server.respond_to? :threaded=
set :running, true
end
end
end
end
end
# htpasswd authentication
# Set up global files
$htpasswd_file = Dir.pwd+"/views/.htpasswd"
if enable_auth == true
module Sinatra
class Application
helpers do
def protect!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
passwd = File.open($htpasswd_file).read.split("\n").map {|credential| credential.split(':')}
if @auth.provided? && @auth.basic? && @auth.credentials
user, pass = @auth.credentials
auth = passwd.assoc(user)
crypt = BCrypt::Password.create(auth[1])
return false unless auth
[user, crypt] == auth
end
end
end
end
end
end
# setup config
check_local_config()
# Set global variables
# Set defaults
# Unlike the reporting script, these currently don't get auto detected
def set_global_vars()
$work_dir = "/tmp"
$output_format = "serverhtml"
$output_file = ""
$script_name = "Parsec"
$verbose = 0
$base_dir = Dir.pwd
$do_disks = 0
$exp_file_list = []
$masked = 0
$exp_file = ""
$exp_dir = $base_dir+"/explorers"
$work_dir = "/tmp"
end
set_global_vars()
before do
check_local_config()
set_global_vars()
end
# Enable uploads
if enable_upload == true
include FileUtils::Verbose
get '/upload' do
if enable_auth == true
protect!
end
head = File.readlines("./views/layout.html")
body = File.readlines("./views/upload.html")
array = head + body
array = array.join("\n")
"#{array}"
end
post '/upload' do
if enable_auth == true
protect!
end
tempfile = params[:file][:tempfile]
filename = params[:file][:filename]
if filename.match(/explorer/) and filename.match(/gz$/)
FileUtils.copy(tempfile.path, "#{$exp_dir}/#{filename}")
else
redirect '/help'
end
redirect '/list'
end
end
# handle error - redirect to help
error do
head = File.readlines("./views/layout.html")
body = File.readlines("./views/help.html")
array = head + body
array = array.join("\n")
"#{array}"
end
# handle /
get '/' do
head = File.readlines("./views/layout.html")
body = File.readlines("./views/help.html")
array = head + body
array = array.join("\n")
"#{array}"
end
# handle 404
not_found do
head = File.readlines("./views/layout.html")
body = File.readlines("./views/help.html")
array = head + body
array = array.join("\n")
"#{array}"
end
# List explorers
get '/list' do
$host_info = {}
$sys_config = {}
if enable_auth == true
protect!
end
if params['example']
$exp_dir = Dir.pwd+"/examples"
else
$exp_dir = Dir.pwd+"/explorers"
end
if params['masked']
if params['masked'].to_s.downcase.match(/true|1/)
$masked = 1
else
$masked = 0
end
else
$masked = 0
end
if params['report']
$report_type = params['report']
else
$report_type = "all"
end
if params['model']
search_model = params['model']
else
search_model = ""
end
if params['date']
search_date = params['date']
else
search_date = ""
end
if params['year']
search_year = params['year']
else
search_year = ""
end
if params['server']
search_name = params['server']
else
search_name = ""
end
if params['search']
search_param = params['search']
else
search_param = ""
end
if params['value']
search_value = params['value']
else
search_value = ""
end
head = File.readlines("./views/layout.html")
body = list_explorers(search_model,search_date,search_year,search_name,search_param,search_value)
array = head + body
array = array.join("\n")
"#{array}"
end
# Do report
get '/report' do
if enable_auth == true
protect!
end
if params['example']
$exp_dir = Dir.pwd+"/examples"
else
$exp_dir = Dir.pwd+"/explorers"
end
if params['masked']
if params['masked'].to_s.downcase.match(/true|1/)
$masked = 1
else
$masked = 0
end
else
$masked = 0
end
if params['report']
$report_type = params['report']
else
$report_type = "all"
end
if params['model']
$search_model = params['model']
else
$search_model = ""
end
if params['date']
$search_date = params['date']
else
$search_date = ""
end
if params['year']
$search_year = params['year']
else
$search_year = ""
end
if params['server']
$search_name = params['server']
else
$search_name = ""
end
if params['search']
$search_param = params['search']
else
$search_param = ""
end
if params['value']
$search_value = params['value']
else
$search_value = ""
end
$host_info = {}
$sys_config = {}
file_array = get_explorer_file_list($search_model,$search_date,$search_year,$search_name,$search_param,$search_value)
file_name = file_array[0]
$exp_file = file_name
layout = $base_dir+"/views/layout.html"
head = File.readlines(layout)
body = config_report($report_type,$search_name)
array = head + body
array = array.join("\n")
"#{array}"
end
# photos
get '/photos' do
if enable_auth == true
protect!
end
if params['image']
photo_file = params['image']
else
redirect '/help'
end
if !photo_file.match(/^[A-Z]|[a-z]/) and !photo_file.match(/jpg$|gif$|png$/)
redirect '/help'
end
photo_dir = $base_dir+"/photos"
image_file = photo_dir+"/"+photo_file
if File.exist?(image_file)
send_file(image_file)
else
redirect '/help'
end
end