Python ldap3 NTLM 无法返回 json.loads 数据

0 投票
0 回答
21 浏览
提问于 2025-04-11 22:28

我正在尝试用 Python 和 ldap3 库把一个 AD 用户账户添加到 AD 组中,使用的脚本如下:

# Import necessary modules and libraries
import requests
from flask import json
from ldap3 import Server, Connection, ALL_ATTRIBUTES, SUBTREE, NTLM
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups

# Test API data
testuser = r"TS\testuser"

# Define LDAP server details
Server_ip = '192.168.2.3'

# Define bind user credentials
#BIND_Username = 'CN=Automation,CN=Users,DC=testnetwerk,DC=com'
BIND_Username = 'TESTNETWERK\\Automation'
BIND_Password = 'Welkom123!'

# Define LDAP paths
Base_DN = "dc=testnetwerk,dc=com"
Filter = "(sAMAccountName={0}*)"  # LDAP filter to search for users based on sAMAccountName
Group_DN = "CN=testgroup,CN=Users,DC=testnetwerk,DC=com"  # DN of the group to which users will be added

# Function to create an LDAP Server object
def server_ldap():
    return Server(Server_ip)


# Function to establish connection to LDAP server
def connect_ldap():
    server = server_ldap()
#    return Connection(server, user=BIND_Username, password=BIND_Password, auto_bind=True)
    return Connection(server, user=BIND_Username, password=BIND_Password, authentication=NTLM)

# Function to search for a user in LDAP directory based on sAMAccountName
def find_user(username):
    with connect_ldap() as c:
        print("Connected to LDAP server")
        # Perform LDAP search operation
        c.search(search_base=Base_DN, search_filter=Filter.format(username[3:]), search_scope=SUBTREE,
                 attributes=ALL_ATTRIBUTES, get_operational_attributes=True)
    # Return search results in JSON format
        print(json.loads(c.response_to_json()))
    return json.loads(c.response_to_json())


# Function to add the found user to the specified LDAP group
def add_user_to_group(username):
    # Retrieve the DN (Distinguished Name) of the user from search results
    user = find_user(username)["entries"][0]["dn"]
    print(user)
    # Add user to the specified group
    ad_add_members_to_groups(connect_ldap(), user, Group_DN)
    # Return confirmation message
    return "Added " + user + " to the group!"



print(find_user(testuser))
try:
    # Attempt to add test user to the group and print confirmation
    print(add_user_to_group(testuser))
except Exception as e:
    # Print error message if an exception occurs
    print("ai ai ai")
    print(e)


但是,当我用 print(json.loads(c.response_to_json())) 打印出应该返回的值时,它却没有返回,反而给我报了个错:TypeError: the JSON object must be str, bytes or bytearray, not NoneType

如果我取消注释 #BIND_Username = 'CN=Automation,CN=Users,DC=testnetwerk,DC=com'# return Connection(server, user=BIND_Username, password=BIND_Password, auto_bind=True),并注释掉其他的代码,它就能正常工作了。

当返回不成功时,打印出来的响应是:

{'entries': [{'attributes': {'accountExpires': '9999-12-31 23:59:59.999999+00:00', 'badPasswordTime': '1601-01-01 00:00:00+00:00', 'badPwdCount': 0, 'cn': 'Test User', 'codePage': 0, 'countryCode': 0, 'dSCorePropagationData': ['1601-01-01 00:00:00+00:00'], 'displayName': 'Test User', 'distinguishedName': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com', 'givenName': 'Test', 'instanceType': 4, 'lastLogoff': '1601-01-01 00:00:00+00:00', 'lastLogon': '1601-01-01 00:00:00+00:00', 'logonCount': 0, 'name': 'Test User', 'objectCategory': 'CN=Person,CN=Schema,CN=Configuration,DC=testnetwerk,DC=com', 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'objectGUID': '{bdfd2aa0-2fcf-46df-9417-2396360fb83f}', 'objectSid': 'S-1-5-21-813124840-2969614714-1384511549-1106', 'primaryGroupID': 513, 'pwdLastSet': '2024-03-29 12:50:26.547474+00:00', 'sAMAccountName': 'testuser', 'sAMAccountType': 805306368, 'sn': 'User', 'uSNChanged': 12835, 'uSNCreated': 12830, 'userAccountControl': 66048, 'userPrincipalName': 'testuser@testnetwerk.com', 'whenChanged': '2024-03-29 12:50:26+00:00', 'whenCreated': '2024-03-29 12:50:26+00:00'}, 'dn': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com'}]}

当返回成功时,打印出来的响应是:

{'entries': [{'attributes': {'accountExpires': '9999-12-31 23:59:59.999999+00:00', 'badPasswordTime': '1601-01-01 00:00:00+00:00', 'badPwdCount': 0, 'cn': 'Test User', 'codePage': 0, 'countryCode': 0, 'dSCorePropagationData': ['1601-01-01 00:00:00+00:00'], 'displayName': 'Test User', 'distinguishedName': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com', 'givenName': 'Test', 'instanceType': 4, 'lastLogoff': '1601-01-01 00:00:00+00:00', 'lastLogon': '1601-01-01 00:00:00+00:00', 'logonCount': 0, 'name': 'Test User', 'objectCategory': 'CN=Person,CN=Schema,CN=Configuration,DC=testnetwerk,DC=com', 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'objectGUID': '{bdfd2aa0-2fcf-46df-9417-2396360fb83f}', 'objectSid': 'S-1-5-21-813124840-2969614714-1384511549-1106', 'primaryGroupID': 513, 'pwdLastSet': '2024-03-29 12:50:26.547474+00:00', 'sAMAccountName': 'testuser', 'sAMAccountType': 805306368, 'sn': 'User', 'uSNChanged': 12835, 'uSNCreated': 12830, 'userAccountControl': 66048, 'userPrincipalName': 'testuser@testnetwerk.com', 'whenChanged': '2024-03-29 12:50:26+00:00', 'whenCreated': '2024-03-29 12:50:26+00:00'}, 'dn': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com'}]}

还有当我调用 print(find_user(testuser)) 时返回的响应:

{'entries': [{'attributes': {'accountExpires': '9999-12-31 23:59:59.999999+00:00', 'badPasswordTime': '1601-01-01 00:00:00+00:00', 'badPwdCount': 0, 'cn': 'Test User', 'codePage': 0, 'countryCode': 0, 'dSCorePropagationData': ['1601-01-01 00:00:00+00:00'], 'displayName': 'Test User', 'distinguishedName': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com', 'givenName': 'Test', 'instanceType': 4, 'lastLogoff': '1601-01-01 00:00:00+00:00', 'lastLogon': '1601-01-01 00:00:00+00:00', 'logonCount': 0, 'name': 'Test User', 'objectCategory': 'CN=Person,CN=Schema,CN=Configuration,DC=testnetwerk,DC=com', 'objectClass': ['top', 'person', 'organizationalPerson', 'user'], 'objectGUID': '{bdfd2aa0-2fcf-46df-9417-2396360fb83f}', 'objectSid': 'S-1-5-21-813124840-2969614714-1384511549-1106', 'primaryGroupID': 513, 'pwdLastSet': '2024-03-29 12:50:26.547474+00:00', 'sAMAccountName': 'testuser', 'sAMAccountType': 805306368, 'sn': 'User', 'uSNChanged': 12835, 'uSNCreated': 12830, 'userAccountControl': 66048, 'userPrincipalName': 'testuser@testnetwerk.com', 'whenChanged': '2024-03-29 12:50:26+00:00', 'whenCreated': '2024-03-29 12:50:26+00:00'}, 'dn': 'CN=Test User,CN=Users,DC=testnetwerk,DC=com'}]}

有没有什么想法?

0 个回答

暂无回答

撰写回答