forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-vnc.c
364 lines (340 loc) · 12.5 KB
/
proto-vnc.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
#include "proto-vnc.h"
#include "proto-banner1.h"
#include "proto-interactive.h"
#include "unusedparm.h"
#include "masscan-app.h"
#include "string_s.h"
#include "smack.h"
#include <ctype.h>
static void
vnc_append_sectype(struct BannerOutput *banout, unsigned sectype)
{
char foo[16];
/*
http://www.iana.org/assignments/rfb/rfb.xml
Value Name Reference
0 Invalid [RFC6143]
1 None [RFC6143]
2 VNC Authentication [RFC6143]
3-15 RealVNC historic assignment
16 Tight historic assignment
17 Ultra historic assignment
18 TLS historic assignment
19 VeNCrypt historic assignment
20 GTK-VNC SASL historic assignment
21 MD5 hash authentication historic assignment
22 Colin Dean xvp historic assignment
23-29 Unassigned
30-35 Apple Inc. [Michael_Stein]
36-127 Unassigned
128-255 RealVNC historic assignment
*/
switch (sectype) {
case 0:
banout_append(banout, PROTO_VNC_RFB, "invalid", AUTO_LEN);
break;
case 1:
banout_append(banout, PROTO_VNC_RFB, "none", AUTO_LEN);
break;
case 2:
banout_append(banout, PROTO_VNC_RFB, "VNC-chap", AUTO_LEN);
break;
case 5:
banout_append(banout, PROTO_VNC_RFB, "RA2", AUTO_LEN);
break;
case 6:
banout_append(banout, PROTO_VNC_RFB, "RA2ne", AUTO_LEN);
break;
case 7:
banout_append(banout, PROTO_VNC_RFB, "SSPI", AUTO_LEN);
break;
case 8:
banout_append(banout, PROTO_VNC_RFB, "SSPIne", AUTO_LEN);
break;
case 16:
banout_append(banout, PROTO_VNC_RFB, "Tight", AUTO_LEN);
break;
case 17:
banout_append(banout, PROTO_VNC_RFB, "Ultra", AUTO_LEN);
break;
case 18:
banout_append(banout, PROTO_VNC_RFB, "TLS", AUTO_LEN);
break;
case 19:
banout_append(banout, PROTO_VNC_RFB, "VeNCrypt", AUTO_LEN);
break;
case 20:
banout_append(banout, PROTO_VNC_RFB, "GTK-VNC-SASL", AUTO_LEN);
break;
case 21:
banout_append(banout, PROTO_VNC_RFB, "MD5", AUTO_LEN);
break;
case 22:
banout_append(banout, PROTO_VNC_RFB, "Colin-Dean-xvp", AUTO_LEN);
break;
case 30:
banout_append(banout, PROTO_VNC_RFB, "Apple30", AUTO_LEN);
break;
case 35:
banout_append(banout, PROTO_VNC_RFB, "Apple35", AUTO_LEN);
break;
default:
sprintf_s(foo, sizeof(foo), "%u", sectype);
banout_append(banout, PROTO_VNC_RFB, foo, AUTO_LEN);
break;
}
}
/***************************************************************************
***************************************************************************/
static void
vnc_parse( const struct Banner1 *banner1,
void *banner1_private,
struct ProtocolState *pstate,
const unsigned char *px, size_t length,
struct BannerOutput *banout,
struct InteractiveData *more)
{
unsigned state = pstate->state;
unsigned i;
char foo[64];
enum {
RFB3_3_SECURITYTYPES=50,
RFB_SECURITYERROR=60,
RFB3_7_SECURITYTYPES=100,
RFB_SERVERINIT=200,
RFB_SECURITYRESULT=300,
RFB_DONE=0x7fffffff,
};
UNUSEDPARM(banner1_private);
UNUSEDPARM(banner1);
for (i=0; i<length; i++)
switch (state) {
case 0:
case 1: case 2: case 3: case 4: case 5: case 6:
case 7: case 8: case 9:
state++;
banout_append_char(banout, PROTO_VNC_RFB, px[i]);
break;
case 10:
state++;
banout_append_char(banout, PROTO_VNC_RFB, px[i]);
break;
case 11:
if ('\n' == px[i]) {
static const char *response[] = {
"RFB 003.003\n",
"RFB 003.003\n",
"RFB 003.003\n",
"RFB 003.003\n",
"RFB 003.003\n",
"RFB 003.003\n",
"RFB 003.003\n",
"RFB 003.007\n",
"RFB 003.008\n",
"RFB 003.008\n",
};
unsigned version = pstate->sub.vnc.version % 10;
tcp_transmit(more, response[version], 12, 0);
if (version < 7)
/* Version 3.3: the server selects either "none" or
* "vnc challenge/response" and informs us which one
* to use */
state = RFB3_3_SECURITYTYPES;
else {
/* Version 3.7 onwards: the server will send us a list
* of security types it supports, from which the
* client will select one */
state = RFB3_7_SECURITYTYPES;
}
} else {
state = 0xFFFFFFFF;
tcp_close(more);
}
break;
case RFB3_3_SECURITYTYPES:
case RFB_SECURITYERROR:
case RFB_SECURITYRESULT:
case RFB_SERVERINIT+20:
pstate->sub.vnc.sectype = px[i];
state++;
break;
case RFB3_3_SECURITYTYPES+1:
case RFB3_3_SECURITYTYPES+2:
case RFB_SECURITYERROR+1:
case RFB_SECURITYERROR+2:
case RFB_SECURITYRESULT+1:
case RFB_SECURITYRESULT+2:
case RFB_SERVERINIT+21:
case RFB_SERVERINIT+22:
pstate->sub.vnc.sectype <<= 8;
pstate->sub.vnc.sectype |= px[i];
state++;
break;
case RFB3_3_SECURITYTYPES+3:
pstate->sub.vnc.sectype <<= 8;
pstate->sub.vnc.sectype |= px[i];
banout_append(banout, PROTO_VNC_RFB, " auth=[", AUTO_LEN);
vnc_append_sectype(banout, pstate->sub.vnc.sectype);
banout_append(banout, PROTO_VNC_RFB, "]", AUTO_LEN);
if (pstate->sub.vnc.sectype == 0)
state = RFB_SECURITYERROR;
else if (pstate->sub.vnc.sectype == 1) {
/* v3.3 sectype=none
* We move immediately to ClientInit stage */
tcp_transmit(more, "\x01", 1, 0);
state = RFB_SERVERINIT;
} else {
state = RFB_DONE;
tcp_close(more);
}
break;
case RFB_SECURITYRESULT+3:
pstate->sub.vnc.sectype <<= 8;
pstate->sub.vnc.sectype |= px[i];
if (pstate->sub.vnc.sectype == 0) {
/* security OK, move to client init */
tcp_transmit(more, "\x01", 1, 0);
state = RFB_SERVERINIT;
} else {
/* error occurred, so grab error message */
state = RFB_SECURITYERROR;
}
break;
case RFB_SECURITYERROR+3:
pstate->sub.vnc.sectype <<= 8;
pstate->sub.vnc.sectype = px[i];
banout_append(banout, PROTO_VNC_RFB, " ERROR=", AUTO_LEN);
state++;
break;
case RFB_SECURITYERROR+4:
if (pstate->sub.vnc.sectype == 0) {
state = RFB_DONE;
tcp_close(more);
} else {
pstate->sub.vnc.sectype--;
banout_append_char(banout, PROTO_VNC_RFB, px[i]);
}
break;
case RFB3_7_SECURITYTYPES:
pstate->sub.vnc.len = px[i];
if (pstate->sub.vnc.len == 0)
state = RFB_SECURITYERROR;
else {
state++;
banout_append(banout, PROTO_VNC_RFB, " auth=[", AUTO_LEN);
}
break;
case RFB3_7_SECURITYTYPES+1:
if (pstate->sub.vnc.len != 0) {
pstate->sub.vnc.len--;
vnc_append_sectype(banout, px[i]);
}
if (pstate->sub.vnc.len == 0) {
banout_append(banout, PROTO_VNC_RFB, "]", AUTO_LEN);
if (pstate->sub.vnc.version < 7) {
state = RFB_SERVERINIT;
tcp_transmit(more, "\x01", 1, 0);
} else if (pstate->sub.vnc.version == 7) {
state = RFB_SERVERINIT;
tcp_transmit(more, "\x01\x01", 2, 0);
} else {
state = RFB_SECURITYRESULT;
tcp_transmit(more, "\x01", 1, 0);
}
} else {
banout_append(banout, PROTO_VNC_RFB, "/", AUTO_LEN);
}
break;
case RFB_SERVERINIT:
pstate->sub.vnc.width = px[i];
state++;
break;
case RFB_SERVERINIT+1:
pstate->sub.vnc.width <<= 8;
pstate->sub.vnc.width |= px[i];
sprintf_s(foo, sizeof(foo), " width=%u", pstate->sub.vnc.width);
banout_append(banout, PROTO_VNC_RFB, foo, AUTO_LEN);
state++;
break;
case RFB_SERVERINIT+2:
pstate->sub.vnc.height = px[i];
state++;
break;
case RFB_SERVERINIT+3:
pstate->sub.vnc.height <<= 8;
pstate->sub.vnc.height |= px[i];
sprintf_s(foo, sizeof(foo), " height=%u", pstate->sub.vnc.height);
banout_append(banout, PROTO_VNC_RFB, foo, AUTO_LEN);
state++;
break;
case RFB_SERVERINIT+ 4:
case RFB_SERVERINIT+ 5:
case RFB_SERVERINIT+ 6:
case RFB_SERVERINIT+ 7:
case RFB_SERVERINIT+ 8:
case RFB_SERVERINIT+ 9:
case RFB_SERVERINIT+10:
case RFB_SERVERINIT+11:
case RFB_SERVERINIT+12:
case RFB_SERVERINIT+13:
case RFB_SERVERINIT+14:
case RFB_SERVERINIT+15:
case RFB_SERVERINIT+16:
case RFB_SERVERINIT+17:
case RFB_SERVERINIT+18:
case RFB_SERVERINIT+19:
state++;
break;
case RFB_SERVERINIT+23:
pstate->sub.vnc.sectype <<= 8;
pstate->sub.vnc.sectype |= px[i];
state++;
if (pstate->sub.vnc.sectype) {
banout_append(banout, PROTO_VNC_RFB, " name=[", AUTO_LEN);
} else {
state = RFB_DONE;
tcp_close(more);
}
break;
case RFB_SERVERINIT+24:
pstate->sub.vnc.sectype--;
banout_append_char(banout, PROTO_VNC_RFB, px[i]);
if (pstate->sub.vnc.sectype == 0) {
banout_append(banout, PROTO_VNC_RFB, "]", AUTO_LEN);
state = RFB_DONE;
tcp_close(more);
}
break;
case RFB_DONE:
tcp_close(more);
i = (unsigned)length;
break;
default:
i = (unsigned)length;
break;
}
pstate->state = state;
}
/***************************************************************************
***************************************************************************/
static void *
vnc_init(struct Banner1 *banner1)
{
UNUSEDPARM(banner1);
return 0;
}
/***************************************************************************
***************************************************************************/
static int
vnc_selftest(void)
{
return 0;
}
/***************************************************************************
***************************************************************************/
const struct ProtocolParserStream banner_vnc = {
"vnc", 5900, 0, 0, 0,
vnc_selftest,
vnc_init,
vnc_parse,
};