Skip to content

Instantly share code, notes, and snippets.

@1upbyte
Created October 25, 2025 17:47
Show Gist options
  • Select an option

  • Save 1upbyte/bf7bafc7c61d1ff4f5a319badcb8a17c to your computer and use it in GitHub Desktop.

Select an option

Save 1upbyte/bf7bafc7c61d1ff4f5a319badcb8a17c to your computer and use it in GitHub Desktop.
convert raw SID to readable format (msssql)
# Get SID from MSSQL
# MSSQL> SELECT SUSER_SID()
import struct
import sys
def convert_sid(raw_sid: bytes) -> str:
if not raw_sid:
raise ValueError("Empty SID input")
revision, sub_authority_count = struct.unpack('BB', raw_sid[:2])
authority = struct.unpack('>Q', b'\x00\x00' + raw_sid[2:8])[0]
sub_authorities = struct.unpack('<' + 'I' * sub_authority_count, raw_sid[8:8 + 4 * sub_authority_count])
sid_str = f"S-{revision}-{authority}"
for sub_auth in sub_authorities:
sid_str += f"-{sub_auth}"
return sid_str
def main():
if len(sys.argv) != 2:
print("Usage: python3 sid_converter.py <hex_sid>")
print("Example: python3 sid_converter.py 010500000000000515000000dcf3ce2c2a9a384aa03b0000")
sys.exit(1)
hex_input = sys.argv[1]
try:
raw_sid = bytes.fromhex(hex_input)
readable_sid = convert_sid(raw_sid)
print(f"Readable SID: {readable_sid}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment