SepAdjustPrivileges

NTSTATUS __stdcall SepAdjustPrivileges(
        _TOKEN *Token,
        UINT8 MakeChanges,
        UINT8 DisableAllPrivileges,
        UINT64 PrivilegeCount,
        _LUID_AND_ATTRIBUTES *NewState,
        _TOKEN_PRIVILEGES *PreviousState,
        UINT64 *ReturnLength,
        UINT64 *ChangeCount,
        UINT8 *ChangesMade){
  unsigned int v10; 
  unsigned int v11; 
  void *Sid; 
  NTSTATUS result; 
  __int64 v16; 
  unsigned __int64 Present; 
  __int64 v18; 
  _LUID Luid; 
  unsigned __int64 v20; 
  unsigned int Attributes; 
  int v22; 
  unsigned int v23; 
  UINT8 v24; 
  NTSTATUS v25; 
  int v26; 
  _LUID v27; 
  __int64 v28; 
  bool v29; 
  __int64 v30; 
  unsigned __int64 v31; 
  _LUID v32; 
  __int64 v33; 
  __int64 v34; 
  unsigned int v35; 
  __int64 v36; 
  int v37; 
  __int64 v38; 
  unsigned __int64 v39; 
  __int64 v40; 
  signed int v41; 
  unsigned __int64 Enabled; 
  _BOOL4 v43; 
  unsigned int v44; 
  __int64 v45; 
  __int64 v46; 
  __int64 v47; 
  UINT64 EnabledCount; 
  INT64 AccessGranted; 
  UINT8 pbDominate; 
  char v52[2]; 
  int v53; 
  unsigned int DisabledCount; 
  NTSTATUS DisabledCount_4; 
  int v56; 
  __int64 v57; 
  _LUID v58; 
  UINT8 *v59; 
  UINT64 *v60; 
  _SID_AND_ATTRIBUTES IntegritySA; 
  _LUID_AND_ATTRIBUTES DisabledPrivileges[36]; 
  _LUID_AND_ATTRIBUTES EnabledPrivileges[36]; 

  v10 = 0;
  v11 = 0;
  v60 = ReturnLength;
  v53 = 0;
  DisabledCount = 0;
  v58 = 0i64;
  pbDominate = 0;
  v52[0] = 0;
  LOBYTE(v56) = 0;
  LODWORD(v57) = PrivilegeCount;
  v59 = ChangesMade;
  IntegritySA = 0i64;
  memset(DisabledPrivileges, 0i64, sizeof(DisabledPrivileges));
  memset(EnabledPrivileges, 0i64, sizeof(EnabledPrivileges));
  *(_DWORD *)ChangeCount = 0;
  *ChangesMade = 0;
  SepCopyTokenIntegrity(Token, &IntegritySA);
  Sid = IntegritySA.Sid;
  result = RtlSidDominates(IntegritySA.Sid, SeHighMandatorySid, &pbDominate);
  DisabledCount_4 = result;
  if( result < 0 )
    return result;
  if( !pbDominate )
  {
    result = RtlSidDominates(Sid, SeMediumMandatorySid, (UINT8 *)v52);
    DisabledCount_4 = result;
    if( result < 0 )
      return result;
    v37 = (unsigned __int8)v56;
    if( !v52[0] )
      v37 = 1;
    v56 = v37;
  }
  if( DisableAllPrivileges )
  {
    v24 = MakeChanges;
    v41 = 0;
    v23 = 0;
    do
    {
      Enabled = Token->Privileges.Enabled;
      if( (Enabled & (1i64 << v41)) != 0 )
      {
        if( v24 )
        {
          v43 = ((1i64 << v41) & Token->Privileges.EnabledByDefault) != 0;
          v57 = v41;
          v44 = v43 + 2;
          v58 = (_LUID)v41;
          v45 = v23++;
          v46 = v45;
          DisabledPrivileges[v46].Luid = (_LUID)v41;
          DisabledPrivileges[v46].Attributes = v44;
          if( PreviousState )
          {
            v47 = *(unsigned int *)ChangeCount;
            PreviousState->Privileges[v47].Luid = (_LUID)v41;
            PreviousState->Privileges[v47].Attributes = v44;
            Enabled = Token->Privileges.Enabled;
          }
          _bittestandreset64((__int64 *)&Enabled, (unsigned int)v41);
          v24 = MakeChanges;
          Token->Privileges.Enabled = Enabled;
        }
        ++*(_DWORD *)ChangeCount;
      }
      ++v41;
    }
    while( (unsigned int)v41 <= 0x24 );
LABEL_15:
    v25 = DisabledCount_4;
    goto LABEL_16;
  }
  if( !(_DWORD)v57 )
  {
    v23 = 0;
LABEL_14:
    v24 = MakeChanges;
    goto LABEL_15;
  }
  v16 = (unsigned int)v57;
  do
  {
    Present = Token->Privileges.Present;
    v18 = 1i64 << NewState->Luid.LowPart;
    if( (Present & v18) == 0 )
      goto LABEL_10;
    Luid = NewState->Luid;
    ++v10;
    v58 = NewState->Luid;
    v20 = Token->Privileges.Enabled;
    Attributes = NewState->Attributes;
    v22 = (((1i64 << SLOBYTE(v58.LowPart)) & v20) != 0 ? 2 : 0) | (((1i64 << SLOBYTE(v58.LowPart)) & Token->Privileges.EnabledByDefault) != 0);
    if( (Attributes & 4) != 0 )
    {
      if( MakeChanges )
      {
        Token->Privileges.Enabled = v20 & ~(1i64 << SLOBYTE(Luid.LowPart));
        Token->TokenFlags |= 0x800u;
        Token->Privileges.Present = Present & ~(1i64 << SLOBYTE(Luid.LowPart));
      }
    }
    else if( (Attributes & 2) != 0 )
    {
      if( (v20 & v18) != 0 )
        goto LABEL_10;
      if( !MakeChanges )
        goto LABEL_30;
      v27 = v58;
      if( PreviousState )
      {
        v36 = *(unsigned int *)ChangeCount;
        PreviousState->Privileges[v36].Luid = v58;
        PreviousState->Privileges[v36].Attributes = v22;
      }
      v28 = v11++;
      v29 = pbDominate == 0;
      v30 = v28;
      EnabledPrivileges[v30].Luid = v27;
      EnabledPrivileges[v30].Attributes = v22;
      if( !v29 )
        goto LABEL_29;
      if( v52[0] )
      {
        v38 = 0x1120160684i64;
        if( _bittest64(&v38, NewState->Luid.LowPart) )
          goto LABEL_52;
      }
      else
      {
        if( !(_BYTE)v56 )
          goto LABEL_30;
        v40 = 1i64 << NewState->Luid.LowPart;
        if( (Token->TokenFlags & 0x4000) != 0 )
        {
          if( (v40 & 0x200800000i64) != 0 )
          {
LABEL_29:
            v31 = Token->Privileges.Enabled;
            _bittestandset64((__int64 *)&v31, Luid.LowPart);
            Token->Privileges.Enabled = v31;
          }
          else
          {
LABEL_52:
            ++v53;
          }
LABEL_30:
          ++*(_DWORD *)ChangeCount;
          goto LABEL_10;
        }
        if( (v40 & 0x202800000i64) == 0 )
          goto LABEL_52;
      }
      v39 = Token->Privileges.Enabled;
      _bittestandset64((__int64 *)&v39, Luid.LowPart);
      Token->Privileges.Enabled = v39;
      ++*(_DWORD *)ChangeCount;
    }
    else if( (v20 & v18) != 0 )
    {
      if( MakeChanges )
      {
        v32 = v58;
        if( PreviousState )
        {
          v33 = *(unsigned int *)ChangeCount;
          PreviousState->Privileges[v33].Luid = v58;
          PreviousState->Privileges[v33].Attributes = v22;
          v20 = Token->Privileges.Enabled;
        }
        v34 = DisabledCount;
        v35 = DisabledCount + 1;
        _bittestandreset64((__int64 *)&v20, Luid.LowPart);
        DisabledPrivileges[v34].Luid = v32;
        Token->Privileges.Enabled = v20;
        DisabledPrivileges[v34].Attributes = v22;
        DisabledCount = v35;
      }
      ++*(_DWORD *)ChangeCount;
    }
LABEL_10:
    ++NewState;
    --v16;
  }
  while( v16 );
  if( v10 >= (unsigned int)v57 && !v53 )
  {
    v23 = DisabledCount;
    goto LABEL_14;
  }
  v24 = MakeChanges;
  v25 = 262;
  v23 = DisabledCount;
LABEL_16:
  if( *(_DWORD *)ChangeCount && v24 )
  {
    *v59 = 1;
    LOBYTE(AccessGranted) = v25 >= 0;
    LODWORD(EnabledCount) = v11;
    SepAdtTokenRightAdjusted(Token, DisabledPrivileges, v23, EnabledPrivileges, EnabledCount, AccessGranted);
  }
  if( PreviousState )
  {
    if( *(_DWORD *)ChangeCount > 1u )
      v26 = 12 * *(_DWORD *)ChangeCount + 4;
    else
      v26 = 16;
    *(_DWORD *)v60 = v26;
  }
  return v25;
}

Referenced by:

NtAdjustPrivilegesToken