吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 581|回复: 3
上一主题 下一主题
收起左侧

[Python 原创] python 通过域控验证用户是否有效

[复制链接]
跳转到指定楼层
楼主
fengjicheng 发表于 2024-10-25 09:06 回帖奖励
业务提出读取某系统导出用户表,验证这些用户是否在域控中有效,故写如下代码
[Python] 纯文本查看 复制代码
from ldap3 import Server, Connection, ALL, NTLM
from ldap3.utils.dn import parse_dn
import pandas as pd


def normalize_dn(dn):
    """Normalize the DN by parsing and reformatting it."""
    parsed_dn = parse_dn(dn)
    sorted_parsed_dn = sorted(parsed_dn, key=lambda x: x[0])  # Sort by attribute type
    normalized_dn = ','.join(['='.join([part[0], part[1]]) for part in sorted_parsed_dn])
    return normalized_dn

def compare_dns(dn1, dn2):
    """Compare two DNs after normalizing them."""
    normalized_dn1 = normalize_dn(dn1)
    normalized_dn2 = normalize_dn(dn2)
    return normalized_dn1.lower() == normalized_dn2.lower()

def checkWmsUserStatus(admin_c, row):
    #账户启用标志
    enabled_user_flag = [512, 544, 66048, 262656]
    # 账户禁用标志
    disabled_user_flag = [514, 546, 66050, 66080, 66082]
    adDn = row["用户名"]
    hostname = row["登录 ID"]
    admin_c.search(search_base=search_base, search_filter='(sAMAccountName=%s)' % hostname, attributes=['distinguishedName', 'description', 'userAccountControl', 'userWorkstations', 'lockoutTime'])
    
    if len(admin_c.entries) > 0:
        adUserInfor = admin_c.entries[0]
        entry_dn = adUserInfor["distinguishedName"].value
        
        # 比较两个dn是否一致
        if not compare_dns(entry_dn,adDn):
            return False
        
        if adUserInfor['lockoutTime'].value is not None and str(adUserInfor['lockoutTime'].value) != '1601-01-01 00:00:00+00:00':
            return False
        
        # 判断是否禁用 1.状态在禁用状态  2.或者 状态不在 启用状态
        if (adUserInfor['userAccountControl'] in disabled_user_flag) or not (adUserInfor['userAccountControl'] in enabled_user_flag) :
            return False
        
        if adUserInfor['description'].value is not None and "禁" in adUserInfor["description"].value:
            return False
        
        # 允许登录所有计算机用户
        if  adUserInfor['userWorkstations']  is not None:
            return True
            
        # 设置允许登录域控yk01 与 yk02 的用户
        if adUserInfor['userWorkstations'].value is not None and ("yk01" in adUserInfor["userWorkstations"].value or "yk02" in adUserInfor["userWorkstations"].value):
            return True
        
        #未找到
        return False
    # 未找到
    return False

if __name__ == '__main__':
    file_path = '*.xlsx'
    server = Server('172.28.*.*', get_info=ALL)
    search_base = "DC=*,DC=com"
    admin_c = Connection(server, user='*', password='*', auto_bind=True, authentication=NTLM)
    # pdf 读取xlsx 文件 seet 标签为 USER_DATA  跳过第一行
    df = pd.read_excel(file_path, sheet_name='USER_DATA', skiprows=1)
    
    results = []
    
    for index, row in df.iterrows():
        # 检查满足条件用户
        if checkWmsUserStatus(admin_c, row):
            results.append(row)
   
    # 写入pd
    result_df = pd.DataFrame(results)
    with pd.ExcelWriter("outfile.xlsx") as writer:
        result_df.to_excel(writer, index=False, sheet_name='Valid Users')

    admin_c.unbind()

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
jun269 发表于 2024-10-25 10:54
楼主能否写成UI界面,并且打包成型,谢谢
3#
 楼主| fengjicheng 发表于 2024-10-25 14:00 |楼主
jun269 发表于 2024-10-25 10:54
楼主能否写成UI界面,并且打包成型,谢谢

都玩py了,再玩界面没啥意义把。
4#
昨天√ 发表于 2024-10-25 15:53
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 09:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表