Python LDAP 更改密码无效

Python LDAP change password has no effect

我目前正在尝试使用 Python (3) 和 LDAP 模块修改 AD 上用户的密码。 当我的脚本完成时,一切看起来都很好。 但是,密码和之前一样。

这是我的脚本:

LDAP_SERVER = <domain>
LDAP_USERNAME = <admin_username>
LDAP_PASSWORD = <admin_password>
dn = <DN>
quoted_new_password = '\"' + <new_password> + '\"'
quoted_new_password_bytes = quoted_new_password.encode('UTF-16LE')

ldap_client = ldap.initialize(LDAP_SERVER)
ldap_client.set_option(ldap.OPT_REFERRALS, 0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
ad_user_filter = '(&(objectClass=user)(sAMAccountName=<username-for-password-modification>))'
res = ldap_client.search_s(dn, ldap.SCOPE_SUBTREE, ad_user_filter)
user_dn = (res[0][1]['distinguishedName'][0]).decode("utf-8")
modlist = [ (ldap.MOD_REPLACE, "userPassword", quoted_new_password_bytes)]
ldap_client.modify_s(user_dn, modlist)

结果是一个类似

的元组
(<number>, [], <number>, [])

然后,当我尝试连接 AD(具有相同域)时,旧密码有效,但新密码无效。

我是不是忘记了什么?

提前致谢!

编辑:当我将空字符串作为新密码时,结果是一样的,即使我的 AD 要求至少 14 个字符。

编辑:"modify_s" 的最后结果是

(103, [], 3, [])

然而,103 代码不对应任何东西...

已解决

域是 ldap://the_domain:389。 但它无法工作,因为我必须使用安全服务器:ldaps 而不是 ldap,端口 636 而不是 389。

所以我把LDAP_SERVER改成了ldaps://the_domain:636

但是,我的脚本不再起作用了。 在改编之前,我从另一个 post 那里获取了这个脚本:

import ldap3

SERVER = 'ldaps://thedomain:636'
BASE_DN = "DC=domain,DC=com"
LDAP_USERNAME = "admin_username@thedomain.com"
LDAP_PASSWORD = "admin_password"
CURRENT_PWD = "the_current_password"
NEW_PWD = "the_new_password"
SEARCHED_USERNAME = "M_tete_en_l_air"

SEARCH_FILTER = '(&(objectClass=User)(samaccountname='+SEARCHED_USERNAME +'))'

USER_DN = ""

ldap_server = ldap3.Server(SERVER, get_info=ldap3.ALL)
conn = ldap3.Connection(ldap_server, LDAP_USERNAME, LDAP_PASSWORD, auto_bind=True)
conn.start_tls()

conn.search(search_base = BASE_DN,
         search_filter = SEARCH_FILTER,
         search_scope = ldap3.SUBTREE,
         attributes = ['cn', 'givenName'],
         paged_size = 5)

for entry in conn.response:
    if entry.get("dn") and entry.get("attributes"):
        if entry.get("attributes").get("cn"):
            USER_DN=entry.get("dn")

print(USER_DN)
success = ldap3.extend.microsoft.modifyPassword.ad_modify_password(conn, USER_DN, NEW_PWD, CURRENT_PWD,  controls=None)
print("Password modified: ", success)

(我没有这个脚本)

Source (Whosebug answer)