forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-ntp.c
299 lines (250 loc) · 7.97 KB
/
proto-ntp.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
/*
NTP protocol handler
*/
#include "proto-ntp.h"
#include <stdint.h>
#include <stdlib.h>
#include "smack.h"
#include "string_s.h"
#include "output.h"
#include "masscan-app.h"
#include "proto-preprocess.h"
#include "proto-banner1.h"
#include "syn-cookie.h"
#include "massip-port.h"
#include "unusedparm.h"
/****************************************************************************
****************************************************************************/
unsigned
ntp_set_cookie(unsigned char *px, size_t length, uint64_t seqno)
{
UNUSEDPARM(px);
UNUSEDPARM(length);
UNUSEDPARM(seqno);
return 0;
}
struct Val2String {
unsigned value;
const char *string;
};
static const struct Val2String request_codes[] = {
{ 0, "PEER_LIST" },
{ 1, "PEER_LIST_SUM" },
{ 2, "PEER_INFO" },
{ 3, "PEER_STATS" },
{ 4, "SYS_INFO" },
{ 5, "SYS_STATS" },
{ 6, "IO_STATS" },
{ 7, "MEM_STATS" },
{ 8, "LOOP_INFO" },
{ 9, "TIMER_STATS" },
{ 10, "CONFIG" },
{ 11, "UNCONFIG" },
{ 12, "SET_SYS_FLAG" },
{ 13, "CLR_SYS_FLAG" },
{ 16, "GET_RESTRICT" },
{ 17, "RESADDFLAGS" },
{ 18, "RESSUBFLAGS" },
{ 19, "UNRESTRICT" },
{ 20, "MON_GETLIST" },
{ 21, "RESET_STATS" },
{ 22, "RESET_PEER" },
{ 23, "REREAD_KEYS" },
{ 26, "TRUSTKEY" },
{ 27, "UNTRUSTKEY" },
{ 28, "AUTHINFO" },
{ 29, "TRAPS" },
{ 30, "ADD_TRAP" },
{ 31, "CLR_TRAP" },
{ 32, "REQUEST_KEY" },
{ 33, "CONTROL_KEY" },
{ 34, "GET_CTLSTATS" },
{ 36, "GET_CLOCKINFO" },
{ 37, "SET_CLKFUDGE" },
{ 38, "GET_KERNEL" },
{ 39, "GET_CLKBUGINFO" },
{ 42, "MON_GETLIST_1" },
{ 43, "HOSTNAME_ASSOCID" },
{ 0, 0}
};
struct Val2String error_codes[] = {
{0, "No Error"},
{1, "Incompatible Implementation Number"},
{2, "Unimplemented Request Code"},
{3, "Format Error"},
{4, "No Data Available"},
{7, "Authentication Failure"},
{0,0}
};
/*****************************************************************************
*****************************************************************************/
static const char *
val2string_lookup(const struct Val2String *list, unsigned val)
{
unsigned i;
for (i=0; list[i].string; i++) {
if (list[i].value == val)
return list[i].string;
}
return 0;
}
/*****************************************************************************
*****************************************************************************/
static void
ntp_modlist_parse(const unsigned char *px,
unsigned length,
struct BannerOutput *banout,
unsigned *request_id)
{
unsigned offset = 4;
unsigned errcode;
unsigned record_count;
unsigned record_size;
UNUSEDPARM(request_id);
if (offset + 4 >= length)
return;
errcode = (px[offset]>>4)&0xF;
record_count = (px[offset+0]&0xF) << 8 | px[offset+1];
record_size = (px[offset+2]&0xF) << 8 | px[offset+3];
if (errcode) {
char foo[12];
const char *errmsg = val2string_lookup(error_codes, errcode);
if (errmsg == 0)
errmsg = "Bogus Error Code";
sprintf_s(foo, sizeof(foo), "%u", errcode);
banout_append(banout, PROTO_NTP, "Response was NTP Error Code ", AUTO_LEN);
banout_append(banout, PROTO_NTP, foo, AUTO_LEN);
banout_append(banout, PROTO_NTP, " - \"", AUTO_LEN);
banout_append(banout, PROTO_NTP, errmsg, AUTO_LEN);
banout_append(banout, PROTO_NTP, "\"", AUTO_LEN);
return;
}
if (4 + record_count * record_size > length) {
banout_append(banout, PROTO_NTP, "response-too-big", AUTO_LEN);
return;
}
if (record_count * record_size > 500) {
banout_append(banout, PROTO_NTP, "response-too-big", AUTO_LEN);
return;
}
//offset += 4;
{
char msg[128];
sprintf_s(msg, sizeof(msg), " response-size=%u-bytes more=%s",
record_count * record_size, ((px[0]>>6)&1)?"true":"false");
banout_append(banout, PROTO_NTP, msg, AUTO_LEN);
}
}
/*****************************************************************************
*****************************************************************************/
static void
ntp_priv(const unsigned char *px,
unsigned length,
struct BannerOutput *banout,
unsigned *request_id)
{
unsigned implementation = px[2];
unsigned request_code = px[3];
const char *request_string;
switch (implementation) {
case 0: banout_append(banout, PROTO_NTP, "UNIV", 4); return;
case 2: banout_append(banout, PROTO_NTP, "XNTPD-OLD", 9); return;
case 3: banout_append(banout, PROTO_NTP, "XNTPD", 5); break;
default:
return;
}
request_string = val2string_lookup(request_codes, request_code);
if (request_string) {
banout_append(banout, PROTO_NTP, " ", 1);
banout_append(banout, PROTO_NTP, request_string, strlen(request_string));
}
switch (request_code) {
case 42:
ntp_modlist_parse(px, length, banout, request_id);
break;
}
}
/*****************************************************************************
*****************************************************************************/
static void
ntp_v2_parse(const unsigned char *px,
unsigned length,
struct BannerOutput *banout,
unsigned *request_id)
{
unsigned mode;
if (length < 4)
return;
/* Validate: response bit is set */
if ((px[0]>>7) != 1)
return;
/* Validate: this is version 2 */
if (((px[0]>>3)&7) != 2)
return;
/* Extract: mode */
mode = px[0] & 7;
switch (mode) {
case 6: /* control */
break;
case 7:
ntp_priv(px, length, banout, request_id);
break;
}
}
/*****************************************************************************
* Handles an NTP response.
*****************************************************************************/
unsigned
ntp_handle_response(struct Output *out, time_t timestamp,
const unsigned char *px, unsigned length,
struct PreprocessedInfo *parsed,
uint64_t entropy
)
{
ipaddress ip_them = parsed->src_ip;
unsigned request_id = 0;
struct BannerOutput banout[1];
unsigned offset = parsed->app_offset;
UNUSEDPARM(length);
UNUSEDPARM(entropy);
if (parsed->app_length < 4)
return 0;
/* Initialize the "banner output" module that we'll use to print
* pretty text in place of the raw packet */
banout_init(banout);
/* Parse the packet */
switch ((px[offset]>>3)&7) {
case 2:
ntp_v2_parse(
px + parsed->app_offset, /* incoming response */
parsed->app_length, /* length of response */
banout, /* banner printing */
&request_id); /* syn-cookie info */
break;
default:
banout_release(banout);
return 0;
}
/* Validate the "syn-cookie" style information. */
//seqno = (unsigned)syn_cookie(ip_them, port_them | Templ_UDP, ip_me, port_me);
//if ((seqno&0x7FFFffff) != request_id)
// return 1;
/* Print the banner information, or save to a file, depending */
output_report_banner(
out, timestamp,
ip_them, 17, parsed->port_src,
PROTO_NTP,
parsed->ip_ttl,
banout_string(banout, PROTO_NTP),
banout_string_length(banout, PROTO_NTP));
/* Free memory for the banner, if there was any allocated */
banout_release(banout);
return 0;
}
/****************************************************************************
****************************************************************************/
int
ntp_selftest(void)
{
return 0;
}