读取其他进程的内存-让程序提升Debug权限

最近一直忙着写毕业论文,但对于我这种没语言天赋的人,写写实际做的工作还可以,但要写课题背景、意义什么的,头都要大了,憋了两天才憋出两页的绪论,我容易吗我。。。

所以这半个月也没时间更新博客,现在论文初稿终于写完了,所以来写点东西吧。

这还是要从我写的魔兽改键显血助手WarKey说起。之前的版本因为判断是否是聊天状态的功能不完善,于是我就按网上的说法,用CE(Cheat Engine,专门用于修改游戏内存的工具)搜索了一下魔兽进程的内存内容,找到存储是否处于聊天状态的内存地址。于是,在WarKey中,只需要取到该内存地址的内容,根据其内容来判断是否处于聊天状态,然后决定是否需要改键即可。但实际上,在调用API函数ReadProcessMemory()时,并不能取出其内容,也就是说无论魔兽是否处于聊天状态,ReadProcessMemory()总是调用失败。经查MSDN,发现该函数操作的句柄需要PROCESS_VM_READ访问权限,而这个是由OpenProcess()函数来处理的。OpenProcess()这个函数指定的访问权限的安全描述符比较复杂,但有一句话很给力:If the caller has enabled the SeDebugPrivilege privilege, the requested access is granted regardless of the contents of the security descriptor.也就是说如果调用者具有SeDebugPrivilege权限,那么就不用管什么安全描述符了。于是只需要给程序提升权限,使其具有SeDebugPrivilege权限即可。

关于提升权限的方法,在MSDN里面查了半天,居然找到了一个很给力的例子:

复制代码
 
  
#include < windows.h > #include < stdio.h > #pragma comment(lib, "cmcfg32.lib") 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 " , 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: %u\n " , GetLastError() ); return FALSE; } if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { printf( " The token does not have the specified privilege. \n " ); return FALSE; } return TRUE; }
复制代码

这个例子应该是比较经典的,经过分析之后,于是写出开关Debug权限的函数:

复制代码
 
  
BOOL CWarKeyDlg::EnableDebugPrivilege(BOOL bEnableDebugPrivilege) { HANDLE hToken; TOKEN_PRIVILEGES tp; LUID luid; if ( ! ::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, & hToken)) { ::MessageBox( this -> GetSafeHwnd(), GET_TOKEN_ERROR, MSG_BOX_TITLE, MB_OK); return FALSE; } if ( ! ::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, & luid)) { ::MessageBox( this -> GetSafeHwnd(), GET_PRIVILEGE_VALUE_ERROR, MSG_BOX_TITLE, MB_OK); ::CloseHandle(hToken); return FALSE; } tp.PrivilegeCount = 1 ; tp.Privileges[ 0 ].Luid = luid; if (bEnableDebugPrivilege) { tp.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED; } else { tp.Privileges[ 0 ].Attributes = 0 ; } if ( ! ::AdjustTokenPrivileges(hToken, FALSE, & tp, sizeof (tp), NULL, NULL)) { ::MessageBox( this -> GetSafeHwnd(), ADJUST_PRIVILEGE_ERROR, MSG_BOX_TITLE, MB_OK); ::CloseHandle(hToken); return FALSE; } ::CloseHandle(hToken); if (::GetLastError() == ERROR_NOT_ALL_ASSIGNED) { ::MessageBox( this -> GetSafeHwnd(), ENABLE_DEBUG_ERROR, MSG_BOX_TITLE, MB_OK); return FALSE; } return TRUE; }
复制代码
利用这个函数给自己的WarKey开启Debug权限,再访问魔兽进程的内存时,就可以读出其中的数据了。

猜你喜欢

转载自blog.csdn.net/lonelyrains/article/details/51822210