NTLM 身份验证 - 在 PHP 中获取 Windows 登录名、域和主机

2022-08-30 23:44:05

我正在开发单点登录 (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!";
            }
        }
    }
}
?>

此脚本正在处理此配置:

  • 视窗服务器 2003
  • 具有模块mod_auth_sspi的 Apache 2.2

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

  • 视窗服务器 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($chained 64[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!";
    }
}

因为总是返回 1。ord($chained64{8})


编辑 2015-05-11 :

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

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

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

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

    <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中,我进入了about:config,将network.automatic-ntlm-authred.trusted-uris设置为我的域,这要归功于@ThaDafinser。现在我在Firefox中不再得到提示,一切都可以正常工作,但我总是需要让它在IE上工作。

  • 在 IE 中,我将本地 Intranet 安全性设置为最低,但没有任何更改。

  • 在 IE 中,“使用当前用户名和密码自动登录”检查本地 Intranet 和受信任的站点。

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


编辑 2015-05-13 :

  • 我在IE中将URL添加到受信任的站点,没有任何更改。

  • 我将受信任站点的安全性设置为低,没有任何变化。

  • 我在IE中取消选中了“通过代理连接使用HTTP 1.1”> Internet选项>高级“,即使使用提示,我仍然无法在Internet Explorer上获得会话信息。

  • 我在Internet Explorer中添加了完整的URL > Internet Options > Security > Local Intranet > Sites > Advanced

  • 在Internet Explorer > Internet Options > Security > Local Intranet > Sites > Advanced 中,我还添加了与在 Firefox 中添加的域相同的部分(mycompany.com)以使其正常工作,但这没有帮助。

编辑 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 基本 AuthName “Authentication Required” AuthUserFile “E:/PATH/.htpasswd” Require valid-user

    订单允许,拒绝全部允许


答案 1

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

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

  • 键入:地址栏中的“关于:配置”
  • 检查 network.automatic-ntlm-auth.trusted-uris
  • 将值设置为您的域或域的一部分,例如 mycompany.com(用逗号分隔多个值)

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


答案 2

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

我怀疑问题出在你的身上,因为你使用的是Apache 2.2语法,这在Apache 2.4中不起作用(除非你启用了模块)。您应该阅读 Apache 文档中的从 2.2 升级到 2.4httpd.confallow/denymod_access_compat

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

以下内容适用于我在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>

推荐