|
在NT系统里,一个进程只允许对本身内存和共享内存进行读写(如果说错了请告诉我)
3 @# [* p' ?' i但经过处理后,我们可以访问安全级别不是很高的进程内存。$ |+ \- C3 {4 W R8 ]
我们在OpenProcess时,如果能取得它的PROCESS_VM_READ、PROCESS_VM_WRITE和PROCESS_VM_OPERATION权限,那就好办了。
3 b6 |* ]# K9 e6 e下面是我编的一个自动扫雷程序的核心代码,它从扫雷程序的内存中读取地雷的分布情况,再通过模拟鼠标点击来扫雷
0 I# A1 `) |7 v7 m注意,这儿地雷在内存中的分布,是在中文XP下跟踪所得,不知道在其它系统上是不是一样的。
- ~+ Y! k v5 y! m6 @* t* H1 i HWND hwnd;; s5 Q' {" g9 B
HANDLE hProcess = NULL;1 R; S6 h+ ]6 I. y7 l
DWORD id;
) G; A, X4 `. o% X BYTE tmpValue;
3 e0 U4 T' z4 d1 J DWORD bytes;. m- q$ ]% t+ i# Y# L
CPoint point;2 v. |1 D6 ]3 ^6 a
CRect rect;
1 r% T" d8 @) U0 Q* z int intWidth, intHeight, i, j;% [+ u; x* L5 z, x- E) |
//找到扫雷游戏的窗口,如果找不到,就出错。
9 G( c5 A) a( u/ z. l1 a! d z/ K hwnd = ::FindWindow(NULL, "扫雷");& Y! k) L( @# h# D
if (!hwnd)8 y- M% m; q8 m& {) O( f) m5 K
{
: a. J& U3 w1 ^9 _$ I% U C% d* _ MessageBox("没有找到扫雷游戏", NULL, MB_OK|MB_ICONINFORMATION);
9 @" e# r. J% u+ g/ e/ {: A: J return;( R0 x j& @! c3 M3 _! n* [) o
}, e+ a% l: x% G+ U
//从窗口ID得到它的进程ID
5 b* G3 z& g, s! w ::GetWindowThreadProcessId(hwnd, &id);
7 N+ C, Y2 C9 S. f, X: r7 {$ ^. d //得到它的进程句柄
3 b4 u O% a6 v2 q& g hProcess = ::OpenProcess(STANDARD_RIGHTS_REQUIRED|; W m% z( L+ H% O" V
PROCESS_VM_READ|* m) S( r8 N! y$ n- c4 Q6 k
PROCESS_VM_WRITE|
8 T& ]- O2 p: B. Z% G PROCESS_VM_OPERATION, FALSE, id);
2 s8 _. G: L4 \ //检查雷区的区域
; f. ^# ?: Q' N& Y3 t5 A ::ReadProcessMemory(hProcess, (void *)0x01005334, (void *)&tmpValue, 1, &bytes);2 W4 B6 b1 U" Q: S
intWidth = tmpValue;6 f5 ^' c7 \* f( j* X( `
. g2 J: D, ~% L$ A" ^& _2 w) v! q ::ReadProcessMemory(hProcess, (void *)0x01005338, (void *)&tmpValue, 1, &bytes);, L" M; |: ]- |
intHeight = tmpValue;
6 y4 E( {) L0 ]0 [1 Y2 K ::SetForegroundWindow(hwnd);1 L/ [, H; |3 Y. s% h
::GetWindowRect(hwnd, &rect);
) b2 ^& i) s/ a3 ]$ z8 x* u ::SetWindowPos(hwnd, HWND_TOP, rect.left, rect.top, 0, 0, SWP_NOSIZE);
- H6 K6 B, O2 N/ }6 f2 z
( D! S) |$ Z$ Y7 f3 O q3 N+ N for (i = 1; i <= intHeight; i ++)
9 A1 h/ D: _4 c {
/ l) R; H- M3 G$ y for (j = 1; j <= intWidth; j ++)
& ]& d: ?) P1 D0 O) e2 ? {
/ m0 z+ j% U3 b, b0 o ]) ` ::ReadProcessMemory(hProcess, (void *)(0x01005340 + i * 32 + j),
$ R% @; l5 `0 V! z (void *)&tmpValue, 1, &bytes);% l( V1 \0 W) m$ Q
if ((tmpValue & 0x80) != 0x80)& z2 e; F" k2 {7 Q# M o
{
! R( [; |- ]) ^$ Q$ ^ point.x = 7 + j * 16 + rect.left;
$ f2 i1 l+ x* ^1 @% N+ w! U point.y = 96 + i * 16 + rect.top;
' ~/ j$ k0 `% R4 j+ l0 G" _ ::SetCursorPos(point.x, point.y);
) R5 {% q( N. ^* U mouse_event(MOUSEEVENTF_LEFTDOWN, point.x, point.y, 0, 0);2 t/ ]3 N+ y! |) }- i x6 g, F
mouse_event(MOUSEEVENTF_LEFTUP, point.x, point.y, 0, 0);# V4 W& @* s3 J# Z0 U
}
$ g/ X& m9 A2 @# \2 Y: \' m }
; a7 F/ K3 P3 T- j# q8 ~5 n }4 z- d+ j% W9 v. V2 `
::CloseHandle(hProcess); |
|