|
| 1 | +import json |
| 2 | +from nxc.helpers.misc import CATEGORY |
| 3 | +from nxc.parsers.ldap_results import parse_result_attributes |
| 4 | + |
| 5 | + |
| 6 | +class NXCModule: |
| 7 | + """ |
| 8 | + Get the scriptPath attribute of users |
| 9 | +
|
| 10 | + Module by @wyndoo |
| 11 | + """ |
| 12 | + name = "get-scriptpath" |
| 13 | + description = "Get the scriptPath attribute of all users." |
| 14 | + supported_protocols = ["ldap"] |
| 15 | + category = CATEGORY.ENUMERATION |
| 16 | + |
| 17 | + def options(self, context, module_options): |
| 18 | + """ |
| 19 | + FILTER Apply the FILTER (grep-like) (default: '') |
| 20 | + OUTPUTFILE Path to a file to save the results (default: None) |
| 21 | + """ |
| 22 | + self.filter = "" |
| 23 | + self.outputfile = None |
| 24 | + |
| 25 | + if "FILTER" in module_options: |
| 26 | + self.filter = module_options["FILTER"] |
| 27 | + |
| 28 | + if "OUTPUTFILE" in module_options: |
| 29 | + self.outputfile = module_options["OUTPUTFILE"] |
| 30 | + |
| 31 | + def on_login(self, context, connection): |
| 32 | + # Building the search filter |
| 33 | + resp = connection.search( |
| 34 | + searchFilter="(scriptPath=*)", |
| 35 | + attributes=["sAMAccountName", "scriptPath"] |
| 36 | + ) |
| 37 | + |
| 38 | + context.log.debug(f"Total of records returned {len(resp)}") |
| 39 | + answers = parse_result_attributes(resp) |
| 40 | + context.log.debug(f"Filtering for scriptPath containing: {self.filter}") |
| 41 | + filtered_answers = list(filter(lambda x: self.filter in x["scriptPath"], answers)) |
| 42 | + |
| 43 | + if filtered_answers: |
| 44 | + context.log.success("Found the following attributes: ") |
| 45 | + for answer in filtered_answers: |
| 46 | + context.log.highlight(f"User: {answer['sAMAccountName']:<20} ScriptPath: {answer['scriptPath']}") |
| 47 | + |
| 48 | + # Save the results to a file |
| 49 | + if self.outputfile: |
| 50 | + self.save_to_file(context, filtered_answers) |
| 51 | + else: |
| 52 | + context.log.fail("No results found after filtering.") |
| 53 | + |
| 54 | + def save_to_file(self, context, answers): |
| 55 | + """Save the results to a JSON file.""" |
| 56 | + try: |
| 57 | + # Format answers as a list of dictionaries for JSON output |
| 58 | + json_data = [{"sAMAccountName": answer["sAMAccountName"], "scriptPath": answer["scriptPath"]} for answer in answers] |
| 59 | + |
| 60 | + # Save the JSON data to the specified file |
| 61 | + with open(self.outputfile, "w") as f: |
| 62 | + json.dump(json_data, f, indent=4) |
| 63 | + context.log.success(f"Results successfully saved to {self.outputfile}") |
| 64 | + |
| 65 | + except Exception as e: |
| 66 | + context.log.error(f"Failed to save results to file: {e}") |
0 commit comments