Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d07f605

Browse files
committed
implementation of Feature #17
1 parent 80df1fd commit d07f605

5 files changed

Lines changed: 84 additions & 43 deletions

File tree

lib/core/option.py

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
from lib.parse.configfile import configFileParser
6868
from lib.parse.queriesfile import queriesParser
6969
from lib.request.proxy import ProxyHTTPSHandler
70+
from lib.request.certhandler import HTTPSCertAuthHandler
7071
from lib.utils.google import Google
7172

7273
authHandler = urllib2.BaseHandler()
@@ -518,13 +519,14 @@ def __setHTTPProxy():
518519

519520
def __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

580604
def __setHTTPMethod():
581605
"""

lib/core/optiondict.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"headers": "string",
4242
"aType": "string",
4343
"aCred": "string",
44+
"aCert": "string",
4445
"proxy": "string",
4546
"threads": "integer",
4647
"delay": "float",

lib/parse/cmdline.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def cmdLineParser():
9595
request.add_option("--auth-cred", dest="aCred",
9696
help="HTTP Authentication credentials (value "
9797
"name:password)")
98+
99+
request.add_option("--auth-cert", dest="aCert",
100+
help="HTTP(s) Authentication certificate (value "
101+
"key_file,cert_file)")
98102

99103
request.add_option("--proxy", dest="proxy",
100104
help="Use a HTTP proxy to connect to the target url")

lib/request/certhandler.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323
"""
2424

25+
import sys
2526
import httplib
2627
import urllib2
2728

29+
from lib.core.data import conf
30+
2831
class HTTPSCertAuthHandler(urllib2.HTTPSHandler):
2932
def __init__(self, key_file, cert_file):
3033
urllib2.HTTPSHandler.__init__(self)
@@ -35,4 +38,8 @@ def https_open(self, req):
3538
return self.do_open(self.getConnection, req)
3639

3740
def getConnection(self, host):
38-
return httplib.HTTPSConnection(host, key_file=self.key_file, cert_file=self.cert_file)
41+
if sys.version_info >= (2,6):
42+
retVal = httplib.HTTPSConnection(host, key_file=self.key_file, cert_file=self.cert_file, timeout=conf.timeout)
43+
else:
44+
retVal = httplib.HTTPSConnection(host, key_file=self.key_file, cert_file=self.cert_file)
45+
return retVal

sqlmap.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ aType =
6565
# Syntax: username:password
6666
aCred =
6767

68+
# HTTP Authentication certificate. Useful only if the target url requires
69+
# logon certificate and you have such data.
70+
# Syntax: key_file,cert_file
71+
aCert =
72+
6873
# Use a HTTP proxy to connect to the target url.
6974
# Syntax: http://address:port
7075
proxy =

0 commit comments

Comments
 (0)