summaryrefslogtreecommitdiff
path: root/docs/getting_started.md
blob: bd8cf49f55c4ba81ac68b9332e35371867df3990 (plain)
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
# Getting started

## Build

```shell
$ mkdir build && cd build
$ cmake ..
$ make
$ make install
```

## Run test

```shell
$ cd build/test
$ ./maat_framework_gtest  #Functional test set
$ ./maat_framework_perf_gtest #Performance test set
```

Maat is used as a dynamic library by applications and its API is defined in the header file(maat.h).

## Use case

A complete use case consists of three parts:

* `table schema`: Define how to parse configuration in different types of tables, that is, specify what each column in the table represents.

* `configuration`: Different types of configurations are stored in tables of the corresponding type.

* `maat_scan_xx/xx_plugin_get_ex_data`: In the scanning scenario, the caller uses the maat_scan_xx api to determine whether the traffic hits effective rules; In the callback scenario, the caller uses the xx_plugin_get_ex_data api to query the ex_data of the specified key. 

### Case1

In the scanning scenario, it is necessary to configure the schema of multiple tables, including the item table, object2rule table, and rule table. If there is also object nesting involved, the schema of the object_group table needs to be configured.

**(1) table schema**

Table schema is stored in a json file(such as table_info.json), which is loaded when maat instance is created.

```json
 [
    {
        "table_id":0,
        "table_name":"RULE",
        "table_type":"rule",
        "default_rule_table":0, /* key:indicate this is the default rule table, value:can be anything(not care) */
        "valid_column":8,
        "custom": {
            "rule_id":1,
            "tags":6,
            "condition_num":9
        }
    },
    {
        "table_id":1,
        "table_name":"OBJECT2RULE",
        "table_type":"object2rule",
        "associated_rule_table_id":0, /* associate rule table_id, object2rule table shares the same runtime with the corresponding rule table, so it needs to be mapped to the corresponding rule table. */
        "valid_column":3,
        "custom": {
            "object_id":1,
            "rule_id":2,
            "negate_option":4,
            "attribute_name":5,
            "condition_index":6
        }
    },
    {
        "table_id":2,
        "table_name":"OBJECT_GROUP",
        "table_type":"object_group",
        "valid_column":4,
        "custom": {
            "object_id":1,
            "included_sub_object_ids":2,
            "excluded_sub_object_ids":3
        }
    },
    {
        "table_id":3,
        "table_name":"HTTP_URL",
        "table_type":"expr",
        "valid_column":7,
        "custom": {
            "item_id":1,
            "object_id":2,
            "keywords":3,
            "expr_type":4,
            "match_method":5,
            "is_hexbin":6
        }
    }
 ]
```

**(2) configuration**

Configurations are stored in a json file(such as maat_json.json), which is loaded when maat instance is created.
```json
{
    "rule_table": "RULE",
    "object2rule_table": "OBJECT2RULE",
    "object_group_table": "OBJECT_GROUP",
    "rules": [
        {
            "rule_id": 123,
            "service": 1,
            "action": 1,
            "do_blacklist": 1,
            "do_log": 1,
            "user_region": "anything",
            "is_valid": "yes",
            "objects": [
                {
                    "object_name": "Untitled",
                    "regions": [
                        {
                            "table_name": "HTTP_URL",
                            "table_type": "expr",
                            "table_content": {
                                "keywords": "Hello Maat",
                                "expr_type": "none",
                                "match_method": "sub",
                                "format": "uncase plain"
                            }
                        }
                    ]
                }
            ]
        }
    ]
}
```

**(3) scanning**

```C
#include <assert.h>

#include "maat.h"

#define ARRAY_SIZE 16

const char *json_filename = "./maat_json.json";
const char *table_info_path = "./table_info.json";

int main()
{
    /* initialize maat options which will be used by maat_new() */
    struct maat_options *opts = maat_options_new();
    maat_options_set_json_file(opts, json_filename);
    maat_options_set_logger(opts, "./sample_test.log", LOG_LEVEL_INFO);

    /* create maat instance, rules in table_info.json will be loaded. */
    struct maat *maat_instance = maat_new(opts, table_info_path);
    assert(maat_instance != NULL);
    maat_options_free(opts);

    const char *table_name = "HTTP_URL"; /* maat_json.json has HTTP_URL rule */
    int table_id = maat_get_table_id(maat_instance, table_name);
    assert(table_id == 3); /* defined in table_info.json */

    int thread_id = 0;
    long long results[ARRAY_SIZE] = {0};
    size_t n_hit_result = 0;
    
    /* store scanning intermediate state */
    struct maat_state *state = maat_state_new(maat_instance, thread_id);
    assert(state != NULL);

    const char *scan_data = "Hello Maat, nice to meet you";

    /**
     * Becase maat instance has loaded rule in table_info.json which keywords is "Hello Maat",
       so maat_scan_string should return hit flag and rule's rule_id stored in results array.
    */
    int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
                               results, ARRAY_SIZE, &n_hit_result, state);
    assert(ret == MAAT_SCAN_HIT);
    assert(n_hit_result == 1);
    assert(results[0] == 123);

    maat_state_free(state);

    return 0;
}

```

### Case2

In the callback scenario, only the schema of the corresponding table needs to be configured.

**(1) table schema**

```json
 [
    {
        "table_id":1,
        "table_name":"TEST_IP_PLUGIN_WITH_EXDATA",
        "table_type":"ip_plugin",
        "valid_column":6,
        "custom": {
            "gc_timeout_s": 3,
            "item_id":1,
            "ip_type":2,
            "start_ip":3,
            "end_ip":4
        }
    }
 ]
```

**(2) configuration**

```json
{
    "rule_table": "RULE",
    "object2rule_table": "OBJECT2RULE",
    "object_group_table": "OBJECT_GROUP",
    "plugin_table": {
        "table_name": "TEST_IP_PLUGIN_WITH_EXDATA",
        "table_content": [
            "101\t4\t192.168.30.99\t192.168.30.101\tSomething-like-json\t1",
            "102\t4\t192.168.30.90\t192.168.30.128\tBigger-range-should-in-the-back\t1",
            "103\t6\t2001:db8:1234::\t2001:db8:1235::\tBigger-range-should-in-the-back\t1",
            "104\t6\t2001:db8:1234::1\t2001:db8:1234::5210\tSomething-like-json\t1",
            ]
        },
}
```

**(3) xx_plugin_get_ex_data**

```C
#include <assert.h>

#include "maat.h"
#include "maat_utils.h"

#define ARRAY_SIZE 16

const char *json_filename = "./maat_json.json";
const char *table_info_path = "./table_info.json";

struct ip_plugin_ud {
	long long rule_id;
	char *buffer;
    size_t buf_len;
};
void ip_plugin_ex_new_cb(const char *table_name, int table_id, const char *key, 
                         const char *table_line, void **ad, long argl, void *argp)
{
	int *counter = (int *)argp;
	size_t column_offset=0, column_len=0;
	struct ip_plugin_ud *ud = ALLOC(struct ip_plugin_ud, 1);

	int ret = get_column_pos(table_line, 1, &column_offset, &column_len);
	EXPECT_EQ(ret, 0);

	ud->rule_id = atoll(table_line + column_offset);

	ret = get_column_pos(table_line, 5, &column_offset, &column_len);	
	EXPECT_EQ(ret, 0);

	ud->buffer = ALLOC(char, column_len + 1);
	strncpy(ud->buffer, table_line + column_offset, column_len);

    ud->buf_len = column_len + 1;
	*ad = ud;
	(*counter)++;
}

void ip_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
{
	struct ip_plugin_ud *ud = (struct ip_plugin_ud *)(*ad);

    ud->rule_id = 0;
    memset(ud->buffer, 0, ud->buf_len);
    ud->buf_len = 0;

	free(ud->buffer);
	free(ud);
	*ad = NULL;
}

void ip_plugin_ex_dup_cb(int table_id, void **to, void **from, long argl, void *argp)
{
	struct ip_plugin_ud *ud = (struct ip_plugin_ud *)(*from);
	
	*to = ud;
}

int main()
{
    /* initialize maat options which will be used by maat_new() */
    struct maat_options *opts = maat_options_new();
    maat_options_set_json_file(opts, json_filename);
    maat_options_set_logger(opts, "./sample_test.log", LOG_LEVEL_INFO);

    /* create maat instance, rules in table_info.json will be loaded. */
    struct maat *maat_instance = maat_new(opts, table_info_path);
    assert(maat_instance != NULL);
    maat_options_free(opts);

    const char *table_name = "TEST_IP_PLUGIN_WITH_EXDATA"; /* maat_json.json has TEST_IP_PLUGIN_WITH_EXDATA rule */
    int table_id = maat_get_table_id(maat_instance, table_name);
    assert(table_id == 1); /* defined in table_info.json */

    int ret = maat_plugin_table_ex_schema_register(maat_inst, table_name,
                                                   ip_plugin_ex_new_cb,
                                                   ip_plugin_ex_free_cb,
                                                   ip_plugin_ex_dup_cb,
                                                   0, &ip_plugin_ex_data_counter);
    EXPECT_EQ(ret, 0);
    EXPECT_EQ(ip_plugin_ex_data_counter, 4);

    struct ip_addr ipv4;
    ipv4.ip_type = IPv4;
    ret = inet_pton(AF_INET, "192.168.30.100", &ipv4.ipv4);
    EXPECT_EQ(ret, 1);

    /* get ex_data */
    struct ip_plugin_ud *results[ARRAY_SIZE];
    ret = maat_ip_plugin_table_get_ex_data(maat_inst, table_id, &ipv4, 
                                           (void **)results, ARRAY_SIZE);
    EXPECT_EQ(ret, 2);
    EXPECT_EQ(results[0]->rule_id, 101);
    EXPECT_EQ(results[1]->rule_id, 102);

    struct ip_addr ipv6;
    ipv6.ip_type = IPv6;
    inet_pton(AF_INET6, "2001:db8:1234::5210", &(ipv6.ipv6));
    memset(results, 0, sizeof(results));

	ret = maat_ip_plugin_table_get_ex_data(maat_inst, table_id, &ipv6, 
                                           (void**)results, ARRAY_SIZE);
	EXPECT_EQ(ret, 2);
	EXPECT_EQ(results[0]->rule_id, 104);
	EXPECT_EQ(results[1]->rule_id, 103);

    maat_state_free(state);

    return 0;
}

```