6767from lib .parse .configfile import configFileParser
6868from lib .parse .queriesfile import queriesParser
6969from lib .request .proxy import ProxyHTTPSHandler
70+ from lib .request .certhandler import HTTPSCertAuthHandler
7071from lib .utils .google import Google
7172
7273authHandler = urllib2 .BaseHandler ()
@@ -518,13 +519,14 @@ def __setHTTPProxy():
518519
519520def __setHTTPAuthentication ():
520521 """
521- Check and set the HTTP authentication method (Basic, Digest or NTLM),
522- username and password to perform HTTP requests with.
522+ Check and set the HTTP(s) authentication method (Basic, Digest, NTLM or Certificate),
523+ username and password for first three methods, or key file and certification file for
524+ certificate authentication
523525 """
524526
525527 global authHandler
526528
527- if not conf .aType and not conf .aCred :
529+ if not conf .aType and not conf .aCred and not conf . aCert :
528530 return
529531
530532 elif conf .aType and not conf .aCred :
@@ -537,45 +539,67 @@ def __setHTTPAuthentication():
537539 errMsg += "but did not provide the type"
538540 raise sqlmapSyntaxException , errMsg
539541
540- debugMsg = "setting the HTTP authentication type and credentials"
541- logger .debug (debugMsg )
542-
543- aTypeLower = conf .aType .lower ()
544-
545- if aTypeLower not in ( "basic" , "digest" , "ntlm" ):
546- errMsg = "HTTP authentication type value must be "
547- errMsg += "Basic, Digest or NTLM"
548- raise sqlmapSyntaxException , errMsg
549-
550- aCredRegExp = re .search ("^(.*?)\:(.*?)$" , conf .aCred )
551-
552- if not aCredRegExp :
553- errMsg = "HTTP authentication credentials value must be "
554- errMsg += "in format username:password"
555- raise sqlmapSyntaxException , errMsg
556-
557- authUsername = aCredRegExp .group (1 )
558- authPassword = aCredRegExp .group (2 )
559-
560- passwordMgr = urllib2 .HTTPPasswordMgrWithDefaultRealm ()
561- passwordMgr .add_password (None , "%s://%s" % (conf .scheme , conf .hostname ), authUsername , authPassword )
562-
563- if aTypeLower == "basic" :
564- authHandler = urllib2 .HTTPBasicAuthHandler (passwordMgr )
565-
566- elif aTypeLower == "digest" :
567- authHandler = urllib2 .HTTPDigestAuthHandler (passwordMgr )
568-
569- elif aTypeLower == "ntlm" :
570- try :
571- from ntlm import HTTPNtlmAuthHandler
572- except ImportError , _ :
573- errMsg = "sqlmap requires Python NTLM third-party library "
574- errMsg += "in order to authenticate via NTLM, "
575- errMsg += "http://code.google.com/p/python-ntlm/"
576- raise sqlmapMissingDependence , errMsg
577-
578- authHandler = HTTPNtlmAuthHandler .HTTPNtlmAuthHandler (passwordMgr )
542+ if not conf .aCert :
543+ debugMsg = "setting the HTTP authentication type and credentials"
544+ logger .debug (debugMsg )
545+
546+ aTypeLower = conf .aType .lower ()
547+
548+ if aTypeLower not in ( "basic" , "digest" , "ntlm" ):
549+ errMsg = "HTTP authentication type value must be "
550+ errMsg += "Basic, Digest or NTLM"
551+ raise sqlmapSyntaxException , errMsg
552+
553+ aCredRegExp = re .search ("^(.*?)\:(.*?)$" , conf .aCred )
554+
555+ if not aCredRegExp :
556+ errMsg = "HTTP authentication credentials value must be "
557+ errMsg += "in format username:password"
558+ raise sqlmapSyntaxException , errMsg
559+
560+ authUsername = aCredRegExp .group (1 )
561+ authPassword = aCredRegExp .group (2 )
562+
563+ passwordMgr = urllib2 .HTTPPasswordMgrWithDefaultRealm ()
564+ passwordMgr .add_password (None , "%s://%s" % (conf .scheme , conf .hostname ), authUsername , authPassword )
565+
566+ if aTypeLower == "basic" :
567+ authHandler = urllib2 .HTTPBasicAuthHandler (passwordMgr )
568+
569+ elif aTypeLower == "digest" :
570+ authHandler = urllib2 .HTTPDigestAuthHandler (passwordMgr )
571+
572+ elif aTypeLower == "ntlm" :
573+ try :
574+ from ntlm import HTTPNtlmAuthHandler
575+ except ImportError , _ :
576+ errMsg = "sqlmap requires Python NTLM third-party library "
577+ errMsg += "in order to authenticate via NTLM, "
578+ errMsg += "http://code.google.com/p/python-ntlm/"
579+ raise sqlmapMissingDependence , errMsg
580+
581+ authHandler = HTTPNtlmAuthHandler .HTTPNtlmAuthHandler (passwordMgr )
582+ else :
583+ debugMsg = "setting the HTTP(s) authentication certificate"
584+ logger .debug (debugMsg )
585+
586+ aCertRegExp = re .search ("^(.+?),\s*(.+?)$" , conf .aCert )
587+
588+ if not aCertRegExp :
589+ errMsg = "HTTP authentication certificate option "
590+ errMsg += "must be in format key_file,cert_file"
591+ raise sqlmapSyntaxException , errMsg
592+
593+ #os.path.expanduser for support of paths with ~
594+ key_file = os .path .expanduser (aCertRegExp .group (1 ))
595+ cert_file = os .path .expanduser (aCertRegExp .group (2 ))
596+
597+ for file in (key_file , cert_file ):
598+ if not os .path .exists (file ):
599+ errMsg = "File '%s' doesn't exist" % file
600+ raise sqlmapSyntaxException , errMsg
601+
602+ authHandler = HTTPSCertAuthHandler (key_file , cert_file )
579603
580604def __setHTTPMethod ():
581605 """
0 commit comments