【问题标题】:Getting Token Information获取令牌信息
【发布时间】:2012-11-08 15:26:24
【问题描述】:

我有以下用 C 编写的程序:

#include "stdafx.h"
#include <Windows.h>

void main()
{
    char buffer[1000];
    int size = sizeof(buffer);
    PDWORD required_size;

    printf("----Application Privileges----\n\n");
    printf("In this program, we are going to obtain information about the application privileges\n\n");

    HANDLE process = OpenProcess(SYNCHRONIZE, FALSE, GetCurrentProcessId()); //Opening the current process
    HANDLE token; //Creating a handle for the token
    OpenProcessToken(process, TOKEN_ADJUST_PRIVILEGES, &token); //Opening the process token

    GetTokenInformation(token, TokenPrivileges, buffer, size, required_size); //Obtaining the token information
    printf("The following information was obtained with regards to the token privileges: \n\n");
    printf("%s\n\n", buffer);

    printf("Press enter to exit the program");
    getchar();

}

现在,我对令牌的使用比较陌生。当我尝试执行程序时,出现以下错误:

运行时检查失败 #3 - 变量“required_size”在未初始化的情况下被使用。

请问我该如何解决这个问题?我要做的是向用户显示有关当前进程的令牌权限的信息。

我不确切知道 GetTokenInformation 方法中的最后一个变量 (ReturnLength [out]) 的作用。我尝试阅读 msdn 文档,但不了解它的用途。

【问题讨论】:

  • 对不起,我编辑了上一个问题,忘记更改标签:s

标签: c windows security winapi token


【解决方案1】:

required_size 参数是一个“out”参数,这意味着它会从函数向您返回信息(即额外的返回值)。您应该将现有 DWORD 变量的地址传递给它,它会在那里填充数据,但您传递给它的是一个未初始化的指针,它试图写入。

您的代码应如下所示:

DWORD required_size;
GetTokenInformation(..., &required_size);  // Pass address of required_size
// required_size now contains the required size of the data buffer

【讨论】:

    【解决方案2】:

    再次查看我给你的example and detailed explanation。您需要先找到缓冲区的长度。然后将缓冲区初始化为您获得的 TOKEN_PRIVILEGES 结构的大小。这是初始化的那一行:

    BYTE* pBuffer = new BYTE[dwLen];
    

    【讨论】:

    • 感谢您的回答 :) 我在显示缓冲区中检索到的信息时遇到了一点问题。出于某种原因,屏幕上只显示无意义的垃圾信息。现在,我只想显示令牌信息。请多多包涵。这些东西都是我自己学的,这就是为什么有时候我很难理解一些概念。
    • 如果在您初始化并尝试循环访问权限后仍然给您带来麻烦,请编辑您的问题并发布新代码。然后我们可以尝试解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2013-06-25
    • 2019-01-03
    • 2016-08-07
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多