4343from xml .dom import minidom
4444from xml .sax import parse
4545
46+ from extra .clientform .clientform import ParseResponse
47+ from extra .clientform .clientform import ParseError
4648from extra .cloak .cloak import decloak
4749from extra .magic import magic
4850from extra .odict .odict import OrderedDict
5355from lib .core .data import queries
5456from lib .core .convert import htmlunescape
5557from lib .core .convert import safecharencode
58+ from lib .core .convert import unicodeencode
5659from lib .core .convert import urldecode
5760from lib .core .convert import urlencode
5861from lib .core .enums import DBMS
5962from lib .core .enums import HTTPHEADER
63+ from lib .core .enums import HTTPMETHOD
6064from lib .core .enums import OS
6165from lib .core .enums import PLACE
6266from lib .core .enums import PAYLOAD
@@ -3013,7 +3017,7 @@ def randomizeParameterValue(value):
30133017
30143018 return retVal
30153019
3016- def asciifyUrl (url , force_quote = False ):
3020+ def asciifyUrl (url , forceQuote = False ):
30173021 """
30183022 Attempts to make a unicode url usuable with ``urllib/urllib2``.
30193023
@@ -3046,7 +3050,7 @@ def quote(s, safe):
30463050 # Triggers on non-ascii characters - another option would be:
30473051 # urllib.quote(s.replace('%', '')) != s.replace('%', '')
30483052 # which would trigger on all %-characters, e.g. "&".
3049- if s .encode ('ascii' , 'replace' ) != s or force_quote :
3053+ if s .encode ('ascii' , 'replace' ) != s or forceQuote :
30503054 return urllib .quote (s .encode ('utf8' ), safe = safe )
30513055 return s
30523056
@@ -3065,4 +3069,66 @@ def quote(s, safe):
30653069 if parts .port :
30663070 netloc += ':' + str (parts .port )
30673071
3068- return urlparse .urlunsplit ([parts .scheme , netloc , path , query , parts .fragment ])
3072+ return urlparse .urlunsplit ([parts .scheme , netloc , path , query , parts .fragment ])
3073+
3074+ def findPageForms (content , url , raise_ = False , addToTargets = False ):
3075+ class _ (StringIO ):
3076+ def __init__ (self ):
3077+ StringIO .__init__ (self , unicodeencode (content , kb .pageEncoding ) if isinstance (content , unicode ) else content )
3078+ self ._url = url
3079+ def geturl (self ):
3080+ return self ._url
3081+
3082+ if raise_ and not content :
3083+ errMsg = "can't parse forms as the page content appears to be blank"
3084+ raise sqlmapGenericException , errMsg
3085+
3086+ retVal = set ()
3087+ response = _ ()
3088+ try :
3089+ forms = ParseResponse (response , backwards_compat = False )
3090+ except ParseError :
3091+ errMsg = "badly formed HTML at the target url. will try to filter it"
3092+ logger .error (errMsg )
3093+ response .seek (0 )
3094+ filtered = _ ("" .join (re .findall (r'<form.+?</form>' , response .read (), re .I | re .S )), response .geturl ())
3095+ try :
3096+ forms = ParseResponse (filtered , backwards_compat = False )
3097+ except ParseError :
3098+ errMsg = "no success"
3099+ if raise_ :
3100+ raise sqlmapGenericException , errMsg
3101+ else :
3102+ logger .debug (errMsg )
3103+
3104+ if forms :
3105+ for form in forms :
3106+ for control in form .controls :
3107+ if hasattr (control , 'items' ):
3108+ # if control has selectable items select first non-disabled
3109+ for item in control .items :
3110+ if not item .disabled :
3111+ item .selected = True
3112+ break
3113+ request = form .click ()
3114+ url = urldecode (request .get_full_url (), kb .pageEncoding )
3115+ method = request .get_method ()
3116+ data = urldecode (request .get_data (), kb .pageEncoding ) if request .has_data () else None
3117+ if not data and method and method .upper () == HTTPMETHOD .POST :
3118+ debugMsg = "invalid POST form with blank data detected"
3119+ logger .debug (debugMsg )
3120+ continue
3121+ target = (url , method , data , conf .cookie )
3122+ retVal .add (target )
3123+ else :
3124+ errMsg = "there were no forms found at the given target url"
3125+ if raise_ :
3126+ raise sqlmapGenericException , errMsg
3127+ else :
3128+ logger .debug (errMsg )
3129+
3130+ if addToTargets and retVal :
3131+ for target in retVal :
3132+ kb .targetUrls .add (target )
3133+
3134+ return retVal
0 commit comments