-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
340 lines (247 loc) · 8.09 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
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
#define _GNU_SOURCE
#include <time.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <openssl/md5.h>
#include <fcntl.h>
#include <limits.h>
typedef struct{
int uid; /* user id (positive integer) */
int access_type; /* access type values [0-2] */
int action_denied; /* is action denied values [0-1] */
time_t timestamp; /* date and time that the action occurred */
char *file; /* filename (string) */
unsigned char *fingerprint; /* file fingerprint */
}log_entry;
void write_log_file(log_entry *logEntry){
// Open file_logging.log
int pfd;
if ( (pfd = open("./file_logging.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) == -1 ){
printf("Cannot open file_logging.log file\n");
abort();
}
// Write uid
if( ( write(pfd,&logEntry->uid,sizeof(int)) ) == -1){
printf("Error on write \n");
abort();
}
// Write access_type
if( ( write(pfd,&logEntry->access_type,sizeof(int)) ) == -1){
printf("Error on write \n");
abort();
}
// Write action_denied flag
if( ( write(pfd,&logEntry->action_denied,sizeof(int)) ) == -1){
printf("Error on write \n");
abort();
}
// Write timestamp of event
if( ( write(pfd,&logEntry->timestamp,sizeof(time_t)) ) == -1){
printf("Error on write \n");
abort();
}
// Write filepath
if( ( write(pfd,logEntry->file,strlen(logEntry->file)) ) == -1){
printf("Error on write \n");
abort();
}
// Write slashes as seperator between filepath and message digest
char* slash = malloc(sizeof(char)*2);
memset(slash,47,sizeof(char)*2);
if( ( write(pfd,slash,2*sizeof(char)) ) == -1){
printf("Error on write \n");
abort();
}
// Write digital fingerprint of file
if( ( write(pfd,logEntry->fingerprint,MD5_DIGEST_LENGTH) ) == -1){
printf("Error on write \n");
abort();
}
// Clean up
free(slash);
// Close file descriptor
close(pfd);
}
FILE *fopen(const char *path, const char *mode){
FILE *original_fopen_ret;
FILE *(*original_fopen)(const char*, const char*);
log_entry* logEntry = (log_entry*)malloc(sizeof(log_entry));
// Get the user id
logEntry->uid = (int)getuid();
// Get the date and time that the action occurred
logEntry->timestamp = time(NULL);
// Identify access type using fopen
// File creation or file does not exist
if ( access(path,F_OK) != 0 ){
// Access type
logEntry->access_type = 0;
// Action denied
logEntry->action_denied = 0;
// File name
logEntry->file = (char*)malloc(sizeof(char)*strlen(path));
memcpy(logEntry->file,path,strlen(path));
// File fingerprint
logEntry->fingerprint = NULL;
}
// File open
else {
// Access type
logEntry->access_type = 1;
// Get status of file using stat
struct stat* sb = (struct stat*)malloc(sizeof(struct stat));
if ( stat(path,sb) != 0 ){
printf("Error on stat\n");
abort();
}
// Action denied
// If mode contain + , then user must have write and read access privileges
if( strrchr(mode,'+') != NULL ){
// Check the user access privileges and set appropriately the action_denied
logEntry->action_denied = ((sb->st_mode & S_IRUSR) && (sb->st_mode & S_IWUSR)) ? 0 : 1;
}
else{
// Write access only
if( ( strchr(mode,'a') != NULL) || ( strchr(mode,'w') != NULL) ){
// Check the user access privileges and set appropriately the action_denied
logEntry->action_denied = (sb->st_mode & S_IWUSR) ? 0 : 1;
}
// Read access only
if( strchr(mode,'r') != NULL ){
// Check the user access privileges and set appropriately the action_denied
logEntry->action_denied = (sb->st_mode & S_IRUSR) ? 0 : 1;
}
}
// File name
logEntry->file = realpath(path, NULL);
// File fingerprint
logEntry->fingerprint = NULL;
free(sb);
}
/* call the original fopen function */
original_fopen = dlsym(RTLD_NEXT, "fopen");
original_fopen_ret = (*original_fopen)(path, mode);
// Fingerprint of file
// Initialize fingerprint
logEntry->fingerprint = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
// File created or opened
if( access(path,F_OK) == 0 ){
// Get status of file using stat
struct stat* sb_new = (struct stat*)malloc(sizeof(struct stat));
if ( stat(path,sb_new) != 0 ){
printf("Error on stat\n");
abort();
}
// If have read access
if( (sb_new->st_mode & S_IRUSR) ){
unsigned char* read_string = (unsigned char*)malloc(sizeof(unsigned char)*((int)sb_new->st_size)+1);
memset(read_string,0,sb_new->st_size+1);
// Open the file
int fd = open(path,O_RDONLY);
// Read from file
read(fd,read_string, sb_new->st_size);
// Initialize digital file fingerprint
unsigned char* md = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
// Compute the message digest
MD5(read_string,sb_new->st_size,md);
// Copy message digest to logEntry fingerprint
memcpy(logEntry->fingerprint,md,MD5_DIGEST_LENGTH);
free(read_string);
free(md);
close(fd);
}
else{ memset(logEntry->fingerprint,0,MD5_DIGEST_LENGTH); }
free(sb_new);
}
// File does not exist
else{ memset(logEntry->fingerprint,0,MD5_DIGEST_LENGTH); }
// Write log entry
write_log_file(logEntry);
// Clean up
free(logEntry->fingerprint);
free(logEntry->file);
free(logEntry);
return original_fopen_ret;
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
size_t original_fwrite_ret;
size_t (*original_fwrite)(const void*, size_t, size_t, FILE*);
// Initialize log_entry
log_entry* logEntry = (log_entry*)malloc(sizeof(log_entry));
// Get the user id
logEntry->uid = (int)getuid();
// Get the date and time that the action occurred
logEntry->timestamp = time(NULL);
// Access type
logEntry->access_type = 2;
/* call the original fwrite function */
original_fwrite = dlsym(RTLD_NEXT, "fwrite");
original_fwrite_ret = (*original_fwrite)(ptr, size, nmemb, stream);
// Get file descriptor using stream
int fds = fileno(stream);
// Get status of file using fstat
struct stat* sb = (struct stat*)malloc(sizeof(struct stat));
if ( fstat(fds,sb) != 0 ){
printf("Error on fstat\n");
abort();
}
// Action denied flag
logEntry->action_denied = (sb->st_mode & S_IWUSR) ? 0 : 1;
// Get file path
char* proclnk = (char *)malloc(sizeof(char)*18);
sprintf(proclnk, "/proc/self/fd/%d", fds);
char* file = (char*)malloc(sizeof(char)*PATH_MAX);
size_t path_bytes;
if ( (path_bytes = readlink(proclnk, file, PATH_MAX)) < 0){
printf("Failed to readlink\n");
abort();
}
// Copy file path to log_entry file field
logEntry->file = (char*)malloc(sizeof(char)*path_bytes + 1);
memcpy(logEntry->file,file,path_bytes);
*(logEntry->file + path_bytes) = '\0';
// Clean up
free(proclnk);
free(file);
// File fingerprint
// Flush the stream buffer so that writted the result to file
fflush(stream);
// Get status of file using fstat after calling the original fwrite
if ( fstat(fds,sb) != 0 ){
printf("Error on fstat\n");
abort();
}
// Compute fingerprint
// Initialize fingerprint
logEntry->fingerprint = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
// If user have got read access
if( (sb->st_mode & S_IRUSR) ){
unsigned char* read_string = (unsigned char*)malloc(sizeof(unsigned char)*((int)sb->st_size)+1);
// Open the file
int fd = open(logEntry->file,O_RDONLY);
// Read from file
read(fd,read_string, sb->st_size);
// Add backslash to read_string
*(read_string+sb->st_size) = '\0';
// Initialize digital file fingerprint
unsigned char* md = (unsigned char*)malloc(MD5_DIGEST_LENGTH);
// Compute the message digest
MD5(read_string,sb->st_size,md);
// Copy message digest to logEntry fingerprint
memcpy(logEntry->fingerprint,md,MD5_DIGEST_LENGTH);
free(read_string);
free(md);
close(fd);
}
else{ memset(logEntry->fingerprint,0,MD5_DIGEST_LENGTH); }
// Write log entry
write_log_file(logEntry);
free(sb);
free(logEntry->fingerprint);
free(logEntry->file);
free(logEntry);
return original_fwrite_ret;
}