-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathvCenterLDAP_Manage.py
255 lines (194 loc) · 7.96 KB
/
vCenterLDAP_Manage.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#python3
import os
import sys
import re
def RunCommand(cmd):
r = os.popen(cmd)
text = r.read()
r.close()
return text
def GetLDAPConfig():
print("[*] Try to get the config of LDAP")
result = RunCommand("/opt/likewise/bin/lwregshell list_values '[HKEY_THIS_MACHINE\\services\\vmdir]'")
index1 = result.find("dcAccount")
dcAccount = result[index1:].split('\n')[0].split('"')[2]
index2 = result.find("dcAccountDN")
dcAccountDN = result[index2:].split('\n')[0].split('"')[2]
index3 = result.find("dcAccountPassword")
dcAccountPassword = result[index3:].split('\n')[0].split(maxsplit=2)[2][1:-1].replace('\\"', '"')
print("[+] dcAccount: " + dcAccount)
print("[+] dcAccountDN: " + dcAccountDN)
print("[+] dcAccountPassword: " + dcAccountPassword)
return dcAccount,dcAccountDN,dcAccountPassword
def AddUser():
dcAccount,dcAccountDN,dcAccountPassword = GetLDAPConfig()
print("[*] Try to generate the ldif")
print("Eg.")
print(" username: test1")
print(" dn: CN=test1,CN=Users,DC=aaa,DC=bbb")
print(" userPrincipalName: test1@AAA.BBB")
username = input("input the new username: ")
dn = input("input the dn: ")
userPrincipalName = input("input the userPrincipalName: ")
ADDUSER = '''dn: {dn}
userPrincipalName: {userPrincipalName}
sAMAccountName: {username}
cn: {username}
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
userPassword: P@ssWord123@@
'''
ADDUSER = ADDUSER.format(dn = dn, userPrincipalName = userPrincipalName, username = username)
print("[*] Confirm the ldif")
print(ADDUSER)
print("[*] Try to generate the ldif")
fo = open("adduser.ldif", "w")
fo.write(ADDUSER)
fo.close()
print("[*] Try to add the data")
command = "ldapadd -x -h {dcAccount} -D \"{dcAccountDN}\" -w '{dcAccountPassword}' -f adduser.ldif".format(dcAccount = dcAccount, dcAccountDN = dcAccountDN, dcAccountPassword = dcAccountPassword)
print("[+] Command: " + command)
result = RunCommand(command)
print(result)
print("[*] Try to clean the ldif")
os.remove("adduser.ldif")
print("\nAll done.")
print("[+] New user: " + userPrincipalName)
print(" Password: P@ssWord123@@")
print("[!] Remember to add it as an admin")
def AddAdmin():
dcAccount,dcAccountDN,dcAccountPassword = GetLDAPConfig()
base = dcAccountDN.split('s,')[1]
print("[*] Try to generate the ldif")
print("Eg.")
print(" user dn: CN=test1,CN=Users,DC=aaa,DC=bbb")
dn = input("input the user dn: ")
ADDADMIN = '''dn: cn=Administrators,cn=Builtin,{base}
changetype: modify
add: member
member: {dn}
'''
ADDADMIN = ADDADMIN.format(base = base, dn = dn)
print("[*] Confirm the ldif")
print(ADDADMIN)
print("[*] Try to generate the ldif")
fo = open("addadmin.ldif", "w")
fo.write(ADDADMIN)
fo.close()
print("[*] Try to modify the data")
command = "ldapmodify -x -h {dcAccount} -D \"{dcAccountDN}\" -w '{dcAccountPassword}' -f addadmin.ldif".format(dcAccount = dcAccount, dcAccountDN = dcAccountDN, dcAccountPassword = dcAccountPassword)
print("[+] Command: " + command)
result = RunCommand(command)
print(result)
print("[*] Try to clean the ldif")
os.remove("addadmin.ldif")
print("\nAll done.")
def ChangePass():
dcAccount,dcAccountDN,dcAccountPassword = GetLDAPConfig()
print("[*] Try to generate the ldif")
print("Eg.")
print(" user dn: CN=test1,CN=Users,DC=aaa,DC=bbb")
print(" new password: P@ssWord123@@45")
dn = input("input the user dn: ")
newpassword = input("input the new password: ")
CHANGEPASS = '''dn: {dn}
changetype: modify
replace: userPassword
userPassword: {newpassword}
'''
CHANGEPASS = CHANGEPASS.format(dn = dn, newpassword = newpassword)
print("[*] Confirm the ldif")
print(CHANGEPASS)
print("[*] Try to generate the ldif")
fo = open("changepass.ldif", "w")
fo.write(CHANGEPASS)
fo.close()
print("[*] Try to modify the data")
command = "ldapmodify -x -h {dcAccount} -D \"{dcAccountDN}\" -w '{dcAccountPassword}' -f changepass.ldif".format(dcAccount = dcAccount, dcAccountDN = dcAccountDN, dcAccountPassword = dcAccountPassword)
print("[+] Command: " + command)
result = RunCommand(command)
print(result)
print("[*] Try to clean the ldif")
os.remove("changepass.ldif")
print("\nAll done.")
print("[+] User: " + dn)
print("[+] New Password: " + newpassword)
def DeleteUser():
dcAccount,dcAccountDN,dcAccountPassword = GetLDAPConfig()
print("[*] Try to input the user dn")
print("Eg.")
print(" user dn: CN=test1,CN=Users,DC=aaa,DC=bbb")
dn = input("input the user dn: ")
print("[*] Try to delete the data")
command = "ldapdelete -x -h {dcAccount} -D \"{dcAccountDN}\" -w '{dcAccountPassword}' \"{dn}\"".format(dcAccount = dcAccount, dcAccountDN = dcAccountDN, dcAccountPassword = dcAccountPassword, dn = dn)
print("[+] Command: " + command)
result = RunCommand(command)
print(result)
print("\nAll done.")
def GetAdmin():
dcAccount,dcAccountDN,dcAccountPassword = GetLDAPConfig()
base = dcAccountDN.split('s,')[1]
adminbase = "cn=Administrators,cn=Builtin," + base
print("[*] Try to get the data")
command = "ldapsearch -x -h {dcAccount} -D \"{dcAccountDN}\" -w '{dcAccountPassword}' -b \"{adminbase}\"".format(dcAccount = dcAccount, dcAccountDN = dcAccountDN, dcAccountPassword = dcAccountPassword, adminbase = adminbase)
print("[+] Command: " + command)
result = RunCommand(command)
print("[+] Admin User:")
pattern_name = re.compile(r"member: (.*?),")
name = pattern_name.findall(result)
for i in range(len(name)):
print(" - %s"%(name[i][3:]))
print("\nAll done.")
def GetUser():
dcAccount,dcAccountDN,dcAccountPassword = GetLDAPConfig()
base = dcAccountDN.split('s,')[1]
adminbase = "cn=Users," + base
print("[*] Try to get the data")
command = "ldapsearch -x -h {dcAccount} -D \"{dcAccountDN}\" -w '{dcAccountPassword}' -b \"{adminbase}\"".format(dcAccount = dcAccount, dcAccountDN = dcAccountDN, dcAccountPassword = dcAccountPassword, adminbase = adminbase)
print("[+] Command: " + command)
result = RunCommand(command)
print("[+] User:")
pattern_name = re.compile(r"dn: (.*?),")
name = pattern_name.findall(result)
for i in range(len(name)):
print(" - %s"%(name[i][3:]))
print("\nAll done.")
if __name__ == "__main__":
if len(sys.argv)!=2:
print("vCenterLDAP_Manage.py")
print("Use to manage the LDAP database.")
print("Usage:")
print("%s <mode>"%(sys.argv[0]))
print("mode:")
print("- adduser")
print("- addadmin")
print("- changepass")
print("- deleteuser")
print("- getadmin")
print("- getuser")
print("Eg.")
print("%s adduser"%(sys.argv[0]))
sys.exit(0)
else:
if sys.argv[1] == "adduser":
print("[*] Try to add a user")
AddUser()
elif sys.argv[1] == "addadmin":
print("[*] Try to add a user as an admin")
AddAdmin()
elif sys.argv[1] == "changepass":
print("[*] Try to change the password")
ChangePass()
elif sys.argv[1] == "deleteuser":
print("[*] Try to delete the user")
DeleteUser()
elif sys.argv[1] == "getadmin":
print("[*] Try to list the admin user")
GetAdmin()
elif sys.argv[1] == "getuser":
print("[*] Try to list the user")
GetUser()
else:
print("[!] Wrong parameter")