Created
November 12, 2013 09:10
-
-
Save 0xdevalias/7427849 to your computer and use it in GitHub Desktop.
Python snippet showing how to enable NTLM authentication for urllib2 (Requires https://code.google.com/p/python-ntlm/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def enable_ntlm_authentication(user = "", password = "", url = ""): | |
print "[+][devalias.net] Enabling NTLM authentication support" | |
# Import ntlm library | |
try: | |
# import ntlm | |
from ntlm import HTTPNtlmAuthHandler | |
print "[+][devalias.net][NTLM Authentication] NTLM Support Library Loaded!" | |
except ImportError: | |
print "[-][devalias.net][NTLM Authentication] Program could not find module : ntlm (Is the ntlm library installed/available locally?" | |
sys.exit (1) | |
try: | |
from urlparse import urlparse, urlunparse | |
except ImportError: | |
print "[-][devalias.net][NTLM Authentication] Program could not find module : urlparse" | |
sys.exit (1) | |
if user == "": | |
user = raw_input("[+][devalias.net][NTLM Authentication] Enter username (DOMAIN\username): ") | |
if password == "": | |
password = raw_input("[+][devalias.net][NTLM Authentication] Enter password: ") | |
parsed_url = urlparse(url) | |
base_uri = urlunparse((parsed_url[0],parsed_url[1],"","","","")) | |
################################################### | |
# BELOW HERE THIS IS WHERE THE MAIN STUFF HAPPENS | |
################################################### | |
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
passman.add_password(None, base_uri, user, password) | |
# create the NTLM authentication handler | |
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman) | |
# other authentication handlers | |
auth_basic = urllib2.HTTPBasicAuthHandler(passman) | |
auth_digest = urllib2.HTTPDigestAuthHandler(passman) | |
# disable proxies (if you want to stay within the corporate network) | |
# proxy_handler = urllib2.ProxyHandler({}) | |
proxy_handler = urllib2.ProxyHandler() | |
# create and install the opener | |
opener = urllib2.build_opener(proxy_handler, auth_NTLM, auth_digest, auth_basic) | |
urllib2.install_opener(opener) | |
################################################### | |
# ABOVE HERE THIS IS WHERE THE MAIN STUFF HAPPENS | |
################################################### | |
print "[+][devalias.net][NTLM authentication] Credentials enabled for " + user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment