Skip to content

Commit 493b12e

Browse files
committed
Fix get-unixUserPassword/get-userPassword modules which sometimes does not retrieve all users with these attributes
1 parent d343e21 commit 493b12e

2 files changed

Lines changed: 13 additions & 45 deletions

File tree

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from impacket.ldap import ldapasn1 as ldapasn1_impacket
22
from impacket.ldap import ldap as ldap_impacket
33
from nxc.logger import nxc_logger
4+
from nxc.parsers.ldap_results import parse_result_attributes
45

56

67
class NXCModule:
@@ -20,7 +21,7 @@ def options(self, context, module_options):
2021
"""
2122

2223
def on_login(self, context, connection):
23-
searchFilter = "(objectclass=user)"
24+
searchFilter = "(unixUserPassword=*)"
2425

2526
try:
2627
context.log.debug(f"Search Filter={searchFilter}")
@@ -37,27 +38,10 @@ def on_login(self, context, connection):
3738
nxc_logger.debug(e)
3839
return False
3940

40-
answers = []
41-
context.log.debug(f"Total of records returned {len(resp)}")
42-
for item in resp:
43-
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
44-
continue
45-
sAMAccountName = ""
46-
unixUserPassword = []
47-
try:
48-
for attribute in item["attributes"]:
49-
if str(attribute["type"]) == "sAMAccountName":
50-
sAMAccountName = str(attribute["vals"][0])
51-
elif str(attribute["type"]) == "unixUserPassword":
52-
unixUserPassword = [str(i) for i in attribute["vals"]]
53-
if sAMAccountName != "" and len(unixUserPassword) > 0:
54-
answers.append([sAMAccountName, unixUserPassword])
55-
except Exception as e:
56-
context.log.debug("Exception:", exc_info=True)
57-
context.log.debug(f"Skipping item, cannot process due to error {e!s}")
58-
if len(answers) > 0:
41+
if resp:
42+
resp_parsed = parse_result_attributes(resp)
5943
context.log.success("Found following users: ")
60-
for answer in answers:
61-
context.log.highlight(f"User: {answer[0]} unixUserPassword: {answer[1]}")
44+
for user in resp_parsed:
45+
context.log.highlight(f"User: {user['sAMAccountName']} unixUserPassword: {user['unixUserPassword']}")
6246
else:
6347
context.log.fail("No unixUserPassword Found")

nxc/modules/get-userPassword.py

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from impacket.ldap import ldapasn1 as ldapasn1_impacket
22
from impacket.ldap import ldap as ldap_impacket
33
from nxc.logger import nxc_logger
4+
from nxc.parsers.ldap_results import parse_result_attributes
45

56

67
class NXCModule:
@@ -20,7 +21,7 @@ def options(self, context, module_options):
2021
"""
2122

2223
def on_login(self, context, connection):
23-
searchFilter = "(objectclass=user)"
24+
searchFilter = "(userPassword=*)"
2425

2526
try:
2627
context.log.debug(f"Search Filter={searchFilter}")
@@ -37,27 +38,10 @@ def on_login(self, context, connection):
3738
nxc_logger.debug(e)
3839
return False
3940

40-
answers = []
41-
context.log.debug(f"Total of records returned {len(resp)}")
42-
for item in resp:
43-
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
44-
continue
45-
sAMAccountName = ""
46-
userPassword = []
47-
try:
48-
for attribute in item["attributes"]:
49-
if str(attribute["type"]) == "sAMAccountName":
50-
sAMAccountName = str(attribute["vals"][0])
51-
elif str(attribute["type"]) == "userPassword":
52-
userPassword = [str(i) for i in attribute["vals"]]
53-
if sAMAccountName != "" and len(userPassword) > 0:
54-
answers.append([sAMAccountName, userPassword])
55-
except Exception as e:
56-
context.log.debug("Exception:", exc_info=True)
57-
context.log.debug(f"Skipping item, cannot process due to error {e!s}")
58-
if len(answers) > 0:
41+
if resp:
42+
resp_parsed = parse_result_attributes(resp)
5943
context.log.success("Found following users: ")
60-
for answer in answers:
61-
context.log.highlight(f"User: {answer[0]} userPassword: {answer[1]}")
44+
for user in resp_parsed:
45+
context.log.highlight(f"User: {user['sAMAccountName']} unixUserPassword: {user['userPassword']}")
6246
else:
63-
context.log.fail("No userPassword Found")
47+
context.log.fail("No unixUserPassword Found")

0 commit comments

Comments
 (0)