Active directory 通过ldapmodify修改Active Directory密码

Active directory 通过ldapmodify修改Active Directory密码,active-directory,ldap,ldif,Active Directory,Ldap,Ldif,我正在研究各种LDAP操作的脚本。然而,我在创建Active Directory用户时遇到了一些困难 通过ldapmodify命令加载时,以下LDIF失败: dn: CN=Frank,CN=Users,DC=domain,dc=local changeType: add objectClass: top objectClass: person objectClass: organizationalPerson objectClass: user cn: Frank userPrincipalNa

我正在研究各种LDAP操作的脚本。然而,我在创建Active Directory用户时遇到了一些困难

通过
ldapmodify
命令加载时,以下LDIF失败:

dn: CN=Frank,CN=Users,DC=domain,dc=local
changeType: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: Frank
userPrincipalName: frank@domain.local
sAMAccountName: frank
givenName: Frank
sn: Stein
displayName: Frank Stein
description: Frankenstein's User
userAccountControl: 512
unicodePwd: "AnExamplePassword1!"
尝试通过LDIF添加用户时,我使用了以下命令:

ldapmodify -H 'ldaps://<ip-of-server>:636' -D 'DOMAIN\Administrator' -x -W -f frank-add.ldif
这是密码策略拒绝用户的问题

但是,以下Python脚本可以工作:

#!/usr/bin/python

import ldap
import ldap.modlist as modlist

AD_LDAP_URL='ldaps://<ip-of-server>:636'
ADMIN_USER='DOMAIN\Administrator'
# User must be authorized to create accounts, naturally.
ADMIN_PASSWORD='password for ADMIN_USER'
BASE_DN='dc=domain,dc=local'

username='frank'
firstname='Frank'
surname='Stein'
displayName = "Frank Stein"

password='AnExamplePassword1!'
# The value of password still needs to adhere to the domain's password policy.
unicode_pass = unicode('\"' + password + '\"', 'iso-8859-1')
password_value = unicode_pass.encode('utf-16-le')

l = ldap.initialize(AD_LDAP_URL)
l.simple_bind_s(ADMIN_USER, ADMIN_PASSWORD)

dn=str('CN=%s,CN=Users,DC=domain,dc=local' % firstname)

attrs = {}

attrs['objectclass'] = ['top','person','organizationalPerson','user']
attrs['cn'] = str(username)
attrs['sAMAccountname'] = str(username)
attrs['unicodePwd'] = str(password_value)
attrs['givenName'] = str(firstname)
attrs['sn'] = str(surname)
attrs['displayName'] = str(displayName)
attrs['description'] = str("Frankenstein's User")
attrs['userPrincipalName'] = str("%s@domain.local" % username)
attrs['userAccountControl'] = str(512)

ldif = modlist.addModlist(attrs)
l.add_s(dn,ldif)
#/usr/bin/python
导入ldap
将ldap.modlist导入为modlist
AD_LDAP_URL='ldaps://:636'
ADMIN\u USER='DOMAIN\Administrator'
#当然,用户必须被授权创建帐户。
ADMIN\u PASSWORD='ADMIN\u用户的密码'
BASE_DN='dc=domain,dc=local'
用户名class='frank'
名为“弗兰克”
姓class='Stein'
displayName=“弗兰克·斯坦”
password='AnExamplePassword1!'
#密码的值仍然需要遵守域的密码策略。
unicode\u pass=unicode(“\”+密码+“\”,“iso-8859-1”)
password\u value=unicode\u pass.encode('utf-16-le')
l=ldap.initialize(AD_ldap_URL)
l、 简单绑定(管理员用户、管理员密码)
dn=str('CN=%s,CN=Users,DC=domain,DC=local'%firstname)
属性={}
属性['objectclass']=['top'、'person'、'organizationalPerson'、'user']
attrs['cn']=str(用户名)
attrs['sAMAccountname']=str(用户名)
attrs['unicodewd']=str(密码值)
attrs['givenName']=str(名字)
attrs['sn']=str(姓氏)
attrs['displayName']=str(displayName)
attrs['description']=str(“弗兰肯斯坦的用户”)
属性['userPrincipalName']=str(“%1”)s@domain.local%username)
attrs['userAccountControl']=str(512)
ldif=modlist.addModlist(attrs)
l、 添加(dn、ldif)
使用Python脚本,我可以立即使用用户的密码(减去转义的引号)登录。我仍然可以通过选择像“password”这样过于简单的密码来触发相同的“不愿意执行”错误。但是,在这种情况下,使用的密码是相同的

据我所知,操作应该是相同的。破坏LDIF文件的区别在于我处理密码所需引号的方式。如果我通过将userAccountControl的值设置为544而不包括密码来创建禁用的帐户,则通过LDIF创建成功。但是,这意味着我需要手动去重置用户的密码

到目前为止,我已经通过LDIF尝试了以下密码格式:

  • 没有引用
  • 简单的引用
  • 转义引号通过\
  • 通过ASCII转义的引号:{\22}
  • 使用Python对密码进行Base64编码(带引号和不带引号,LDIF的格式修改为
    unicodewd::

虽然我很高兴我有了一种通过Python添加用户的有效方法,但我仍然有点困惑如何在使用LDIF文件和
ldapmodify
时正确地转义密码值。是否有我不考虑的替代方法?

为什么不使用ldifde和unicode base64编码密码,如下所述:

您的python脚本似乎将密码编码为unicode/base64。也许您的密码需要在ldif文件中编码(编码时使用引号),而不是像您在示例中所做的那个样使用纯文本

例如:


请参阅您提供的示例密码。

我已尝试使用引号对密码进行编码,但我的编码方法必须关闭。我试图用Python使用
base64.b64encode(unicode(“\”AnExamplePassword1!\”)
对我的密码进行编码,这产生了
IKfurXHbXBzVBHC3N3B3JKMSEI
。我能够通过LDIF成功地添加用户和您的密码版本。我能够按照Python脚本生成密码的方法生成unicodePwd的值。我认为一些字符编码是理所当然的,因为它在打印时看起来没有变化。我完成了与Python脚本相同的所有步骤,然后使用了
base64.b64encode(password\u value)
。这给了我你的
IgBB…
值。你在创建密码后测试过密码吗?我已经确认密码是正确的。我可以使用LDIF创建的帐户,使用指定的密码“AnExamplePassword1!”登录工作站(减去引号)。干得好。。!很抱歉,我没有正确阅读您的问题,因为您已经说过您已经尝试了编码。祝你白天/晚上/等愉快:)
#!/usr/bin/python

import ldap
import ldap.modlist as modlist

AD_LDAP_URL='ldaps://<ip-of-server>:636'
ADMIN_USER='DOMAIN\Administrator'
# User must be authorized to create accounts, naturally.
ADMIN_PASSWORD='password for ADMIN_USER'
BASE_DN='dc=domain,dc=local'

username='frank'
firstname='Frank'
surname='Stein'
displayName = "Frank Stein"

password='AnExamplePassword1!'
# The value of password still needs to adhere to the domain's password policy.
unicode_pass = unicode('\"' + password + '\"', 'iso-8859-1')
password_value = unicode_pass.encode('utf-16-le')

l = ldap.initialize(AD_LDAP_URL)
l.simple_bind_s(ADMIN_USER, ADMIN_PASSWORD)

dn=str('CN=%s,CN=Users,DC=domain,dc=local' % firstname)

attrs = {}

attrs['objectclass'] = ['top','person','organizationalPerson','user']
attrs['cn'] = str(username)
attrs['sAMAccountname'] = str(username)
attrs['unicodePwd'] = str(password_value)
attrs['givenName'] = str(firstname)
attrs['sn'] = str(surname)
attrs['displayName'] = str(displayName)
attrs['description'] = str("Frankenstein's User")
attrs['userPrincipalName'] = str("%s@domain.local" % username)
attrs['userAccountControl'] = str(512)

ldif = modlist.addModlist(attrs)
l.add_s(dn,ldif)
unicodePwd:: IgBBAG4ARQB4AGEAbQBwAGwAZQBQAGEAcwBzAHcAbwByAGQAMQAhACIA