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



我正在处理单个登录(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("","",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("","",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("","",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($ CHANED64 [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("","",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("","",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("","",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');->当我在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中,我进入了以下方面:config to network.automation-ntlm-auth.trusted-uris到我的域,这要归功于@thadafinser。现在,我不再在firefox中得到提示,一切都起作用,但是我总是需要使它在IE上起作用。

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

  • 在IE中,检查了本地Intranet&amp;值得信赖的网站。

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


编辑2015-05-13:

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

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

  • 我在IE> Internet选项>高级中未选中"使用http 1.1通过代理连接",即使我使用了提示,我仍然无法在Internet Explorer上进行会话信息。

  • 我在Internet Explorer> Internet选项中添加了完整URL

  • 在Internet Explorer> Internet选项中>安全>安全>本地Intranet>"高级",我还添加了域的同一部分(MyCompany.com)(MyCompany.com),而不是在Firefox中添加以使其正常工作,但这确实如此无济于事。

编辑2015-05-18:

根据@timclutton在答案中所说的:

<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 AUTHORITYANONYMOUS LOGON" denied 
</Directory>

编辑2015-05-19:

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

    Authtype Basic authname"需要身份验证" authuserfile" e:/路径/。htpasswd" 需要有效的用户

    订单允许,否认 从所有人那里允许

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

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

  • type:" abter:config"在addressbar中
  • 检查网络。
  • 将值设置为您的域或域的一部分,例如mycompany.com(用逗号多个值分开)

对于IE,您需要为您的页面(Intranet)设置比其他互联网的安全设置。请参阅https://superuser.com/questions/148063/why-does-internet-ecplorer-ecplorer-epplorer-keep-masking-me-for-ntlm-ntlm-credentials-in-in-an-in-in-in-in-intranet-zo

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

我怀疑问题是在您的httpd.conf上,因为您使用的是Apache 2.2 allow/deny语法,并且在Apache 2.4中不起作用(除非您启用了mod_access_compat模块)。您应该阅读Apache文档中的2.2的升级到2.4。

确保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 AUTHORITYANONYMOUS LOGON" denied
</Directory>

相关内容

最新更新