前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python使用ldap3操作微软AD

Python使用ldap3操作微软AD

作者头像
py3study
发布2020-01-03 12:15:03
1.6K0
发布2020-01-03 12:15:03
举报
文章被收录于专栏:python3python3

对于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(),同时会返回发送的请求字典。

建立连接:

blob.png
blob.png
blob.png
blob.png
blob.png
blob.png
blob.png
blob.png

建立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,

blob.png
blob.png

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中的对象支持

blob.png
blob.png

使用鉴权用户连接ldap server后可以查看server.shema等高级别操作。查看当前鉴权用户信息。以下连接使用的不安全的连接,密码信息明文传输,可以被抓取。使用authentication=ldap3.NTLM的鉴权方式无法显示的看到鉴权信息。

blob.png
blob.png
blob.png
blob.png
blob.png
blob.png

可以使用以下方式建立安全连接,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

blob.png
blob.png

可以使用访问字典或者访问对象属性的方式访问从server上获取到的attribute值,有些属性不区分大小写,raw_values获取到的是从server返回的原始的值:

blob.png
blob.png
blob.png
blob.png
blob.png
blob.png

返回的entry可以格式化为json字符串

blob.png
blob.png

如果查询的属性的值为空,返回的entries中将不包含此属性,除非在Connection中指定return_empty_attributes=False,微软AD中貌似不起作用。

blob.png
blob.png

对ldap server进行search操作之后,Connection有以下属性可以访问:

blob.png
blob.png

在AD上增加entry,第一个参数为增加的对象dn,第二个参数为object_class,指定创建的object的类型,第三个参数为object提供的个性化attribute:

blob.png
blob.png

域控支持的objectclass可以通过server.schema获取到,创建不同类型的objectclass支持哪些attribute可以通过server.schema.object_classes['user']方式获取到,大多数attribute在创建object的时候都是可选的,必选参数会单独列出:

blob.png
blob.png
blob.png
blob.png

重命名一个dn,利用modify_dn提供的参数new_superior=new_dn,还可以将dn从一个ou移动到另一个ou:

blob.png
blob.png
blob.png
blob.png

检查object的属性是否和给定值一样。

blob.png
blob.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档