티스토리 뷰

[삽질노트]
1. dll파일은 cpp파일로 작성후 일반적인 VS 빌드를 거쳐 생성.
단, 프로젝트 생성시 Win32 Console, MFC 선택하듯이 여기서 DLL을 선택해야함.

2. dll.cpp에서 메인진입부를 DllMain이 아닌, DLLMain으로 작성하여 시간허비
(dll 코딩및빌드는 처음이였지만, 당연히 될거라 생각했다. 생각해보면 일반콘솔코드에서도 int mAin()~도 당연히 안되는건데..)

3. VirtualMachine에서 Win7 64bit에서 계속 실행함. 32bit에서 했어야했다.
(inject하는 dll위치뿐만아니라 많은 요소들이 변경가능성. 변수많음)

[참고]
1. scanf => scanf_s로 대표되는 UNSAFE WARNING ERROR 컴파일오류시, 구글링하면 몇가지 방법이 있으나, 프로젝트 속성에서 전처리기에 ;~에러문장~; 추가해서 옵션 제외시킬수있다.
2. debugview.exe 편리함.
3. VM_Win7_32bit에서 VCRUNTIME140.dll이 없어 실행되지않는문제는 구글링 및 MS홈페이지에서 필요한 모듈을 설치해 간단히 해결.

 

[VS Project 2개 생성 : HookMain & KeyHook]

HookMain.cpp

#include "stdio.h"
#include "conio.h"
#include "windows.h"

#define DEF_DLL_NAME	"KeyHook.dll"
#define DEF_HOOKSTART	"HookStart"
#define DEF_HOOKSTOP	"HookStop"

typedef void(*PFN_HOOKSTART)();
typedef void(*PFN_HOOKSTOP)();

void main() {
	HMODULE			hDll = NULL;
	PFN_HOOKSTART	HookStart = NULL;
	PFN_HOOKSTOP	HookStop = NULL;
	char			ch = 0;

	hDll = LoadLibraryA(DEF_DLL_NAME);
	if (hDll == NULL) {
		printf("LoadLibrary(%s) failed. [%d]", DEF_DLL_NAME, GetLastError());
		return;
	}

	HookStart = (PFN_HOOKSTART)GetProcAddress(hDll, DEF_HOOKSTART);
	HookStop = (PFN_HOOKSTOP)GetProcAddress(hDll, DEF_HOOKSTOP);

	HookStart();

	printf("press 'q' to quit.\n");
	while (_getch() != 'q');

	HookStop();

	FreeLibrary(hDll);
}

KeyHook.cpp

#include "stdio.h"
#include "windows.h"

#define DEF_PROCESS_NAME "notepad.exe"

HINSTANCE g_hInstance = NULL;
HHOOK g_hHook = NULL;
HWND g_hWnd = NULL;

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved) {
	switch (dwReason) {
	case DLL_PROCESS_ATTACH:
		g_hInstance = hinstDLL;
		break;
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
	char szPath[MAX_PATH] = { 0, };
	char *p = NULL;

	if (nCode >= 0) {
		if (!(lParam & 0x80000000)) {
			GetModuleFileNameA(NULL, szPath, MAX_PATH);
			p = strrchr(szPath, '\\');

			if (!_stricmp(p + 1, DEF_PROCESS_NAME))
				return 1;
		}
	}
	return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

#ifdef __cplusplus
extern "C" {
#endif
	__declspec(dllexport) void HookStart() {
		g_hHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstance, 0);
	}

	__declspec(dllexport) void HookStop() {
		if (g_hHook) {
			UnhookWindowsHookEx(g_hHook);
			g_hHook = NULL;
		}
	}
#ifdef __cplusplus
}
#endif

 

[참고자료]
- code from '리버싱 핵심원리'
- http://yokang90.tistory.com/60
http://hisjournal.net/blog/253
https://asecurity.so/2017/01/hooking-%ED%9B%84%ED%82%B9-setwindowshookex/

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함