Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NTLM login

I am trying to pull information from a url that requires NTLM login.

I originaly received 401 error and after some tweaks have been able to pull the page stating that I have input invalid credentials.

The username and password are correct yet I cannot get past the invalid credentials page.

Lgn2.py:

import urllib2
import HTTPNtlmAuthHandler

login = open('c:/temp/login.txt')
open = login.read()
to = open.split()
user = str(to[0])
password = str(to[1])

url = "http://INSERT URL HERE.com/"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)


opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)


response = urllib2.urlopen(url)
print(response.read())

I have a username including a \ using the method above I do not get a double backslash in the name when it prints. Should I keep it to where print will have the double backslash in the name as apposed to exactly as the txt file has the username spelled?

The txt file is just a txt document with only: domain\user\name password.

The second backslash in the middle of username would be part of the username.

Any help would be appreciated.

like image 978
Joe N. Avatar asked Nov 02 '12 03:11

Joe N.


People also ask

How do you check if I use NTLM?

To find applications that use NTLMv1, enable Logon Success Auditing on the domain controller, and then look for Success auditing Event 4624, which contains information about the version of NTLM.

Is NTLM outdated?

NTLM is considered an outdated protocol. As such, its benefits — when compared to a more modern solution, such as Kerberos — are limited.

Is NTLM deprecated?

Following this end of availability, on October 24, 2019, the NTLM protocol-based authentication will be deprecated and will no longer be available in VMware Identity Manager. On August 22, 2019, NTLM protocol support in VMware Identity Manager will reach the end of life.


2 Answers

Our company has a proxy and uses NTLM. To connect without having to place credentials in script I used:

import win32com.client

url = 'https://...'

h = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')
h.SetAutoLogonPolicy(0)
h.Open('GET', url, False)
h.Send()
result = h.responseText
result
like image 190
ctrl-alt-delete Avatar answered Oct 20 '22 01:10

ctrl-alt-delete


Maybe you didn't use a raw string:

Unless an 'r' or 'R' prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.

>>> 'domain\user'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 6-7: truncated \uXXXX escape
>>> r'domain\user'
'domain\\user'

This works for me (in Python 2, not 3):

from ntlm import HTTPNtlmAuthHandler
import urllib2

user = r'domain\user'
password = "passphrase"
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, "http://projects/", user, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

url = "http://projects/_vti_bin/owssvr.dll?Cmd=Display&List=etc"
response = urllib2.urlopen(url)
headers = response.info()
print("headers: {}".format(headers))
body = response.read()
print("response: " + body)
like image 32
Cees Timmerman Avatar answered Oct 20 '22 01:10

Cees Timmerman