设为首页 收藏本站
查看: 1238|回复: 0

Powershell监控操作系统用户账号事件并预警

[复制链接]

尚未签到

发表于 2018-9-2 06:35:54 | 显示全部楼层 |阅读模式
#  
# 操作系统账号事件(登录、注销、新增、删除、软件安装)
  
# 主函数 Main
  
# @param string $str not null
  
# @param string $code not null
  
#
  
# Description:
  
# 设置登录事件的任务计划时,必须传递这两个参数
  
#
  

  
#region get-serverip 获取IP
  
function get-serverip
  
{
  
    $serverip=gwmi win32_networkadapterconfiguration | ?{$_.IPAddress -ne $null -and $_.dhcpenabled -eq $false -and {$_.IPEnabled}} | %{$_.IPAddress}
  
    if(($serverip.gettype()).isarray)
  
    {
  
        return $serverip[0]
  
    }
  
    else
  
    {
  
        return $serverip
  
    }
  
}
  
#endregion
  

  
#region Send-Mail 发送邮件
  
function Send-Mail($Subject,$Body)
  
{
  
    $password = ConvertTo-SecureString 'password' -AsPlainText -Force
  
    $Credential = New-Object System.Management.Automation.PSCredential('account',$password)
  
    $SmptServer="mail.xx.com.cn"
  
    $From='a@xx.com.cn'
  
    $To="test@xx.com.cn"
  

  
    #抄送
  
    #$Cc="cc@xx.com.cn"
  
    $encode=[System.Text.UTF8Encoding]::UTF8
  

  
    Send-MailMessage -SmtpServer $SmptServer -Credential $Credential -From $From -to $To -Encoding $Encode -Body $Body -Subject $Subject -Priority High -BodyAsHtml
  
}
  
#endregion
  

  
#region cut-string 裁剪字符串
  
function cut-string
  
{
  
    param(
  
        $str,
  
        $start,
  
        $end
  
    )
  
    return $str.substring($str.indexof($start),$str.indexof($end)-$str.indexof($start))
  
}
  
#endregion
  

  
#region get_login_user 获取登录账户
  
#return string
  
function get_login_user
  
{
  
    $users=query user
  

  
    $lists=New-Object system.Collections.ArrayList
  

  
    for($i=1;$i -lt $users.Count;$i++)
  
    {
  
        $user = $users[$i] -replace('  ',' ')
  

  
        while($user.indexof('  ') -gt 0)
  
        {
  
            $user = $user -replace('  ',' ')
  
        }
  
        if($user.indexof(' ') -eq 0 -or $user.indexof('>') -eq 0)
  
        {
  
            $user=$user.substring(1)
  
        }
  
        $user=$user -split(' ')
  

  
        $list=New-Object psobject
  
        #$time=$user[5]+" "+$user[6]
  

  
        Add-Member -Name name -Value $user[0] -MemberType NoteProperty -InputObject $list
  
        Add-Member -Name status -Value $user[3] -MemberType NoteProperty -InputObject $list
  
        #Add-Member -Name time -Value $time -MemberType NoteProperty -InputObject $list
  
        $lists +=@($list)
  
    }
  

  
    $loginUser = $lists | ?{$_.status -eq '运行中'} | select name
  
    foreach($userName in $loginUser)
  
    {
  
        if($userNames -eq $null)
  
        {
  
            $userNames=$userName.name
  
        }
  
        else
  
        {
  
            $userNames=$userNames + ',' + $userName.name
  
        }
  
    }
  
    return $userNames
  
}
  
#endregion
  

  
#region Login-Succ-Notice 成功登录事件
  
function Login-Succ-Notice
  
{
  
    $loginInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4624} | select timecreated,message
  
    if($loginInfo -eq $null)
  
    {
  
        break
  
    }
  
    if(($loginInfo.gettype()).isarray)
  
    {
  
        $time=$loginInfo[0].timecreated
  
        $message=$loginInfo[0].message
  
    }
  
    else
  
    {
  
        $time=$loginInfo.timecreated
  
        $message=$loginInfo.message
  
    }
  

  
    if($code -eq 1)
  
    {
  
        $loginType=cut-string $message '登录类型:' '新登录:'
  
        $loginType=$loginType -replace('登录类型:','')
  
        $loginType=$loginType -replace('            ','')
  
        if($loginType -eq 4)
  
        {
  
            break
  
        }
  
    }
  

  
    $processInfo=cut-string $message '进程名:' '网络信息:'
  
    $processInfo=$processInfo -replace('进程名:        ','')
  

  
    $message=cut-string $message '新登录' '详细身份验证信息'
  
    $loginName=cut-string $message '帐户名:' '帐户域:'
  
    $loginName=$loginName -replace('帐户名:','')
  

  
    $loginIp=cut-string $message '源网络地址:' '源端口:'
  
    $loginIp=$loginIp -replace('源网络地址:','')
  

  
    $ip=get-serverip
  
    $loginedName=get_login_user
  
    $Body="
  
  
  
    服务器
  
    登录账号
  
    进程
  
    登录时间
  
    客户端IP
  
    已登录账号
  
  
  
  
  
    $ip
  
    $loginName
  
    $processInfo
  
    $time
  
    $loginIp
  
    $loginedName
  
  
  
"
  
    try
  
    {
  
        Send-Mail "Login on $ip" $Body
  
    }
  
    catch
  
    {
  
        ac -Path c:\UserNotice.log -Value "[ $time Login] $error[0]"
  
    }
  
}
  
#endregion
  

  
#region Cancel-Succ-Notice 注销登录事件
  
function Cancel-Succ-Notice
  
{
  
    $cancelInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4634} | select timecreated,message
  
    if($cancelInfo -eq $null)
  
    {
  
        break
  
    }
  
    if(($cancelInfo.gettype()).isarray)
  
    {
  
        $time=$cancelInfo[0].timecreated
  
        $message=$cancelInfo[0].message
  
    }
  
    else
  
    {
  
        $time=$cancelInfo.timecreated
  
        $message=$cancelInfo.message
  
    }
  
    $cancelName=cut-string $message '帐户名:' '帐户域:'
  
    $cancelName=$cancelName -replace('帐户名:','')
  

  
    $ip=get-serverip
  
    $loginedName=get_login_user
  
    $Body="
  
  
  
    服务器
  
    注销账号
  
    注销时间
  
    未注销账号
  
  
  
  
  
    $ip
  
    $cancelName
  
    $time
  
    $loginedName
  
  
  
"
  
    try
  
    {
  
        Send-Mail "Cancel on $ip" $Body
  
    }
  
    catch
  
    {
  
        ac -Path c:\UserNotice.log -Value "[ $time Cancel] $error[0]"
  
    }
  
}
  
#endregion
  

  
#region Create-User-Notice 新增账号事件
  
function Create-User-Notice
  
{
  
    $userinfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4722} | select timecreated,message
  
    if($userinfo -eq $null)
  
    {
  
        break
  
    }
  
    if(($userinfo.gettype()).isarray)
  
    {
  
        $time=$userinfo[0].timecreated
  
        $message=$userinfo[0].message
  
    }
  
    else
  
    {
  
        $time=$userinfo.timecreated
  
        $message=$userinfo.message
  
    }
  
    $operateUser=cut-string $message '主题:' '目标帐户:'
  
    $operateUser=cut-string $operateUser '帐户名:' '帐户域:'
  
    $operateUser=$operateUser -replace('帐户名:','')
  

  
    $addUser=$message.substring($message.indexof('目标帐户:'))
  
    $addUser=cut-string $addUser '帐户名:' '帐户域:'
  
    $addUser=$addUser -replace('帐户名:','')
  

  
    $ip=get-serverip
  
    $loginedUser=get_login_user
  

  
    $Body="
  
  
  
    服务器
  
    操作账号
  
    被添加账号
  
    操作时间
  
    已登录账号
  
  
  
  
  
    $ip
  
    $operateUser
  
    $addUser
  
    $time
  
    $loginedUser
  
  
  
"
  
    try
  
    {
  
        Send-Mail "AddUser on $ip" $Body
  
    }
  
    catch
  
    {
  
        ac -Path c:\UserNotice.log -Value "[ $time AddUser] $error[0]"
  
    }
  
}
  
#endregion
  

  
#region Delete-User-Notice 删除账号事件
  
function Delete-User-Notice{
  

  
    $userInfo=Get-WinEvent -logname security -maxevents 10 | ? {$_.id -eq 4726} | select timecreated,message
  
    if($userinfo -eq $null)
  
    {
  
        break
  
    }
  
    if(($userinfo.gettype()).isarray)
  
    {
  
        $time=$userinfo[0].timecreated
  
        $message=$userinfo[0].message
  
    }
  
    else
  
    {
  
        $time=$userinfo.timecreated
  
        $message=$userinfo.message
  
    }
  
    $ip=get-serverip
  
    $loginedUser=get_login_user
  

  
    $operateUser=cut-string $message '主题:' '目标帐户:'
  
    $operateUser=cut-string $operateUser '帐户名:' '帐户域:'
  
    $operateUser=$operateUser -replace('帐户名:','')
  
    $delUser=$message.substring($message.indexof('目标帐户:'))
  
    $delUser=cut-string $delUser '帐户名:' '帐户域:'
  
    $delUser=$delUser -replace('帐户名:','')
  

  
    $Body="
  
  
  
    服务器
  
    操作账号
  
    被删除账号
  
    操作时间
  
    已登录账号
  
  
  
  
  
    $ip
  
    $operateUser
  
    $delUser
  
    $time
  
    $loginedUser
  
  
  
"
  
    try
  
    {
  
        Send-Mail "Delete on $ip" $Body
  
    }
  
    catch
  
    {
  
        ac -Path c:\UserNotice.log -Value "[ $time Delete] $error[0]"
  
    }
  
}
  
#endregion
  

  
#region Software-Setup-Notice 软件安装事件
  
function Software-Setup-Notice
  
{
  
    $softinfo=Get-WinEvent -logname setup -maxevents 10 | ? {$_.id -eq 1610} | select timecreated,message
  
    if($softinfo -eq $null)
  
    {
  
        break
  
    }
  
    if(($softinfo.gettype()).isarray)
  
    {
  
        $time=$softinfo[0].timecreated
  
        $time=$softinfo[0].tostring()
  
        $message=$softinfo[0].message
  
    }
  
    else
  
    {
  
        $time=$softinfo.timecreated
  
        $time=$time.tostring()
  
        $message=$softinfo.message
  
    }
  
    $ip=get-serverip
  
    $loginedUser=get_login_user
  

  
    $Body="
  
  
  
    服务器
  
    已登录账号
  
    安装时间
  
    安装信息
  
  
  
  
  
    $ip
  
    $loginedUser
  
    $time
  
    $message
  
  
  
"
  

  
    try
  
    {
  
        Send-Mail 'Setup on $ip' $Body
  
    }
  
    catch
  
    {
  
        ac -Path c:\UserNotice.log -Value "[ $time Setup] $error[0]"
  
    }
  
}
  
#endregion
  

  
#region Main 入口函数
  
function Main{
  

  
    param(
  
    $str,
  
    $script:code
  
    )
  

  
    if($str -eq $null)
  
    {
  
        Write-Warning 参数丢失!
  
        sleep 2
  
        break
  
    }
  
    if($str -eq 'login')
  
    {
  
        Login-Succ-Notice
  
    }
  
    if($str -eq 'cancel')
  
    {
  
        Cancel-Succ-Notice
  
    }
  
    if($str -eq 'add')
  
    {
  
        Create-User-Notice
  
    }
  
    if($str -eq 'delete')
  
    {
  
        Delete-User-Notice
  
    }
  
    if($str -eq 'setup')
  
    {
  
        Software-Setup-Notice
  
    }
  
}
  
#endregion
  

  
main $args[0] $args[1]



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-561247-1-1.html 上篇帖子: Powershell管理系列(三十七)PowerShell操作之比较两个CSV文件内容 下篇帖子: powershell 之数据库操作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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