<s> import re <EOL> from burst . color import * <EOL> try : <EOL> import lxml . html <EOL> has_lxml = True <EOL> except ImportError : <EOL> has_lxml = False <EOL> class NullAlerter : <EOL> def analyse_request ( self , r ) : <EOL> return [ ] <EOL> def analyse_response ( self , r ) : <EOL> return [ ] <EOL> class GenericAlerter : <EOL> html_keywords = [ r'<STR_LIT>' , r'<STR_LIT>' , r'<STR_LIT>' , r'<STR_LIT>' , r'<STR_LIT>' ] <EOL> js_keywords = [ r'<STR_LIT:password>' , r'<STR_LIT>' ] <EOL> def __init__ ( self ) : <EOL> self . html_patterns = [ re . compile ( s , re . I ) for s in self . html_keywords ] <EOL> self . js_patterns = [ re . compile ( s , re . I ) for s in self . js_keywords ] <EOL> def cookies_in_body ( self , r ) : <EOL> alerts = [ ] <EOL> for c in r . response . cookies : <EOL> if c . value in r . response . content : <EOL> alerts . append ( error ( "<STR_LIT>" + c . value ) ) <EOL> return alerts <EOL> def parse_html ( self , r ) : <EOL> alerts = [ ] <EOL> if has_lxml : <EOL> try : <EOL> root = lxml . html . fromstring ( r . response . content ) <EOL> content = '<STR_LIT>' . join ( [ x for x in root . xpath ( "<STR_LIT>" ) if <EOL> x . getparent ( ) . tag != "<STR_LIT>" ] ) <EOL> except UnicodeDecodeError : <EOL> alerts . append ( stealthy ( "<STR_LIT>" "<STR_LIT>" ) ) <EOL> return alerts <EOL> except lxml . etree . ParserError : <EOL> return alerts <EOL> else : <EOL> content = r . response . content <EOL> for e in self . html_patterns : <EOL> if e . search ( content ) : <EOL> if has_lxml : <EOL> alerts . append ( error ( "<STR_LIT>" + e . pattern ) ) <EOL> else : <EOL> alerts . append ( warning ( "<STR_LIT>" + e . pattern ) ) <EOL> return alerts <EOL> def parse_javascript ( self , r ) : <EOL> alerts = [ ] <EOL> for e in self . js_patterns : <EOL> if e . search ( r . response . content ) : <EOL> alerts . append ( error ( "<STR_LIT>" + e . pattern ) ) <EOL> return alerts <EOL> def analyse_request ( self , r ) : <EOL> return [ ] <EOL> def analyse_response ( self , r ) : <EOL> if r . response and r . response . content : <EOL> if r . response . is_html : <EOL> return self . parse_html ( r ) + self . cookies_in_body ( r ) <EOL> if r . response . is_javascript : <EOL> return self . parse_javascript ( r ) <EOL> return [ ] <EOL> class RequestKeywordAlerter ( GenericAlerter ) : <EOL> def __init__ ( self , words ) : <EOL> GenericAlerter . __init__ ( self ) <EOL> self . keywords_patterns = [ re . compile ( s , re . I ) for s in words ] <EOL> def analyse_request ( self , r ) : <EOL> alerts = [ ] <EOL> alerts . extend ( GenericAlerter . analyse_request ( self , r ) ) <EOL> for e in self . keywords_patterns : <EOL> if e . search ( r . content ) : <EOL> alerts . append ( error ( "<STR_LIT>" + e . pattern ) ) <EOL> return alerts <EOL> class InjectedAlerter ( GenericAlerter ) : <EOL> def analyse_response ( self , r ) : <EOL> alerts = [ ] <EOL> if not hasattr ( r , "<STR_LIT>" ) : <EOL> return alerts <EOL> if "<STR_LIT>" in r . payload and r . response . is_html and "<STR_LIT>" in r . response . content : <EOL> alerts . append ( error ( "<STR_LIT>" ) ) <EOL> def scan ( rs ) : <EOL> a = InjectedAlerter ( ) <EOL> for i , r in enumerate ( rs ) : <EOL> alerts = a . analyse_response ( r ) <EOL> if alerts : <EOL> print i , repr ( r ) , repr ( r . response ) <EOL> for alert in alerts : <EOL> print "<STR_LIT:U+0020>" + alert </s>
