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
|
/**
* @file
* Parse and display individual attacks from standard input
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "metrics.h"
#include "parser.h"
#include "sandbox.h"
#define MAX_LEN 4096
unsigned int test_counter = 0;
static void print_attack(const attack_t *attack) {
printf("%d %s %d %d\n", attack->service, attack->address.value,
attack->address.kind, attack->dangerousness);
}
static void parse_to_buf(char buf[static 1], char dst[static MAX_LEN]) {
attack_t attack;
bool is_attack = !parse_line(buf, &attack);
if (is_attack) {
snprintf(dst, MAX_LEN,
"%d %s %d %d\n", attack.service, attack.address.value,
attack.address.kind, attack.dangerousness);
} else {
strncpy(dst, "*\n", MAX_LEN);
}
}
static void print_usage() {
fprintf(stderr, "usage: sshg-parser [-adht]\n");
}
static void test_next_line(char buf[static MAX_LEN], unsigned int lineno) {
static unsigned char state = 0;
static char expected[MAX_LEN], result[MAX_LEN];
static bool match;
if (buf[0] == '\n' || buf[0] == '#') {
// skip blank lines and comments
return;
}
switch (state) {
case 0: // line with input
strncpy(expected, buf, sizeof(expected));
parse_to_buf(buf, result);
state++;
break;
case 1: // line with expected output
match = strcmp(buf, result) == 0;
state++;
break;
case 2: // line with type of test
test_counter += 1;
if (match) {
printf("ok %d", test_counter);
}
else {
printf("not ok %d", test_counter);
}
switch (buf[0]) {
case 'M': // expected match
if (match) {
putchar('\n');
} else {
printf(" # %u: actual: %s", lineno, result);
}
break;
case 'X': // expected fail
printf(" # TODO\n");
break;
default:
puts("Bail out! Malformed expected test result");
exit(99);
}
printf("# %s", expected);
state = 0;
break;
default:
abort();
}
}
int main(int argc, char *argv[]) {
bool annotate = false, debug = false, test_mode = false;
char buf[MAX_LEN];
int flag;
init_log();
metrics_init("parser");
sandbox_init();
while ((flag = getopt(argc, argv, "adht")) != -1) {
switch (flag) {
case 'a':
annotate = true;
break;
case 'd':
debug = true;
break;
case 'h':
print_usage();
return 0;
case 't':
test_mode = true;
break;
case '?':
print_usage();
return 1;
}
}
yydebug = yy_flex_debug = debug;
// Number of input lines read. Used to report line number in test mode,
// otherwise serves as a counter for total number of logs ingested.
unsigned int lineno = 0;
unsigned int attacks_total = 0;
unsigned int danger_total = 0;
while (fgets(buf, sizeof(buf), stdin) != NULL) {
lineno++;
if (test_mode) {
test_next_line(buf, lineno);
} else {
attack_t attack;
bool is_attack = !parse_line(buf, &attack);
if (is_attack) {
attacks_total++;
danger_total += attack.dangerousness;
}
if (annotate) {
if (is_attack) {
fputs("* ", stdout);
} else {
fputs(" ", stdout);
}
fputs(buf, stdout);
fflush(stdout);
} else {
if (is_attack) {
print_attack(&attack);
fflush(stdout);
}
}
}
if (metrics_begin()) {
metrics_write("logs_read_total", lineno);
metrics_write("attacks_total", attacks_total);
metrics_write("danger_total", danger_total);
metrics_fin();
}
}
if (test_mode) {
printf("1..%d\n", test_counter);
}
}
|