python - 在 Python 3.x 中更改 Active Directory 用户密码

标签 python active-directory passwords ldap python-3.5

我正在尝试制作一个 Python 脚本,它将打开与运行 AD 的服务器的 LDAP 连接,获取搜索条目(在本例中为名称),搜索该条目并将该用户密码更改为随机生成的密码(以及设置在登录时更改密码的选项),然后向他们发送一封包含新临时密码的自动安全电子邮件。

到目前为止,我已经能够连接到服务器,并搜索返回的单个 DN。正在生成临时密码,并正在发送电子邮件(尽管密码未经过哈希处理,并且电子邮件尚不安全)。但是,我找不到关于从这里去哪里的任何信息。

我找到了 Change windows user password with python但是我发现这在 AD 上效果不佳,而且我发现的 Python 文档中的其他 LDAP 似乎从 2.x 开始就过时了,不再有效。 ldap3 ( https://media.readthedocs.org/pdf/ldap3/stable/ldap3.pdf ) 的文档似乎也没有真正提到它,详尽的谷歌搜索也没有结果。我是这种编程的新手,以前只有低水平或学术知识,所以这有点令人沮丧,但 Python 是我最强的语言。

----------------当前状态的编辑代码--------------------

#Takes input for name which will be used for search criterion
zid = input("ZID: ")
zid = str(zid).lower()
print(zid)

#Binds session to the server and opens a connection
try:
    server = ldap3.Server('ldap://<IP_Address>', get_info=all)
    conn = ldap3.Connection(server, '%s@something.com' %zid, password = "<something>", auto_bind=True) 
    print("Successfully bound to server.\n")
except:
    print("Unsucessful initialization of <IP_Address>")
    try:
        server = ldap3.Server('ldap://<IP_Address>', get_info=all)
        conn = ldap3.Connection(server, '%s@something.com' %zid, password = "<something>", auto_bind=True) 
        print("Successfully bound to server.\n")
    except:
        print("Unsucessful initialization of <IP_Address>")
        try:
            server = ldap3.Server('ldap://<IP_Address>', get_info=all)
            conn = ldap3.Connection(server, '%s@something.com', password = "<something>", auto_bind=True) %zid 
            print("Successfully bound to server.\n")
        except:
            print("Unsucessful initialization of <IP_Address>")
            sys.exit(0)

#Searches and prints LDAP entries
try:
    base_dn = 'DC=<something>,DC=<something>,DC=<something>,DC=<something>,DC=com'
    zid_filter = '(sAMAccountName=%s)' %zid
    conn.search(base_dn, zid_filter, attributes=['mail'])

    #i.e. "DN: CN=<First Last>,OU=<something>, DC= <something>
    user_dn = str(conn.entries)

    #i.e. "CN=<First Last>"
    front = user_dn.find('C')
    back = user_dn.find(',')
    user_cn = user_dn[front:back]

    #i.e. "<First Last>"
    display_name = user_cn[3:]

    #i.e. "first.last@<something>.com"
    raw_email = str(conn.entries)
    front = raw_email.find('mail: ')
    back = raw_email.find('@<something>.com')
    user_email = raw_email[front + 6:back] + '@<something>.com'
except:
    print("Could not search entries")

#Generates random 12 digit alpha-numeric password
try:
    new_password = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(12))
    print(new_password)
    print("New password successfully generated")
except:
    print("New password could not be generated")


#Set and replace AD Password
try:
    conn.extend.microsoft.modify_password(user_dn, None, new_password)
    print ("Active Directory password was set successfully!")
except:
    print('Error setting AD password')
    sys.exit(0)

在整个过程中,对于如何获取/设置用户密码以及出于安全目的对密码进行哈希处理,您有什么建议吗?对于电子邮件,我想我可以强制它使用 HTTPS,这就足够了,但是我想保护将 new_password 传递给服务器的连接。

最佳答案

ldap3包含修改AD密码的具体方法,生成新密码后添加以下内容即可:

dn = conn.entries[0].entry_get_dn() # 假设你得到了一个条目 conn.extend.microsoft.modify_password(dn,无,新密码)

这应该正确编码密码并将其存储在 AD 中。

关于python - 在 Python 3.x 中更改 Active Directory 用户密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37847042/

相关文章:

python - 漂亮的汤刮刀和 if...else

ruby - 如何连接到使用 LDAP 进行身份验证的 HTTP Rest API

php - 散列密码,从 splinter 的方法到现在最安全的方法

security - 什么可以保护无线网络免受同名假冒网络的侵害?

python multiprocessing - 在使用 Process.start(target=func) 调用的函数中访问进程名称

python - pyQt5:无法连接 QSpinBox::valueChanged(int)

python - Python中的NameError - 无法调用函数

powershell - 获取联系人通讯组

active-directory - 访问创建用户 : Exception 0000202B: RefErr: DSID-031007EF, 数据 0、1 个访问点时出现重大事件异常”[扩展错误 8235]

javascript - 为另一个函数保留 JQuery 变量(全局变量?)