質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Win32 API

Win32 APIはMicrosoft Windowsの32bitプロセッサのOSで動作するAPIです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

1708閲覧

Windows APIのフックによってAPIの使用したプロセスを明らかにする

sitsumon_jirou

総合スコア5

Win32 API

Win32 APIはMicrosoft Windowsの32bitプロセッサのOSで動作するAPIです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2020/10/05 06:08

前提・実現したいこと

Windows APIをフックして,APIを使用したプロセスの情報(PID,プロセス名など)を出力しようとしています.
そのために,IATの書き換えで実現しようとしています.

現在,準備段階としてMessageBoxA関数やWinExec関数をフックすることができたらDebugViewに「Hooked●●(関数名)!」出力しようとしていますが,出力されません.また,debugViewにはnotepad_start1!,notepad_start2!まで表示されています.
[https://qiita.com/_pochi/items/4e20e38deee16a7615e1#fn2]のコードを参考にしています.
言語はc++です.

以下のコードを使用しなくてもAPIのフックを簡単に実現する方法があればご教授ください.

###コード
mainで自作dllをばらまきます.

main

1#include "stdafx.h" 2 3int main() 4{ 5 Inject(); 6 printf("I am WaruiApp!\n"); 7 getchar(); 8 return 0; 9}

dllmainは自作dllファイルです.

dllmain

1#include "stdafx.h" 2#include "rewrite.h" 3 4 5fp_CreateFileW createFileWPtr = NULL;//fp_API名で定義 6fp_MessageBoxA MessageBoxAPtr = NULL; 7fp_WinExec WinExecPtr = NULL; 8 9 10HANDLE 11WINAPI 12HookedCreateFileW( 13 _In_ LPCWSTR lpFileName, 14 _In_ DWORD dwDesiredAccess, 15 _In_ DWORD dwShareMode, 16 _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, 17 _In_ DWORD dwCreationDisposition, 18 _In_ DWORD dwFlagsAndAttributes, 19 _In_opt_ HANDLE hTemplateFile 20) 21{ 22 OutputDebugString(TEXT("HookedCreateFileW!\n")); 23 if (lpFileName != NULL) { 24 OutputDebugString(TEXT("HookedCreateFileW2!\n")); 25 if (auto fileNameLength = lstrlen(lpFileName) > 5) { 26 auto extPtr = lpFileName + lstrlen(lpFileName) - 5; 27 if (lstrcmpW(extPtr, TEXT(".java")) == 0) { 28 // よいこは真似しないこと 29 OutputDebugString(TEXT("HookedCreateFileW3!\n")); 30 lstrcpy((LPWSTR)extPtr, TEXT(".cs")); 31 } 32 } 33 } 34 return createFileWPtr(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile); 35} 36 37int 38WINAPI 39HookedMessageBoxA( 40 HWND hWnd, 41 LPCSTR lpText, 42 LPCSTR lpCaption, 43 UINT uType 44) 45{ 46 OutputDebugString(TEXT("HookedMessageBoxA!\n")); 47 return MessageBoxAPtr(hWnd,lpText,lpCaption,uType); 48} 49 50UINT 51WINAPI 52HookedWinExec( 53 LPCSTR lpCmdLine, 54 UINT uCmdShow 55) 56{ 57 OutputDebugString(TEXT("HookedWinExec!\n")); 58 return WinExec(lpCmdLine,uCmdShow); 59} 60 61 62void APIHook() { 63 TCHAR fileNameBuf[MAX_PATH]; 64 if (GetModuleFileName(GetModuleHandle(NULL), fileNameBuf, MAX_PATH) > 0) { 65 OutputDebugString(fileNameBuf); 66 if (_tcsstr(fileNameBuf, TEXT("notepad.exe")) != NULL) { 67 OutputDebugString(TEXT("nodepad_start1!\n")); 68 MessageBoxAPtr = (fp_MessageBoxA)RewriteFunction("User32.dll", "MessageBoxA", HookedMessageBoxA); 69 //createFileWPtr = (fp_CreateFileW)RewriteFunction("kernel32.dll", "CreateFileW", HookedCreateFileW); 70 OutputDebugString(TEXT("nodepad_start2!\n")); 71 WinExecPtr = (fp_WinExec)RewriteFunction("Kernel32.dll", "WinExec", HookedWinExec); 72 } 73 } 74} 75 76BOOL APIENTRY DllMain( HMODULE hModule, 77 DWORD dwReason, 78 LPVOID lpReserved 79 ) 80{ 81 if (dwReason == DLL_PROCESS_ATTACH) 82 { 83 DisableThreadLibraryCalls(hModule); 84 APIHook(); 85 } 86 return TRUE; 87} 88

rewriteではdllmain内で呼び出されるRewriteFunction関数を定義しています.

rewrite

1#include "stdafx.h" 2#include <imagehlp.h> 3#include "rewrite.h" 4#pragma comment(lib,"imagehlp.lib") 5 6void* RewriteFunctionImp(const char* szRewriteModuleName, const char* szRewriteFunctionName, void* pRewriteFunctionPointer) 7{ 8 for (int i = 0; i < 2; i++) { 9 // ベースアドレス 10 intptr_t pBase = 0; 11 if (i == 0) { 12 if (szRewriteModuleName) { 13 pBase = (intptr_t)::GetModuleHandleA(szRewriteModuleName); 14 } 15 } 16 else if (i == 1) { 17 pBase = (intptr_t)GetModuleHandle(NULL); 18 } 19 if (!pBase)continue; 20 21 // イメージ列挙 22 ULONG ulSize; 23 PIMAGE_IMPORT_DESCRIPTOR pImgDesc = (PIMAGE_IMPORT_DESCRIPTOR)ImageDirectoryEntryToData((HMODULE)pBase, TRUE, IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize); 24 for (; pImgDesc->Name; pImgDesc++) { 25 const char* szModuleName = (char*)(intptr_t)(pBase + pImgDesc->Name); 26 // THUNK情報 27 PIMAGE_THUNK_DATA pFirstThunk = (PIMAGE_THUNK_DATA)(intptr_t)(pBase + pImgDesc->FirstThunk); 28 PIMAGE_THUNK_DATA pOrgFirstThunk = (PIMAGE_THUNK_DATA)(intptr_t)(pBase + pImgDesc->OriginalFirstThunk); 29 // 関数列挙 30 for (; pFirstThunk->u1.Function; pFirstThunk++, pOrgFirstThunk++) { 31 if (IMAGE_SNAP_BY_ORDINAL(pOrgFirstThunk->u1.Ordinal))continue; 32 PIMAGE_IMPORT_BY_NAME pImportName = (PIMAGE_IMPORT_BY_NAME)(intptr_t)(pBase + (intptr_t)pOrgFirstThunk->u1.AddressOfData); 33 if (!szRewriteFunctionName) { 34 // 表示のみ 35 printf("Module:%s Hint:%d, Name:%s\n", szModuleName, pImportName->Hint, pImportName->Name); 36 } 37 else { 38 // 書き換え判定 39 if (stricmp((const char*)pImportName->Name, szRewriteFunctionName) != 0)continue; 40 41 // 保護状態変更 42 DWORD dwOldProtect; 43 if (!VirtualProtect(&pFirstThunk->u1.Function, sizeof(pFirstThunk->u1.Function), PAGE_READWRITE, &dwOldProtect)) 44 return NULL; // エラー 45 46 // 書き換え 47 //OutputDebugString(TEXT("5")); 48 void* pOrgFunc = (void*)(intptr_t)pFirstThunk->u1.Function; // 元のアドレスを保存しておく 49 WriteProcessMemory(GetCurrentProcess(), &pFirstThunk->u1.Function, &pRewriteFunctionPointer, sizeof(pFirstThunk->u1.Function), NULL); 50 pFirstThunk->u1.Function = (intptr_t)pRewriteFunctionPointer; 51 52 // 保護状態戻し 53 VirtualProtect(&pFirstThunk->u1.Function, sizeof(pFirstThunk->u1.Function), dwOldProtect, &dwOldProtect); 54 return pOrgFunc; // 元のアドレスを返す 55 } 56 } 57 } 58 } 59 return NULL; 60} 61 62void* RewriteFunction(const char* szRewriteModuleName, const char* szRewriteFunctionName, void* pRewriteFunctionPointer) 63{ 64 //OutputDebugString(TEXT("Rewrite1!\n")); 65 return RewriteFunctionImp(szRewriteModuleName, szRewriteFunctionName, pRewriteFunctionPointer); 66} 67 68void PrintFunctions() 69{ 70 printf("----\n"); 71 RewriteFunctionImp(NULL, NULL, NULL); 72 printf("----\n"); 73}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

if (_tcsstr(fileNameBuf, TEXT("notepad.exe")) != NULL) { OutputDebugString(TEXT("nodepad_start1!\n")); MessageBoxAPtr = (fp_MessageBoxA)RewriteFunction("User32.dll", "MessageBoxA", HookedMessageBoxA); //createFileWPtr = (fp_CreateFileW)RewriteFunction("kernel32.dll", "CreateFileW", HookedCreateFileW); OutputDebugString(TEXT("nodepad_start2!\n")); WinExecPtr = (fp_WinExec)RewriteFunction("Kernel32.dll", "WinExec", HookedWinExec); }

notepad.exeMessageBoxAWinExecを使う場面を想像できませんがifで場合分けする必要がありますか?

また、対象のプロセスがMessageBoxWなどの別の手段を用いている可能性はありませんか?

投稿2020/10/07 22:40

asm

総合スコア15147

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sitsumon_jirou

2020/10/08 07:30

回答ありがとうございます. if文の削除が必要でした. API Monitorを確認したら,MessageBoxWを使用していました.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問