è¤æ°ã®ããã»ã¹éã§ã¡ã¢ãªãå
±æããæ¹æ³ã¨ãã¦ãã使ãããã®ã¯å
±æã¡ã¢ãªã§ãããWin32APIã® WriteProcessMemory() ãç¨ããã¨ç°ãªãããã»ã¹ã®ã¡ã¢ãªãæ¸ãæãããã¨ãã§ãã¾ã. ããã使ã£ã¦ã°ãã¼ãã«å¤æ° ãä»ããã»ã¹ããæ¸ãæãã¦ã¿ã¾ããã.
ç°å¢
ã½ãªã¥ã¼ã·ã§ã³ã®ä½æ
Visual Studio ã§å®é¨ç¨ã®ã½ãªã¥ã¼ã·ã§ã³ãä½æãã¾ã. ãã®ä¸ã«Child(æ¸ãæããããæ¹)ã¨Parent(æ¸ãæããæ¹)ã¨ããC++ ã®ç©ºã®ããã¸ã§ã¯ããä½ãã¾ã.
Childããã¸ã§ã¯ãã®è¨å®
ããã©ã«ãã§ã¯ASLR(ã¢ãã¬ã¹ç©ºé ã¬ã¤ã¢ã¦ãã®ã©ã³ãã å)ãæå¹ã«ãªã£ã¦ãããããããã°ã©ã ãå®è¡ãããã³ã«ä»®æ³ã¢ãã¬ã¹ãå¤åãã¦ãã¾ãã¾ã. ã»ãã¥ãªãã£å¼·åã®ããã®æ©è½ã§ãããæ¬ããã°ã©ã ã®å ´åä»ããã»ã¹ã®ã°ãã¼ãã«å¤æ° ã®ã¢ãã¬ã¹ãæ¯åå¤åãã¦ã¯å°ããããã®æ©è½ãç¡å¹ã«ãã¾ã.
Chileããã¸ã§ã¯ãã®ããããã£->ãªã³ã«ã¼->詳細è¨å®ã®ãã©ã³ãã åããããã¼ã¹ã¢ãã¬ã¹ãããããããã«ãã¾ã.
ASLRãç¡å¹ã«ãã
ã¾ãããªã³ã¯å¾ã®ä»®æ³ã¢ãã¬ã¹ãç¥ãããã®ã§ãmapãã¡ã¤ã«ãåºåããè¨å®ã«ãã¦ããã¾ããã.
Chileããã¸ã§ã¯ãã®ããããã£->ãªã³ã«ã¼->ãããã° ã®ãããããã¡ã¤ã«ã®ä½æãããã¯ããã«ãã¾ã.
ããããã¡ã¤ã«ãä½æãã
Childããã¸ã§ã¯ãã®ã½ã¼ã¹ãã©ã«ãã«main.cppã追å ããä¸ã®ããã«ã³ã¼ãã£ã³ã°ãã¾ã.
#include <Windows.h>
#include <iostream>
using namespace std ;
int g_val = 1 ;
int main ()
{
cout << &g_val << endl ;
cout << "initial val:" << g_val << endl ;
HANDLE hEvent = CreateEvent (
NULL ,
FALSE ,
FALSE ,
L"EVENT.HOGE"
);
if (hEvent) {
WaitForSingleObject (hEvent, INFINITE);
}
cout << "updated val:" << g_val << endl ;
return 0 ;
}
Parentããã¸ã§ã¯ãã§ãåæ§ã«ã½ã¼ã¹ãã©ã«ãã«main.cppã追å ã以ä¸ã®ããã«ã³ã¼ãã£ã³ã°ãã¾ã.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>
#include <vector>
#include <iostream>
using namespace std ;
const long * pVal = (long *)(0x000000014001D000 );
void GetPidByName (const TCHAR* targetName, vector <DWORD>& ret)
{
DWORD pids[1024 ], byteNeeded;
if (!EnumProcesses (pids, sizeof (pids), &byteNeeded))
{
return ;
}
DWORD processNum;
processNum = byteNeeded / sizeof (DWORD);
for (unsigned int i = 0 ; i < processNum; ++i) {
DWORD pid = pids[i];
HANDLE hProcess = OpenProcess (
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE ,
pid
);
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
TCHAR processName[MAX_PATH];
if (EnumProcessModules (hProcess, &hMod, sizeof (hMod), &cbNeeded))
{
GetModuleBaseName (
hProcess,
hMod,
processName,
sizeof (processName) / sizeof (TCHAR)
);
if (!_tcscmp (targetName, processName)) {
ret.push_back (pid);
}
}
CloseHandle (hProcess);
}
}
}
int main (void )
{
vector <DWORD> pids;
GetPidByName (TEXT ("Child.exe" ), pids);
for (const DWORD pid : pids) {
cout << "Child.exe's PID:" << pid << endl ;
}
HANDLE hProcess = OpenProcess (
PROCESS_VM_WRITE | PROCESS_VM_OPERATION,
FALSE ,
pids.front ()
);
Sleep (2000 );
DWORD newVal = 2 ;
size_t retBytes = 0 ;
WriteProcessMemory (
hProcess,
(LPVOID)pVal,
&newVal,
sizeof (newVal),
&retBytes
);
HANDLE hEvent = CreateEvent (
NULL ,
FALSE ,
FALSE ,
L"EVENT.HOGE"
);
if (hEvent) {
SetEvent (hEvent);
}
cout << "written:" << retBytes << endl ;
int a;
cin >> a;
return 0 ;
}
ãªããèªä½é¢æ°GetPidByName()ã®è©³ç´°ã¯ä¸è¨ããåç
§ä¸ãã.
prupru-prune.hatenablog.com
ã¹ã¿ã¼ãã¢ããããã¸ã§ã¯ãã®è¨å®
ä¸è¨ã§ããã°ã©ã ã¯å®äºã§ããVisual Studio ãããããã¯èµ·åã§ããããã«ã¹ã¿ã¼ãã¢ããããã¸ã§ã¯ãã¨ãã¦2ã¤ã®ããã¸ã§ã¯ããè¨å®ãã¾ããã.
ã½ãªã¥ã¼ã·ã§ã³ã¨ã¯ã¹ããã¼ã© ã¼ããã½ãªã¥ã¼ã·ã§ã³ãå³ã¯ãªãã¯->ããããã£ãé¸æããã¨å
±éããããã£->ã¹ã¿ã¼ãã¢ããããã¸ã§ã¯ãã¨ããæ¬ãããã®ã§ãããã®ãã«ãã¹ã¿ã¼ãã¢ããããã¸ã§ã¯ãã«ãã§ãã¯ãå
¥ããããããã®ããã¸ã§ã¯ãã®ã¢ã¯ã·ã§ã³ãéå§ã«é¸æãã¾ã.
ã½ãªã¥ã¼ã·ã§ã³ã®ã¹ã¿ã¼ãã¢ããããã¸ã§ã¯ãè¨å®
ããã§Visual Studio ãã2ã¤ã®ããã¸ã§ã¯ããåæã«ãããã° ã§ããããã«ãªãã¾ãã.
å®è¡
ã½ãªã¥ã¼ã·ã§ã³å
¨ä½ããã«ããã¦ãããã° ãéå§ããã¨ãä¸æãããã°ä»¥ä¸ã®ãããªå®è¡çµæã¨ãªãã¾ã. g_valã®å¤ãå¤åãã¦ãããã¨ãããå¥ããã»ã¹ããã¡ã¢ãªãæ¸ãæãããã¦ãããã¨ããããã¾ã.
å®è¡çµæ
åè
learn.microsoft.com
learn.microsoft.com