Skip to content

Instantly share code, notes, and snippets.

@axelgenus
Last active August 31, 2020 14:09
Show Gist options
  • Select an option

  • Save axelgenus/a9872a8bb57e3d3fa614dab3b9fa0b86 to your computer and use it in GitHub Desktop.

Select an option

Save axelgenus/a9872a8bb57e3d3fa614dab3b9fa0b86 to your computer and use it in GitHub Desktop.
Get object SID SDDL string representation from binary value.
using System.Text;
namespace LdapSearch
{
public static class Extensions
{
public static string GetObjectSid(this byte[] objectSid)
{
var sid = new StringBuilder("S-");
// get byte(0) - revision level
sid.AppendFormat("{0}", objectSid[0]);
// byte(1) - count of sub-authorities
int countSubAuths = objectSid[1] & 0xFF;
// byte(2-7) - 48 bit authority ([Big-Endian])
long authority = 0;
for (var i = 2; i <= 7; i++)
{
authority |= (long) objectSid[i] << (8 * (5 - (i - 2)));
}
sid.AppendFormat("-{0:X}", authority);
// iterate all the sub-auths and then countSubAuths x 32 bit sub authorities ([Little-Endian])
var offset = 8;
var size = 4; //4 bytes for each sub auth
for (var j = 0; j < countSubAuths; j++)
{
long subAuthority = 0;
for (var k = 0; k < size; k++)
{
subAuthority |= (long) (objectSid[offset + k] & 0xFF) << (8 * k);
}
// format it
sid.AppendFormat("-{0}", subAuthority);
offset += size;
}
return sid.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment