'''
xssedDotCom.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

'''

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.baseDiscoveryPlugin import baseDiscoveryPlugin
from core.controllers.w3afException import w3afException, w3afRunOnce
import core.data.parsers.urlParser as urlParser

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

from extlib.BeautifulSoup import BeautifulSoup
import re, urllib2


class xssedDotCom(baseDiscoveryPlugin):
    '''
    Search in xssed.com to find xssed pages.
    
    @author: Nicolas Crocfer (shatter@shatter-blog.net)
    Fix: Set "." in front of the root domain to limit the search - Raul Siles
    '''    
    def __init__(self):
        baseDiscoveryPlugin.__init__(self)
        
        # Internal variables
        self._exec = True
        self._xssed_url = "http://www.xssed.com"
        self._fuzzable_requests_to_return = []
        
    def discover(self, fuzzableRequest ):
        '''
        Search in xssed.com and parse the output.
        
        @parameter fuzzableRequest: A fuzzableRequest instance that contains 
                                                    (among other things) the URL to test.
        '''

        if not self._exec :
            # This will remove the plugin from the discovery plugins to be runned.
            raise w3afRunOnce()
        else:
            # Only run once
            self._exec = False
                        
            target_domain = urlParser.getRootDomain( fuzzableRequest.getURL() )

            try:
                response = self._urlOpener.GET( self._xssed_url + "/search?key=." + target_domain )
            except w3afException, e:
                msg = 'An exception was raised while running xssedDotCom plugin. Exception: ' + str(e)
                om.out.debug( msg )
            
            try:
                return self._parse_xssed_result( response )
            except w3afException, e:
                self._exec = True
            	msg = 'An exception was raised while running xssedDotCom plugin. Exception: ' + str(e)
                om.out.debug( msg )

    def _decode_xssed_url(self, url):
        '''
        Replace the URL in the good format.
        
        @return: None
        '''
        return urllib2.unquote((((((url.replace('<br>', '').replace('</th>','')).replace('URL: ','')).replace('&lt;','<')).replace('&gt;','>')).replace('&quot;','\'')).replace('&amp;','&'))
    
    def _parse_xssed_result(self, response):
        '''
        Parse the result from the xssed site and create the corresponding info objects.
        
        @return: None
        '''
        resultat = response.getBody()

        # Work!
        if "<b>XSS:</b>" in response.getBody() :
            regex_many_vulns = re.findall("<a href='/mirror/.+</a> XSS vulnerability notified by", response.getBody())
            soup = BeautifulSoup(regex_many_vulns[0])
            soup_list = soup.findAll('a', target="_blank")
            for l in soup_list:
                response2 = self._urlOpener.GET( self._xssed_url+str(l['href']) )
                regex = re.findall("URL:.+", response2.getBody())
                v = vuln.vuln()
                v.setName('Previous xssed pages')
                v.setURL( str(l.contents[0]) )
                v.setSeverity( severity.MEDIUM )
                msg = 'Previous xssed page : '
                msg += self._decode_xssed_url(str(regex[0]))
                v.setDesc( msg )
                kb.kb.append( self, 'xss', v )
                om.out.information( v.getDesc() )
                
                fuzzable_requests = self._createFuzzableRequests( response2 )
                self._fuzzable_requests_to_return.extend( fuzzable_requests )
        else:
            om.out.debug('xssedDotCom did not find any xssed pages.')

        return self._fuzzable_requests_to_return

                
    def getOptions( self ):
        '''
        @return: A list of option objects for this plugin.
        '''    
        ol = optionList()
        return ol
        
    def setOptions( self, options ):
        '''
        This method sets all the options that are configured using the user interface 
        generated by the framework using the result of getOptions().
        
        @parameter OptionList: A dictionary with the options for the plugin.
        @return: No value is returned.
        ''' 
        pass
        
    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 '''
        This plugin searches the xssed.com database and parses the result. The information
        stored in that database is useful to know about previous xssed pages in the target website.
        '''
