-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursead.py
214 lines (200 loc) · 8.59 KB
/
recursead.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Recurse AD looking for passwords which will expire in the next 7 days. If found email the user a reminder every day until they change their PW.
# v1.0
#
# Next steps: Add logging, Capture and log any errors
MAXPASSWORDAGEINDAYS = 45
VERSIONINFO = "pycursead.py 0.6 11/27/2018"
SMTPSERVER = '10.0.0.69'
import sys
import socket
import smtplib
import traceback
from email.message import EmailMessage
from datetime import date, datetime, timedelta
from pyad import adquery, pyadconstants, aduser
import logging
from collections import deque
# Prep for running the program
filename = datetime.today().strftime("%Y%m%d") + ".log"
logging.basicConfig(filename=filename, filemode='a', format='%(asctime)s - %(funcName)s - %(levelname)s - %(message)s', level=logging.INFO)
logging.info(__file__ + " is starting.\n")
# Create a deque
deque = deque()
# Make a fancy message
def makeMessage(humanoid, xDays, xDate):
msg = """
<html>
<head>
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;
font-weight:normal;
font-style:normal;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=WordSection1>
<p class=MsoNormal>
<span style='color:#1F497D'>Greetings {humanoid} </span>
<o:p></o:p>
</p>
<p class=MsoNormal><span style='color:#1F497D'>
<o:p> </o:p></span></p>
<p class=MsoNormal>
Your password is expiring in the next {xDays} days, please change it before the expiration date of {xDate} <o:p></o:p>
</p>
<p class=MsoNormal>If your password expires, your supervisor will have to request I.T. to reset your password.<o:p></o:p>
</p>
<p class=MsoNormal>
<o:p> </o:p>
</p>
<p class=MsoNormal style='margin-left:.5in'>
<b><span style='color:#0000CC'>To change your password, press CTLR + ALT + DEL -></span></b>
<span style='color:#0000CC'> Change a password <o:p></o:p></span>
</b>
</p>
<p class=MsoNormal style='margin-left:.5in'>
<b><span style='color:#0000CC'>Enter your current password and a new one in the two boxes and click OK<o:p></o:p></span></b>
</p>
<p class=MsoNormal style='margin-left:.5in'><o:p> </o:p>
</p>
<p class=MsoNormal style='margin-left:.5in'>
<span style='color:#3333CC'>The notice of your password expiring is seen when you are logging onto your PC, <o:p></o:p></span>
</p>
<p class=MsoNormal style='margin-left:.5in'>
<span style='color:#3333CC'>If you have not logged out in some time that would be why you are not seeing the notice.<o:p></o:p></span>
</p>
<p class=MsoNormal style='margin-left:.5in'>
<span style='color:#3333CC'><o:p> </o:p></span>
</p>
<p class=MsoNormal style='margin-left:.5in'>
<span style='color:#3333CC'>It is recommended to reboot your PC at least once a week to refresh your system, <o:p></o:p></span>
</p>
<p class=MsoNormal style='text-indent:.5in'>
<span style='color:#3333CC'>write your profile back to the server and see these type of notices.<o:p></o:p></span>
</p>
<p class=MsoNormal>
<o:p> </o:p>
</p>
<p class=MsoNormal style='text-autospace:none'>
<b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:#000099'>Thank you,<o:p></o:p></span></b>
</p>
<p class=MsoNormal style='text-autospace:none'>
<b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif";color:#000099'>The I.T. Department</span></b><span style='font-size:8.0pt;font-family:"Tahoma","sans-serif";color:#0000CC'><o:p></o:p></span>
</p>
<p class=MsoNormal>
<o:p> </o:p>
</p>
</div>
----------------------------------------------------------------------------------------------------------------
<br>Process: {version}
<br>-----------------------------------------------------------------------------------------------------------------
</body>
</html>
"""
logging.info("Human:" + humanoid + " Password will expire in " + str(xDays) + " days on " + str(xDate))
msg = msg.replace("{humanoid}", humanoid)
msg = msg.replace("{xDays}", str(xDays))
msg = msg.replace("{xDate}", str(xDate))
msg = msg.replace("{version}", VERSIONINFO)
return msg;
# Send the message via our own SMTP server.
def mailMsg(address, msg, days, humanoid):
logging.info("eMailing PW expiration warning message.\n")
msg = "Content-Type: text/html;\nMime-Version: 1.0;\nTo: [email protected]\nSubject: "+ humanoid + ", your password will expire in " + str(days) + " days\n\n" + msg
try:
s = None
s = smtplib.SMTP(SMTPSERVER)
s.set_debuglevel(0)
s.sendmail("[email protected]", address, msg)
except socket.error as e:
msg = "Could not connect to " + spamTitan + ":25" + " - is it listening / up?"
logging.error (msg)
raise
except:
print ("Unknown error:", sys.exc_info()[0])
finally:
if s != None:
s.quit()
# logging.info(msg)
# MAIN: Query AD for delinquent passwords
def main():
q = adquery.ADQuery()
q.execute_query(
attributes = ["distinguishedName", "displayName", "memberOf", "mail"],
where_clause = "objectClass = 'User'",
base_dn = "OU=PKW Corp,DC=pkwillis,DC=local"
)
for row in q.get_results():
logging.info ("Inspecting " + row["distinguishedName"])
user1 = aduser.ADUser.from_dn(row["distinguishedName"])
address = row["mail"]
pwLastSet = user1.get_password_last_set()
pwAge = (datetime.today() - pwLastSet).days
uctl = user1.get_user_account_control_settings()
pwDoesntExpire = uctl["DONT_EXPIRE_PASSWD"]
pwDisabled = uctl["ACCOUNTDISABLE"]
badOU = "Computers" in row["distinguishedName"] or "SecuredProfiles" in row["distinguishedName"] or \
"Security Groups" in row["distinguishedName"] or "Training" in row["distinguishedName"] or "PKW7" in row["distinguishedName"]
pwExpirationDate = (pwLastSet + timedelta(MAXPASSWORDAGEINDAYS)).strftime("%m/%d/%Y")
if badOU == False and pwDoesntExpire == False and pwDisabled == False and pwAge > MAXPASSWORDAGEINDAYS-7:
if pwAge > MAXPASSWORDAGEINDAYS:
# push to a stack so we can report all of these at the same time at the end of the log.
deque.append("Found expired PW for " + row["displayName"] + ": PW is " + str(pwAge) + " days old. PW_Doesn't_Expire flag is " + str(pwDoesntExpire) + " and Expires on " + str(pwExpirationDate))
else:
logging.info ("Preparing to email warning to " + row["displayName"] + ": PW is " + str(pwAge) + " days old. PW_Doesn't_Expire flag is " + str(pwDoesntExpire) + " and Expires on " + str(pwExpirationDate))
msg = makeMessage(row["displayName"], MAXPASSWORDAGEINDAYS-pwAge, pwExpirationDate)
mailMsg(address, msg, MAXPASSWORDAGEINDAYS-pwAge, row["displayName"])
#main()
try:
main()
except:
etype, error_message, error_traceback = sys.exc_info()
s = traceback.format_tb(error_traceback)
s1 = ""
for m in s:
m = m.replace("<string>", __file__)
m = m.replace("<module>", __name__)
s1 = s1 + m
logging.error("Error: %s-%s\n%s" % (etype, error_message, s1))
# log all the negative pwAge entries
for e in deque:
logging.warning(e)
logging.info(__file__ + " completed.\n")