-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
290 lines (233 loc) · 8.36 KB
/
logger.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
#include <dirent.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <X11/Xlib.h>
#define BUFFER_LEN 1000
// #define DEBUG
static volatile int keepRunning = 1;
int catcher()
{
// Display* disp, XErrorEvent* xe
// No window
#ifdef DEBUG
printf("=== Catched error ===\n");
printf("Catched error : %d\n", xe->type);
printf("Catched error : %lu\n", xe->resourceid);
printf("Catched error : %lu\n", xe->serial);
printf("Catched error : %lu\n", xe->error_code);
printf("Catched error : %lu\n", xe->request_code);
printf("Catched error : %lu\n", xe->minor_code);
printf("=== Catched error ===\n");
#endif
return 0;
}
unsigned char *get_string_property(Display *display, Window *window, char *property_name)
{
Atom actual_type, filter_atom;
int actual_format, status;
unsigned long nitems, bytes_after;
unsigned char *prop;
filter_atom = XInternAtom(display, property_name, True);
status = XGetWindowProperty(display, *window, filter_atom, 0, BUFFER_LEN, False, AnyPropertyType,
&actual_type, &actual_format, &nitems, &bytes_after, &prop);
#ifdef DEBUG
if (status != Success) {
printf("Catched error : %d\n", status);
}
#endif
if (status == BadWindow)
return (unsigned char *)""; // window id # 0x%lx does not exists
else if (status != Success)
return (unsigned char *)""; // XGetWindowProperty failed!
else
return prop;
}
long get_long_property(Display *display, Window *window, char *property_name)
{
unsigned char *prop = get_string_property(display, window, property_name);
if (!prop)
return -1;
if (!strcmp((char *)prop, ""))
return -1;
long long_property = prop[0] + (prop[1] << 8) + (prop[2] << 16) + (prop[3] << 24);
XFree(prop);
return long_property;
}
void SIGINT_handler()
{
printf("closing...\n");
// -fno-stack-protector
keepRunning = 0;
}
void check(unsigned char **var)
{
if (!*var) {
XFree(*var);
*var = (unsigned char *)strdup("ERROR");
}
}
int main(void)
{
Display *display = XOpenDisplay(NULL);
Window root = XDefaultRootWindow(display);
Window window;
XSelectInput(display, root, PropertyChangeMask);
XEvent ev;
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char *start_time = asctime(tm);
char time_sec[12];
struct timeval time_milisec;
char *last_name = NULL;
unsigned char *name;
unsigned char *data;
char *proc_path = malloc(sizeof(char) * BUFFER_LEN);
char *comm = malloc(sizeof(char) * BUFFER_LEN);
Atom type;
int format;
unsigned long items, bytes_left;
long pid = 0;
FILE *fp;
char *logger_dir = getenv("LOGGER_DIR");
if (logger_dir) {
DIR *dir = opendir(logger_dir);
if (!dir && errno == ENOENT)
mkdir(logger_dir, 0755);
else
closedir(dir);
if (chdir(logger_dir))
return printf("Folder %s cannot be accessed\n", logger_dir), 0;
}
char *fd_filepath = malloc(sizeof(char) * BUFFER_LEN);
strftime(time_sec, BUFFER_LEN, "%d-%m-%Y_%Hh%Mm%S", tm);
sprintf(fd_filepath, "%s.wins", time_sec);
FILE *fd = fopen(fd_filepath, "w");
if (fd == NULL) {
printf("File couldn't be opened.\nPlease check folder permissions.\n");
goto failure;
}
printf("Starting at %s\n", start_time);
fprintf(fd, "Starting at %s\n", start_time);
fclose(fd);
signal(SIGINT, SIGINT_handler);
XSetErrorHandler(catcher);
while (keepRunning) {
XNextEvent(display, &ev);
if (!keepRunning)
break;
// printf("Event Type : %d\n", ev.type);
if (ev.type == ButtonPress)
continue;
if (ev.type == ButtonRelease) {
name = get_string_property(display, &window, "_NET_WM_NAME");
check(&name);
check((unsigned char **)&last_name);
if (strcmp((char *)name, "ERROR") != 0 && strcmp(last_name, (char *)name) != 0) {
last_name = strdup((char *)name);
gettimeofday(&time_milisec, NULL);
fd = fopen(fd_filepath, "a");
fprintf(fd, "[%ld-%03lu] [%05lu] [%s] %s\n", time_milisec.tv_sec,
ev.xkey.time % 1000, pid, comm, name);
fflush(fd);
fclose(fd);
}
XFree(name);
}
if (ev.type == KeyRelease)
continue;
if (ev.type == KeyPress) {
// printf("KEY : %d\n", ev.xkey.type);
// printf("KEY Time : %lu\n", ev.xkey.time); // TIME OF EVENT
// printf("KEY Time : %lu\n", ev.xkey.time % 1000); // TIME OF EVENT
// printf("KEY Position : %d_%d\n", ev.xkey.x, ev.xkey.y);
// printf("KEY State : %u\n", ev.xkey.state);
// printf("KEY keycode : %u\n", ev.xkey.keycode);
name = get_string_property(display, &window, "_NET_WM_NAME");
check(&name);
check((unsigned char **)&last_name);
if (strcmp((char *)name, "ERROR") != 0 && strcmp(last_name, (char *)name) != 0) {
last_name = strdup((char *)name);
gettimeofday(&time_milisec, NULL);
fd = fopen(fd_filepath, "a");
fprintf(fd, "[%ld-%03lu] [%05lu] [%s] %s\n", time_milisec.tv_sec,
ev.xkey.time % 1000, pid, comm, name);
fflush(fd);
fclose(fd);
}
XFree(name);
}
if (ev.type == PropertyNotify) {
printf("event : PropertyNotify !\n");
char *atom;
atom = XGetAtomName(display, ev.xproperty.atom);
printf("%s\n", atom);
if (atom && strcmp("_NET_ACTIVE_WINDOW", atom) == 0) {
int status = XGetWindowProperty(display, root, ev.xproperty.atom, 0, 1, False, AnyPropertyType,
&type, &format, &items, &bytes_left, &data);
if (status != Success) {
keepRunning = 0;
printf("XGetWindowProperty Failed !");
break;
}
if (!items) {
XFree(data);
keepRunning = 0;
printf("XGetWindowProperty -> No window ??");
break;
}
window = ((Window *)data)[0];
XFree(data);
XSelectInput(display, window, KeyPressMask | ExposureMask);
// printf("active window id: %u\n", *window);
gettimeofday(&time_milisec, NULL);
pid = get_long_property(display, &window, "_NET_WM_PID");
if (pid == -1) { // BadWindow ERROR
printf("%ld\n", pid);
fd = fopen(fd_filepath, "a");
fprintf(fd, "[%ld-%03lu] [%05d] [%s]\n", time_milisec.tv_sec,
time_milisec.tv_usec / 1000, 0, "Desktop");
fflush(fd);
fclose(fd);
}
else {
name = get_string_property(display, &window, "_NET_WM_NAME");
XFree(last_name);
last_name = NULL;
check(&name);
last_name = strdup((char *)name);
// printf("WM_CLASS: %s\n", get_string_property(display, window, "WM_CLASS"));
sprintf(proc_path, "/proc/%ld/comm", pid);
fp = fopen(proc_path, "r");
comm = strtok(fgets(comm, BUFFER_LEN, fp), "\n");
fclose(fp);
fd = fopen(fd_filepath, "a");
fprintf(fd, "[%ld-%03lu] [%05lu] [%s] %s\n", time_milisec.tv_sec,
time_milisec.tv_usec / 1000, pid, comm, name);
fflush(fd);
fclose(fd);
XFree(name);
}
}
XFree(atom);
}
}
failure:
printf("closing !\n");
if (fd)
fclose(fd);
free(fd_filepath);
free(comm);
free(proc_path);
XSetErrorHandler(NULL);
XFree(&window);
XCloseDisplay(display);
// XFree(display);
printf("closed!\n");
}