我已启用SeDubugPrivilege
但GetModuleBaseName
无法正常工作,我拥有所有管理员权限。我在不同的电脑上试过它工作正常。但在我的电脑中我无法获得所需的输出。
这是我的代码:
void printError(){
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, sizeof(buf), NULL);
wcout<<buf;
}
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if ( !LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid ) ) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", (unsigned int)GetLastError()
);
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if ( !AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL) )
{
printf("AdjustTokenPrivileges error: \n");
printError();
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
printError();
printf("The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
int main(){
Sleep(5000);
HWND currWindow = GetForegroundWindow();
int titleLength = GetWindowTextLengthW(currWindow)+1;
wchar_t s[titleLength];
GetWindowTextW(currWindow,s,titleLength);
wcout<<s<<endl;
unsigned long i = 0;
long unsigned *p = &i;
GetWindowThreadProcessId(currWindow,p);
cout<<*p<<endl;
HANDLE handleForCurrentProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,FALSE,*p);
HANDLE accessToken;
OpenProcessToken(handleForCurrentProcess,TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY ,&accessToken);
SetPrivilege(accessToken,SE_DEBUG_NAME,TRUE);
wchar_t moduleName[500];
cout<<GetModuleBaseNameW(handleForCurrentProcess,NULL,moduleName,500);
wcout<<moduleName<<endl;
cout<<GetModuleFileNameExW(handleForCurrentProcess,NULL,moduleName,500);
wcout<<moduleName;
return 0;
}
这在另一台电脑上工作正常。我也在安全策略中启用了SeDebugPrivilege
。
编辑这里更新的代码与main
中的错误检查调用
int main(){
Sleep(3000);
HWND currWindow = GetForegroundWindow();
int titleLength = GetWindowTextLengthW(currWindow)+1;
wchar_t s[titleLength];
DWORD status = GetWindowTextW(currWindow,s,titleLength);
if(status == 0){
cout<<"Error in GetWindowTextW";
printLastError();
}
wcout<<"Title : "<<s<<endl;
unsigned long id = 0;
GetWindowThreadProcessId(currWindow,&id);
cout<<"Process Id : "<<id<<endl;
HANDLE handleForForegroundProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,id);
if(handleForForegroundProcess == NULL){
cout<<"Error in OpenProcess";
printLastError();
}
HANDLE accessToken;
BOOL processStatus = OpenProcessToken(handleForForegroundProcess,TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY ,&accessToken);
if(processStatus == 0){
cout<<"Error in OpenProcessToken";
printLastError();
}
SetPrivilege(accessToken,SE_DEBUG_NAME,TRUE);
wchar_t moduleName[500];
status = GetModuleBaseNameW(handleForForegroundProcess,NULL,moduleName,500);
if(status == 0){
cout<<"Error in GetModuleBaseNameW";
printLastError();
}
wcout<<"Module Name : "<<moduleName<<endl;
wchar_t modulePath[2000];
status = GetModuleFileNameExW(handleForForegroundProcess,NULL,modulePath,2000);
if(status == 0){
cout<<"Error in GetModuleFileNameExW";
printLastError();
}
wcout<<"Module path : "<<modulePath;
return 0;
}
这是前景窗口是Google Chrome时的输出
标题:使用C ++进行错误检查(Windows) - 谷歌浏览器 过程ID:14528 令牌没有指定的权限。 并非所有引用的权限或组都分配给调用者。 GetModuleBaseNameWOnly中的一部分ReadProcessMemory或WriteProcessMemory请求已完成。 模块名称: - 模块路径:C:\ Program Files(x86)\ Google \ Chrome \ Application \ chrome.exe
这是前台窗口是运行代码的过程的输出,即代码块
标题:main.cpp - 代码::块17.12 流程ID:4008 令牌没有指定的权限。 并非所有引用的权限或组都分配给调用者。 模块名称:codeblocks.exe 模块路径:C:\ Program Files(x86)\ CodeBlocks \ codeblocks.exe
我没有获得铬的modulename
。谢谢。
这是该程序的工作版本。您不需要启用SE_DEBUG_PRIVILEGE来获取模块的基本名称(或者它的完整路径,如果您使用QueryFullProcessImageName
而不是GetModuleBaseName
)。这简化了代码:
#include <windows.h>
#include <TlHelp32.h>
#include <psapi.h>
#include <iostream>
#include <iomanip>
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "user32.lib")
void show_task(DWORD processID) {
HANDLE process = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE,
processID);
char name[MAX_PATH];
DWORD length = sizeof(name);
GetModuleBaseName(process, NULL, name, sizeof(name));
//QueryFullProcessImageName(process, NULL, name, &length);
std::cout << name << "\n";
}
int main() {
Sleep(5000);
HWND currWindow = GetForegroundWindow();
DWORD proc_id;
GetWindowThreadProcessId(currWindow, &proc_id);
show_task(proc_id);
}