NTLM身份验证 - 以PHP

时间:2015-05-06 07:21:19

标签: php apache login ntlm windows-server

我正在开发单点登录(SSO)PHP应用程序  用户登录他们的Windows会话,他们希望使用他们的Windows帐户(与LDAP Active Directory连接)自动登录应用程序。

我试过这个剧本:

<?php
$headers = apache_request_headers();    // Récupération des l'entêtes client

if (@$_SERVER['HTTP_VIA'] != NULL){ // nous verifions si un proxy est utilisé : parceque l'identification par ntlm ne peut pas passer par un proxy
    echo "Proxy bypass!";
} elseif(!isset($headers['Authorization'])) {           //si l'entete autorisation est inexistante
    header( "HTTP/1.0 401 Unauthorized" );          //envoi au client le mode d'identification
    header( "WWW-Authenticate: NTLM" );         //dans notre cas le NTLM
    exit;                           //on quitte

}

if(isset($headers['Authorization']))                //dans le cas d'une authorisation (identification)
{   
    if(substr($headers['Authorization'],0,5) == 'NTLM '){   // on vérifit que le client soit en NTLM

        $chaine=$headers['Authorization'];                  
        $chaine=substr($chaine, 5);             // recuperation du base64-encoded type1 message
        $chained64=base64_decode($chaine);      // decodage base64 dans $chained64

        if(ord($chained64{8}) == 1){                    
        //        |_ byte signifiant l'etape du processus d'identification (etape 3)        

        // verification du drapeau NTLM "0xb2" à l'offset 13 dans le message type-1-message (comp ie 5.5+) :
            if (ord($chained64[13]) != 178){
                echo "NTLM Flag error!";
                exit;
            }

            $retAuth = "NTLMSSP".chr(000).chr(002).chr(000).chr(000).chr(000).chr(000).chr(000).chr(000);
            $retAuth .= chr(000).chr(040).chr(000).chr(000).chr(000).chr(001).chr(130).chr(000).chr(000);
            $retAuth .= chr(000).chr(002).chr(002).chr(002).chr(000).chr(000).chr(000).chr(000).chr(000);
            $retAuth .= chr(000).chr(000).chr(000).chr(000).chr(000).chr(000).chr(000);

            $retAuth64 =base64_encode($retAuth);        // encode en base64
            $retAuth64 = trim($retAuth64);          // enleve les espaces de debut et de fin
            header( "HTTP/1.0 401 Unauthorized" );      // envoi le nouveau header
            header( "WWW-Authenticate: NTLM $retAuth64" );  // avec l'identification supplémentaire
            exit;

        } else if(ord($chained64{8}) == 3) {
        //             |_ byte signifiant l'etape du processus d'identification (etape 5)

            // on recupere le domaine
            $lenght_domain = (ord($chained64[31])*256 + ord($chained64[30])); // longueur du domain
            $offset_domain = (ord($chained64[33])*256 + ord($chained64[32])); // position du domain.    
            $domain = str_replace("\0","",substr($chained64, $offset_domain, $lenght_domain)); // decoupage du du domain

            //le login
            $lenght_login = (ord($chained64[39])*256 + ord($chained64[38])); // longueur du login.
            $offset_login = (ord($chained64[41])*256 + ord($chained64[40])); // position du login.
            $login = str_replace("\0","",substr($chained64, $offset_login, $lenght_login)); // decoupage du login

            $lenght_host = (ord($chained64[47])*256 + ord($chained64[46]));
            $offset_host = (ord($chained64[49])*256 + ord($chained64[48]));
            $host = str_replace("\0","",substr($chained64, $offset_host, $lenght_host));


            if ( $login != NULL){
                echo $login;
            } else {
                echo "NT Login empty!";
            }
        }
    }
}
?>

此脚本正在处理此配置:

  • Windows server 2003
  • Apache 2.2,模块为mod_auth_sspi

但是现在我需要在这个配置上实现它,它不起作用:

  • Windows server 2008
  • Apache 2.4.6,模块为mod_authnz_sspi

由于这种情况,我一直收到“NTLM标志错误!”:

if (ord($chained64[13]) != 178){
    echo "NTLM Flag error!";
    exit;
}

我试过了:

if (ord($chained64[13]) != 130){

因为ord($ chained64 [13])返回130,但我无法进入这种情况:

} else if(ord($chained64{8}) == 3) {
    $lenght_domain = (ord($chained64[31])*256 + ord($chained64[30])); // longueur du domain
    $offset_domain = (ord($chained64[33])*256 + ord($chained64[32])); // position du domain. 
    $domain = str_replace("\0","",substr($chained64, $offset_domain, $lenght_domain)); // decoupage du du domain

    //le login
    $lenght_login = (ord($chained64[39])*256 + ord($chained64[38])); // longueur du login.
    $offset_login = (ord($chained64[41])*256 + ord($chained64[40])); // position du login.
    $login = str_replace("\0","",substr($chained64, $offset_login, $lenght_login)); // decoupage du login

    $lenght_host = (ord($chained64[47])*256 + ord($chained64[46]));
    $offset_host = (ord($chained64[49])*256 + ord($chained64[48]));
    $host = str_replace("\0","",substr($chained64, $offset_host, $lenght_host));


    if ( $login != NULL){
        echo $login;
    } else {
        echo "NT Login empty!";
    }
}

因为ord($chained64{8})始终返回1.

编辑2015-05-11:

  • 我尝试在php中执行'whoami'命令,如下所示:echo exec('whoami'); - &gt;当我在cmd.exe中执行此命令时,我得到当前记录的用户,但是当我在PHP中执行它时,我得到了nt_authority / system。

  • 我认为当PHP执行'whoami'命令时,Windows会检查Apache服务的登录。我在“登录”选项卡中进入Apache属性,以作为Active Directory的有效用户登录。但是,当PHP执行echo exec('whoami');时,我只获得用于Apache的登录,而不是当前用户。

  • 我正在使用Internet Explorer 8来执行PHP脚本。

  • 我在我的Apache httpd.conf中有这个(_PATH_是我的php文件的路径,也许这是错的?):

    <Directory "E:/_PATH_"> Options None AllowOverride All Order allow,deny Allow from all AuthName "SSPI Protected Place" AuthType SSPI SSPIAuth On SSPIAuthoritative On SSPIOfferBasic On SSPIOmitDomain On Require valid-user </Directory>

编辑2015-05-12:

  • 我在计算机上以域用户身份登录

  • 当我尝试使用Firefox时,会收到登录信息和密码提示。当我发布提示时,脚本从提示符获取登录,但这不是我想要做的:我必须让它与IE一起工作,我不想再次输入登录名和密码。我想要登录当前的Windows会话。

  • 在Firefox中,由于@ThaDafinser,我进入about:config将network.automatic-ntlm-auth.trusted-uris设置为我的域名。现在我在Firefox中没有得到提示,一切正常,但我总是需要让它在IE上运行。

  • 在IE中,我将Local Intranet Security设置为最低,但没有任何变化。

  • 在IE中,检查“使用当前用户名和密码进行自动登录”的Local Intranet&amp;可信站点。

  • 当我强制IE在提示中询问凭据时,如果我发布提示,则IE不会返回凭据,与Firefox相反。

编辑2015-05-13:

  • 我在IE中向可信站点添加了URL,没有任何改变。

  • 我将可信站点的安全性设置为低,没有任何更改。

  • 我在IE&gt;中取消选中“通过代理连接使用HTTP 1.1”互联网选项&gt;高级,即使我使用提示,我仍然无法在Internet Explorer上拥有会话信息。

  • 我在Internet Explorer中添加了完整的网址&gt;互联网选项&gt;安全&gt; <本地Intranet>网站&gt;高级

  • 在Internet Explorer中&gt;互联网选项&gt;安全&gt; <本地Intranet>网站&gt;高级,我还添加了相同的域名(mycompany.com),而不是我在Firefox中添加它以使其工作,但这没有帮助。

编辑2015-05-18:

根据@timclutton在他的回答中所说的,改变了我的httpd.conf以与Apache 2.4兼容:

<Directory "E:/_PATH_"> 
    Require all denied
    AllowOverride     All
    Options None 

    AuthName          "SSPI Authentication"
    AuthType          SSPI
    SSPIAuth          On
    SSPIAuthoritative On
    SSPIOmitDomain    On
    Require           valid-user
    Require           user "NT AUTHORITY\ANONYMOUS LOGON" denied 
</Directory>

编辑2015-05-19:

  • 我尝试设置SSPI的基本身份验证内容,但它不起作用。

      AuthType Basic   AuthName“需要身份验证”   AuthUserFile“E:/ PATH /。htpasswd”   需要有效用户

    订单允许,拒绝   允许所有人

2 个答案:

答案 0 :(得分:4)

  

当我尝试使用Firefox时,会收到登录信息和密码提示。当我发布提示时,脚本会从提示符中获取登录信息,但这不是我想要做的事情:我必须让它与IE一起工作,我不想再次输入登录名和密码。我想要登录当前的Windows会话。

您可以通过更改Firefox设置来删除提示:

  • 输入:&#34; about:config&#34;在地址栏中
  • 检查network.automatic-ntlm-auth.trusted-uris
  • 将值设置为您的域或域的一部分,例如mycompany.com(使用逗号分隔多个值)

对于IE,您需要将页面(Intranet)的安全设置设置为低于其他Internet的安全设置。 请参阅https://superuser.com/questions/148063/why-does-internet-explorer-keep-asking-me-for-ntlm-credentials-in-an-intranet-zo

答案 1 :(得分:2)

mod_authnz_sspi模块透明地处理身份验证过程的所有方面,这意味着您不需要PHP身份验证脚本。如果模块配置正确,您应该只能在脚本中引用$_SERVER['REMOTE_USER']。任何无法通过身份验证的用户都将收到标准的Apache 403 - Forbidden错误。

我怀疑问题出在您的httpd.conf,因为您使用的是Apache 2.2 allow/deny语法,这在Apache 2.4中无效(除非您启用了mod_access_compat模块) 。您应该阅读Apache文档中的Upgrading to 2.4 from 2.2

确保E:/_PATH_是运行PHP脚本的确切文件夹,并且对该路径的每个请求都需要进行身份验证。

以下适用于Apache 2.4:

<Directory "/path/to/webroot">
    AllowOverride     All
    Options           ExecCGI
    # since I run PHP via mod_fcgi; should also work as 'Options none'.

    AuthName          "SSPI Authentication"
    AuthType          SSPI
    SSPIAuth          On
    SSPIAuthoritative On
    SSPIOmitDomain    On
    Require           valid-user
    Require           user "NT AUTHORITY\ANONYMOUS LOGON" denied
</Directory>
相关问题