forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-banout.c
451 lines (376 loc) · 12.9 KB
/
proto-banout.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
Banner Output
This module remembers "banners" from a connection. These are often
simple strings, like the FTP hello string. The can also be more
complex strings, parsed from binary protocols. They also may
contain bulk data, such as BASE64 encoded X.509 certificates from
SSL.
One complication is that since we can extract multiple types of
information from the same connection, we can have more than one
banner for the same connection.
*/
#include "proto-banner1.h"
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
/***************************************************************************
***************************************************************************/
void
banout_init(struct BannerOutput *banout)
{
banout->length = 0;
banout->protocol = 0;
banout->next = 0;
banout->max_length = sizeof(banout->banner);
}
/***************************************************************************
***************************************************************************/
void
banout_release(struct BannerOutput *banout)
{
while (banout->next) {
struct BannerOutput *next = banout->next->next;
free(banout->next);
banout->next = next;
}
}
/***************************************************************************
***************************************************************************/
static struct BannerOutput *
banout_find_proto(struct BannerOutput *banout, unsigned proto)
{
while (banout && banout->protocol != proto)
banout = banout->next;
return banout;
}
/***************************************************************************
***************************************************************************/
const unsigned char *
banout_string(const struct BannerOutput *banout, unsigned proto)
{
while (banout && (banout->protocol&0xFFFF) != proto)
banout = banout->next;
if (banout)
return banout->banner;
else
return NULL;
}
/***************************************************************************
***************************************************************************/
unsigned
banout_is_equal(const struct BannerOutput *banout, unsigned proto,
const char *string)
{
const unsigned char *string2;
size_t string_length;
size_t string2_length;
/*
* Grab the string
*/
string2 = banout_string(banout, proto);
if (string2 == NULL)
return string == NULL;
if (string == NULL)
return 0;
string_length = strlen(string);
string2_length = banout_string_length(banout, proto);
if (string_length != string2_length)
return 0;
return memcmp(string, string2, string2_length) == 0;
}
/***************************************************************************
***************************************************************************/
unsigned
banout_is_contains(const struct BannerOutput *banout, unsigned proto,
const char *string)
{
const unsigned char *string2;
size_t string_length;
size_t string2_length;
size_t i;
/*
* Grab the string
*/
string2 = banout_string(banout, proto);
if (string2 == NULL)
return string == NULL;
if (string == NULL)
return 0;
string_length = strlen(string);
string2_length = banout_string_length(banout, proto);
if (string_length > string2_length)
return 0;
for (i=0; i<string2_length-string_length; i++) {
if (memcmp(string, string2+i, string_length) == 0)
return 1;
}
return 0;
}
/***************************************************************************
***************************************************************************/
unsigned
banout_string_length(const struct BannerOutput *banout, unsigned proto)
{
while (banout && banout->protocol != proto)
banout = banout->next;
if (banout)
return banout->length;
else
return 0;
}
/***************************************************************************
***************************************************************************/
void
banout_newline(struct BannerOutput *banout, unsigned proto)
{
struct BannerOutput *p;
p = banout_find_proto(banout, proto);
if (p && p->length) {
banout_append_char(banout, proto, '\n');
}
}
/***************************************************************************
***************************************************************************/
void
banout_end(struct BannerOutput *banout, unsigned proto)
{
struct BannerOutput *p;
p = banout_find_proto(banout, proto);
if (p && p->length) {
p->protocol |= 0x80000000;
}
}
/***************************************************************************
***************************************************************************/
void
banout_append_char(struct BannerOutput *banout, unsigned proto, int c)
{
char cc = (char)c;
banout_append(banout, proto, &cc, 1);
}
/***************************************************************************
***************************************************************************/
static struct BannerOutput *
banout_new_proto(struct BannerOutput *banout, unsigned proto)
{
struct BannerOutput *p;
if (banout->protocol == 0 && banout->length == 0) {
banout->protocol = proto;
return banout;
}
p = (struct BannerOutput *)malloc(sizeof(*p));
memset(p, 0, sizeof(*p));
p->protocol = proto;
p->max_length = sizeof(p->banner);
p->next = banout->next;
banout->next = p;
return p;
}
/***************************************************************************
***************************************************************************/
static struct BannerOutput *
banout_expand(struct BannerOutput *banout, struct BannerOutput *p)
{
struct BannerOutput *n;
/* Double the space */
n = (struct BannerOutput *)malloc( offsetof(struct BannerOutput, banner)
+ 2 * p->max_length);
if (n == NULL)
exit(1);
/* Copy the old structure */
memcpy(n, p, offsetof(struct BannerOutput, banner) + p->max_length);
n->max_length *= 2;
if (p == banout) {
/* 'p' is the head of the linked list, so we can't free it */
banout->next = n;
p->protocol = 0;
p->length = 0;
} else {
/* 'p' is not the head, so replace it in the list with 'n',
* then free it. */
while (banout->next != p)
banout = banout->next;
banout->next = n;
free(p);
}
return n;
}
/***************************************************************************
***************************************************************************/
void
banout_append(struct BannerOutput *banout, unsigned proto,
const void *px, size_t length)
{
struct BannerOutput *p;
if (length == AUTO_LEN)
length = strlen((const char*)px);
/*
* Get the matching record for the protocol (e.g. HTML, SSL, etc.).
* If it doesn't already exist, add the protocol object to the linked
* list.
*/
p = banout_find_proto(banout, proto);
if (p == NULL) {
p = banout_new_proto(banout, proto);
}
/*
* If the current object isn't big enough, expand it
*/
while (p->length + length >= p->max_length) {
p = banout_expand(banout, p);
}
/*
* Now that we are assured there is enough space, do the copy
*/
memcpy(p->banner + p->length, px, length);
p->length = (unsigned)(p->length + length);
}
/*****************************************************************************
*****************************************************************************/
static const char *b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789"
"+/";
/*****************************************************************************
*****************************************************************************/
void
banout_init_base64(struct BannerBase64 *base64)
{
base64->state = 0;
base64->temp = 0;
}
/*****************************************************************************
*****************************************************************************/
void
banout_append_base64(struct BannerOutput *banout, unsigned proto,
const void *vpx, size_t length,
struct BannerBase64 *base64)
{
const unsigned char *px = (const unsigned char *)vpx;
size_t i;
unsigned x = base64->temp;
unsigned state = base64->state;
for (i=0; i<length; i++) {
switch (state) {
case 0:
x = px[i]<<16;
state++;
break;
case 1:
x |= px[i]<<8;
state++;
break;
case 2:
x |= px[i];
state = 0;
banout_append_char(banout, proto, b64[(x>>18)&0x3F]);
banout_append_char(banout, proto, b64[(x>>12)&0x3F]);
banout_append_char(banout, proto, b64[(x>> 6)&0x3F]);
banout_append_char(banout, proto, b64[(x>> 0)&0x3F]);
}
}
base64->temp = x;
base64->state = state;
}
/*****************************************************************************
*****************************************************************************/
void
banout_finalize_base64(struct BannerOutput *banout, unsigned proto,
struct BannerBase64 *base64)
{
unsigned x = base64->temp;
switch (base64->state) {
case 0:
break;
case 1:
banout_append_char(banout, proto, b64[(x>>18)&0x3F]);
banout_append_char(banout, proto, b64[(x>>12)&0x3F]);
banout_append_char(banout, proto, '=');
banout_append_char(banout, proto, '=');
break;
case 2:
banout_append_char(banout, proto, b64[(x>>18)&0x3F]);
banout_append_char(banout, proto, b64[(x>>12)&0x3F]);
banout_append_char(banout, proto, b64[(x>>6)&0x3F]);
banout_append_char(banout, proto, '=');
break;
}
}
/*****************************************************************************
*****************************************************************************/
static int
banout_string_equals(struct BannerOutput *banout, unsigned proto,
const char *rhs)
{
const unsigned char *lhs = banout_string(banout, proto);
size_t lhs_length = banout_string_length(banout, proto);
size_t rhs_length = strlen(rhs);
if (lhs_length != rhs_length)
return 0;
return memcmp(lhs, rhs, rhs_length) == 0;
}
/*****************************************************************************
*****************************************************************************/
int
banout_selftest(void)
{
/*
* Basic test
*/
{
struct BannerOutput banout[1];
unsigned i;
banout_init(banout);
for (i=0; i<10; i++) {
banout_append(banout, 1, "xxxx", 4);
banout_append(banout, 2, "yyyyy", 5);
}
if (banout->next == 0)
return 1;
if (banout_string_length(banout, 1) != 40)
return 1;
if (banout_string_length(banout, 2) != 50)
return 1;
banout_release(banout);
if (banout->next != 0)
return 1;
}
/*
* Test BASE64 encoding. We are going to do strings of various lengths
* in order to test the boundary condition of finalizing various strings
* properly
*/
{
struct BannerOutput banout[1];
struct BannerBase64 base64[1];
banout_init(banout);
banout_init_base64(base64);
banout_append_base64(banout, 1, "x", 1, base64);
banout_finalize_base64(banout, 1, base64);
banout_init_base64(base64);
banout_append_base64(banout, 2, "bc", 2, base64);
banout_finalize_base64(banout, 2, base64);
banout_init_base64(base64);
banout_append_base64(banout, 3, "mno", 3, base64);
banout_finalize_base64(banout, 3, base64);
banout_init_base64(base64);
banout_append_base64(banout, 4, "stuv", 4, base64);
banout_finalize_base64(banout, 4, base64);
banout_init_base64(base64);
banout_append_base64(banout, 5, "fghij", 5, base64);
banout_finalize_base64(banout, 5, base64);
if (!banout_string_equals(banout, 1, "eA=="))
return 1;
if (!banout_string_equals(banout, 2, "YmM="))
return 1;
if (!banout_string_equals(banout, 3, "bW5v"))
return 1;
if (!banout_string_equals(banout, 4, "c3R1dg=="))
return 1;
if (!banout_string_equals(banout, 5, "ZmdoaWo="))
return 1;
banout_release(banout);
}
return 0;
}