Class: Arachni::Module::HTTP

Inherits:
Object
  • Object
show all
Includes:
Arachni::UI::Output
Defined in:
lib/module/http.rb

Overview

Arachni::Module::HTTP class

Provides a simple HTTP interface for modules.

Exceptions

Any exceptions or session corruption is handled by the class.
Some are ignored, on others the HTTP session is refreshed.
Point is, you don’t need to worry about it.

@author: Anastasios “Zapotek” Laskos

                                     <tasos.laskos@gmail.com>
                                     <zapotek@segfault.gr>

@version: 0.1-pre

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Arachni::UI::Output

#debug!, #debug?, #only_positives!, #only_positives?, #print_debug, #print_debug_backtrace, #print_debug_pp, #print_error, #print_info, #print_line, #print_ok, #print_status, #print_verbose, #verbose!, #verbose?

Constructor Details

- (Net::HTTP) initialize(url, opts = {})

Initializes the HTTP session given a start URL respecting system wide settings for HTTP basic auth and proxy

Parameters:

  • (String) url

    start URL



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/module/http.rb', line 72

def initialize( url, opts = {} )
    @url = parse_url( url )

    @opts = Hash.new

    @opts = @opts.merge( opts )

    # create a new HTTP session
    refresh( )
    
    @trainers = []
    
    @init_headers = Hash.new
    @init_headers['user-agent'] = Options.instance.user_agent
    @init_headers['cookie']     = ''
end

Instance Attribute Details

The user supplied cookie jar

Returns:

  • (Hash)


55
56
57
# File 'lib/module/http.rb', line 55

def cookie_jar
  @cookie_jar
end

- (Hash) init_headers (readonly)

The headers with which the HTTP client is initialized
This is always kept updated.

Returns:

  • (Hash)


48
49
50
# File 'lib/module/http.rb', line 48

def init_headers
  @init_headers
end

- (Net::HTTP) session (readonly)

The HTTP session

Returns:

  • (Net::HTTP)


62
63
64
# File 'lib/module/http.rb', line 62

def session
  @session
end

- (URI) url (readonly)

The url of the session

Returns:

  • (URI)


40
41
42
# File 'lib/module/http.rb', line 40

def url
  @url
end

Class Method Details

+ (Hash) parse_cookiejar(cookie_jar)

Class method

Parses netscape HTTP cookie file

Parameters:

  • (String) cookie_jar

    the location of the cookie file

Returns:

  • (Hash)

    cookies in name=>value pairs



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/module/http.rb', line 292

def HTTP.parse_cookiejar( cookie_jar )
    
    cookies = Hash.new
    
    jar = File.open( cookie_jar, 'r' ) 
    jar.each_line {
        |line|
        
        # skip empty lines
        if (line = line.strip).size == 0 then next end
            
        # skip comment lines
        if line[0] == '#' then next end
            
        cookie_arr = line.split( "\t" )
        
        cookies[cookie_arr[-2]] = cookie_arr[-1]
    }
    
    cookies
end

Instance Method Details

- (Object) add_trainer(&block)

Blocks passed to this method will be passed each HTTP response
and in cases of redirection the new location as well.



329
330
331
# File 'lib/module/http.rb', line 329

def add_trainer( &block )
    @trainers << block
end

Gets a url with cookies and url variables

Parameters:

  • (URI) url

    URL to get

  • (Array<Hash<String, String>>) cookie_vars

    array of name=>value pairs

  • (Array<Hash<String, String>>) url_vars (defaults to: nil)

    array of name=>value pairs

Returns:

  • (HTTP::Response)


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
# File 'lib/module/http.rb', line 177

def cookie( url, cookie_vars, url_vars = nil)

    orig_cookiejar = @init_headers['cookie'].clone 
    
    cookies = Hash.new
    jar = parse_cookie_str( orig_cookiejar )
    
    cookie_vars.each_pair {
        |name, value|

        # don't audit cookies in the cookie jar                
#            next if Options.instance.exclude_cookies.include?( name )
        
        cookies[name] = value
    }
    
    cookies.reject {
        |cookie|
        Options.instance.exclude_cookies.include?( cookie['name'] )
    }
    
    set_cookies( jar.merge( cookies ) )
    
    # wrap the code in exception handling
    exception_jail {
        url = parse_url( url )
        
        if( url.query && url.query.size > 0 )
            query = '?' + url.query
            append = true
        else
            query = ''
            append = false
        end
        
        full_url = url.path + URI.encode( query ) + a_to_s( url_vars, append )
                    
        res = @session.get( full_url, @init_headers )
        @init_headers['cookie'] = orig_cookiejar.clone
        train( res )
        return res
    }
end

- (HTTP::Response) get(url, url_vars = {}, redirect = false)

Gets a URL passing the provided variables

Parameters:

  • (URI) url

    URL to get

  • (Array<Hash<String, String>>) url_vars (defaults to: {})

    array of name=>value pairs

Returns:

  • (HTTP::Response)


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
# File 'lib/module/http.rb', line 97

def get( url, url_vars = {}, redirect = false )
    url = parse_url( url )

    url_vars = {} if( !url_vars )
    
    url_vars = url_vars.merge( { '__arachni__' => '' } ) 
    #
    # the exception jail function wraps the block passed to it
    # in exception handling and runs it
    #
    # how cool is Ruby? Seriously....
    #
    exception_jail {

        if( url.query && url.query.size > 0 )
            query = '?' + url.query
            append = true
        else
            query = ''
            append = false
        end
        
        if( redirect )
            full_url = url.path + query
        else
            full_url = url.path + URI.encode( query ) + a_to_s( url_vars, append )
        end
        
        res = @session.get( full_url, @init_headers )
        
        # handle redirections
        if( ( redir = redirect?( res ) ).is_a?( String ) )
            res = get( redir, nil, true )
            train( res, redir )
        else
            train( res )
        end
        
        return res
    }
    
end

- (HTTP::Response) header(url, headers, url_vars = nil)

Gets a url with optional url variables and modified headers

Parameters:

  • (URI) url

    URL to get

  • (Hash<String, String>) headers

    hash of name=>value pairs

  • (Array<Hash<String, String>>) url_vars (defaults to: nil)

    array of name=>value pairs

Returns:

  • (HTTP::Response)


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
# File 'lib/module/http.rb', line 230

def header( url, headers, url_vars = nil)

    # wrap the code in exception handling
    exception_jail {
        url = parse_url( url )
        
        if( url.query && url.query.size > 0 )
            query = '?' + url.query
            append = true
        else
            query = ''
            append = false
        end
        
        full_url = url.path + URI.encode( query ) + a_to_s( url_vars, append )
        
        orig_headers  = @init_headers.clone
        @init_headers = @init_headers.merge( headers )
        
        res = @session.get( full_url, @init_headers )
        
        @init_headers = orig_headers.clone
        train( res )
        return res
    }

end


274
275
276
277
278
279
280
281
# File 'lib/module/http.rb', line 274

def parse_cookie_str( str )
    cookie_jar = Hash.new
    str.split( ';' ).each {
        |kvp|
        cookie_jar[kvp.split( "=" )[0]] = kvp.split( "=" )[1] 
    }
    return cookie_jar
end

- (URI) parse_url(url)

Encodes and parses a URL String

Parameters:

  • (String) url

    URL String

Returns:

  • (URI)

    URI object



321
322
323
# File 'lib/module/http.rb', line 321

def parse_url( url )
    URI.parse( URI.encode( url ) )
end

- (HTTP::Response) post(url, form_vars)

Posts a form to a URL with the provided variables

Parameters:

  • (URI) url

    URL to get

  • (Array<Hash<String, String>>) form_vars

    array of name=>value pairs

Returns:

  • (HTTP::Response)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/module/http.rb', line 148

def post( url, form_vars )

    req = Net::HTTP::Post.new( url, @init_headers )
    req.set_form_data( form_vars )

    exception_jail {
        res = @session.request( req )
        
        # handle redirections
        if( ( redir = redirect?( res ) ).is_a?( String ) )
            res =  get( redir, nil, true )
            train( res, redir )
        else
            train( res )
        end

        return res
    }
end

- (void) set_cookies(cookie_hash)

This method returns an undefined value.

Sets cookies for the HTTP session

Parameters:

  • (Hash) cookie_hash

    name=>value pair cookies



266
267
268
269
270
271
272
# File 'lib/module/http.rb', line 266

def set_cookies( cookie_hash )
    @init_headers['cookie'] = ''
    @cookie_jar = cookie_hash.each_pair {
        |name, value|
        @init_headers['cookie'] += "#{name}=#{value};" 
    }
end