Skip to content

Instantly share code, notes, and snippets.

@vesse
Last active July 5, 2018 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vesse/b96802eb0714db34b19e to your computer and use it in GitHub Desktop.
Save vesse/b96802eb0714db34b19e to your computer and use it in GitHub Desktop.
Convert binary buffer object SID to string
# Convert binary encoded object SID to a string
# (eg. S-1-5-21-1004336348-1177238915-682003330-512)
#
# SID format: https://technet.microsoft.com/en-us/library/cc962011.aspx
#
# ldapjs `searchEntry` has attribute `raw` that holds the raw
# values, including the `objectSid` buffer when one exists. Pass that
# buffer to get the string representation that can then be easily
# used in LDAP search filters, for example.
#
pad = (s) -> if s.length < 2 then "0#{s}" else s
sidBufferToString = (buf) ->
return null unless buf?
version = buf[0]
subAuthorityCount = buf[1]
identifierAuthority = parseInt((buf[i].toString(16) for i in [2..7]).join(''), 16)
sidString = "S-#{version}-#{identifierAuthority}"
for i in [0..subAuthorityCount-1]
subAuthOffset = i * 4
tmp =
pad(buf[11 + subAuthOffset].toString(16)) +
pad(buf[10 + subAuthOffset].toString(16)) +
pad(buf[9 + subAuthOffset].toString(16)) +
pad(buf[8 + subAuthOffset].toString(16))
sidString += '-' + parseInt(tmp, 16)
sidString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment