|
2 | 2 | """ |
3 | 3 | SID Converter Script |
4 | 4 |
|
5 | | -This script converts a Domain SID from its standard string format to a hexadecimal representation. |
6 | | -
|
7 | | -Usage: |
8 | | - python3 ScriptName.py -s S-1-5-21-1154311717-913441446-2400334863-1114 |
| 5 | +This script converts a Domain SID from its standard string format to a |
| 6 | +hexadecimal representation. |
9 | 7 | """ |
10 | 8 |
|
11 | | -import sys |
12 | 9 | import argparse |
13 | | -from struct import pack |
14 | | -from typing import Any |
| 10 | +import shutil |
| 11 | +import subprocess |
| 12 | +import sys |
15 | 13 |
|
16 | 14 |
|
17 | | -def convert_sid_to_hex(sid: str) -> str: |
18 | | - """ |
19 | | - Convert a SID string to its hexadecimal representation. |
20 | | - """ |
21 | | - items = sid.split('-') |
22 | | - if len(items) < 4 or items[0] != "S": |
23 | | - raise ValueError("Invalid SID format. It should start with 'S' and contain at least 4 parts.") |
24 | | - |
| 15 | +GREEN = "\033[1;92m" |
| 16 | +MAX_IDENTIFIER_AUTHORITY = (1 << 48) - 1 |
| 17 | +MAX_SUB_AUTHORITY = (1 << 32) - 1 |
| 18 | +RESET = "\033[0m" |
| 19 | + |
| 20 | + |
| 21 | +def create_argument_parser(*args, **kwargs): |
25 | 22 | try: |
26 | | - revision = pack('B', int(items[1])) |
27 | | - dash_number = pack('B', len(items) - 3) |
28 | | - identifier_authority = b'\x00\x00' + pack('>L', int(items[2])) |
29 | | - except Exception as e: |
30 | | - raise ValueError("Error processing SID components. Please check the input format.") from e |
| 23 | + return argparse.ArgumentParser(*args, color=False, **kwargs) |
| 24 | + except TypeError: |
| 25 | + return argparse.ArgumentParser(*args, **kwargs) |
| 26 | + |
31 | 27 |
|
32 | | - sub_authority = b'' |
| 28 | +def parse_int_in_range(value: str, label: str, minimum: int, maximum: int) -> int: |
33 | 29 | try: |
34 | | - for i in range(len(items) - 3): |
35 | | - sub_authority += pack('<L', int(items[i + 3])) |
36 | | - except Exception as e: |
37 | | - raise ValueError("Error processing sub-authority values.") from e |
| 30 | + parsed = int(value, 10) |
| 31 | + except ValueError as exc: |
| 32 | + raise ValueError(f"Invalid {label}: {value}") from exc |
| 33 | + |
| 34 | + if not (minimum <= parsed <= maximum): |
| 35 | + raise ValueError( |
| 36 | + f"{label.capitalize()} out of range: {value} " |
| 37 | + f"(expected {minimum}..{maximum})" |
| 38 | + ) |
| 39 | + |
| 40 | + return parsed |
38 | 41 |
|
39 | | - hex_sid = revision + dash_number + identifier_authority + sub_authority |
40 | | - return '0x' + ''.join('{:02X}'.format(b) for b in hex_sid) |
41 | 42 |
|
| 43 | +def parse_sid_components(sid: str): |
| 44 | + items = sid.strip().split("-") |
| 45 | + if len(items) < 4 or items[0] != "S": |
| 46 | + raise ValueError( |
| 47 | + "Invalid SID format. It should start with 'S-' and contain at least 4 parts." |
| 48 | + ) |
42 | 49 |
|
43 | | -def parse_arguments() -> argparse.Namespace: |
44 | | - """ |
45 | | - Parse command line arguments. |
46 | | - """ |
47 | | - parser = argparse.ArgumentParser( |
48 | | - description="Convert SID from string format to hexadecimal." |
| 50 | + revision = parse_int_in_range(items[1], "revision", 0, 255) |
| 51 | + identifier_authority = parse_int_in_range( |
| 52 | + items[2], |
| 53 | + "identifier authority", |
| 54 | + 0, |
| 55 | + MAX_IDENTIFIER_AUTHORITY, |
| 56 | + ) |
| 57 | + |
| 58 | + sub_authorities = [] |
| 59 | + for raw_value in items[3:]: |
| 60 | + sub_authorities.append( |
| 61 | + parse_int_in_range( |
| 62 | + raw_value, |
| 63 | + "sub-authority", |
| 64 | + 0, |
| 65 | + MAX_SUB_AUTHORITY, |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + if len(sub_authorities) > 255: |
| 70 | + raise ValueError("SID contains too many sub-authorities.") |
| 71 | + |
| 72 | + return revision, identifier_authority, sub_authorities |
| 73 | + |
| 74 | + |
| 75 | +def convert_sid_to_hex(sid: str) -> str: |
| 76 | + revision, identifier_authority, sub_authorities = parse_sid_components(sid) |
| 77 | + |
| 78 | + sid_bytes = bytearray() |
| 79 | + sid_bytes.append(revision) |
| 80 | + sid_bytes.append(len(sub_authorities)) |
| 81 | + sid_bytes.extend(identifier_authority.to_bytes(6, byteorder="big")) |
| 82 | + |
| 83 | + for sub_authority in sub_authorities: |
| 84 | + sid_bytes.extend(sub_authority.to_bytes(4, byteorder="little")) |
| 85 | + |
| 86 | + return "0x" + sid_bytes.hex().upper() |
| 87 | + |
| 88 | + |
| 89 | +def build_parser() -> argparse.ArgumentParser: |
| 90 | + parser = create_argument_parser( |
| 91 | + description="Convert SID from string format to hexadecimal.", |
| 92 | + add_help=False, |
49 | 93 | ) |
50 | 94 | parser.add_argument( |
51 | | - "-s", "--sid", |
| 95 | + "-s", |
| 96 | + "--sid", |
52 | 97 | type=str, |
53 | | - help="Domain SID in standard format (e.g. S-1-5-21-1154311717-913441446-2400334863-1114)" |
| 98 | + help=( |
| 99 | + "Domain SID in standard format " |
| 100 | + "(e.g. S-1-5-21-1154311717-913441446-2400334863-1114)" |
| 101 | + ), |
54 | 102 | ) |
55 | | - if len(sys.argv) == 1: |
56 | | - parser.print_help() |
57 | | - sys.exit(1) |
58 | | - |
59 | | - return parser.parse_args() |
| 103 | + parser.add_argument( |
| 104 | + "-h", |
| 105 | + "--help", |
| 106 | + action="store_true", |
| 107 | + help="Show this help message and exit.", |
| 108 | + ) |
| 109 | + return parser |
| 110 | + |
| 111 | + |
| 112 | +def clipboard_commands() -> list[list[str]]: |
| 113 | + commands = [] |
| 114 | + candidates = [ |
| 115 | + ["pbcopy"], |
| 116 | + ["wl-copy"], |
| 117 | + ["xclip", "-selection", "clipboard"], |
| 118 | + ["xsel", "--clipboard", "--input"], |
| 119 | + ["clip.exe"], |
| 120 | + ["clip"], |
| 121 | + [ |
| 122 | + "powershell.exe", |
| 123 | + "-NoProfile", |
| 124 | + "-Command", |
| 125 | + "Set-Clipboard -Value ([Console]::In.ReadToEnd())", |
| 126 | + ], |
| 127 | + ] |
| 128 | + for command in candidates: |
| 129 | + if shutil.which(command[0]): |
| 130 | + commands.append(command) |
| 131 | + return commands |
| 132 | + |
| 133 | + |
| 134 | +def copy_to_clipboard(text: str) -> bool: |
| 135 | + for command in clipboard_commands(): |
| 136 | + try: |
| 137 | + completed = subprocess.run( |
| 138 | + command, |
| 139 | + input=text, |
| 140 | + text=True, |
| 141 | + capture_output=True, |
| 142 | + check=False, |
| 143 | + ) |
| 144 | + except OSError: |
| 145 | + continue |
| 146 | + if completed.returncode == 0: |
| 147 | + return True |
| 148 | + return False |
60 | 149 |
|
61 | 150 |
|
62 | | -def main() -> None: |
63 | | - """ |
64 | | - Main function that handles argument parsing, SID conversion, and result output. |
65 | | - """ |
66 | | - args: argparse.Namespace = parse_arguments() |
| 151 | +def print_green_status(message: str) -> None: |
| 152 | + print(f"{GREEN}{message}{RESET}", file=sys.stderr, flush=True) |
| 153 | + |
| 154 | + |
| 155 | +def main(argv=None) -> int: |
| 156 | + argv_list = list(sys.argv[1:] if argv is None else argv) |
| 157 | + parser = build_parser() |
| 158 | + |
| 159 | + if not argv_list: |
| 160 | + parser.print_help(sys.stderr) |
| 161 | + return 1 |
| 162 | + |
| 163 | + args = parser.parse_args(argv_list) |
| 164 | + |
| 165 | + if args.help: |
| 166 | + parser.print_help() |
| 167 | + return 0 |
67 | 168 | if not args.sid: |
68 | | - print("Error: The SID argument is required.") |
69 | | - sys.exit(1) |
70 | | - sid: str = args.sid |
71 | | - print(f"[+] SID: {sid}") |
| 169 | + print("Error: The SID argument is required.", file=sys.stderr) |
| 170 | + return 1 |
| 171 | + |
72 | 172 | try: |
73 | | - result: str = convert_sid_to_hex(sid) |
74 | | - print(f"[+] Result: {result}") |
| 173 | + result = convert_sid_to_hex(args.sid) |
75 | 174 | except ValueError as err: |
76 | | - print(f"Error: {err}") |
77 | | - sys.exit(1) |
| 175 | + print(f"Error: {err}", file=sys.stderr) |
| 176 | + return 1 |
| 177 | + |
| 178 | + if copy_to_clipboard(result): |
| 179 | + print_green_status("Result copied to clipboard.") |
| 180 | + else: |
| 181 | + print( |
| 182 | + "Warning: clipboard tool not found or copy failed; result printed to stdout.", |
| 183 | + file=sys.stderr, |
| 184 | + flush=True, |
| 185 | + ) |
| 186 | + |
| 187 | + print(f"[+] SID: {args.sid}") |
| 188 | + print(f"[+] Result: {result}") |
| 189 | + return 0 |
78 | 190 |
|
79 | 191 |
|
80 | 192 | if __name__ == "__main__": |
81 | | - main() |
| 193 | + raise SystemExit(main()) |
0 commit comments