TExcelTAD
Home › Windows & Active Directory Security › Security Principals, SIDs, Tokens and Integrity Levels

Security Principals, SIDs, Tokens and Integrity Levels

An analyst exports the members of Domain Admins and finds four names, all expected. A week later the incident responders show that a fifth account had been a domain admin all along. It was never a direct member: it sat in a group, inside a group, inside Domain Admins; and on another machine it carried the domain admins' SID in a hidden attribute. Both are ways to end up with a powerful token without appearing on the obvious list. This lesson reads SIDs and tokens the way a domain reviewer must: as numbers to be decoded and expanded, not names to be read.

The basics (what a SID is, local well-known SIDs, how the kernel walks a DACL, the UAC split token) are in Windows Users, UAC & ACLs and Windows Accounts, Tokens and UAC. Here we add the domain side.

Security principals and domain SIDs

A security principal is anything that can be granted access: a user, a computer, a group, a service, or one of the built-in identities. In a domain every principal gets a SID made of the domain SID plus a relative ID (RID). Some RIDs are fixed in every domain, which is why attackers and defenders both know them by heart:

RID / SID Principal Notes
500 the domain Administrator exists in every domain, can't be deleted
502 krbtgt its key encrypts every Kerberos ticket-granting ticket
512 Domain Admins admins of every domain-joined machine by default
513 / 515 Domain Users / Domain Computers every user / every machine
516 Domain Controllers the DCs' computer accounts
518 / 519 Schema Admins / Enterprise Admins exist only in the forest root domain
525 Protected Users the hardened group of a later lesson
S-1-5-32-544 (built-in) Administrators on a DC, this is "administrators of all DCs"
S-1-5-32-548, -549, -551 Account, Server, Backup Operators quietly powerful on DCs: log on locally, back up the database
S-1-5-9 Enterprise Domain Controllers used in replication permissions
S-1-5-11 Authenticated Users every authenticated account of the forest and of trusted domains

New accounts receive RIDs from 1000 up, handed out in blocks by the RID master (see Domain Controllers, Replication and FSMO Roles).

Reading objectSid from an LDAP export

Directory tools return a SID as raw bytes, not as the S-1-… string. The layout is short: one byte of revision, one byte saying how many sub-authorities follow, a 6-byte big-endian identifier authority, then each sub-authority as a 4-byte little-endian integer. The program decodes three values as they might appear base64-encoded in an LDIF export of the lab domain (the domain identifier is made up), and classifies them by RID.

python · try it yourself
import base64, struct

WELL_KNOWN_RID = {500: "Administrator", 502: "krbtgt", 512: "Domain Admins",
                  513: "Domain Users", 516: "Domain Controllers", 519: "Enterprise Admins"}

def sid_to_str(raw: bytes) -> str:
    revision, count = raw[0], raw[1]
    authority = int.from_bytes(raw[2:8], "big")
    subs = struct.unpack("<" + "I" * count, raw[8:8 + 4 * count])
    return "S-" + "-".join(str(x) for x in (revision, authority, *subs))

def sid_from_str(text: str) -> bytes:
    parts = [int(p) for p in text.split("-")[1:]]
    revision, authority, subs = parts[0], parts[1], parts[2:]
    return bytes([revision, len(subs)]) + authority.to_bytes(6, "big") + struct.pack("<" + "I" * len(subs), *subs)

DOMAIN = "S-1-5-21-1111111111-2222222222-3333333333"   # lab domain corp.lab.test (made up)
ldif = {name: base64.b64encode(sid_from_str(f"{DOMAIN}-{rid}")).decode()
        for name, rid in [("svc-sql", 1107), ("krbtgt", 502), ("Domain Admins", 512)]}

for name, b64 in ldif.items():
    raw = base64.b64decode(b64)
    sid = sid_to_str(raw)
    rid = int(sid.rsplit("-", 1)[1])
    kind = WELL_KNOWN_RID.get(rid, "ordinary account (RID >= 1000)" if rid >= 1000 else "reserved")
    print(f"{name:14} objectSid:: {b64}")
    print(f"{'':14} -> {sid}  RID {rid}: {kind}")
print("bytes for svc-sql:", sid_from_str(f"{DOMAIN}-1107").hex(" "))
svc-sql        objectSid:: AQUAAAAAAAUVAAAAxzU6Qo5rdIRVoa7GUwQAAA==
               -> S-1-5-21-1111111111-2222222222-3333333333-1107  RID 1107: ordinary account (RID >= 1000)
krbtgt         objectSid:: AQUAAAAAAAUVAAAAxzU6Qo5rdIRVoa7G9gEAAA==
               -> S-1-5-21-1111111111-2222222222-3333333333-502  RID 502: krbtgt
Domain Admins  objectSid:: AQUAAAAAAAUVAAAAxzU6Qo5rdIRVoa7GAAIAAA==
               -> S-1-5-21-1111111111-2222222222-3333333333-512  RID 512: Domain Admins
bytes for svc-sql: 01 05 00 00 00 00 00 05 15 00 00 00 c7 35 3a 42 8e 6b 74 84 55 a1 ae c6 53 04 00 00

The hex line shows the layout: 01 revision, 05 sub-authorities, 00 00 00 00 00 05 the NT authority, then 15 00 00 00 (21, little-endian) and the four domain and RID values. A detection rule that compares SIDs as strings in one log and bytes in another will silently miss matches, so normalise first.

How a domain token is built

When a user signs in to a domain machine, the token holds the user's SID, the SIDs of every group the user belongs to, directly or through nesting, the SIDs in the account's sIDHistory attribute, plus well-known SIDs for the logon (Authenticated Users, Interactive or Network, …). In a domain, the group list comes from the Kerberos ticket's PAC (The PAC, Encryption Types and the End of RC4). Two consequences matter:

  • Nesting is transitive. If IT-Helpdesk is a member of Server-Ops, which is a member of Domain Admins, every help-desk member has Domain Admins (RID 512) in the token, though no listing of Domain Admins shows their names.
  • SID history counts like membership. sIDHistory exists to keep access working after a migration between domains; a SID put there is honoured as if the user still had it. An attacker with enough rights can write the RID 512 SID into an ordinary account's history.

Expanding nesting is a graph walk. The program builds the effective membership of the privileged groups from a synthetic group export and reports who has which privileged SID and why.

python · try it yourself
import networkx as nx

member_of = [  # (member, group) from a synthetic export of corp.lab.test
    ("alice", "Domain Admins"), ("bob", "IT-Helpdesk"), ("carol", "IT-Helpdesk"),
    ("IT-Helpdesk", "Server-Ops"), ("Server-Ops", "Domain Admins"),
    ("dave", "Backup Operators"), ("erin", "Marketing"), ("svc-sql", "Server-Ops"),
]
sid_history = {"erin": ["Domain Admins"]}          # attribute written outside normal processes
PRIVILEGED = ["Domain Admins", "Enterprise Admins", "Administrators", "Backup Operators", "Account Operators"]

g = nx.DiGraph(member_of)
users = sorted(n for n in g if g.in_degree(n) == 0)
for u in users:
    groups = nx.descendants(g, u)
    hits = []
    for grp in PRIVILEGED:
        if grp in groups:
            path = nx.shortest_path(g, u, grp)
            hits.append(f"{grp} via {' > '.join(path[1:-1]) or 'direct membership'}")
    hits += [f"{s} via sIDHistory" for s in sid_history.get(u, [])]
    if hits:
        print(f"{u:8} " + "; ".join(hits))
direct = sorted(m for m, grp in member_of if grp == "Domain Admins" and m in users)
print("direct members listed by a naive export:", direct)
alice    Domain Admins via direct membership
bob      Domain Admins via IT-Helpdesk > Server-Ops
carol    Domain Admins via IT-Helpdesk > Server-Ops
dave     Backup Operators via direct membership
erin     Domain Admins via sIDHistory
svc-sql  Domain Admins via Server-Ops
direct members listed by a naive export: ['alice']

A naive check lists only alice; the token view finds six accounts with a privileged SID: three through nesting (bob, carol, svc-sql), one through SID history (erin), and dave through Backup Operators, a built-in group that rarely appears on anyone's list of "admins".

Integrity levels and UAC, in a domain

Every token also carries a mandatory label (Low, Medium, High, System). An object labelled High can't be written by a Medium process even if the DACL allows it ("no write up"). UAC gives admins a Medium filtered token and a High full one. One UAC rule is decisive in a domain: remote UAC restrictions. When a local administrator account (other than RID 500) connects over the network, it gets a filtered token, so it can't administer the machine remotely; a domain account in the local Administrators group gets the full token. The registry value LocalAccountTokenFilterPolicy = 1 turns the restriction off, which makes a shared local admin password usable for lateral movement. It is a frequent finding.

How it is detected

  • 4728 / 4732 / 4756: a member was added to a global / local (domain local) / universal security group. Alert on any change to the privileged groups, including nested additions (the change happens to Server-Ops, not to Domain Admins, so alert on every group that is privileged by expansion).
  • 4765 / 4766: SID history was added to an account / an attempt failed. Outside a planned migration, 4765 is an incident by itself. 5136 (directory object modified) on sIDHistory catches it when directory-service auditing is on.
  • 4672: special privileges assigned at logon; compare the accounts that trigger it with your list of expected admins.
  • 4627 (group membership information, with Audit Group Membership) lists the token's groups at logon: the expansion done by Windows itself.
  • Periodic expansion reviews like the program above (in AD: Get-ADGroupMember -Recursive, or an attack-path tool) beat reading direct-membership lists.

How it is fixed or prevented

  • Keep privileged groups flat and small: no nested groups inside Domain Admins, Enterprise Admins, Administrators, Account/Server/Backup Operators. Leave Account Operators and Server Operators empty.
  • Clear sIDHistory after migrations finish; enable SID filtering on trusts (Forests, Domains, OUs and Trusts).
  • Keep LocalAccountTokenFilterPolicy at 0 and give every machine a unique local admin password (Windows LAPS).
  • Treat every group that expands into a privileged SID as Tier 0 (Tiered Administration).

Pitfalls

  • Trusting names. Accounts can be renamed; SIDs can't. Detections and allow-lists should key on SIDs.
  • Forgetting built-in groups. Backup Operators can read the AD database on a DC; Print Operators can load drivers on DCs. They look harmless in a group list.
  • Expanding only one level. Nesting depth is unlimited; always compute the transitive closure.
  • Ignoring the token refresh. A user removed from a group keeps the SID in tokens and Kerberos tickets until they expire (10 hours by default for a TGT), so removal is not instant revocation.

Practice

1. Decode the first eight bytes 01 02 00 00 00 00 00 05, followed by 20 00 00 00 20 02 00 00.

Revision 1, two sub-authorities, authority 5, sub-authorities 32 and 544: S-1-5-32-544, the built-in Administrators group.

2. In the nesting model, `svc-sql` is a member of `Server-Ops`. What happens to its rights if a technician removes `Server-Ops` from `Domain Admins`?

svc-sql, bob and carol lose the Domain Admins SID at their next logon (existing tokens and tickets keep it until they expire). alice (direct) and erin (SID history) keep it; erin's case needs the attribute cleared.

3. Why does `LocalAccountTokenFilterPolicy = 1` matter for lateral movement?

It gives local admin accounts a full token over the network. With one shared local admin password, an attacker who learns it on one machine can administer all of them remotely.

Test yourself

Which RID belongs to the account whose key encrypts all ticket-granting tickets?

  1. 500
  2. 502
  3. 512
  4. 519

In the binary form of a SID, how are the sub-authorities stored?

  1. As ASCII text
  2. As 4-byte little-endian integers after a 6-byte big-endian authority
  3. As one big-endian 64-bit number
  4. Base64-encoded

Which event ID records that SID history was added to an account?

  1. 4624
  2. 4728
  3. 4765
  4. 1102

Why can a user have Domain Admins rights without being a direct member of Domain Admins?

  1. Because of UAC
  2. Group nesting and SID history both put the RID 512 SID into the token
  3. Because Authenticated Users is in Domain Admins by default
  4. It is impossible