Windows API 之 OpenProcessToken、GetTokenInformation
The following example uses the OpenProcessToken and GetTokenInformation functions to get the group memberships in an access token.
The GetTokenInformation function retrieves a specified type of information about an access token. The calling process must have appropriate access rights to obtain the information.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379554%28v=vs.85%29.aspx
The OpenProcessToken function opens the access token associated with a process.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379295%28v=vs.85%29.aspx
参数如下:
BOOL WINAPI GetTokenInformation(
_In_ HANDLE TokenHandle,
_In_ TOKEN_INFORMATION_CLASS TokenInformationClass,
_Out_opt_ LPVOID TokenInformation,
_In_ DWORD TokenInformationLength,
_Out_ PDWORD ReturnLength
);
The AllocateAndInitializeSid function allocates and initializes a security identifier (SID) with up to eight subauthorities.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa375213%28v=vs.85%29.aspx
参数如下:
pIdentifierAuthority [in] A pointer to a SID_IDENTIFIER_AUTHORITY structure. This structure provides the top-level identifier authority value to set in the SID.
nSubAuthorityCount [in] Specifies the number of subauthorities to place in the SID. This parameter also identifies how many of the subauthority parameters have meaningful values. This parameter must contain a value from to . For example, a value of indicates that the subauthority values specified by the dwSubAuthority0, dwSubAuthority1, and dwSubAuthority2 parameters have meaningful values and to ignore the remainder.
dwSubAuthority0 [in] Subauthority value to place in the SID.
pSid [out] A pointer to a variable that receives the pointer to the allocated and initialized SID structure.
access token含义:
参考:
An access token contains the security information for a logon session. The system creates an access token when a user logs on, and every process executed on behalf of the user has a copy of the token. The token identifies the user, the user's groups, and the user's privileges. The system uses the token to control access to securable objects and to control the ability of the user to perform various system-related operations on the local computer. There are two kinds of access token, primary and impersonation.
SID含义:
The system uses the SID in the access token to identify the user in all subsequent interactions with Windows security.
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa379571%28v=vs.85%29.aspx
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "advapi32.lib") #define MAX_NAME 256 using namespace std; int main()
{
DWORD i, dwSize = , dwResult = ;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
PSID pSID = NULL;
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
char lpName[MAX_NAME];
char lpDomain[MAX_NAME];
SID_NAME_USE SidType; // Open a handle to the access token for the calling process.
//TOKEN_QUERY:Required to query an access token.
//GetCurrentProcess()返回进程句柄
//[out]hToken是access token的句柄
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
{
printf("OpenProcessToken Error %u\n", GetLastError());
return FALSE;
} //前后两次调用GetTokenInformation的目的不同
// Call GetTokenInformation to get the buffer size.
//The TOKEN_GROUPS structure contains information about the group security identifiers (SIDs) in an access token.
if (!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
{
dwResult = GetLastError();
if (dwResult != ERROR_INSUFFICIENT_BUFFER) {
printf("GetTokenInformation Error %u\n", dwResult);
return FALSE;
}
} // Allocate the buffer.
pGroupInfo = (PTOKEN_GROUPS)GlobalAlloc(GPTR, dwSize); // Call GetTokenInformation again to get the group information. if (!GetTokenInformation(hToken, TokenGroups, pGroupInfo,
dwSize, &dwSize))
{
printf("GetTokenInformation Error %u\n", GetLastError());
return FALSE;
} // Create a SID for the BUILTIN\Administrators group. if (!AllocateAndInitializeSid(&SIDAuth, ,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
, , , , , ,
&pSID))
{
printf("AllocateAndInitializeSid Error %u\n", GetLastError());
return FALSE;
}
// Loop through the group SIDs looking for the administrator SID.
//
for (i = ; i < pGroupInfo->GroupCount; i++)
{
if (EqualSid(pSID, pGroupInfo->Groups[i].Sid))
{ // Lookup the account name and print it. dwSize = MAX_NAME;
if (!LookupAccountSid(NULL, pGroupInfo->Groups[i].Sid,
lpName, &dwSize, lpDomain,
&dwSize, &SidType))
{
dwResult = GetLastError();
if (dwResult == ERROR_NONE_MAPPED)
strcpy_s(lpName, dwSize, "NONE_MAPPED");
else
{
printf("LookupAccountSid Error %u\n", GetLastError());
return FALSE;
}
} printf("Current user is a member of the %s\\%s group\n",
lpDomain, lpName); // Find out whether the SID is enabled in the token.
if (pGroupInfo->Groups[i].Attributes & SE_GROUP_ENABLED)
printf("The group SID is enabled.\n");
else if (pGroupInfo->Groups[i].Attributes &
SE_GROUP_USE_FOR_DENY_ONLY)
printf("The group SID is a deny-only SID.\n");
else
printf("The group SID is not enabled.\n");
}
}
if (pSID)
FreeSid(pSID);
if (pGroupInfo)
GlobalFree(pGroupInfo); system("pause");
return ;
}
整体流程:
OpenProcessToken:获取token句柄
GetTokenInformation:获取group information
for循环:在group中查找
Windows API 之 OpenProcessToken、GetTokenInformation的更多相关文章
- Windows API 进程相关笔记
0. 前言 最近做了一个进程信息相关的项目,整理了一下自己做项目时的笔记,分享给大家 1. 相关概念 1.1 HANDLE 概念 HANDLE(句柄)是Windows操作系统中的一个概念. 在Wind ...
- Windows API 学习
Windows API学习 以下都是我个人一些理解,笔者不太了解windows开发,如有错误请告知,非常感谢,一切以microsoft官方文档为准. https://docs.microsoft.co ...
- C# Windows API
API:应用程序接口(API:Application Program Interface)应用程序接口(API:application programming interface)是一组定义.程序及协 ...
- Windows API 函数列表 附帮助手册
所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...
- Windows API Hooking in Python
catalogue . 相关基础知识 . Deviare API Hook Overview . 使用ctypes调用Windows API . pydbg . winappdbg . dll inj ...
- 初识【Windows API】--文本去重
最近学习操作系统中,老师布置了一个作业,运用系统调用函数删除文件夹下两个重复文本类文件,Linux玩不动,于是就只能在Windows下进行了. 看了一下介绍Windows API的博客: 点击打开 基 ...
- C#调用windows API的一些方法
使用C#调用windows API(从其它地方总结来的,以备查询) C#调用windows API也可以叫做C#如何直接调用非托管代码,通常有2种方法: 1. 直接调用从 DLL 导出的函数. 2. ...
- Qt中使用Windows API
在Windows平台上进行开发,不可避免与Windows API打交道,Qt中使用的时候要添加对应API的头文件和链接lib文件,另外使用的Windows API的代码部分要使用#ifdef Q_O ...
- 在VBA中使用Windows API
VBA是一种强大的编程语言,可用于自定义Microsoft Office解决方案.通过使用VBA处理一个或多个Office应用程序对象模型,可以容易地修改Office应用程序的功能或者能够使两个或多个 ...
随机推荐
- swift UILable的用法
- POJ1613 147/思维题
题目链接[https://www.oj.swust.edu.cn/problem/show/1613] 题意:输出第K小的由1.4.7数字组成的数字. 解题过程:1.4.7.11.14.17.41.4 ...
- 《JavaScript高级程序设计》读书笔记 ---语句
do-while语句do-while 语句是一种后测试循环语句,即只有在循环体中的代码执行之后,才会测试出口条件.换句话说,在对条件表达式求值之前,循环体内的代码至少会被执行一次.以下是do-whil ...
- HDU 5543 Pick The Sticks
背包变形.与普通的背包问题不同的是:允许有两个物品可以花费减半. 因此加一维即可,dp[i][j][k]表示前i个物品,有j个花费减半了,总花费为k的情况下的最优解. #pragma comment( ...
- Linux系统监控实用工具Glances
Linux系统监控实用工具Glances Glances安装 Glances安装要求:python >= 2.6 和 psutil >= 0.4.1 1.第一步,安装了python-> ...
- linux常用服务软件搭建及使用技巧
一.Webmin安装: Webmin 是一个基于浏览器的管理工具,可以应用于Linux 和其他一些平台,提供了可以完成很多管理和操作任务的图形化界面 •安装完成后,root 用户会被自动创建,密码为系 ...
- Chapter 2 Open Book——15
The rest of the week was uneventful. I got used to the routine of my classes. 这周剩下的时间都是平淡无事的.我就是正常的上 ...
- php数据排序---array_multisort
PHP代码 <?php $ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); ...
- bingo 跨action异步获取参数
html(定时器模拟异步) <script> setTimeout(function(){ window.teacInfo = {a:1,b:2}; },2000);</script ...
- velocity 教程
1,<title> $!{product.name} - $!{title} $!{about.title} - $!{title} $!{news.title} - $!{title} ...