Class: Arachni::UI::Web::Server

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Module::Utilities, Utilities
Defined in:
lib/arachni/ui/web/server.rb

Constant Summary collapse

HELPER_OWNER =

This will be used for the “owner” field of the helper instance

"WebUI helper"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#escape, #escape_hash, #parse_datetime, #port_to_url, #remove_proto, #unescape, #unescape_hash

Methods included from Module::Utilities

#get_path, #hash_keys_to_str, #normalize_url, #read_file, #seed, #uri_decode, #uri_encode, #uri_parse, #uri_parser, #url_sanitize

Class Method Details

.prep_thinObject



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'lib/arachni/ui/web/server.rb', line 1154

def self.prep_thin
    if @@conf['ssl']['server']['key']
        pkey = ::OpenSSL::PKey::RSA.new( File.read( @@conf['ssl']['server']['key'] ) )
    end

    if @@conf['ssl']['server']['cert']
        cert = ::OpenSSL::X509::Certificate.new( File.read( @@conf['ssl']['server']['cert'] ) )
    end

    if @@conf['ssl']['key'] || @@conf['ssl']['cert'] || @@conf['ssl']['ca']
        verification = true
    end

    return {
        :ssl        => true,
        :ssl_verify => verification,
        :ssl_cert_file  => cert,
        :ssl_key_file   => pkey,
    }
end

.run!(options = {}) ⇒ Object

override run! using this patch: github.com/sinatra/sinatra/pull/132



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/arachni/ui/web/server.rb', line 1132

def self.run!( options = {} )
    set options

    handler = detect_rack_handler
    handler_name = handler.name.gsub( /.*::/, '' )

    # handler specific options use the lower case handler name as hash key, if present
    handler_opts = options[handler_name.downcase.to_sym] || {}

    puts "== Sinatra/#{Sinatra::VERSION} has taken the stage " +
        "on #{port} for #{environment} with backup from #{handler_name}" unless handler_name =~/cgi/i

    handler.run self, handler_opts.merge( :Host => options[:host], :Port => options[:port] ) do
        |server|
        [ :INT, :TERM ].each { |sig| trap( sig ) { quit!( server, handler_name ) } }

        set :running, true
    end
rescue Errno::EADDRINUSE => e
    puts "== Someone is already performing on port #{port}!"
end

Instance Method Details

#addonsObject



320
321
322
# File 'lib/arachni/ui/web/server.rb', line 320

def addons
    settings.addons
end

#async_redirect(location, opts = {}) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/arachni/ui/web/server.rb', line 290

def async_redirect( location, opts = {} )
    response.status = 302

    if methods.include?( :current_addon ) && current_addon &&
        location != '/dispatchers/edit'
        location = current_addon.path_root + location
    end

    if ( flash = opts[:flash] ) && !flash.empty?
        location += "?#{flash.keys[0]}=#{URI.encode( flash.values[0] )}"
    end

    response.headers['Location'] = location

    body ''
end

#component_cache_filled?Boolean

Returns:

  • (Boolean)


542
543
544
545
546
547
548
# File 'lib/arachni/ui/web/server.rb', line 542

def component_cache_filled?
    begin
        return @@modules.size + @@plugins.size
    rescue
        return false
    end
end

#dispatcher_statsObject

Provides statistics about running jobs etc using the dispatcher



339
340
341
# File 'lib/arachni/ui/web/server.rb', line 339

def dispatcher_stats
    dispatchers.stats
end

#dispatchersObject



332
333
334
# File 'lib/arachni/ui/web/server.rb', line 332

def dispatchers
    settings.dispatchers
end

#ensure_dispatcherBool

Makes sure that we have a dispatcher, if not it redirects the user to an appropriate error page.

Returns:

  • (Bool)

    true if alive, redirect if not



628
629
630
631
632
633
634
635
636
637
# File 'lib/arachni/ui/web/server.rb', line 628

def ensure_dispatcher
    if dispatchers.all.empty?
        async_redirect '/dispatchers/edit'
    else
        dispatchers.first_alive {
            |dispatcher|
            async_redirect '/dispatchers/edit' if !dispatcher
        }
    end
end

#ensure_welcomedObject



413
414
415
416
# File 'lib/arachni/ui/web/server.rb', line 413

def ensure_welcomed
    return if welcomed?
    async_redirect '/welcome'
end

#exception_jail(&block) ⇒ Object



351
352
353
354
355
356
357
# File 'lib/arachni/ui/web/server.rb', line 351

def exception_jail( &block )
    # begin
        block.call
    # rescue Errno::ECONNREFUSED => e
    #     erb :error, { :layout => true }, :error => 'Remote server has been shut down.'
    # end
end

#fill_component_cache(&block) ⇒ Object



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
# File 'lib/arachni/ui/web/server.rb', line 550

def fill_component_cache( &block )
    if !component_cache_filled?

        helper_instance {
            |inst|

            if !inst
                block.call
            else

                inst.framework.lsmod {
                    |mods|

                    @@modules = mods.map { |mod| hash_keys_to_str( mod ) }

                    inst.framework.lsplug {
                        |plugs|

                        @@plugins = plugs.map { |plug| hash_keys_to_str( plug ) }

                        # shutdown the helper instance, we got what we wanted
                        inst.service.shutdown!{
                            block.call
                        }
                    }
                }
            end
        }

    else
        block.call
    end
end

#helper_instance(&block) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/arachni/ui/web/server.rb', line 519

def helper_instance( &block )
    raise( "This method requires a block!" ) if !block_given?

    dispatchers.first_alive {
        |dispatcher|
        if !dispatcher
            async_redirect '/dispatchers/edit'
        else
            dispatchers.connect( dispatcher.url ).dispatch( HELPER_OWNER ){
                |instance|

                if instance.rpc_exception?
                    log.webui_helper_instance_connect_failed( env, url )
                    next
                end

                @@arachni = instances.connect( instance['url'], session, instance['token'] )
                block.call( @@arachni )
            }
        end
    }
end

#instancesObject



347
348
349
# File 'lib/arachni/ui/web/server.rb', line 347

def instances
    settings.instances
end

#logObject



324
325
326
# File 'lib/arachni/ui/web/server.rb', line 324

def log
    settings.log
end

#optionsObject



418
419
420
# File 'lib/arachni/ui/web/server.rb', line 418

def options
    Arachni::Options.instance
end

#prep_modules(params) ⇒ Object



500
501
502
503
504
505
# File 'lib/arachni/ui/web/server.rb', line 500

def prep_modules( params )
    return ['-'] if !params['modules']
    mods = params['modules'].keys
    return ['*'] if mods.empty?
    return mods
end

#prep_opts(params) ⇒ Hash

Prepares form params to be used as options for RPC transmission

Parameters:

  • params (Hash)

Returns:

  • (Hash)

    normalized hash



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
# File 'lib/arachni/ui/web/server.rb', line 442

def prep_opts( params )

    need_to_split = [
        'exclude_cookies',
        'exclude',
        'include'
    ]

    cparams = {}
    params.each_pair {
        |name, value|

        next if [ '_csrf', 'modules', 'plugins' ].include?( name ) || ( value.is_a?( String ) && value.empty?)

        value = true if value == 'on'

        if name == 'cookiejar'
           cparams['cookies'] = Arachni::HTTP.parse_cookiejar( value[:tempfile] )
        elsif name == 'extend_paths'
           cparams['extend_paths'] = Arachni::Options.instance.paths_from_file( value[:tempfile] )
        elsif name == 'restrict_paths'
           cparams['restrict_paths'] = Arachni::Options.instance.paths_from_file( value[:tempfile] )
        elsif need_to_split.include?( name ) && value.is_a?( String )
            cparams[name] = value.split( "\r\n" )

        elsif name == 'redundant'
            cparams[name] = []
            value.split( "\r\n" ).each {
                |rule|
                regexp, counter = rule.split( ':', 2 )
                cparams[name] << {
                    'regexp'  => regexp,
                    'count'   => counter
                }
            }
        elsif name == 'custom_headers'
            cparams[name] = {}
            value.split( "\r\n" ).each {
                |line|
                header, val = line.to_s.split( /=/, 2 )
                cparams[name][header] = val
            }
        else
            cparams[name] = to_i( value )
        end
    }

    if !cparams['audit_links'] && !cparams['audit_forms'] &&
          !cparams['audit_cookies'] && !cparams['audit_headers']

        cparams['audit_links']   = true
        cparams['audit_forms']   = true
        cparams['audit_cookies'] = true
    end

    return cparams
end

#prep_plugins(params) ⇒ Object



507
508
509
510
511
512
513
514
515
516
517
# File 'lib/arachni/ui/web/server.rb', line 507

def prep_plugins( params )
    plugins  = {}

    return plugins if !params['plugins']
    params['plugins'].keys.each {
        |name|
        plugins[name] = params['options'][name] || {}
    }

    return plugins
end

#prep_session(skip_dispatcher = false) ⇒ Object

Makes sure that all systems are go and populates the session with default values



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
# File 'lib/arachni/ui/web/server.rb', line 587

def prep_session( skip_dispatcher = false )
    session[:flash] ||= {}

    ensure_dispatcher if !skip_dispatcher

    session['opts'] ||= {}
    session['opts']['settings'] ||= {
        'audit_links'    => true,
        'audit_forms'    => true,
        'audit_cookies'  => true,
        'http_req_limit' => 20,
        'user_agent'     => 'Arachni/' + Arachni::VERSION
    }
    session['opts']['modules'] ||= [ '*' ]


    require Arachni::Options.instance.dir['lib'] + 'framework'
    framework = Arachni::Framework.new( Arachni::Options.instance )
    plugins = Arachni::Plugin::Manager.new( framework )

    default_plugins = {}
    plugins.parse( Arachni::Plugin::Manager::DEFAULT ).each {
        |mod|
        default_plugins[mod] = {}
    }

    session['opts']['plugins'] ||= YAML::dump( default_plugins )

    #
    # Garbage collector, zombie killer. Reaps idle processes every 60 seconds.
    #
    @@zombie_reaper ||=
        ::EM.add_periodic_timer( 60 ){ ::EM.defer { shutdown_zombies } }
end

#redirect(location, opts = {}) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/arachni/ui/web/server.rb', line 307

def redirect( location, opts = {} )
    if methods.include?( :current_addon ) && current_addon
        location = current_addon.path_root + location
    end

    if ( flash = opts[:flash] ) && !flash.empty?
        location += "?#{flash.keys[0]}=#{URI.encode( flash.values[0] )}"
    end

    super( location )
end

#reportsObject



328
329
330
# File 'lib/arachni/ui/web/server.rb', line 328

def reports
    settings.reports
end

#save_and_shutdown(url, &block) ⇒ Object

Saves the report and shuts down the instance

Parameters:



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/arachni/ui/web/server.rb', line 644

def save_and_shutdown( url, &block )
    instance = instances.connect( url, session )
    instance.framework.clean_up!{
        |res|

        if !res.rpc_connection_error?
            instance.framework.auditstore {
                |auditstore|

                if !auditstore.rpc_connection_error?
                    log.webui_save_and_shutdown_auditstore_success( env, url )
                    report_path = reports.save( auditstore )
                    instance.service.shutdown!{ block.call( report_path ) }
                else
                    log.webui_save_and_shutdown_auditstore_failed( env, url )
                    block.call( auditstore )
                end
            }
        else
            log.webui_save_and_shutdown_clean_up_failed( env, url )
            block.call( res )
        end
    }
end

#schedulerObject



343
344
345
# File 'lib/arachni/ui/web/server.rb', line 343

def scheduler
    settings.scheduler
end

#show(page, layout = true) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/arachni/ui/web/server.rb', line 359

def show( page, layout = true )

    case page
        when :dispatchers
            ensure_dispatcher
            dispatcher.stats {
                |stats|
                erb :dispatchers
            }

        when :home
            dispatchers.stats {
                |stats|
                body erb page, { :layout => true }, :stats => stats
            }
        else
            erb page.to_sym, { :layout => layout }
    end
end

#show_dispatcher_line(stats) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/arachni/ui/web/server.rb', line 379

def show_dispatcher_line( stats )

    str = "#{escape( '@' + stats['node']['url'] )}" +
        " - #{stats['running_jobs'].size} running scans, "

    rss = 0
    mem = 0
    cpu = 0

    stats['running_jobs'].each {
        |job|
        rss += proc_mem( job['proc']['rss'] ).to_i
        mem += Float( job['proc']['pctmem'] ) if job['proc']['pctmem']
        cpu += Float( job['proc']['pctcpu'] ) if job['proc']['pctcpu']
    }
    str += rss.to_s + 'MB RAM usage '
    str += '(' + mem.to_s[0..4] + '%), '
    str += cpu.to_s[0..4] + '% CPU usage'
end

#show_dispatcher_node_line(stats) ⇒ Object



399
400
401
402
403
# File 'lib/arachni/ui/web/server.rb', line 399

def show_dispatcher_node_line( stats )
    str = "Nickname: #{stats['node']['nickname']} - "
    str += "Pipe ID: #{stats['node']['pipe_id']} - "
    str += "Weight: #{stats['node']['weight']}"
end

#show_error(title, message = '', backtrace = []) ⇒ Object



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
# File 'lib/arachni/ui/web/server.rb', line 716

def show_error( title, message = '', backtrace = [] )
    skip = [
        'rack.input',
        'rack.errors',
        'async.callback',
        'async.close',
        'rack.logger',
    ]

    err_env = {}
    env.each {
        |k, v|
        next if skip.include?( k )
        err_env[k] = v
    }

    err_env['rack.session.options'].delete( :coder )
    err_env['rack.session.options'].delete( :secure_random )

    err_env.merge!(
        :title     => title,
        :message   => message,
        :backtrace => backtrace.join( "\n" )
    )

    erb :error, { :layout => true },
        :title     => escape( title ),
        :message   => escape( message ).gsub( "\n", '<br />' ),
        :backtrace => escape( backtrace.join( "\n" ) ),
        :env       => escape( err_env.to_yaml )
end

#shutdown_all(url, &block) ⇒ Object

Kills all running instances



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'lib/arachni/ui/web/server.rb', line 672

def shutdown_all( url, &block )
    dispatchers.connect( url ).stats {
        |stats|
        log.dispatcher_global_shutdown( env, url )

        stats['running_jobs'].each {
            |instance|

            next if instance['helpers']['rank'] == 'slave'

            save_and_shutdown( instance['url'] ){
                log.instance_shutdown( env, instance['url'] )
            }
        }
        block.call
    }
end

#shutdown_zombiesInteger

Kills all idle instances

Returns:

  • (Integer)

    the number of reaped instances



695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/arachni/ui/web/server.rb', line 695

def shutdown_zombies
    dispatchers.jobs {
        |jobs|
        jobs.each {
            |job|
            next if job['helpers']['rank'] == 'slave' ||
                job['owner'] == HELPER_OWNER

            instances.connect( job['url'], session ).framework.busy? {
                |busy|

                if !busy.rpc_exception? && !busy
                    save_and_shutdown( job['url'] ){
                        log.webui_zombie_cleanup( env, job['url'] )
                    }
                end
            }
        }
    }
end

#to_i(str) ⇒ Object

Similar to String.to_i but it returns the original object if String is not a number



425
426
427
428
429
430
431
432
433
# File 'lib/arachni/ui/web/server.rb', line 425

def to_i( str )
    return str if !str.is_a?( String )

    if str.match( /\d+/ ).to_s.size == str.size
        return str.to_i
    else
        return str
    end
end

#welcomed!Object



409
410
411
# File 'lib/arachni/ui/web/server.rb', line 409

def welcomed!
    File.new( settings.db + '/welcomed', 'w' ).close
end

#welcomed?Boolean

Returns:

  • (Boolean)


405
406
407
# File 'lib/arachni/ui/web/server.rb', line 405

def welcomed?
    File.exist?( settings.db + '/welcomed' )
end