-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckPWchangeDate.py
executable file
·177 lines (159 loc) · 5.54 KB
/
checkPWchangeDate.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
#!/usr/bin/env python3
# MIT License
# Copyright (c) 2020 Luke Strohm
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import datetime
import time
from ldap3 import ALL, Connection, Server, Tls
class Color:
PURPLE = "\033[95m"
CYAN = "\033[96m"
DARKCYAN = "\033[36m"
BLUE = "\033[94m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
END = "\033[0m"
def cred():
print(
Color.DARKCYAN
+ "\n"
+ "*********************************\n"
+ "* Utility to Check When *\n"
+ "* A Password Was Changed *\n"
+ "* *\n"
+ "* Written and maintained by: *\n"
+ "* Luke Strohm *\n"
+ "* strohm.luke@gmail.com *\n"
+ "* https://github.com/strohmy86 *\n"
+ "*********************************\n"
+ "\n"
+ Color.END
)
def main(username):
today = str(datetime.datetime.today())[:-16]
today2 = datetime.datetime.strptime(today, "%Y-%m-%d")
# Connect and bind to LDAP server
f = open("/home/lstrohm/Scripts/ADcreds.txt", "r")
lines = f.readlines()
usern = lines[0]
password = lines[1]
f.close()
tls = Tls(
local_private_key_file=None,
local_certificate_file=None,
)
s = Server("madhs01dc3.mlsd.local", use_ssl=True, get_info=ALL, tls=tls)
c = Connection(s, user=usern.strip(), password=password.strip())
c.bind()
# Search for user. Lists all usernames matching string provided.
try:
c.search(
"ou=Madison,dc=mlsd,dc=local",
"(&(objectclass=person)" + "(cn=*" + username + "*))",
attributes=[
"title",
"displayName",
"pwdLastSet",
],
)
users = c.entries
if len(users) <= 0:
raise IndexError
print(Color.BOLD + "\nI found the following users:\n" + Color.END)
ent = 0 # Start of result list
for i in users:
print(
str(ent)
+ ") Name: "
+ Color.GREEN
+ str(users[ent].displayName.value)
+ Color.END
+ ", AD Location: "
+ Color.GREEN
+ str(users[ent].entry_dn)
+ Color.END
+ ", Title: "
+ Color.GREEN
+ str(users[ent].title)
+ Color.END
)
ent = ent + 1 # Moves to next in results list
# Prompts to select user from search results
usn = int(input(Color.BOLD + "\nPlease select a user: " + Color.END))
user = c.entries[usn]
name = str(user.displayName.value)
setstr = str(user.pwdLastSet.value)[:-22]
setdate = datetime.datetime.strptime(setstr, "%Y-%m-%d")
expDate = setdate + datetime.timedelta(days=90) # Checks PW expiration
expStr = expDate.strftime("%m-%d-%Y") # Pretty date
setStr = setdate.strftime("%m-%d-%Y") # Pretty date
if today2 > expDate: # If PW is expired
print(
Color.RED
+ name
+ "'s password expired on "
+ expStr
+ "!"
+ Color.END
)
else: # PW not expired
print(
Color.GREEN
+ name
+ "'s"
+ Color.END
+ " password expires on "
+ Color.CYAN
+ expStr
+ Color.END
)
print(
Color.GREEN
+ name
+ Color.END
+ " changed their password on "
+ Color.YELLOW
+ setStr
+ Color.END
)
time.sleep(1)
c.unbind()
except IndexError: # Error received if empty search result
print(Color.RED + "No username found! Try again.\n" + Color.END)
except KeyboardInterrupt: # User exited script with CTRL + C
print(Color.CYAN + "\nExiting..." + Color.END)
exit()
# Sets up parser and adds arguement
parser = argparse.ArgumentParser(
description="Script to check the password\
expiration date for a user."
)
parser.add_argument(
"username",
metavar="Username",
default="",
type=str,
help="Username of user to check.",
)
args = parser.parse_args()
username = args.username
cred()
main(username)