千家信息网

统计频繁被锁定的AD帐号

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,一、数据处理过程在WinSer执行排程脚本远程控制DC作业,导出7天内帐号的锁定EventLog;通过WinSer中转至LinuxSer;在LinuxSer执行排程对数据进行格式化,过滤出(50次/月
千家信息网最后更新 2025年12月02日统计频繁被锁定的AD帐号

一、数据处理过程

  1. 在WinSer执行排程脚本远程控制DC作业,导出7天内帐号的锁定EventLog;
  2. 通过WinSer中转至LinuxSer;
  3. 在LinuxSer执行排程对数据进行格式化,过滤出(50次/月)的数据。
  4. 通过访问访问\\WinSer可访问共享,(每28天的星期三)拿到处理过的文件(ad20170101.txt..)

二、脚本

2.1 WinSer服务器

  • 脚本一:
    因Windows默认不允许直接在排程运行powshell脚本(服务器有更多限制),所以选择运行Bat脚本调用运行。
powershell  D:\PS\AccountLockOut\Start-AccountLockOut.ps1   
  • 脚本二:
    发送本地脚本文件到DC上执行(便于管理)。
$CredUser="ikulin"    #定义用户$PWD=ConvertTo-SecureString "Iku963" -AsPlainText -Force    #定义密码,转换安全字符,强制明文$Cred=New-Object System.Management.Automation.PSCredential($CredUser,$PWD)   #定义认证对象Invoke-Command -FilePath  "D:\PS\AccountLockOut\Get-AccountLockOut.ps1" -ComputerName 10.10.10.10 -Credential  $Cred net use \\DC\D$\PS\AccountLockOut Iku963.. /u:ikulinrobocopy \\DC \D$\PS\AccountLockOut   D:\PS\AccountLockOut\LOG net use \\LinuxSer\ad  passwd   /u:usernamerobocopy   D:\PS\AccountLockOut\LOG  \\LinuxSer\adnet use /d * /y
  • 脚本三:
    读取7天内帐号的锁定日志并导出csv文件。
[CmdletBinding()]param([INT]$Num=7)    $After=((Get-Date).adddays(-$Num+1)).ToString('yyyy-MM-dd')    $Before=(Get-Date).ToString('yyyy-MM-dd')    $Filename="D:\PS\AccountLockOut\"+"$After"+'-'+"$Before"+'.csv'    Get-EventLog -LogName Security -After $After -InstanceId 4740 |    select @{Name="USER";Expression={(($_.Message).Split(":"))[8].Trim().Split("")[0]}},        @{Name="TIME";Expression={$_.TimeGenerated}},        @{Name="COMPUTER";Expression={(($_.Message).Split(":"))[10].Trim()}} |    Export-Csv  -Encoding UTF8 -path  "$Filename" -Force 

2.2 LinuxSer服务器

  • 脚本四:
    使用shell脚本过得数据。
#!/bin/bash#Date:2017-09-21#Version:1.0.0#Author:linxianyu#Description:Format out  for AD AccountLock.csv#将锁定次数超过50次的帐号统计并存入变量aa=$(cut -d ',' -f 1 $@ | sort | uniq -c | sort -n |awk -F ' ' '{ if ($1>50) print $1,$2  }' |tr -d '"' )#打印变量a的内容#因从变量输入原格式会改变,所以有awk对输出格式化echo $a|awk -F ' ' ' BEGIN{printf "-s -s \n","Statistics","Account";print "-----------------------------"}{ for(i=1;i<=NF;i++){if(i%2==1){printf "%-10s \t",$i } else{printf "%-10s\n",$i}}}END{print "-----------------------------"}'#将变量a中的帐号筛选并存入变量bb=$(echo $a | awk -F ' ' '{for(i=1;i<=NF;i++){if(i%2==0){print $i }}}')#for循环数组变量b中的帐号并再次查找、统计、打印#若同一帐号在不同pc上登陆则分开打印for i in ${b[@]};do        grep "$i" $@ |        cut -d "," -f1,3 |        cut -d ":" -f2 |        sort -t ',' -k2 |        sed -e 's#"##g' -e 's#,#\t#g' |#清除pcname中与帐号同名的行        grep "^$i" |        uniq -c |        sort -b -k2done
  • 脚本六:
    调用tj.sh统计脚本,计算周期。
#!/bin/bash#Date:20171121#Version:1.0#Discription: The creat date for tj.shpath=/backup/adcd $path#测试文件是否存在[ -e missionnum ]if [ $? = 0 ];then#查看运行资料num=$(cat missionnum)#定义循环4次(周)调用一次脚本mouth=4#判断是否满足4周        if [ $num -ne $mouth ];        then#循环计数+1                echo $[ num += 1 ] > missionnum        else#定义文件名                filename=$(date +"ad%Y%m%d.txt")#查找4周内产生的日志并调用执行脚本tj.sh                           find -name "2017-*" -mtime -28 | xargs sh tj.sh > $filename#进行linux to windows文本格式转换                                unix2dos $filename#重置计数                                echo 1 > missionnum        fielse#若无计数文件则创建(因第一次执行后值因为2所以直接赋值2)echo 2 > missionnumfi
  • 脚本七:
    计划任务调用mission.sh脚本。
08 17 * * 3 sh /backup/ad/mission.sh

三、文件

  • 导出的csv文本:
#TYPE Selected.System.Diagnostics.EventLogEntry"USER","TIME","COMPUTER""Administrator","2017/11/29 下午 12:14:18","PC1""USER1","2017/11/29 下午 12:06:53","PC2""USER3","2017/11/29 下午 12:02:35","PC3""USER6","2017/11/29 上午 11:53:51","PC9""Administrator","2017/11/29 上午 11:48:39","PC2"
  • 最终的文本:
[root@bogon ~]# cat /backup/ad/ad20171122.txt Statistics      Account    -----------------------------60              USER1 161            USER3  482            USER9971            Administrator-----------------------------    60    USER1       PC2    100  USER3       PC3    30    USER3       PC4    31    USER3       PC11    209  USER9       PC6    273  USER9       PC100    531  Administrator      PC1    440  Administrator      PC2      
0