|
| 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 | + |
| 18 | + def options(self, context, module_options): |
| 19 | + """ |
| 20 | + FILTER Apply the FILTER (grep-like) (default: '') |
| 21 | + OUTPUTFILE Path to a file to save the results (default: None) |
| 22 | + """ |
| 23 | + self.FILTER = "" |
| 24 | + self.OUTPUTFILE = None |
| 25 | + |
| 26 | + if "FILTER" in module_options: |
| 27 | + self.FILTER = module_options["FILTER"] |
| 28 | + |
| 29 | + if "OUTPUTFILE" in module_options: |
| 30 | + self.OUTPUTFILE = module_options["OUTPUTFILE"] |
| 31 | + |
| 32 | + def on_login(self, context, connection): |
| 33 | + # Building the search filter |
| 34 | + resp = connection.search( |
| 35 | + searchFilter="(scriptPath=*)", |
| 36 | + attributes=["sAMAccountName", "scriptPath"] |
| 37 | + ) |
| 38 | + |
| 39 | + context.log.debug(f"Total of records returned {len(resp)}") |
| 40 | + resp_parsed = parse_result_attributes(resp) |
| 41 | + answers = [[x["sAMAccountName"], x["scriptPath"]] for x in resp_parsed] |
| 42 | + |
| 43 | + answers = self.filter_answer(context, answers) |
| 44 | + |
| 45 | + if answers: |
| 46 | + context.log.success("Found the following attributes: ") |
| 47 | + for answer in answers: |
| 48 | + context.log.highlight(f"User: {answer[0]:<20} ScriptPath: {answer[1]}") |
| 49 | + |
| 50 | + # Save the results to a file |
| 51 | + if self.OUTPUTFILE: |
| 52 | + self.save_to_file(context, answers) |
| 53 | + else: |
| 54 | + context.log.warning("No results found after filtering.") |
| 55 | + |
| 56 | + def filter_answer(self, context, answers): |
| 57 | + # No filter |
| 58 | + if not self.FILTER: |
| 59 | + context.log.debug("No filter option enabled") |
| 60 | + return answers |
| 61 | + # Filter |
| 62 | + context.log.debug(f"Filter info field with: {self.FILTER}") |
| 63 | + return [answer for answer in answers if self.FILTER in answer[0]] |
| 64 | + |
| 65 | + def save_to_file(self, context, answers): |
| 66 | + """Save the results to a JSON file.""" |
| 67 | + try: |
| 68 | + # Format answers as a list of dictionaries for JSON output |
| 69 | + json_data = [{"sAMAccountName": answer[0], "scriptPath": answer[1]} for answer in answers] |
| 70 | + |
| 71 | + # Save the JSON data to the specified file |
| 72 | + with open(self.OUTPUTFILE, "w") as f: |
| 73 | + json.dump(json_data, f, indent=4) |
| 74 | + context.log.success(f"Results successfully saved to {self.OUTPUTFILE}") |
| 75 | + |
| 76 | + except Exception as e: |
| 77 | + context.log.error(f"Failed to save results to file: {e}") |
0 commit comments