首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用NTLMv2的单点登录

使用NTLMv2的单点登录
EN

Stack Overflow用户
提问于 2013-04-15 06:18:25
回答 1查看 3.7K关注 0票数 5

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

例如,当我转到/login.php时,我可以建立到代理服务器的上游连接,只要它将关于客户机的信息传回给服务器,例如在Type-3消息中找到的用户名,我就可以在他们的会话中保存这个信息,并从那时起使用它。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-19 17:39:58

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

代码语言:javascript
运行
复制
<?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

我希望它能帮到你。请随时发表评论。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16008765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档