Skip to content

Instantly share code, notes, and snippets.

@Kucharskov
Created March 10, 2020 11:07
Show Gist options
  • Save Kucharskov/6a5f8de42a4218e139945cf0393a1df3 to your computer and use it in GitHub Desktop.
Save Kucharskov/6a5f8de42a4218e139945cf0393a1df3 to your computer and use it in GitHub Desktop.
Script which converts SID (domain/user) to HEX format. Eg "S-1-5-21-3167813660-1240564177-918740779-0" becomes "0105000000000005150000001C00D1BCD181F1492BDFC23606040000"
#!/usr/bin/env python
# Created by: M. Kucharskov (https://kucharskov.pl)
import struct
def convert(sid):
# Remove "S-"
sid = sid.replace("S-", "")
# Split values
data = sid.split("-")
# Declare output
output = ""
# First element - revision
output += data.pop(0).zfill(2)
# Second element - number of dashes
dashes = int(data.pop(0))
output += str(dashes).zfill(2)
output += str(dashes).zfill(12)
# Rest of data is in hex
data = [struct.pack('<Q', int(x)).encode("hex") for x in data]
data = [x[:8] for x in data]
output += "".join(data)
return output.upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment