进程的内核对象是等着别人拿着令牌访问我们的

    令牌是我们拿着令牌区访问别人的内核对象的,与别的安全描述符权限比较

    windows完整性机制:是对windows安全授权的一个补充

    安全模块在拿token和安全描述符SECURITY_DESCRIPTOR比照之前,还会做一个完整性检查的工作

    完整性等级低的不能访问完整性等级高的,如果没有设置,内核对象会设置默认的完整性等级medium。

    让进程在一个低完整性等级下运行。

    1. BOOL GetProcessIntegrityLevel(HANDLE hProcess, PDWORD pIntegrityLevel,
    2. PDWORD pPolicy, PDWORD pResourceIntegrityLevel, PDWORD pResourcePolicy){}

    pIntegrityLevel:完整性等级,高的完整性等级是3000,中的是2000

    Value Description Symbol
    0x0000 Untrusted level SECURITY_MANDATORY_UNTRUSTED_RID
    0x1000 Low integrity level SECURITY_MANDATORY_LOW_RID
    0x2000 Medium integrity level SECURITY_MANDATORY_MEDIUM_RID
    0x3000 High integrity level SECURITY_MANDATORY_HIGH_RID
    0x4000 System integrity level SECURITY_MANDATORY_SYSTEM_RID

    pPolicy:策略等级

    Value Meaning
    TOKEN_MANDATORY_POLICY_OFF
    0x0
    No mandatory integrity policy is enforced for the token.
    TOKEN_MANDATORY_POLICY_NO_WRITE_UP
    0x1
    A process associated with the token cannot write to objects that have a greater mandatory integrity level.
    TOKEN_MANDATORY_POLICY_NEW_PROCESS_MIN
    0x2
    A process created with the token has an integrity level that is the lesser of the parent-process integrity level and the executable-file integrity level.
    TOKEN_MANDATORY_POLICY_VALID_MASK
    0x3
    A combination of TOKEN_MANDATORY_POLICY_NO_WRITE_UP and TOKEN_MANDATORY_POLICY_NEW_PROCESS_MIN.

    完整代码如下

    1. #include<Windows.h>
    2. #include<tchar.h>
    3. #include"Aclapi.h"
    4. BOOL GetProcessIntegrityLevel(HANDLE hProcess, PDWORD pIntegrityLevel,
    5. PDWORD pPolicy, PDWORD pResourceIntegrityLevel, PDWORD pResourcePolicy) {
    6. HANDLE hToken = NULL;
    7. if (!OpenProcessToken(hProcess, TOKEN_READ, &hToken)) {
    8. return(FALSE);
    9. }
    10. BOOL bReturn = FALSE;
    11. // First, compute the size of the buffer to get the Integrity level
    12. DWORD dwNeededSize = 0;
    13. if (!GetTokenInformation(
    14. hToken, TokenIntegrityLevel, NULL, 0, &dwNeededSize)) {
    15. PTOKEN_MANDATORY_LABEL pTokenInfo = NULL;
    16. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
    17. // Second, allocate a memory block with the the required size
    18. pTokenInfo = (PTOKEN_MANDATORY_LABEL)LocalAlloc(0, dwNeededSize);
    19. if (pTokenInfo != NULL) {
    20. // And finally, ask for the integrity level
    21. if (GetTokenInformation(hToken, TokenIntegrityLevel, pTokenInfo,
    22. dwNeededSize, &dwNeededSize)) {
    23. *pIntegrityLevel =
    24. *GetSidSubAuthority(
    25. pTokenInfo->Label.Sid,
    26. (*GetSidSubAuthorityCount(pTokenInfo->Label.Sid)-1)
    27. );
    28. bReturn = TRUE;
    29. }
    30. // Don't forget to free the memory
    31. LocalFree(pTokenInfo);
    32. }
    33. }
    34. }
    35. // Try to get the policy if the integrity level was available
    36. if (bReturn) {
    37. *pPolicy = TOKEN_MANDATORY_POLICY_OFF;
    38. dwNeededSize = sizeof(DWORD);
    39. GetTokenInformation(hToken, TokenMandatoryPolicy, pPolicy,
    40. dwNeededSize, &dwNeededSize);
    41. }
    42. // Look for the resource policy
    43. *pResourceIntegrityLevel = 0; // 0 means none explicitely set
    44. *pResourcePolicy = 0;
    45. PACL pSACL = NULL;
    46. PSECURITY_DESCRIPTOR pSD = NULL;
    47. DWORD dwResult = ERROR_SUCCESS;
    48. // Look for the no-read-up/no-write-up policy in the SACL
    49. if (hToken != NULL) {
    50. dwResult =
    51. GetSecurityInfo(
    52. hProcess, SE_KERNEL_OBJECT,
    53. LABEL_SECURITY_INFORMATION,
    54. NULL, NULL, NULL,
    55. &pSACL, &pSD
    56. );
    57. if (dwResult == ERROR_SUCCESS) {
    58. if (pSACL != NULL) {
    59. SYSTEM_MANDATORY_LABEL_ACE* pACE = NULL;
    60. if ((pSACL->AceCount > 0) && (GetAce(pSACL, 0, (PVOID*)&pACE))) {
    61. if (pACE != NULL) {
    62. SID* pSID = (SID*)(&pACE->SidStart);
    63. *pResourceIntegrityLevel = pSID->SubAuthority[0];
    64. *pResourcePolicy = pACE->Mask;
    65. }
    66. }
    67. }
    68. }
    69. // Cleanup memory allocated on our behalf
    70. if (pSD != NULL) LocalFree(pSD);
    71. }
    72. // Don't forget to close the token handle.
    73. CloseHandle(hToken);
    74. return(bReturn);
    75. }
    76. int _tmain()
    77. {
    78. DWORD IntegrityLevel,Policy,ResourceIntegrityLevel,ResourcePolicy;
    79. /*
    80. 第一个参数,是进程句柄
    81. 第二到第五个参数,是四个DWORD类型的指针
    82. */
    83. GetProcessIntegrityLevel(GetCurrentProcess(),&IntegrityLevel,
    84. &Policy,&ResourceIntegrityLevel,&ResourcePolicy);
    85. _tprintf(L"IntegrityLevel=%0X\n",IntegrityLevel);
    86. _tprintf(L"Policy=%0X\n",Policy);
    87. _tprintf(L"ResourceIntegrityLevel=%0X\n",ResourceIntegrityLevel);
    88. _tprintf(L"ResourcePolicy=%0X\n",ResourcePolicy);
    89. _gettchar();
    90. return 0;
    91. }

    内存内核对象
    令牌