File: fingerprint_WAF.py

package info (click to toggle)
w3af 1.0-rc3svn3489-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 59,908 kB
  • ctags: 16,916
  • sloc: python: 136,990; xml: 63,472; sh: 153; ruby: 94; makefile: 40; asm: 35; jsp: 32; perl: 18; php: 5
file content (377 lines) | stat: -rw-r--r-- 15,396 bytes parent folder | download
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'''
fingerprint_WAF.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
from core.controllers.w3afException import w3afRunOnce
from core.data.fuzzer.fuzzer import createRandAlpha

import core.data.kb.knowledgeBase as kb
import core.data.kb.info as info

import re


class fingerprint_WAF(baseDiscoveryPlugin):
    '''
    Identify if a Web Application Firewall is present and if possible identify the vendor and version.
    
    @author: Andres Riancho ( andres.riancho@gmail.com )
    '''
    
    '''
    CHANGELOG:
    Feb/17/2009- Added Signatures by Aung Khant (aungkhant[at]yehg.net):
    - Old version F5 Traffic Shield, NetContinuum, TEROS, BinarySec
    '''
    
    def __init__(self):
        baseDiscoveryPlugin.__init__(self)
        
        # Internal variables
        self._run = True
        
    def discover(self, fuzzableRequest ):
        '''
        @parameter fuzzableRequest: A fuzzableRequest instance that contains
                                                     (among other things) the URL to test.
        '''
        if not self._run:
            # This will remove the plugin from the discovery plugins 
            # to be runned.
            raise w3afRunOnce()
        
        else:
            # I will only run this one time. All calls to fingerprint_WAF return 
            # the same url's ( none! )
            self._run = False
            
            self._fingerprint_URLScan( fuzzableRequest )
            self._fingerprint_ModSecurity( fuzzableRequest )
            self._fingerprint_SecureIIS( fuzzableRequest )
            self._fingerprint_Airlock( fuzzableRequest )
            self._fingerprint_Barracuda( fuzzableRequest )
            self._fingerprint_DenyAll( fuzzableRequest )
            self._fingerprint_F5ASM( fuzzableRequest )
            self._fingerprint_F5TrafficShield( fuzzableRequest )
            self._fingerprint_TEROS( fuzzableRequest )
            self._fingerprint_NetContinuum( fuzzableRequest )
            self._fingerprint_BinarySec( fuzzableRequest )
            self._fingerprint_HyperGuard( fuzzableRequest )

        return []
    
    def _fingerprint_SecureIIS(self,  fuzzableRequest):
        '''
        Try to verify if SecureIIS is installed or not.
        '''
        # And now a final check for SecureIIS
        headers = fuzzableRequest.getHeaders()
        headers['Transfer-Encoding'] = createRandAlpha(1024 + 1)
        try:
            lock_response2 = self._urlOpener.GET( fuzzableRequest.getURL(), 
                                                                    headers=headers, useCache=True )
        except w3afException, w3:
            om.out.debug('Failed to identify secure IIS, exception: ' + str(w3) )
        else:
            if lock_response2.getCode() == 404:
                self._report_finding('SecureIIS', lock_response2)
        
    def _fingerprint_ModSecurity(self,  fuzzableRequest):
        '''
        Try to verify if mod_security is installed or not AND try to get the installed version.
        '''
        pass

    def _fingerprint_Airlock(self,  fuzzableRequest):
        '''
        Try to verify if Airlock is present.
        '''
        om.out.debug( 'detect Airlock' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^AL[_-]?(SESS|LB)=', protected_by):
                        self._report_finding('Airlock', response, protected_by)
                        return
                # else 
                    # more checks, like path /error_path or encrypted URL in response

    def _fingerprint_Barracuda(self,  fuzzableRequest):
        '''
        Try to verify if Barracuda is present.
        '''
        om.out.debug( 'detect Barracuda' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    # ToDo: not sure if this is always there (08jul08 Achim)
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^barra_counter_session=', protected_by):
                        self._report_finding('Barracuda', protected_by)
                        return
                # else 
                    # don't know ...

    def _fingerprint_DenyAll(self,  fuzzableRequest):
        '''
        Try to verify if Deny All rWeb is present.
        '''
        om.out.debug( 'detect Deny All' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^sessioncookie=', protected_by):
                        self._report_finding('Deny All rWeb', response, protected_by)
                        return
                # else
                    # more checks like detection=detected cookie

    def _fingerprint_F5ASM(self,  fuzzableRequest):
        '''
        Try to verify if F5 ASM (also TrafficShield) is present.
        '''
        om.out.debug( 'detect F5 ASM or TrafficShield' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^TS[a-zA-Z0-9]{3,6}=', protected_by):
                        self._report_finding('F5 ASM', response, protected_by)
                        return
                # else
                    # more checks like special string in response

    def _fingerprint_F5TrafficShield(self,  fuzzableRequest):
        '''
        Try to verify if the older version F5 TrafficShield is present.
        Ref: Hacking Exposed - Web Application
        
        '''
        om.out.debug( 'detect the older version F5 TrafficShield' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^ASINFO=', protected_by):
                        self._report_finding('F5 TrafficShield', response, protected_by)
                        return
                # else
                    # more checks like special string in response
                    
    def _fingerprint_TEROS(self,  fuzzableRequest):
        '''
        Try to verify if TEROS is present.
        Ref: Hacking Exposed - Web Application
        
        '''
        om.out.debug( 'detect TEROS' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^st8id=', protected_by):
                        self._report_finding('TEROS', response, protected_by)
                        return
                # else
                    # more checks like special string in response
     
    def _fingerprint_NetContinuum(self,  fuzzableRequest):
        '''
        Try to verify if NetContinuum is present.
        Ref: Hacking Exposed - Web Application
        
        '''
        om.out.debug( 'detect NetContinuum' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^NCI__SessionId=', protected_by):
                        self._report_finding('NetContinuum', response, protected_by)
                        return
                # else
                    # more checks like special string in response
    
    def _fingerprint_BinarySec(self,  fuzzableRequest):
        '''
        Try to verify if BinarySec is present.
        '''
        om.out.debug( 'detect BinarySec' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'server':
                    protected_by = response.getHeaders()[header_name]                    
                    if re.match('BinarySec', protected_by, re.IGNORECASE):
                        self._report_finding('BinarySec', response, protected_by)
                        return
                # else
                    # more checks like special string in response

    
    def _fingerprint_HyperGuard(self,  fuzzableRequest):
        '''
        Try to verify if HyperGuard is present.
        '''
        om.out.debug( 'detect HyperGuard' )
        try:
            response = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        except KeyboardInterrupt,e:
            raise e
        else:
            for header_name in response.getHeaders().keys():
                if header_name.lower() == 'set-cookie':
                    protected_by = response.getHeaders()[header_name]
                    if re.match('^WODSESSION=', protected_by):
                        self._report_finding('HyperGuard', response, protected_by)
                        return
                # else
                    # more checks like special string in response

    def _fingerprint_URLScan(self,  fuzzableRequest):
        '''
        Try to verify if URLScan is installed or not.
        '''
        # detect using GET
        # Get the original response
        originalResponse = self._urlOpener.GET( fuzzableRequest.getURL(), useCache=True )
        if originalResponse.getCode() != 404:
            # Now add the if header and try again
            headers = fuzzableRequest.getHeaders()
            headers['If'] = createRandAlpha(8)
            if_response = self._urlOpener.GET( fuzzableRequest.getURL(), headers=headers,
                                                                useCache=True )
            headers = fuzzableRequest.getHeaders()
            headers['Translate'] = createRandAlpha(8)
            translate_response = self._urlOpener.GET( fuzzableRequest.getURL(), headers=headers, 
                                                                            useCache=True )
            
            headers = fuzzableRequest.getHeaders()
            headers['Lock-Token'] = createRandAlpha(8)
            lock_response = self._urlOpener.GET( fuzzableRequest.getURL(), headers=headers, 
                                                                    useCache=True )
            
            headers = fuzzableRequest.getHeaders()
            headers['Transfer-Encoding'] = createRandAlpha(8)
            transfer_enc_response = self._urlOpener.GET( fuzzableRequest.getURL(), 
                                                                                    headers=headers,
                                                                                    useCache=True )
        
            if if_response.getCode() == 404 or translate_response.getCode() == 404 or\
            lock_response.getCode() == 404 or transfer_enc_response.getCode() == 404:
                self._report_finding('URLScan', lock_response)

    
    def _report_finding( self, name, response, protected_by=None):
        '''
        Creates a information object based on the name and the response parameter and
        saves the data in the kb.
        
        @parameter name: The name of the WAF
        @parameter response: The HTTP response object that was used to identify the WAF
        @parameter protected_by: A more detailed description/version of the WAF
        '''
        i = info.info()
        i.setURL( response.getURL() )
        i.setId( response.id )
        msg = 'The remote web server seems to deploy a "'+name+'" WAF.'
        if protected_by:
            msg += ' The following is a detailed version of the WAF: "' + protected_by + '".'
        i.setDesc( msg )
        i.setName('Found '+name)
        kb.kb.append( self, name, i )
        om.out.information( i.getDesc() )

    def getOptions( self ):
        '''
        @return: A list of option objects for this plugin.
        '''    
        ol = optionList()
        return ol

    def setOptions( self, OptionList ):
        '''
        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 ['discovery.afd']

    def getLongDesc( self ):
        '''
        @return: A DETAILED description of the plugin functions and features.
        '''
        return '''
        Try to fingerprint the Web Application Firewall that is running on the remote end.
        
        Please note that the detection of the WAF is performed by the discovery.afd plugin ( afd stands
        for Active Filter Detection).
        '''