Class: Arachni::ComponentManager

Inherits:
Hash
  • Object
show all
Includes:
UI::Output
Defined in:
lib/arachni/component_manager.rb

Overview

Component Manager

Handles modules, reports, path extrator modules, plug-ins, pretty much every modular aspect of the framework.

It is usually extended to fill-in for system specific functionality.

@author: Tasos “Zapotek” Laskos

<[email protected]>
<[email protected]>

@version: 0.1

Constant Summary collapse

WILDCARD =

The following are used by #parse:

* '*' means all modules
* module names prefixed with '-' will be excluded
'*'
EXCLUDE =
'-'

Instance Method Summary collapse

Methods included from UI::Output

#buffer, #debug!, #debug?, #flush_buffer, #mute!, #muted?, #only_positives!, #only_positives?, #print_bad, #print_debug, #print_debug_backtrace, #print_debug_pp, #print_error, #print_error_backtrace, #print_info, #print_line, #print_ok, #print_status, #print_verbose, #reroute_to_file, #reroute_to_file?, #uncap_buffer!, #unmute!, #verbose!, #verbose?

Constructor Details

#initialize(lib, namespace) ⇒ ComponentManager

Returns a new instance of ComponentManager.

Parameters:

  • lib (String)

    the path to the component library/folder

  • namespace (Module)

    the namespace of the components



45
46
47
48
# File 'lib/arachni/component_manager.rb', line 45

def initialize( lib, namespace )
    @lib    = lib
    @namespace = namespace
end

Instance Method Details

#[](name) ⇒ Class

Returns a component class object by name, loading it on the fly need be.

Parameters:

  • name (String)

    component name

Returns:

  • (Class)


176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/arachni/component_manager.rb', line 176

def []( name )

    return fetch( name ) if include?( name )

    paths.each {
        |path|

        next if name != path_to_name( path )
        self[path_to_name( path )] = load_from_path( path )
    }

    return fetch( name ) rescue nil
end

#availableArray

Returns array of available component names.

Returns:



206
207
208
209
210
211
212
213
214
# File 'lib/arachni/component_manager.rb', line 206

def available
    components = []
    paths.each {
        |path|
        name = path_to_name( path )
        components << name
    }
    return components
end

#load(components) ⇒ Object

Loads components.

Parameters:

  • components (Array)

    array of names of components to load



55
56
57
58
59
60
# File 'lib/arachni/component_manager.rb', line 55

def load( components )
    parse( components ).each {
        |component|
        self.[]( component )
    }
end

#name_to_path(name) ⇒ String

Converts the name of a component to a file-path.

Parameters:

  • name (String)

    the name of the component

Returns:



223
224
225
226
227
228
229
# File 'lib/arachni/component_manager.rb', line 223

def name_to_path( name )
    paths.each {
        |path|
        return path if name == path_to_name( path )
    }
    return
end

#parse(components) ⇒ Array

It parses the component array making sure that its structure is valid

Parameters:

  • components (Array)

    array of component names

Returns:

  • (Array)

    array of modules to load



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
# File 'lib/arachni/component_manager.rb', line 117

def parse( components )
    unload = []
    load   = []

    return load if components[0] == EXCLUDE

    components.each {
        |component|
        if component[0] == EXCLUDE
            component[0] = ''

            if component[WILDCARD]
                unload |= wilcard_to_names( component )
            else
                unload << component
            end

        end
    }

    if( !components.include?( WILDCARD ) )

        avail_components  = available(  )

        components.each {
            |component|

            if component.substring?( WILDCARD )
                load |= wilcard_to_names( component )
            else

                if( avail_components.include?( component ) )
                    load << component
                else
                    raise( Arachni::Exceptions::ComponentNotFound,
                        "Error: Component #{component} wasn't found." )
                end
            end

        }
        load.flatten!

    else
        available(  ).map {
            |component|
            load << component
        }
    end

    return load - unload
end

#path_to_name(path) ⇒ String

Converts the path of a component to a component name.

Parameters:

  • path (String)

    the file-path of the component

Returns:



238
239
240
# File 'lib/arachni/component_manager.rb', line 238

def path_to_name( path )
    File.basename( path, '.rb' )
end

#pathsArray

Returns the paths of all available components (excluding helper files).

Returns:



247
248
249
250
# File 'lib/arachni/component_manager.rb', line 247

def paths
    cpaths = paths = Dir.glob( File.join( "#{@lib}**", "*.rb" ) )
    return paths.reject { |path| helper?( path ) }
end

#prep_opts(component_name, component, user_opts = {}) ⇒ Hash

Validates and prepares options for a given component.

Parameters:

  • component_name (String)

    the name of the component

  • component (Class)

    the component

  • user_opts (Hash) (defaults to: {})

    the user options

Returns:

  • (Hash)

    the prepared options to be passed to the component



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
# File 'lib/arachni/component_manager.rb', line 71

def prep_opts( component_name, component, user_opts = {} )
    info = component.info
    return {} if !info.include?( :options ) || info[:options].empty?

    user_opts ||= {}
    options = { }
    errors  = { }
    info[:options].each {
        |opt|

        name  = opt.name
        val   = user_opts[name] || opt.default

        if( opt.empty_required_value?( val ) )
            errors[name] = {
                :opt   => opt,
                :value => val,
                :type  => :empty_required_value
            }
        elsif( !opt.valid?( val ) )
            errors[name] = {
                :opt   => opt,
                :value => val,
                :type  => :invalid
            }
        end

        val = !val.nil? ? val : opt.default
        options[name] = opt.normalize( val )
    }

    if( !errors.empty? )
        print_errors( component_name, errors )
    end

    return options
end

#wilcard_to_names(name) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/arachni/component_manager.rb', line 190

def wilcard_to_names( name )
    if name[WILDCARD]
        return paths.map {
            |path|
            path_to_name( path ) if path.match( Regexp.new( name ) )
        }.compact
    end

    return
end