forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto-ftp.c
147 lines (133 loc) · 4.6 KB
/
proto-ftp.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
#include "proto-ftp.h"
#include "proto-banner1.h"
#include "unusedparm.h"
#include "masscan-app.h"
#include "proto-interactive.h"
#include "proto-ssl.h"
#include <ctype.h>
#include <string.h>
/***************************************************************************
***************************************************************************/
static void
ftp_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;
struct FTPSTUFF *ftp = &pstate->sub.ftp;
UNUSEDPARM(banner1_private);
UNUSEDPARM(banner1);
for (i=0; i<length; i++) {
switch (state) {
case 0:
case 100:
ftp->code = 0;
state++;
/* fall through */
case 1:
case 2:
case 3:
case 101:
case 102:
case 103:
if (!isdigit(px[i]&0xFF)) {
state = STATE_DONE;
} else {
ftp->code *= 10;
ftp->code += (px[i] - '0');
state++;
banout_append_char(banout, PROTO_FTP, px[i]);
}
break;
case 4:
case 104:
if (px[i] == ' ') {
ftp->is_last = 1;
state++;
banout_append_char(banout, PROTO_FTP, px[i]);
} else if (px[i] == '-') {
ftp->is_last = 0;
state++;
banout_append_char(banout, PROTO_FTP, px[i]);
} else {
state = STATE_DONE;
}
break;
case 5:
if (px[i] == '\r')
continue;
else if (px[i] == '\n') {
if (ftp->is_last) {
more->payload = "AUTH TLS\r\n";
more->length = 10;
state = 100;
banout_append_char(banout, PROTO_FTP, px[i]);
} else {
banout_append_char(banout, PROTO_FTP, px[i]);
state = 0;
}
} else if (px[i] == '\0' || !isprint(px[i])) {
state = STATE_DONE;
continue;
} else {
banout_append_char(banout, PROTO_FTP, px[i]);
}
break;
case 105:
if (px[i] == '\r')
continue;
else if (px[i] == '\n') {
if (ftp->code == 234) {
/* change the state here to SSL */
unsigned port = pstate->port;
memset(pstate, 0, sizeof(*pstate));
pstate->app_proto = PROTO_SSL3;
pstate->is_sent_sslhello = 1;
pstate->port = (unsigned short)port;
state = 0;
more->payload = banner_ssl.hello;
more->length = (unsigned)banner_ssl.hello_length;
} else {
state = STATE_DONE;
}
} else if (px[i] == '\0' || !isprint(px[i])) {
state = STATE_DONE;
continue;
} else {
banout_append_char(banout, PROTO_FTP, px[i]);
}
break;
default:
i = (unsigned)length;
break;
}
}
pstate->state = state;
}
/***************************************************************************
***************************************************************************/
static void *
ftp_init(struct Banner1 *banner1)
{
UNUSEDPARM(banner1);
return 0;
}
/***************************************************************************
***************************************************************************/
static int
ftp_selftest(void)
{
return 0;
}
/***************************************************************************
***************************************************************************/
const struct ProtocolParserStream banner_ftp = {
"ftp", 21, 0, 0, 0,
ftp_selftest,
ftp_init,
ftp_parse,
};