Python使用ldap3操作微軟AD
- 2020 年 1 月 3 日
- 筆記
對於client連接ldap server的策略,ldap3提供了4種選擇,可以通過client_strategy設置Connection object應用哪種策略:
l SYNC
l ASYNC
l RESTARTABLE
l REUSABLE
同步策略(SYNC, RESTARTABLE),所有的ldap操作返回True/False
非同步策略(ASYNC, REUSABLE)返回一個msgid(一個數值),非同步策略發送請求後不用等待響應,需要響應的時候直接使用get_response(message_id)獲取結果。等待響應的超時時間可以通過get_response的timeout參數指定,默認10s。如果使用get_request=True in the get_response(),同時會返回發送的請求字典。
建立連接:




建立Server對象時使用get_info=ldap3.ALL參數,建立Connection連接之後可以獲取到server資訊(匿名獲取),從中可以獲取到域名資訊,域控電腦名,ldap server支援的Extension和Control
建立Server時指定 active=True,建立連接前會先檢查ldap server的可用性;active=5指定拋出 LDAPServerPoolExhaustedError異常之前重試的次數
exhaust=True : 如果ldap server不時active,server將會從pool中移除。exhaust=10:設置為數值,表示認為server 10s不可達,則認為它為offline,

When all servers in a pool are not available the strategy will wait for the number of seconds specified in ldap.
POOLING_LOOP_TIMEOUT before starting a new cycle. This defaults to 10 seconds.
The pool can have different HA strategies:
• FIRST: gets the first server in the pool, if 『active』 is set to True gets the first available server
• ROUND_ROBIN: each time the connection is open the subsequent server in the pool is used. If active is set to
True unavailable servers will be discarded
• RANDOM: each time the connection is open a random server is chosen in the pool. If active is set to True
unavailable servers will be discarded
A server pool can be defined in different ways:
server1 = Server('server1')
server2 = Server('server2')
server3 = Server('server1', port=636, use_ssl=True)
• explicitly with Server objects in the init:
server_pool = ServerPool([server1, server2, server3], POOLING_STRATEGY_ROUND_
˓→ ROBIN, active=True, exhaust=True)
• explicitly with an add operation in the pool object:
server_pool = ServerPool(None, POOLING_STRATEGY_ROUND_ROBIN_ACTIVE)
server_pool.add(server1)
server_pool.add(server2)
server_pool.add(server3)
44 Chapter 1. Contents
ldap3 Documentation, Release 2.5
• implicitly directly in the Connection object init (passing a list of servers):
conn = Connection([server1, server2, server3]) # the ServerPool object is
˓→ defined with the default pooling strategy
Pools can be dynamically changed. You can add and remove Server objects from pools even if they are already used
in Connection:
server4 = Server('server2', port=636, use_ssl=True)
server_pool.remove(server2)
server_pool.add(server4)
Connections are notified of the change and can reopen the socket to the new server at next open() operation.
You can also save the schema and info in a json string:
json_info = server.info.to_json()
json_schema = server.schema.to_json()
or can have them saved on file:
server.info.to_file('server-info.json')
server.schema.to_file('server-schema.json')
to build a new server object with the saved json files you can retrieve them with:
from ldap3 import DsaInfo, SchemaInfo
dsa_info = DsaInfo.from_file('server-info.json')
schema_info = SchemaInfo.from_file('server-schema.json')
server = Server('hostname', dsa_info, schema_info)
ldap server的Schema資料庫中存儲了ldap server中的對象的已知類型資訊,可以通過server.schema獲取到(微軟AD需要鑒權,匿名用戶無法獲取),裡面存儲了ldap server理解那些數據類型,同時也指定,哪些屬性被ldap server中的對象支援

使用鑒權用戶連接ldap server後可以查看server.shema等高級別操作。查看當前鑒權用戶資訊。以下連接使用的不安全的連接,密碼資訊明文傳輸,可以被抓取。使用authentication=ldap3.NTLM的鑒權方式無法顯示的看到鑒權資訊。



可以使用以下方式建立安全連接,2種方式都是建立TLS連接:
l LDAP over TLS
l the StartTLS extended operation ##微軟AD不支援
ldap查詢
ldap查詢基於search_base和search_filter,filter是個表達式:
l 查詢所有顯示名叫John並且email以『@example.org』結尾的用戶:(&(givenName=John)(mail=*@example.org))
l 查詢顯示名為Jhon或者Fred並且郵箱以@example.org結尾的用戶
(&
(|
(GivenName=Jhon)
(givenName=Fred)
)
( mail=*@example.org)
)
搜索search_base下的所有用戶,默認search_scope='SUBTREE',沒有指定請求任何attribute,只返回entries的distinguished Name,請求成功(同步strategy)返回True,conn.entries獲取查詢到的結果:
conn.search(base_search,'(objectclass=person)')
conn.entries

可以使用訪問字典或者訪問對象屬性的方式訪問從server上獲取到的attribute值,有些屬性不區分大小寫,raw_values獲取到的是從server返回的原始的值:



返回的entry可以格式化為json字元串

如果查詢的屬性的值為空,返回的entries中將不包含此屬性,除非在Connection中指定return_empty_attributes=False,微軟AD中貌似不起作用。

對ldap server進行search操作之後,Connection有以下屬性可以訪問:

在AD上增加entry,第一個參數為增加的對象dn,第二個參數為object_class,指定創建的object的類型,第三個參數為object提供的個性化attribute:

域控支援的objectclass可以通過server.schema獲取到,創建不同類型的objectclass支援哪些attribute可以通過server.schema.object_classes['user']方式獲取到,大多數attribute在創建object的時候都是可選的,必選參數會單獨列出:


重命名一個dn,利用modify_dn提供的參數new_superior=new_dn,還可以將dn從一個ou移動到另一個ou:


檢查object的屬性是否和給定值一樣。
