window安全相关

整理于 http://www.cppblog.com/weiym/archive/2013/08/25/202751.html?opt=admin

xmind

判断是否管理员权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Windows API IsUserAnAdmin()
BOOL Am_I_In_Admin_Group(BOOL bCheckAdminMode /*= FALSE*/)
{
BOOL fAdmin;
HANDLE hThread;
TOKEN_GROUPS *ptg = NULL;
DWORD cbTokenGroups;
DWORD dwGroup;
PSID psidAdmin;
SID_IDENTIFIER_AUTHORITY SystemSidAuthority= SECURITY_NT_AUTHORITY;
if ( !OpenThreadToken ( GetCurrentThread(), TOKEN_QUERY, FALSE, &hThread))
{
if ( GetLastError() == ERROR_NO_TOKEN)
{
if (! OpenProcessToken ( GetCurrentProcess(), TOKEN_QUERY,
&hThread))
return ( FALSE);
}
else
return ( FALSE);
}
if ( GetTokenInformation ( hThread, TokenGroups, NULL, 0, &cbTokenGroups))
return ( FALSE);
if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return ( FALSE);
if ( ! ( ptg= (TOKEN_GROUPS*)_alloca ( cbTokenGroups)))
return ( FALSE);
if ( !GetTokenInformation ( hThread, TokenGroups, ptg, cbTokenGroups,
&cbTokenGroups) )
return ( FALSE);
if ( ! AllocateAndInitializeSid ( &SystemSidAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &psidAdmin) )
return ( FALSE);
fAdmin= FALSE;
for ( dwGroup= 0; dwGroup < ptg->GroupCount; dwGroup++)
{
if ( EqualSid ( ptg->Groups[dwGroup].Sid, psidAdmin))
{
if(bCheckAdminMode)
{
if((ptg->Groups[dwGroup].Attributes) & SE_GROUP_ENABLED)
{
fAdmin = TRUE;
}
}
else
{
fAdmin = TRUE;
}
break;
}
}
FreeSid ( psidAdmin);
return ( fAdmin);
}

提权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//注意只有原来是Disable的权限才可以提成Enable, 如果原来就没有这个权限, 是提不上去的。

BOOL EnablePrivilege(HANDLE hToken, LPCTSTR lpszPrivilegeName)
{
TOKEN_PRIVILEGES tkp = {0};
BOOL bRet = LookupPrivilegeValue( NULL, lpszPrivilegeName, &tkp.Privileges[0].Luid );
if(!bRet) return FALSE;

tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
bRet = AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL );

return bRet;
}

如何判断用户的进程完整性级别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void CIntegrityLevel::Print(std::wostream& os) const
{
SID* pSid = (SID*)m_pIntegrity->Label.Sid;
DWORD rid = pSid->SubAuthority[0];

LPCTSTR lpszIntegrity = L"Unknown";
switch (rid)
{
case SECURITY_MANDATORY_UNTRUSTED_RID:
{
lpszIntegrity = L"Untrusted";
break;
}
case SECURITY_MANDATORY_LOW_RID:
{
lpszIntegrity = L"Low";
break;
}
case SECURITY_MANDATORY_MEDIUM_RID:
{
lpszIntegrity = L"Medium";
break;
}

case SECURITY_MANDATORY_MEDIUM_PLUS_RID:
{
lpszIntegrity = L"Medium +";
break;
}
case SECURITY_MANDATORY_HIGH_RID:
{
lpszIntegrity = L"High";
break;
}
case SECURITY_MANDATORY_SYSTEM_RID:
{
lpszIntegrity = L"System";
break;
}
default:
{
lpszIntegrity = L"XXXXX";
}
}

os << L"Integrity: " << lpszIntegrity << endl;
}

如何指定程序默认启动运行的级别?

在VC里配置Manifest文件.

  • asInvoker:默认选项,新的进程将简单地继承其父进程的访问令牌

  • highestAvailable:应用程序会选择该用户允许范围内尽可能高的权限。对于标准用户来说,该选项与asInvoker一样,而对于管理员来说,这就意味着请求Admin令牌。

  • requireAdministrator:应用程序需要Admin令牌。运行该程序时,标准用户将要输入管理员的用户名和密码,而管理员则要在弹出的确认对话框中进行确认。

  • AAA

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2020-2021 一潭清泓
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信