王工自研域自助做事台架构图,具备长期未改密企业微信提醒、自助改密解锁等功能全面对标宁盾微软AD自助修正密码办理方案https://www.nington.com/solution-adpassword/每年可为公司节省5W-10W元
解释王工域控为windows2022,Self Service Password搭建在OracleLinux8上,python版本为python3最新版本,PHP为OracleLinux8默认源中的PHP7
预览关照改密

自助改密
架构解析:1、域控上域账户掩护pager属性(寻呼机),修正为企业微信ID2、域控运行扫描脚本,通过打算上次修正密码韶光,超过指定日期,进行企业微信提醒;如果未掩护pager属性,写入日志3、Self Service Password域控自助做事台二次开拓,改为企业微信吸收验证码改密4、进行企业微信提醒时,先查询redis缓存,如果access_token不存在,则获取一次,如果存在,直策应用,缓存5400秒自动过期。5、建立企业微信运用,可参考我的zabbix文章
搭建条件1、已掩护域控pager属性为企业微信userid,此信息须要企业微信管理员后台查询。2、已精确支配Self Service Password,可以看我之前的文章。3、已支配redis,建议利用docker支配,一定要设置redis密码4、已为php增加php-redis扩展
docker一键支配redis红帽系系统默认为podman替代docker
podman pull redispodman run --restart=always -p 6379:6379 --name myredis -d redis --requirepass passwd@123
扫描脚本:扫描脚本同样有两部分组成,第一部分是powershell脚本,用于获取域用户信息可指定OU、可自定义要获取的用户属性,天生的文件放在C盘根目录下1.txt,与python脚本对应adgetuser.ps1
Get-ADUser -Filter 'Name -like ""' -SearchBase "OU=测试组,OU=用户OU,DC=90apt,DC=com" -Properties | Select-Object name,passwordlastset,pager > c:/1.txt
运行结果
name passwordlastset pager ---- --------------- ----- 王忘杰1 2023/5/18 16:39:05 WangWangJie1 王忘杰2 2022/9/26 16:50:41 WangWangJie2
第二部分是扫描关照脚本,由主python文件和配置文件ad.config组成,运行后天生errlog.txt日志文件ad.config
属性解释corpid:appsecret:agentid:content:内容1content1:内容2content2:内容3admin:闲置属性ip:redis地址port:redis端口passwd:redis密码passwddate:密码多少天未修正进行提醒
{"corpid" : "xxxx","appsecret" : "xxxx","agentid" : "xxxx","content" : "亲爱的 ","content1" : " 域用户 :\n您的打算机域账户已经超过 ","content2" : " 天没有修正密码了(电脑登录密码),请您立即变动。\n重置密码过程请遵照以下原则:\n○密码长度最少 8 位;\n○密码中不可涌现公司和本人中英文拼写\n○密码符合繁芜性需求(大写字母、小写字母、数字和符号四种中必须有三种)\n操作办法:\n您可以通过 自助密码做事台http://xx/修正密码,在公司内网中,手机、条记本、台式机均可访问","admin" : "xxxx","ip" : "xxxx","port" : "xxxx","passwd" : "xxxx","passwddate" : xx}
主python文件
import requests,json,redis,time,loggingfrom datetime import datetime, timedeltadef get_weixintoken(): #获取微信token token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + config[0] + '&corpsecret=' + config[1] req = requests.get(token_url) accesstoken = req.json()['access_token'] return accesstokendef get_redistoken(): readredis = redis.Redis(connection_pool=redis.ConnectionPool(host=config[7],port=config[8],password=config[9],decode_responses=True)) if readredis.get('key') == None: readredis.set('key', get_weixintoken(),ex=5400) return (readredis.get('key')) else: return readredis.get('key')def post_weixin(userweixin,content): body = { "touser": userweixin, "msgtype": "text", "agentid": config[2], "text": { "content": content } } postweixin = requests.post( 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='+get_redistoken(),data=json.dumps(body)) return(postweixin.text)def get_config(): config = json.loads(open("ad.config", encoding='utf-8').read()) return [config['corpid'],config['appsecret'],config['agentid'],config['content'],config['content1'],config['content2'],config['admin'],config['ip'],config['port'],config['passwd'],config['passwddate']]def user_check(): f = open("C:\\1.txt", "r", encoding='utf-16') lines = f.readlines() f = open('errlog.txt', 'w') for line in lines: try: x = line.replace("/", "-") y = x.split() time_1 = y[1] time_2 = time.strftime("%Y-%m-%d", time.localtime()) time_1_struct = datetime.strptime(time_1, "%Y-%m-%d") time_2_struct = datetime.strptime(time_2, "%Y-%m-%d") day = (time_2_struct - time_1_struct).days userweixin = y[3] username= y[0] if day > config[10]: day = str(day) time.sleep(1) try: post = post_weixin(userweixin,config[3]+username+config[4]+day+config[5]) postjson=json.loads(post) if postjson['errmsg'] != "ok": f.write("发送失落败,可能微旗子暗记缺点 " + userweixin+"\n") except : None else: None except: f.write("没有微旗子暗记 "+ line) f.close()config = get_config()#post_weixin()user_check()
脚本利用编译为EXE文件,和ad.config,放在域控做事器通过定时任务运行即可。
Self Service Password企业微信脚本项目目录/usr/share/self-service-password/配置文件/usr/share/self-service-password/conf/config.inc.local.php配置文件中修正短信关照办法
## SMS# Use sms$use_sms = true;# SMS method (mail, api)$sms_method = "api";$sms_api_lib = "lib/weixin.inc.php";# GSM number attribute$sms_attributes = array( "pager" );
编写企业微信关照脚本/usr/share/self-service-password/lib/weixin.inc.php
<?php//连接本地的 Redis 做事function get_token(){ $redis = new Redis(); $redis->connect('修正用自己的IP地址', 修正用自己的端口); $redis->auth('修正用自己的redis密码'); $key = $redis->get("key"); if ($key) { return $key; } else { $url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=修正用自己的&corpsecret=修正用自己的'; $jsondb = file_get_contents($url); $jsondb = json_decode($jsondb, true); $key = $jsondb['access_token']; $redis->set("key", $key); $redis->expire("key", 5400); return $key; }}function send_sms_by_api($mobile, $message) { $postdata = array( 'touser' => "$mobile", 'msgtype' => 'text', 'agentid' => '修正用自己的', 'text' => array( 'content' => "$message" ) ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' . get_token()); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $errmsg = json_decode(curl_exec($ch))->errmsg; if ($errmsg=="ok") { return 1; } else { return 0; }}?>
修正中文显示比如把短信修正成企业微信,可直接修正措辞文件
/usr/share/self-service-password/lang/zh-CN.inc.php
PHP安装redis扩展
总结大略