VBS判断本地账户密码过期邮件提醒

简介:

VBS判断本地账户密码过期邮件提醒

          近期公司在外网发布的一个文件服务器(FTP)被受攻击,上面的文件全部被修改无法正常使用,然后最主要的是里面放了一个说明,说如果需要解开文件使用的话,需要给一个账户支付解密费用,但是最终我们还是没有按照提示的付费解开文件的相关操作,找了一些杀毒软件还是没有破解,最终放弃了,通过备份的数据进行还原,该事件主要说明我们在日常中放的公网服务器需要经常更换密码,由于公网的服务器没有加域管理,所以只能通过vbs脚本来获取本地账户然后获取账户的信息,借助本地策略密码最长使用周期为30天来提醒管理员进行定期修改,来提高服务器的安全性,以下为此次操作的脚本信息,供有需要的学习。

        本地账户信息属性有的时候会设置密码永不过期了,所以我们也会通过脚本进行判断。使用系统的计划任务来定义执行脚本,来达到最终的脚本执行结果。具体见下:

最终我们通过以下格式进行邮件提醒。

image

代码开始了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Dim getd
getd = datediffn
sendmail GetIP,getd
Function datediffn()
Dim ws
dim rt
dim rtarr
Dim lastset
Dim enddate
set  ws = createobject( "wscript.shell" )
rt = ws. exec ( "net user administrator" ).StdOut.ReadAll
rtarr =  split (rt,vbcrlf)
for  i=0 to ubound(rtarr)
If Mid(rtarr(i),1,3)= "上次设"  then
' if  mid(rtarr(i),1,10)= "Password l"  Then
lastset = Mid(rtarr(i),InStr(rtarr(i), "20" ),Len(rtarr(i)))
'WScript.Echo lastset
end If
If Mid(rtarr(i),1,3)= "密码到"  then
'If mid(rtarr(i),1,10)= "Password e"  Then
If InStr(rtarr(i), "20" ) <> 0  then
enddate = Mid(rtarr(i),InStr(rtarr(i), "20" ),Len(rtarr(i)))
If DateDiff( "d" ,lastset,enddate) <=66 Then
'datediffn = 5
datediffn = datediff( "d" ,lastset,enddate)
ElseIf DateDiff( "d" ,lastset,enddate) >=365  then
datediffn =  "never"
End If
'WScript.Echo enddate
Else
datediffn =  "never"
End  if
end  if
Next
End  function
Function sendmail(ip,dat)
Dim ns,eo
Dim password
Dim stmp
Dim fromName
Dim Toname
Dim msg1
Dim msg2
Dim msg
Dim  users
fromName =  "gavin@ixmsoft.com"         '发件人
Toname =  "gaowenlong@ixmsoft.com"                 '收件人
stmp =  "smtp.ixmsoft.com"                 'stmp 服务器地址
users  "gavin"
password =  "Password2016"         '发件人密码
msg1 =  "<html> <body bgcolor='wheat'>"  & VbCrLf & _
"<h2><font color = 'red'>管理员账户密码过期提醒:</font></h2>"  & vbCrLf & _
"" & vbCrLf & vbCrLf & _
"<br/><font color='blue' size = 4>服务器IP地址为:</font><b><font color='red' size = 6>" &ip& "</font></b><font color='blue' size = 4>的管理员账户(Administrator)将在</font><b><font color='red' size = 6>" &dat& "</font></b><font color='blue' size = 4>天过期,请登录后及时修改;</font>"  & vbCRLF & _
"" & vbCrLf & vbCrLf & _
"</br>"  & _
"<br/><font color='green' size = 5>账号管理中心</font>"  & _
"</body>"  & VbCrLf & _
"</html>"
msg2 =  "<html> <body bgcolor='wheat'>"  & VbCrLf & _
"<h2><font color = 'red'>管理员账户密码过期提醒:</font></h2>"  & vbCrLf & _
"" & vbCrLf & vbCrLf & _
"<br/><b><font size =6 color='red'>Warning:</font></b><font color='blue' size = 4>服务器IP地址为:</font><b><font color='red' size = 6>" &ip& "</font></b><font color='blue' size = 4>的管理员账户(Administrator)属性设置</font><b><font color='red' size = 6>密码永不过期</font></b><font color='blue' size = 4>,为了保证账户安全,请登录后取消用户属性选项" "密码永不过期" ";</font>"  & vbCRLF & _
"" & vbCrLf & vbCrLf & _
"</br>"  & _
"<br/><b><font size=4 color = 'red'>Note:</font></b><font color='blue'>本地组策略默认账户密码最长使用周期为42天,同时密码启用复杂度.</font>" & vbCrLf & _
"" & vbCrLf & vbCrLf & _
"</br>"  & _
"</br>"  & _
"</br>"  & _
"</br>"  & _
"<br/><font color='green' size = 5>账号管理中心</font>"  & _
"</body>"  & VbCrLf & _
"</html>"
If dat= "never"  Then
msg = msg2
Else
msg = msg1
End  if
Set eo = CreateObject( "CDO.Message" )
ns =  "http://schemas.microsoft.com/cdo/configuration/"
eo.From = fromName
eo.To =Toname
eo.Subject =  "管理员账户密码过期提醒"
eo.Textbody = text
eo.HTMLBody = msg
With eo.Configuration.Fields
.Item(ns& "sendusing" ) = 2
.Item(ns& "smtpserver" ) = stmp
.Item(ns& "smtpserverport" ) = 25
.Item(ns& "smtpauthenticate" ) = 1
.Item(ns& "sendusername" ) =  users
.Item(ns& "sendpassword" ) = password
.Update
End With
eo.Send
End Function
'获取本机IP
'owner DeViL
' return  本机的IP地址
Public Function GetIP
ComputerName= "."
Dim objWMIService,colItems,objItem,objAddress
Set objWMIService = GetObject( "winmgmts:\\"  & ComputerName &  "\root\cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True" )
For Each objItem  in  colItems
For Each objAddress  in  objItem.IPAddress
If objAddress <>  ""  then
GetIP = objAddress
Exit Function
End If
Next
Next
End Function

我们通过两个条件进行判断,如果用户密码将要在多少天过期的时候会给指定邮件发送提示邮件。

image

如果用户属性开启了密码永不过期,也会有邮件提醒。

image

我们通过计划任务进行执行脚本

image



本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1786749,如需转载请自行联系原作者

相关文章
|
数据安全/隐私保护
Win系统 - 学个简单命令,一秒重置账户密码!
Win系统 - 学个简单命令,一秒重置账户密码!
157 0
Win系统 - 学个简单命令,一秒重置账户密码!
|
安全 数据安全/隐私保护 容器
|
数据安全/隐私保护