修改程序的权限需要用到3个函数:

1. 获取进程的令牌句柄: OpenProcessToken

2. 查找特权类型的ID: LookupPrivilegeValue

3. 修改进程的特权:AdjustTokenPrivilege

下面详细介绍每个函数的参数及使用方法:

BOOL OpenProcessToken( HANDLE ProcessHandle,
DWORD DesiredAccess,
PHANDLE TokenHandle );

Parameters

ProcessHandle

A handle to the process whose access token is opened. The process must have the PROCESS_QUERY_INFORMATION access permission.

for example: GetCurrentProcess() will return the handle of current process.

DesiredAccess

Specifies an access mask that specifies the requested types of access to the access token. These requested access types are compared with the discretionary access control list (DACL) of the token to determine which accesses are granted or denied.

For a list of access rights for access tokens, see Access Rights for Access-Token Objects.

TokenHandle

A pointer to a handle that identifies the newly opened access token when the function returns.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

BOOL LookupPrivilegeValueA( LPCSTR lpSystemName,
LPCSTR lpName,
PLUID lpLuid);

Parameters

lpSystemName

A pointer to a null-terminated string that specifies the name of the system on which the privilege name is retrieved. If a null string is specified, the function attempts to find the privilege name on the local system.

lpName

A pointer to a null-terminated string that specifies the name of the privilege, as defined in the Winnt.h header file. For example, this parameter could specify the constant, SE_SECURITY_NAME, or its corresponding string, "SeSecurityPrivilege".

lpLuid

A pointer to a variable that receives the LUID by which the privilege is known on the system specified by the lpSystemNameparameter.

Return Value

If the function succeeds, the function returns nonzero.

If the function fails, it returns zero. To get extended error information, call GetLastError.

BOOL AdjustTokenPrivileges( HANDLE            TokenHandle,
BOOL DisableAllPrivileges,
PTOKEN_PRIVILEGES NewState,
DWORD BufferLength,
PTOKEN_PRIVILEGES PreviousState,
PDWORD ReturnLength);

Parameters

TokenHandle

A handle to the access token that contains the privileges to be modified. The handle must have TOKEN_ADJUST_PRIVILEGES access to the token. If the PreviousState parameter is not NULL, the handle must also have TOKEN_QUERY access.

DisableAllPrivileges

Specifies whether the function disables all of the token's privileges. If this value is TRUE, the function disables all privileges and ignores the NewState parameter. If it is FALSE, the function modifies privileges based on the information pointed to by the NewStateparameter.

NewState

A pointer to a TOKEN_PRIVILEGES structure that specifies an array of privileges and their attributes. If the DisableAllPrivilegesparameter is FALSE, the AdjustTokenPrivileges function enables, disables, or removes these privileges for the token. The following table describes the action taken by the AdjustTokenPrivileges function, based on the privilege attribute.

Value Meaning

SE_PRIVILEGE_ENABLED

The function enables the privilege.

SE_PRIVILEGE_REMOVED

The privilege is removed from the list of privileges in the token. The other privileges in the list are reordered to remain contiguous.

SE_PRIVILEGE_REMOVED supersedes SE_PRIVILEGE_ENABLED.

Because the privilege has been removed from the token, attempts to reenable the privilege result in the warning ERROR_NOT_ALL_ASSIGNED as if the privilege had never existed.

Attempting to remove a privilege that does not exist in the token results in ERROR_NOT_ALL_ASSIGNED being returned.

Privilege checks for removed privileges result in STATUS_PRIVILEGE_NOT_HELD. Failed privilege check auditing occurs as normal.

The removal of the privilege is irreversible, so the name of the removed privilege is not included in the PreviousState parameter after a call to AdjustTokenPrivileges.

Windows XP with SP1:  The function cannot remove privileges. This value is not supported.

None

The function disables the privilege.

If DisableAllPrivileges is TRUE, the function ignores this parameter.

BufferLength

Specifies the size, in bytes, of the buffer pointed to by the PreviousState parameter. This parameter can be zero if the PreviousStateparameter is NULL.

PreviousState

A pointer to a buffer that the function fills with a TOKEN_PRIVILEGES structure that contains the previous state of any privileges that the function modifies. That is, if a privilege has been modified by this function, the privilege and its previous state are contained in the TOKEN_PRIVILEGES structure referenced by PreviousState. If the PrivilegeCount member of TOKEN_PRIVILEGES is zero, then no privileges have been changed by this function. This parameter can be NULL.

If you specify a buffer that is too small to receive the complete list of modified privileges, the function fails and does not adjust any privileges. In this case, the function sets the variable pointed to by the ReturnLength parameter to the number of bytes required to hold the complete list of modified privileges.

ReturnLength

A pointer to a variable that receives the required size, in bytes, of the buffer pointed to by the PreviousState parameter. This parameter can be NULL if PreviousState is NULL.

Return Value

If the function succeeds, the return value is nonzero. To determine whether the function adjusted all of the specified privileges, callGetLastError, which returns one of the following values when the function succeeds:

Return code Description
ERROR_SUCCESS
The function adjusted all specified privileges.
ERROR_NOT_ALL_ASSIGNED
The token does not have one or more of the privileges specified in the NewStateparameter. The function may succeed with this error value even if no privileges were adjusted. The PreviousState parameter indicates the privileges that were adjusted.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

完成实例:

#include <windows.h>
#include <iostream>
using namespace std; void main()
{
BOOL retn;
HANDLE hToken;
retn = OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken);
if(retn != TRUE)
{
cout<<"获取令牌句柄失败!"<<endl;
return;
} TOKEN_PRIVILEGES tp; //新特权结构体
LUID Luid;
retn = LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&Luid); if(retn != TRUE)
{
cout<<"获取Luid失败"<<endl;
return;
}
//给TP和TP里的LUID结构体赋值
tp.PrivilegeCount = ;
tp.Privileges[].Attributes = SE_PRIVILEGE_ENABLED;
tp.Privileges[].Luid = Luid; AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);
if(GetLastError() != ERROR_SUCCESS)
{
cout<<"修改特权不完全或失败!"<<endl;
}
else
{
cout<<"修改成功!"<<endl;
}
}

如何修改Windows程序的权限?的更多相关文章

  1. golang windows程序获取管理员权限(UAC ) via gocn

    golang windows程序获取管理员权限(UAC ) 在windows上执行有关系统设置命令的时候需要管理员权限才能操作,比如修改网卡的禁用.启用状态.双击执行是不能正确执行命令的,只有右键以管 ...

  2. 应用程序-特定 权限设置并未向在应用程序容器不可用 SID (不可用)中运行的地址 LocalHost (使用 LRPC) 中的用户...的 COM 服务器应用程序的 本地 激活 权限。此安全权限可以使用组件服务管理工具进行修改。

    很久以前发现我们的业务服务器上出现一个System的系统严重错误,查找很久都没有找到解决办法,今日再次查看服务器发现报错更频繁,于是就搜集各种资料进行查找解决办法,终于找到了一个解决办法. 错误截图介 ...

  3. 在vs2017和vs2019下发布应用之Windows程序打包-附图标修改和默认安装路径定义全教程

    title: 在vs2017和vs2019下发布应用之Windows程序打包-附图标修改和默认安装路径定义全教程 date: 2020-04-25 sidebarDepth: 2 tags: wind ...

  4. 修改windows系统文件权限

    修改windows系统文件总是提示没有权限,虽然已是administrator也不管用. 以下方法可以解决: 右键属性,安全,高级,所有者,编辑,选择当前用户并确定, 回到上一页再确定, 然后在安全页 ...

  5. C程序之修改Windows的控制台颜色(转载)

    Windows的CMD可以和Linux下的终端一样可以有五颜六色,目前我在网上找到2种方法可以修改Windows的CMD,当然都是在代码中修改的.在“CMD”->“属性”->“颜色”,这种 ...

  6. sqlserver修改sa密码(在windows登陆没有权限的情况下)

    对于windows用户没有权限执行alter login sa enable的情况下,采用如下方法可以成功修改sa密码登陆. . 用Run as a administrator打开命令提示符里输入NE ...

  7. Java 修改Windows注册表,以实现开机自启动应用程序。

    使用Java修改Windows注册表,使用最基本的就是cmd命令. 事例和运行结果如下所示: package day01; import java.io.IOException; /* 1,reg a ...

  8. Windows服务器SYSTEM权限Webshell无法添加3389账户情况突破总结

    转自:http://bbs.blackbap.org/thread-2331-1-1.html 近好多Silic的朋友在Windows下SYSTEM权限的php webshell下添加账户,但是却无法 ...

  9. 初识Windows程序

    首先,我们创建第一个Windows程序,一共分为4个步骤: 1.打开Visual Studio开发工具 2.选择"文件"→"新建"→"项目" ...

随机推荐

  1. Linux shell中&,&&,|,||的用法

    前言 在玩dvwa的命令注入漏洞的时候,遇到了没有预料到的错误,执行 ping 127.0.0.1 & echo "<?php phpinfo(); ?>" & ...

  2. Python20-Day07

    面向对象之继承与派生 什么是继承? 继承是一种创建新类的方式,新建的类可以继承一个或者多个父类,父类又称为基类或者超类,新建的类称为派生类或者子类 子类会‘遗传’父类的特性,从而解决代码重用问题 py ...

  3. mysql实现oracle sequence方案

    转自: http://blog.csdn.net/javaGirlOne/article/details/47256183 背景:先总结一下MYSQL 自增长与ORACLE 序列的区别: 自增长只能用 ...

  4. Set up classpath in Eclipse

    On Eclipse main window Right click on .java file-> Run As -> Run Configurations... On Run Conf ...

  5. 互评Beta版本 - Hello World团队项目空天猎

    由于改组项目未提供可以直接进行安装运行的安装包或可执行文件,所以我找到了该组组长陈同学,由他根据其小组项目的功能说明书进行演示. 基于NABCD评论作品,及改进建议 每个小组评论其他小组beta发布的 ...

  6. TeamWork#1,Week 5,Suggestions for Team Project

    我们团队联系到了我们六班的直系学长,并向他咨询了软件工程基础这门课的团队项目相关的问题.他们团队的名字命名为Z-XML,团队中的几个学长也都是我平时所熟识的.虽然学长已经大四,忙着考研工作等各种事务, ...

  7. 团队博客作业Week4 --- 学霸网站--NABC

    1.需求(Need) 伴随着经济的发展,科学技术取得了飞速的发展,互联网在各行各业的发展中取得了广泛的应用.随着这些事物的发展,我们每个人都会接触到相当庞大的数据.如何在这些数据中找到自己需要的,如何 ...

  8. web14 validation.xml配置 登录验证文件配置

    电影网站:www.aikan66.com 项目网站:www.aikan66.com 游戏网站:www.aikan66.com 图片网站:www.aikan66.com 书籍网站:www.aikan66 ...

  9. 超级迷宫之NABCD

    模式之一:双人模式 N:基于双人之间的竞争与协作,朋友之间可以有一个竞争比赛,一决高下,男女朋友之间适合双人协作模式,共同完成游戏. A:双人竞争模式为双人同起点或不同起点来进行游戏,在竞争的紧张压力 ...

  10. 构建基于Chromium的应用程序(Winform程序加载Html页面)

    chromium是google chrome浏览器所采用的内核,最开始由苹果的webkit发展而出,由于webkit在发展上存在分歧,而google希望在开发上有更大的自由度,2013年google决 ...