This file is indexed.

/usr/share/w3af/plugins/audit/remoteFileInclude.py is in w3af-console 1.0-rc3svn3489-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
'''
remoteFileInclude.py

Copyright 2006 Andres Riancho

This file is part of w3af, w3af.sourceforge.net .

w3af is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.

w3af is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with w3af; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

'''
from __future__ import with_statement

import core.controllers.outputManager as om

# options
from core.data.options.option import option
from core.data.options.optionList import optionList

from core.controllers.basePlugin.baseAuditPlugin import baseAuditPlugin
import core.data.parsers.urlParser as urlParser
from core.data.fuzzer.fuzzer import createMutants, createRandAlNum
from core.controllers.misc.homeDir import get_home_dir
from core.controllers.misc.get_local_ip import get_local_ip
from core.controllers.misc.is_private_site import is_private_site

import core.data.kb.knowledgeBase as kb
import core.data.kb.vuln as vuln
import core.data.constants.severity as severity

from core.controllers.w3afException import w3afException
from core.controllers.daemons.webserver import webserver
import core.data.constants.w3afPorts as w3afPorts

import os, time
import re

CONFIG_ERROR_MSG = 'audit.remoteFileInclude plugin has to be correctly configured to use.'
CONFIG_ERROR_MSG += ' Please set the local address and port, or use the official w3af site'
CONFIG_ERROR_MSG += ' as the target server for remote inclusions.'


class remoteFileInclude(baseAuditPlugin):
    '''
    Find remote file inclusion vulnerabilities.
    @author: Andres Riancho ( andres.riancho@gmail.com )
    '''

    def __init__(self):
        baseAuditPlugin.__init__(self)
        
        # Internal variables
        self._error_reported = False
        self._webserver = None
        
        # User configured parameters
        self._rfi_url = ''
        self._rfi_result = ''
        self._listen_port = w3afPorts.REMOTEFILEINCLUDE
        self._listen_address = get_local_ip() or ''
        self._use_w3af_site = True
        
    def audit(self, freq ):
        '''
        Tests an URL for remote file inclusion vulnerabilities.
        
        @param freq: A fuzzableRequest object
        '''
        # Sanity check
        if not self._correctly_configured():
            # Report error to the user only once
            self._error_reported = True
            raise w3afException(CONFIG_ERROR_MSG)
        
        if not self._error_reported:
            # The plugin is going to use two different techniques:
            # 1- create a request that will include a file from the w3af official site
            self._local_test_inclusion( freq )
            
            # 2- create a request that will include a file from a local web server        
            if self._use_w3af_site:
                self._w3af_site_test_inclusion( freq )
                
        self._tm.join( self )
    
    def _correctly_configured(self):
        '''
        @return: True if the plugin is correctly configured to run.
        '''
        if not self._use_w3af_site and self._listen_address == '':
            return False
        else:
            return True
    
    def _local_test_inclusion(self, freq):
        '''
        Check for RFI using a local web server
        
        @param freq: A fuzzableRequest object
        @return: None, everything is saved to the kb
        '''
        #
        #   The listen address is an empty string when I have no default route
        #
        #   Only work if:
        #       - The listen address is private and the target address is private
        #       - The listen address is public and the target address is public
        #
        if self._listen_address == '':
            return
            
        if (is_private_site(self._listen_address) and is_private_site(urlParser.getDomain(freq.getURL()))) or\
        (not is_private_site(self._listen_address) and not is_private_site(urlParser.getDomain(freq.getURL()))):
            om.out.debug( 'RFI test using local web server for URL: ' + freq.getURL() )
            om.out.debug('w3af is running a webserver')
            self._start_server()             
            
            # Perform the real work
            self._test_inclusion( freq )
                
            self._stop_server()
            
            # Wait for threads to finish
            self._tm.join( self )
            
    def _w3af_site_test_inclusion(self, freq):
        '''
        Check for RFI using the official w3af site.
        
        @param freq: A fuzzableRequest object
        @return: None, everything is saved to the kb
        '''        
        self._rfi_url = 'http://w3af.sourceforge.net/w3af/remoteFileInclude.html'
        self._rfi_result = 'w3af is goood!'
        # Perform the real work
        self._test_inclusion( freq )
        
    def _test_inclusion( self, freq ):
        '''
        Checks a fuzzableRequest for remote file inclusion bugs.
        
        @return: None
        '''
        oResponse = self._sendMutant( freq , analyze=False ).getBody()
        
        rfi_url_list = [ self._rfi_url,  ]
        mutants = createMutants( freq, rfi_url_list, oResponse=oResponse )
        
        for mutant in mutants:
            
            # Only spawn a thread if the mutant has a modified variable
            # that has no reported bugs in the kb
            if self._hasNoBug( 'remoteFileInclude' , 'remoteFileInclude',\
                                        mutant.getURL() , mutant.getVar() ):
                
                targs = (mutant,)
                self._tm.startFunction( target=self._sendMutant, args=targs , ownerObj=self )
                
    def _analyzeResult( self, mutant, response ):
        '''
        Analyze results of the _sendMutant method.
        '''
        #
        #   Only one thread at the time can enter here. This is because I want to report each
        #   vulnerability only once, and by only adding the "if self._hasNoBug" statement, that
        #   could not be done.
        #
        with self._plugin_lock:
            
            #
            #   I will only report the vulnerability once.
            #
            if self._hasNoBug( 'remoteFileInclude' , 'remoteFileInclude' ,\
                                        mutant.getURL() , mutant.getVar() ):
                
                if self._rfi_result in response:
                    v = vuln.vuln( mutant )
                    v.setId( response.id )
                    v.setSeverity(severity.HIGH)
                    v.setName( 'Remote file inclusion vulnerability' )
                    v.setDesc( 'Remote file inclusion was found at: ' + mutant.foundAt() )
                    kb.kb.append( self, 'remoteFileInclude', v )
                
                else:
                    #
                    #   Analyze some errors that indicate that there is a RFI but with some
                    #   "configuration problems"
                    #
                    rfi_errors = ['php_network_getaddresses: getaddrinfo',
                                        'failed to open stream: Connection refused in']
                    for error in rfi_errors:
                        if error in response and not error in mutant.getOriginalResponseBody():
                            v = vuln.vuln( mutant )
                            v.setId( response.id )
                            v.setSeverity(severity.MEDIUM)
                            v.addToHighlight(error)
                            v.setName( 'Remote file inclusion vulnerability' )
                            v.setDesc( 'Remote file inclusion was found at: ' + mutant.foundAt() )
                            kb.kb.append( self, 'remoteFileInclude', v )
    
    def end(self):
        '''
        This method is called when the plugin wont be used anymore.
        '''
        self._tm.join( self )
        self.printUniq( kb.kb.getData( 'remoteFileInclude', 'remoteFileInclude' ), 'VAR' )

    def _start_server(self):
        '''
        Starts a webserver for including files, and configure the parameters.
        '''
        # First, generate the php file to be included.
        rand1 = createRandAlNum( 9 )
        rand2 = createRandAlNum( 9 )
        filename = createRandAlNum()
        php_code = '<? \n echo "'
        php_code += rand1 + '";\n'
        php_code += ' echo "'
        php_code += rand2 + '";\n'
        php_code += ' ?>'
        
        # Write the php to the webroot
        file_handler = open( os.path.join( get_home_dir(), 'webroot', filename ) , 'w')
        file_handler.write( php_code )
        file_handler.close()
        
        # Define the required parameters
        self._rfi_url = 'http://' + self._listen_address +':' + str(self._listen_port)
        self._rfi_url += '/' + filename
        self._rfi_result = rand1 + rand2
        
        webroot = os.path.join(get_home_dir(), 'webroot')
        self._webserver = webserver( self._listen_address, self._listen_port , webroot )
        self._webserver.start2()
        time.sleep( 0.2 )
        
    def _stop_server( self ):
        '''
        Stop the server, remove the file from the webroot.
        '''
        if self._webserver != None:
            self._webserver.stop()
            # Remove the file
            filename = urlParser.getFileName(self._rfi_url)
            os.remove( os.path.join(get_home_dir(), 'webroot', filename ) )
            self._webserver = None

    def getOptions( self ):
        '''
        @return: A list of option objects for this plugin.
        '''
        d1 = 'IP address that the webserver will use to receive requests'
        h1 = 'w3af runs a webserver to serve the files to the target web application \
        when doing remote file inclusions. This setting configures where the webserver\
        is going to listen for requests.'
        o1 = option('listenAddress', self._listen_address, d1, 'string', help=h1)

        d2 = 'TCP port that the webserver will use to receive requests'
        o2 = option('listenPort', self._listen_port, d2, 'integer')

        d3 = 'Use w3af site to test for remote file inclusion'
        h3 =  'The plugin can use the w3af site to test for remote file inclusions, which is\
        convinient when you are performing a test behind a NAT firewall.'
        o3 = option('usew3afSite', self._use_w3af_site, d3, 'boolean',  help=h3)
        
        ol = optionList()
        ol.add(o1)
        ol.add(o2)
        ol.add(o3)
        return ol
        
    def setOptions( self, optionsMap ):
        '''
        This method sets all the options that are configured using the user interface 
        generated by the framework using the result of getOptions().
        
        @parameter optionsMap: A dictionary with the options for the plugin.
        @return: No value is returned.
        ''' 
        self._listen_address = optionsMap['listenAddress'].getValue()
        self._listen_port = optionsMap['listenPort'].getValue()
        self._use_w3af_site = optionsMap['usew3afSite'].getValue()
                
        if self._listen_address == '' and not self._use_w3af_site:
            om.out.error(CONFIG_ERROR_MSG)

    def getPluginDeps( self ):
        '''
        @return: A list with the names of the plugins that should be runned before the
        current one.
        '''
        return []
    
    def getLongDesc( self ):
        '''
        @return: A DETAILED description of the plugin functions and features.
        '''
        return '''
        This plugin finds remote file inclusion vulnerabilities.
        
        Three configurable parameters exist:
            - listenAddress
            - listenPort
            - usew3afSite
        
        There are two ways of running this plugin, one is the most common one, by using the w3af
        site ( w3af.sf.net ) as the place from where the target web application will fetch the
        remote file. The other way to test for inclusion is to run a webserver on the local machine
        that is performing the scan. The second option is configured using the "listenAddress" and
        "listenPort" parameters.
        '''