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

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Utilities, Arachni::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, #remove_proto, #unescape, #unescape_hash

Methods included from Arachni::Utilities

#cookie_encode, #cookies_from_document, #cookies_from_file, #cookies_from_response, #exclude_path?, #extract_domain, #form_decode, #form_encode, #form_parse_request_body, #forms_from_document, #forms_from_response, #get_path, #hash_keys_to_str, #html_decode, #html_encode, #include_path?, #links_from_document, #links_from_response, #normalize_url, #page_from_response, #page_from_url, #parse_query, #parse_set_cookie, #parse_url_vars, #path_in_domain?, #path_too_deep?, #remove_constants, #seed, #skip_path?, #to_absolute, #uri_decode, #uri_encode, #uri_parse, #uri_parser, #url_sanitize

Class Method Details

.prep_thinObject



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
# File 'lib/arachni/ui/web/server.rb', line 1160

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



1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
# File 'lib/arachni/ui/web/server.rb', line 1138

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



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

def addons
    settings.addons
end

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



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/arachni/ui/web/server.rb', line 294

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)


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

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



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

def dispatcher_stats
    dispatchers.stats
end

#dispatchersObject



336
337
338
# File 'lib/arachni/ui/web/server.rb', line 336

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



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

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



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

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

#exception_jail(&block) ⇒ Object



355
356
357
358
359
360
361
# File 'lib/arachni/ui/web/server.rb', line 355

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



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

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



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

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



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

def instances
    settings.instances
end

#logObject



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

def log
    settings.log
end

#optionsObject



422
423
424
# File 'lib/arachni/ui/web/server.rb', line 422

def options
    Arachni::Options.instance
end

#prep_modules(params) ⇒ Object



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

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



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

def prep_opts( params )
    need_to_split = %w(exclude_cookies exclude_vectors exclude include)

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

        next if %w(_csrf modules plugins).include?( name ) || ( value.is_a?( String ) && value.empty?)

        value = true if value == 'on'

        if name == 'cookiejar' && value[:tempfile]
            cparams['cookies'] = {}
            cparams['cookie_string'] = ''
            Arachni::Element::Cookie.from_file( '', value[:tempfile] ).each do |c|
                cparams['cookies'][c.name] = c.value
                cparams['cookie_string'] += c.to_s + ';'
            end
        elsif name == 'extend_paths' && !value.is_a?( Array ) && value[:tempfile]
           cparams['extend_paths'] = Arachni::Options.instance.paths_from_file( value[:tempfile] )
        elsif name == 'restrict_paths' && !value.is_a?( Array ) && value[:tempfile]
           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' && value.is_a?( String )
            cparams[name] = {}
            value.split( "\r\n" ).each {
                |rule|
                regexp, counter = rule.split( ':', 2 )
                cparams[name][regexp] = counter
            }
        elsif name == 'custom_headers' && value.is_a?( String )
            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



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

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



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

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



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/arachni/ui/web/server.rb', line 311

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



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

def reports
    settings.reports
end

#save_and_shutdown(url, &block) ⇒ Object

Saves the report and shuts down the instance

Parameters:

  • url (String)

    of the instance



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

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



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

def scheduler
    settings.scheduler
end

#show(page, layout = true) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/arachni/ui/web/server.rb', line 363

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



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/arachni/ui/web/server.rb', line 383

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



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

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



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

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



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

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



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

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



429
430
431
432
433
434
435
436
437
# File 'lib/arachni/ui/web/server.rb', line 429

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



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

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

#welcomed?Boolean

Returns:

  • (Boolean)


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

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