使用PHP脚本更改Active Directory用户密码

Moh*_*din 2 php ldap active-directory

我试图获得一个非常简单的PHP脚本来更改我的Active Directory域中的用户密码.

这是我在网上找到的一些脚本:

<?php
$uid = 'Mohammed Noureldin';
$newPassword = '5omeGoodP@ssword';
$bindDn = 'CN=Administrator,OU=UsersOU,DC=example,DC=local';
$bindPassword = 'An0therGoodP@ssword';
$baseDn = 'OU=UsersOU,DC=example,DC=local';
$protocolVersion = 3;

$ldap = ldap_connect('localhost');
if (!ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, $protocolVersion))
{
    exit('Failed to set protocol version to '.$protocolVersion);
}
// bind anonymously so that we can verify if the server really is running
ldap_bind($ldap);
if (ldap_errno($ldap) !== 0)
{
    exit('Could not connect to LDAP server');
}

// now bind with the correct username and password
ldap_bind($ldap, $bindDn, $bindPassword);
if (ldap_errno($ldap) !== 0)
{
    exit('ERROR: '.ldap_error($ldap));
}

$searchResults = ldap_search($ldap, $baseDn, 'cn='.$uid);
// no matching records
if ($searchResults === false)
{
    exit('No user found');
}

if (!is_resource($searchResults))
{
    exit('Error in search results.');
}
// create the unicode password
$len = strlen($newPassword);
$newPass = '"';
for ($i = 0; $i < $len; $i++)
{
    $newPass .= "{$newPassword{$i}}\000";
}
$newPass .= '"';

$entry = ldap_first_entry($ldap, $searchResults);
if (!is_resource($entry))
{
    exit('Couldn\'t get entry');
}
$userDn = ldap_get_dn($ldap, $entry);
if (!$userDn)
{
exit('Errrrrrrrrr1');

}
if (!ldap_modify($ldap, $userDn, array('unicodePwd' => $newPass)))
{
exit(ldap_errno($ldap)." ". ldap_error($ldap));

}
?>
Run Code Online (Sandbox Code Playgroud)

此PHP页面的输出是以下错误消息:

53服务器不愿意执行

并且脚本根本不起作用(用户的密码没有改变).

我知道AD存储密码在unicodePwd字段中的主要原则(如果到目前为止仍然如此),我知道我必须使用安全连接并且我正在使用它(很多时候它正确设置).

我搜索了该错误消息,但我找不到任何功能解决方案.

我也试过了一些其他的脚本,但是这个是迄今为止最好的,因为其他人在之前的一些步骤中给了我一些错误(例如绑定).

我真的很感激任何帮助解决这个问题,甚至另一个功能脚本可能是一个好主意!提前致谢.

Rya*_*ies 5

除非通过SSL/TLS连接,否则不能使用此方法更改密码.如果您使用Google或Bing作为单词unicodePwd,因为您已将其包含在帖子中,那么第一个(如果不是第一个)结果中的一个将是unicodePwd的MSDN文档,该文档在前三个句子中说明:

此属性由LDAP Modify在以下限制条件下写入.Windows 2000操作系统服务器要求客户端具有到DC的128位(或更好)SSL/TLS加密连接,以便修改此属性.在Windows Server 2003操作系统,Windows Server 2008操作系统,Windows Server 2008 R2操作系统,Windows Server 2012操作系统,Windows Server 2012 R2操作系统和Windows Server 2016 Technical Preview操作系统上,DC还允许修改unicodePwd受128位(或更好)简单身份验证和安全层(SASL)层加密而非SSL/TLS保护的连接上的属性.在Windows Server 2008,Windows Server 2008 R2,Windows Server 2012,Windows Server 2012 R2和Windows Server 2016 Technical Preview中,如果dSHeuristics属性(第6.1.1.2.4.1.2节)的fAllowPasswordOperationsOverNonSecureConnection启发式为true且Active Directory为作为AD LDS运行,DC允许通过既不进行SSL/TLS加密也不进行SASL加密的连接修改unicodePwd属性.LDAP搜索永远不会返回unicodePwd属性.

如果您只是执行一个简单的搜索unicodePwd,那么您将获得的第一个结果之一是STEP BY STEP CODE如何执行此操作:

https://support.microsoft.com/en-us/kb/269190