提升权限

#include "stdafx.h"
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <Windows.h>
using namespace std;

//获取电脑管理员权限
void GetPrivilege()
{

	HANDLE hToken;
	TOKEN_PRIVILEGES tkp;
	if(!OpenProcessToken(
		GetCurrentProcess(),
		TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
		&hToken))
		printf("OpenProcessToken failed");

	LookupPrivilegeValue(NULL,
		SE_SHUTDOWN_NAME,
		&tkp.Privileges[0].Luid);
	tkp.PrivilegeCount=1;
	tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;

	AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
	if(GetLastError()!=ERROR_SUCCESS)
		printf("AdjustTokenPrivileges");
}


//打开注册表 RegOpenKeyEx
//修改注册表的值 RegSetValueEx

void AutoRun()
{
	HKEY hKey={0};
	DWORD err=RegOpenKeyExW(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_WRITE,&hKey);
	if(err!=ERROR_SUCCESS)
	{
		cout<<"RegOPenKeyExW failed"<<endl;
	}

	char szPath[MAXBYTE]={0};
	GetModuleFileNameA(NULL,szPath,MAXBYTE);
	err=RegSetValueExA(hKey,"AutoRun",0,REG_SZ,(byte*)szPath,sizeof(szPath));
	if(err!=ERROR_SUCCESS)
	{
		cout<<"RegSetValueEx failed"<<endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])    
{
	GetPrivilege();
	cout<<"输入开机密码,否则关机重启"<<'\n'<<"密码:";
	int nPassword=123;
	cin>>nPassword;
	AutoRun();
	if(nPassword!=123)
	{
		ExitWindowsEx(EWX_SHUTDOWN,0);
		//cout<<"我很帅气"<<endl;
	}
	system("pause");
	return 0;
}

//进程提权

BOOL ElevatePrivileges()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
tkp.PrivilegeCount = 1;
if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
return FALSE;
LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tkp.Privileges[0].Luid);
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(hToken,FALSE,&tkp,sizeof(TOKEN_PRIVILEGES),NULL,NULL))
{
return FALSE;
}

return TRUE;
}





  

posted @ 2019-06-02 15:57  gd_沐辰  阅读(323)  评论(0编辑  收藏  举报