Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
352 lines (277 loc) · 7.54 KB
/
Copy pathmain.c
File metadata and controls
352 lines (277 loc) · 7.54 KB
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
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<string.h>
int line_is_blank(const char *line){
while(*line != '\0') {
if(!isspace((unsigned char) *line)){
return 0;
}
line++;
}
return 1;
};
void trim_whitespaces(char *line){
char *start = line;
char *end;
while(*start != '\0' && isspace((unsigned char) *start)){
start++;
};
if(start != line){
memmove(line, start, strlen(start) + 1);
};
if(*line == '\0'){
return;
};
end = line + strlen(line) - 1;
while(end >= line && isspace((unsigned char) *end)){
*end = '\0';
end --;
};
};
void normalize_limiters (char *line) {
while(*line != '\0'){
if(*line == ',' || *line == '|'){
*line = ';';
}
line++;
};
};
int split_fields(char *line, char *fields[], int max_fields){
char *token;
int count = 0;
token = strtok(line, ";");
while (token != NULL && count < max_fields) {
trim_whitespaces(token);
fields[count] = token;
count++;
token = strtok(NULL, ";");
};
return count;
};
int has_empty_field(char *fields[], int count) {
for (int i = 0; i < count; i ++) {
if (fields[i][0] == '\0'){
return 1;
}
}
return 0;
};
int is_numeric(const char *line) {
int i = 0;
if (line[0] == '\0'){
return 0;
}
while (line[i] != '\0'){
if (!isdigit((unsigned char)line[i])) {
return 0;
}
i++;
}
return 1;
};
char *extract_value(char *field) {
char *equal = strchr(field, '=');
if (equal == NULL) {
return field;
}
return equal + 1;
};
int is_valid_event_id(const char *id) {
int i = 4;
if (strncmp(id, "EVT-", 4) != 0) {
return 0;
}
if (id[4] == '\0') {
return 0;
}
while (id[i] != '\0') {
if (!isdigit((unsigned char)id[i])) {
return 0;
}
i++;
}
return 1;
};
void to_uppercase(char *str) {
int i = 0;
while (str[i] != '\0') {
str[i] = toupper((unsigned char)str[i]);
i++;
}
}
const char *normalize_severity(char *severity) {
char temp[32];
strncpy(temp, severity, sizeof(temp) - 1);
temp[sizeof(temp) - 1] = '\0';
to_uppercase(temp);
if (strcmp(temp, "LOW") == 0) {
return "LOW";
}
if (strcmp(temp, "MEDIUM") == 0 || strcmp(temp, "MED") == 0) {
return "MEDIUM";
}
if (strcmp(temp, "HIGH") == 0) {
return "HIGH";
}
if (strcmp(temp, "CRITICAL") == 0 || strcmp(temp, "CRIT") == 0) {
return "CRITICAL";
}
return NULL;
}
const char *normalize_status(char *status) {
char temp[32];
strncpy(temp, status, sizeof(temp) - 1);
temp[sizeof(temp) - 1] = '\0';
to_uppercase(temp);
if (strcmp(temp, "OPEN") == 0) {
return "OPEN";
}
if (strcmp(temp, "CLOSED") == 0 ||
strcmp(temp, "DONE") == 0 ||
strcmp(temp, "RESOLVED") == 0) {
return "CLOSED";
}
if (strcmp(temp, "INVESTIGATING") == 0 ||
strcmp(temp, "ANALYSIS") == 0 ||
strcmp(temp, "IN_PROGRESS") == 0) {
return "INVESTIGATING";
}
return NULL;
}
typedef struct {
char event_id[32];
char device[64];
char severity[16];
char status[20];
int failed_logins;
char source[32];
} SecurityEvent;
int main (void){
char line_buffer[512];
int total_lines = 0;
int blank_lines = 0;
char *fields[10];
int count = 0;
int invalid_structure = 0;
int empty_field_lines = 0;
int invalid_numeric = 0;
int invalid_event_id = 0;
int invalid_severity = 0;
int invalid_status = 0;
FILE *security_events_file = fopen("raw_security_events.txt", "r");
if (security_events_file == NULL){
printf("Erro ao tentar ler o arquivo");
return 1;
}
FILE *security_events_clean_file = fopen("security_events_cleaned.txt", "w");
if (security_events_clean_file == NULL) {
printf("Erro ao criar arquivo de saída\n");
fclose(security_events_file);
return 1;
}
SecurityEvent *events = NULL;
int total_valid = 0;
int capacity = 4;
events = malloc(capacity *sizeof(SecurityEvent));
if (events == NULL) {
printf("Erro ao alocar memória\n");
fclose(security_events_file);
fclose(security_events_clean_file);
return 1;
}
while(fgets(line_buffer, sizeof(line_buffer), security_events_file) != NULL){
total_lines ++;
trim_whitespaces(line_buffer);
if(line_is_blank(line_buffer)){
blank_lines++;
continue;
};
normalize_limiters(line_buffer);
count = split_fields(line_buffer, fields, 10);
if (count != 6) {
invalid_structure++;
continue;
}
char *values[6];
for (int i = 0; i < 6; i++) {
values[i] = extract_value(fields[i]);
trim_whitespaces(values[i]);
}
to_uppercase(values[0]);
to_uppercase(values[5]);
if (has_empty_field(values, 6)){
empty_field_lines++;
continue;
}
if(!is_numeric(values[4])) {
invalid_numeric++;
continue;
}
if (!is_valid_event_id(values[0])){
invalid_event_id++;
continue;
}
const char *normalized_severity = normalize_severity(values[2]);
if (normalized_severity == NULL) {
invalid_severity++;
continue;
}
const char *normalized_status = normalize_status(values[3]);
if (normalized_status == NULL) {
invalid_status++;
continue;
}
if(total_valid >= capacity) {
capacity *=2;
SecurityEvent *temp = realloc(events, capacity * sizeof(SecurityEvent));
if(temp == NULL) {
printf("Erro ao realocar memória\n");
free(events);
fclose(security_events_file);
fclose(security_events_clean_file);
return 1;
}
events = temp;
}
SecurityEvent *ptr = &events[total_valid];
strncpy(ptr->event_id, values[0], sizeof(ptr->event_id) - 1);
ptr->event_id[sizeof(ptr->event_id) - 1] = '\0';
strncpy(ptr->device, values[1], sizeof(ptr->device) - 1);
ptr->device[sizeof(ptr->device) - 1] = '\0';
strncpy(ptr->severity, normalized_severity, sizeof(ptr->severity) - 1);
ptr->severity[sizeof(ptr->severity) - 1] = '\0';
strncpy(ptr->status, normalized_status, sizeof(ptr->status) - 1);
ptr->status[sizeof(ptr->status) - 1] = '\0';
ptr->failed_logins = atoi(values[4]);
strncpy(ptr->source, values[5], sizeof(ptr->source) - 1);
ptr->source[sizeof(ptr->source) - 1] = '\0';
total_valid++;
};
fprintf(security_events_clean_file, "EVENT_ID;DEVICE;SEVERITY;STATUS;FAILED_LOGINS;SOURCE\n");
for (int i = 0; i < total_valid; i++){
fprintf(security_events_clean_file, "%s;%s;%s;%s;%d;%s\n",
events[i].event_id,
events[i].device,
events[i].severity,
events[i].status,
events[i].failed_logins,
events[i].source
);
};
printf("---Relatório de execução----");
printf("\nTotal de linhas lidas: %d\n", total_lines);
printf("Registros válidos: %d\n", total_valid);
printf("Registros inválidos: %d\n", blank_lines + invalid_structure + empty_field_lines + invalid_numeric + invalid_event_id + invalid_severity + invalid_status);
//printf("Linhas em branco removidas: %d\n", blank_lines);
//printf("Linhas estruturalmente inválidas: %d\n", invalid_structure);
//printf("Linhas com campo vazio: %d\n", empty_field_lines);
//printf("Linhas com failed_logins inválido: %d\n", invalid_numeric);
//printf("Linhas com event_id inválido: %d\n", invalid_event_id);
//printf("Linhas com severity inválida: %d\n", invalid_severity);
//printf("Linhas com status inválido: %d\n", invalid_status);
fclose(security_events_file);
fclose(security_events_clean_file);
free(events);
return 0;
};