1+ from impacket .ldap import ldap as ldap_impacket
2+ import re
3+ from nxc .logger import nxc_logger
4+ from nxc .parsers .ldap_results import parse_result_attributes
5+
6+
7+ class NXCModule :
8+ """
9+ Get the info field of users
10+ Module by @sepauli
11+ """
12+
13+ name = "get-info-users"
14+ description = "Get the info field of the users. May contained password"
15+ supported_protocols = ["ldap" ]
16+ opsec_safe = True
17+ multiple_hosts = True
18+
19+ def options (self , context , module_options ):
20+ """
21+ FILTER Apply the FILTER (grep-like) (default: '')
22+ """
23+ self .FILTER = ""
24+ if "FILTER" in module_options :
25+ self .FILTER = module_options ["FILTER" ]
26+
27+ def on_login (self , context , connection ):
28+ # Building the search filter
29+ searchFilter = "(objectclass=user)"
30+
31+ try :
32+ context .log .debug (f"Search Filter={ searchFilter } " )
33+ resp = connection .ldap_connection .search (
34+ searchFilter = searchFilter ,
35+ attributes = ["sAMAccountName" , "info" ],
36+ sizeLimit = 0 ,
37+ )
38+ except ldap_impacket .LDAPSearchError as e :
39+ if e .getErrorString ().find ("sizeLimitExceeded" ) >= 0 :
40+ context .log .debug ("sizeLimitExceeded exception caught, giving up and processing the data received" )
41+ resp = e .getAnswers ()
42+ else :
43+ nxc_logger .debug (e )
44+ return False
45+
46+ context .log .debug (f"Total of records returned { len (resp )} " )
47+ resp_parsed = parse_result_attributes (resp )
48+ answers = [[x ["sAMAccountName" ], x .get ("info" )] for x in resp_parsed if x .get ("info" )]
49+
50+ answers = self .filter_answer (context , answers )
51+ if len (answers ) > 0 :
52+ context .log .success ("Found following users: " )
53+ for answer in answers :
54+ context .log .highlight (f"User: { answer [0 ]} Info: { answer [1 ]} " )
55+
56+ def filter_answer (self , context , answers ):
57+ # No option to filter
58+ if self .FILTER == "" :
59+ context .log .debug ("No filter option enabled" )
60+ return answers
61+
62+ answersFiltered = []
63+ context .log .debug ("Prepare to filter" )
64+ if len (answers ) > 0 :
65+ for answer in answers :
66+ conditionFilter = False
67+ info = str (answer [1 ])
68+ # Filter
69+ if self .FILTER != "" :
70+ conditionFilter = False
71+ if self .FILTER in info :
72+ conditionFilter = True
73+
74+ if conditionFilter :
75+ context .log .highlight (f"'{ self .FILTER } ' found in Info: '{ info } '" )
76+ elif self .FILTER == "" :
77+ answersFiltered .append ([answer [0 ], info ])
78+
79+ return answersFiltered
0 commit comments