PHP单点登录使用NTLMv2


PHP Single Sign On using NTLMv2

我有一个nginx web服务器,使用php-fpm脚本执行,我想获得客户端浏览服务器的NTLMv2凭据。我在本地网络中有一个代理服务器来验证我的用户。问题是,我如何使nginx服务器身份验证,或PHP获得我的用户使用NTLMv2的凭据,并传递回我的信息?显然,我至少需要知道他们的用户名,以确保客户端在系统中获得正确的凭据。

我可以做一个上游连接到代理服务器时,例如我去/login.php,只要它传递关于客户端的信息回服务器关于客户端,例如在Type-3消息中找到的用户名,我可以保存这些信息在他们的会话和使用它从那一点。


我有一个Linux服务器在局域网内运行nginx, PHP和SQLite。连接到此服务器的计算机都是基于Windows的,使用Windows登录到网络。登录使用NTLMv2身份验证,并通过代理访问网络外的网站,所有客户端都必须通过代理才能连接到外部web。我想做的是使用NTLMv2认证信息登录到局域网web服务器。有什么建议吗?

我认为完成这样的事情最简单的方法是在nginx服务器上模拟NTLMv2身份验证,将请求重定向到代理并检查答案。我无法重现你的设置,所以下面的代码没有经过测试,但它应该可以工作,或者它应该给你一点帮助。

<?php
$headers = getallheaders() //Equivalent to apache_request_headers() to get the headers of the request.
if(!isset($headers['Authorization'])) //Check Authorization Header
{
    header('HTTP/1.1 401 Unauthorized'); //Return Unauthorized Http-Header (NTLM protocol)
    header('WWW-Authenticate: NTLM'); //Authenticcation Information (NTLM protocol)
}
else
{
    if(substr($headers['Authorization'],0,4) == 'NTLM') //Check whether Authorization Header is valid
    {
        $message = base64_decode(substr($headers['Authorization'], 5)) //Get NTLM Message from Authrization header
        if(substr($message, 0, 8) == "NTLMSSP'x00") //Check whether NTLM Message is valid
        {
            if($message[8] == "'x01") //Check whether it's type-1-NTLM Message
            {
                //$message holds the base64 encoded type-1-NTLM message
                $ch = curl_init(); //Use cURL to connect to web via proxy
                curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$headers['Authorization']));
                curl_setopt($ch, CURLOPT_PROXY, <Your Proxy Adress>);
                curl_setopt($ch, CURLOPT_PROXYPORT, <Your Proxy Port>);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $result = curl_exec($ch);
                $info = curl_getinfo($ch);
                curl_close($ch);
                $header = substr($result, 0, $info['header_size']);
                $body = substr($result, $info['header_size'], $info['download_content_length']-$info['header_size']);
                $c_headers = explode("'r'n", $header);
                for($i = 0; $i < (count($c_headers) - 2); $i++)
                {
                    header($c_headers[$i]);
                    if(substr($c_headers[$i], 0, 16) == "WWW-Authenticate")
                    {
                        //Thats your type-2-message header Format: WWW-Authenticate: NTLM <base64-type-2-message>
                    }
                }
            }
            else if ($message[8] == "'x03") //Check whether it's type-3-NTLM Message
            {
                $ch = curl_init(); //Use cURL to connect to web via proxy 
                curl_setopt($ch, CURLOPT_URL, "http://www.google.com");
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$headers['Authorization']));
                curl_setopt($ch, CURLOPT_PROXY, <Your Proxy Adress>);
                curl_setopt($ch, CURLOPT_PROXYPORT, <Your Proxy Port>);
                $result = curl_exec($ch);
                $info = curl_getinfo($ch);
                curl_close($ch);
                if($info['CURLINFO_HTTP_CODE'] == 200)
                {
                    //Authenticated
                    //$msg holds the base64 encoded type-3-NTLM message (which includes username, domain, workstation)
                }
            }
        }
    }
}?>

我使用了NTLM协议的这个参考:http://davenport.sourceforge.net/ntlm.html

我希望它能帮助你。欢迎评论