Skip to content

Instantly share code, notes, and snippets.

@gesslar
Forked from Krizzzn/sid.js
Created May 2, 2024 19:19
Show Gist options
  • Save gesslar/98c90dee0f37c54f625fc88e7bf288c6 to your computer and use it in GitHub Desktop.
Save gesslar/98c90dee0f37c54f625fc88e7bf288c6 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.
#
*/
let pad = function(s) { if (s.length < 2) { return `0${s}`; } else { return s; } };
let sidBufferToString = function(buf) {
let asc, end;
let i;
if (buf == null) { return null; }
let version = buf[0];
let subAuthorityCount = buf[1];
let identifierAuthority = parseInt(((() => {
let result = [];
for (i = 2; i <= 7; i++) {
result.push(buf[i].toString(16));
}
return result;
})()).join(''), 16);
let sidString = `S-${version}-${identifierAuthority}`;
for (i = 0, end = subAuthorityCount-1, asc = 0 <= end; asc ? i <= end : i >= end; asc ? i++ : i--) {
let subAuthOffset = i * 4;
let 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)}`;
}
return sidString;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment