標籤:xxxx net trie title cep std uri 微軟 class
本文轉自:http://www.cnblogs.com/myx/archive/2013/03/25/php-ntlm-python-net.html
早期SMB協議在網路上傳輸明文口令。後來出現 LAN Manager Challenge/Response 驗證機制,簡稱LM,它是如此簡單以至很容易就被破解。微軟提出了WindowsNT挑戰/響應驗證機制,稱之為NTLM。現在已經有了更新的NTLMv2以及Kerberos驗證體系。NTLM是windows早期安全性通訊協定,因向後相容性而保留下來。NTLM是NT LAN Manager的縮寫,即NT LAN管理器。NTLM 是為沒有加入到域中的電腦(如獨立伺服器和工作群組)提供的身分識別驗證協議。
NTLM驗證允許Windows使用者使用當前登入系統的身份進行認證,目前使用者應該是登陸在一個域(domain)上,他的身份是可以自動通過瀏覽器傳遞給伺服器的。它是一種單點登入的策略,系統可以通過NTLM重用登入到Windows系統中的使用者憑證,不用再次要求使用者輸入密碼進行認證。
其實這次要做的功能主要是PHP的NTLM登入,不過搜尋了很久,都沒找到具體的。見到最多資料就是: https://github.com/loune/php-ntlm 不過ntlm_prompt("testwebsite", "testdomain", "mycomputer", "testdomain.local", "mycomputer.local", "get_ntlm_user_hash"); 好像沒有具體的使用者名稱與密碼,測試的時候還是在遊覽器快顯視窗要求輸入使用者名稱密碼。在其他地方都沒找到可用了。後來看CURL裡面有個--ntlm,一測試,原來還這麼簡單的。
$url = ‘http://xxxx.com/HomePage/info.aspx‘;//注意:是要擷取資訊的頁面地址,不是登入頁的地址。 $user =‘test‘; $password =‘testpwd‘; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); //加上這可以擷取cookies,就是輸出的$result的前面有header資訊 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); curl_setopt($ch, CURLOPT_USERPWD, $user.‘:‘.$password); $result = curl_exec($ch); preg_match_all (‘/^Set-Cookie: (.*?);/m‘,$result,$m); //擷取cookies var_dump($m);
.Net的擷取方式也很簡單,代碼如下:
try { CredentialCache MyCredentialCache = new CredentialCache(); MyCredentialCache.Add(new Uri("http://www.xxx.com/infot.aspx"), "NTLM", new NetworkCredential("test", "testpwd", "domain")); HttpWebRequest req; req = (HttpWebRequest)HttpWebRequest.Create("http://www.xxx.com/info.aspx"); req.Method = "GET"; req.KeepAlive = true; req.Credentials = MyCredentialCache; //儲存cookie CookieContainer cc = new CookieContainer(); req.CookieContainer = cc; HttpWebResponse res; res = (HttpWebResponse)req.GetResponse(); Console.WriteLine(res.StatusCode); Console.WriteLine("------------------------"); Console.WriteLine(res.Headers.ToString()); if (res.StatusCode == HttpStatusCode.OK) { //驗證成功 Console.WriteLine(res.StatusCode); } } catch (Exception ex) { //驗證失敗 }
Python:python-ntlm(官網地址:http://code.google.com/p/python-ntlm/)是一個用來訪問NTLM認證網址的module, 代碼的那邊搬過來的。我測試可用:
url = "http://www.xxx.com/info.aspx" #就是注意這個是擷取資訊的地址。不是登入的。剛開始測試用的登入的地址。那樣是不行的。 user = u‘randy\\test‘ password = ‘testpwd‘ passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, user, password) # create the NTLM authentication handler auth_NTLM = HTTPNtlmAuthHandler(passman) # create and install the opener opener = urllib2.build_opener(auth_NTLM) urllib2.install_opener(opener) # retrieve the result response = urllib2.urlopen(url) print(response.info()) print(os.path.join(os.getcwd(),"1.txt")) #outfile = open(os.path.join(os.getcwd(),"1.htm"), "w") #outfile.write(response.read())
[轉]關於NTLM認證的.NET,php,python登入