forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto-smtp.c
193 lines (172 loc) · 6.28 KB
/
proto-smtp.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
/*
SMTP banner checker
This file interacts with an SMTP server when it finds a connection on
an SMTP port like 25 with a "220 " as the banner.
Firstly, SMTP requires that the client send a "EHLO" command in order to
announce its presence. This command will tell us about some optional
features of the server, which we'll record as part of the [smtp] banner.
Secondly, we'll attempt to do a STARTTLS command, regardless whether the
server advertised the capability. This should either get back an "OK"
message or an error, which we also record as part of the banner.
If we get an OK, then we switch the parser to SSL, and continue as if
this were an SSL connection. Any SSL data will show up as an [ssl] protocol
rather than an SMTP protocol.
*/
#include "proto-smtp.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
smtp_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 SMTPSTUFF *smtp = &pstate->sub.smtp;
UNUSEDPARM(banner1_private);
UNUSEDPARM(banner1);
for (i=0; i<length; i++) {
switch (state) {
case 0:
case 100:
case 200:
smtp->code = 0;
state++;
/* fall through */
case 1:
case 2:
case 3:
case 101:
case 102:
case 103:
case 201:
case 202:
case 203:
if (!isdigit(px[i]&0xFF)) {
state = STATE_DONE;
} else {
smtp->code *= 10;
smtp->code += (px[i] - '0');
state++;
banout_append_char(banout, PROTO_SMTP, px[i]);
}
break;
case 4:
case 104:
case 204:
if (px[i] == ' ') {
smtp->is_last = 1;
state++;
banout_append_char(banout, PROTO_SMTP, px[i]);
} else if (px[i] == '-') {
smtp->is_last = 0;
state++;
banout_append_char(banout, PROTO_SMTP, px[i]);
} else {
state = STATE_DONE;
}
break;
case 5:
if (px[i] == '\r')
continue;
else if (px[i] == '\n') {
if (smtp->is_last) {
more->payload = "EHLO masscan\r\n";
more->length = 14;
state = 100;
banout_append_char(banout, PROTO_SMTP, px[i]);
} else {
banout_append_char(banout, PROTO_SMTP, px[i]);
state = 0;
}
} else if (px[i] == '\0' || !isprint(px[i])) {
state = STATE_DONE;
continue;
} else {
banout_append_char(banout, PROTO_SMTP, px[i]);
}
break;
case 105:
if (px[i] == '\r')
continue;
else if (px[i] == '\n') {
if (smtp->is_last) {
more->payload = "STARTTLS\r\n";
more->length = 10;
state = 200;
banout_append_char(banout, PROTO_SMTP, px[i]);
} else {
banout_append_char(banout, PROTO_SMTP, px[i]);
state = 100;
}
} else if (px[i] == '\0' || !isprint(px[i])) {
state = STATE_DONE;
continue;
} else {
banout_append_char(banout, PROTO_SMTP, px[i]);
}
break;
case 205:
if (px[i] == '\r')
continue;
else if (px[i] == '\n') {
if (smtp->code == 220) {
/* 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_SMTP, px[i]);
}
break;
default:
i = (unsigned)length;
break;
}
}
pstate->state = state;
}
/***************************************************************************
***************************************************************************/
static void *
smtp_init(struct Banner1 *banner1)
{
UNUSEDPARM(banner1);
return 0;
}
/***************************************************************************
***************************************************************************/
static int
smtp_selftest(void)
{
return 0;
}
/***************************************************************************
***************************************************************************/
const struct ProtocolParserStream banner_smtp = {
"smtp", 21, 0, 0, 0,
smtp_selftest,
smtp_init,
smtp_parse,
};