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
|
/*
ettercap -- dissector IMAP -- TCP 143 220
Copyright (C) ALoR & NaGA
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* The authentication schema can be found here:
*
* ftp://ftp.rfc-editor.org/in-notes/rfc1730.txt
* ftp://ftp.rfc-editor.org/in-notes/rfc1731.txt
*
* we currently support:
* - LOGIN
* - AUTHENTICATE LOGIN
*/
#include <ec.h>
#include <ec_decode.h>
#include <ec_dissect.h>
#include <ec_session.h>
#include <ec_sslwrap.h>
/* protos */
FUNC_DECODER(dissector_imap);
void imap_init(void);
/************************************************/
/*
* this function is the initializer.
* it adds the entry in the table of registered decoder
*/
void __init imap_init(void)
{
dissect_add("imap", APP_LAYER_TCP, 143, dissector_imap);
dissect_add("imap", APP_LAYER_TCP, 220, dissector_imap);
sslw_dissect_add("imaps", 993, dissector_imap, SSL_ENABLED);
}
FUNC_DECODER(dissector_imap)
{
DECLARE_DISP_PTR_END(ptr, end);
struct ec_session *s = NULL;
void *ident = NULL;
char tmp[MAX_ASCII_ADDR_LEN];
/* don't complain about unused var */
(void) DECODE_DATA;
(void) DECODE_DATALEN;
(void) DECODED_LEN;
/* the connection is starting... create the session */
CREATE_SESSION_ON_SYN_ACK("imap", s, dissector_imap);
/* create the session even if we are into an ssl tunnel */
CREATE_SESSION_ON_SYN_ACK("imaps", s, dissector_imap);
/* check if it is the first packet sent by the server */
IF_FIRST_PACKET_FROM_SERVER_SSL("smtp", "ssmtp", s, ident, dissector_imap) {
DEBUG_MSG("\tdissector_imap BANNER");
/*
* get the banner
* "* OK banner"
*/
/* skip the number, go to response */
while(*ptr != ' ' && ptr != end) ptr++;
/* reached the end */
if (ptr == end) return NULL;
if (!strncmp((const char*)ptr, " OK ", 4)) {
PACKET->DISSECTOR.banner = strdup((const char*)ptr + 3);
/* remove the \r\n */
if ( (ptr = (u_char*)strchr(PACKET->DISSECTOR.banner, '\r')) != NULL )
*ptr = '\0';
}
} ENDIF_FIRST_PACKET_FROM_SERVER(s, ident)
/* skip messages coming from the server */
if (FROM_SERVER("imap", PACKET) || FROM_SERVER("imaps", PACKET))
return NULL;
/* skip empty packets (ACK packets) */
if (PACKET->DATA.len == 0)
return NULL;
DEBUG_MSG("IMAP --> TCP dissector_imap");
/* skip the number, move to the command
* if there is no space in the line, we are
* probably already transferring credentials
*/
if (strchr((char*)ptr, ' ')) {
while(*ptr != ' ' && ptr != end) ptr++;
}
/* reached the end */
if (ptr == end) return NULL;
/*
* LOGIN authentication:
*
* n LOGIN user pass
*/
if ( !strncasecmp((const char*)ptr, " LOGIN ", 7) ) {
DEBUG_MSG("\tDissector_imap LOGIN");
ptr += 7;
PACKET->DISSECTOR.user = strdup((const char*)ptr);
/* split the string */
if ( (ptr = (u_char*)strchr(PACKET->DISSECTOR.user, ' ')) != NULL )
*ptr = '\0';
else {
SAFE_FREE(PACKET->DISSECTOR.user);
return NULL;
}
/* save the second part */
PACKET->DISSECTOR.pass = strdup((const char*)ptr + 1);
if ( (ptr = (u_char*)strchr(PACKET->DISSECTOR.pass, '\r')) != NULL )
*ptr = '\0';
/* print the message */
DISSECT_MSG("IMAP : %s:%d -> USER: %s PASS: %s\n", ip_addr_ntoa(&PACKET->L3.dst, tmp),
ntohs(PACKET->L4.dst),
PACKET->DISSECTOR.user,
PACKET->DISSECTOR.pass);
return NULL;
}
/*
* AUTHENTICATE LOGIN
*
* digest(user)
* digest(pass)
*
* the digests are in base64
*/
if ( !strncasecmp((const char*)ptr, " AUTHENTICATE LOGIN", 19) ) {
DEBUG_MSG("\tDissector_imap AUTHENTICATE LOGIN");
/* destroy any previous session */
dissect_wipe_session(PACKET, DISSECT_CODE(dissector_imap));
/* create the new session */
dissect_create_session(&s, PACKET, DISSECT_CODE(dissector_imap));
/* remember the state (used later) */
s->data = strdup("AUTH");
/* save the session */
session_put(s);
/* username is in the next packet */
return NULL;
}
/*
* AUTHENTICATE PLAIN
*
* digest(authcid+\0+user+\0+pass)
*
* the digest is in base64
*
* we ignore the authzid (authorization identity) for now
*/
if ( !strncasecmp((const char*)ptr, " AUTHENTICATE PLAIN", 19) ) {
DEBUG_MSG("\tDissector_imap AUTHENTICATE PLAIN");
/* destroy any previous session */
dissect_wipe_session(PACKET, DISSECT_CODE(dissector_imap));
/* create the new session */
dissect_create_session(&s, PACKET, DISSECT_CODE(dissector_imap));
/* remember the state (used later) */
s->data = strdup("PLAIN");
/* save the session */
session_put(s);
/* username is in the next packet */
return NULL;
}
/* search the session (if it exist) */
dissect_create_ident(&ident, PACKET, DISSECT_CODE(dissector_imap));
if (session_get(&s, ident, DISSECT_IDENT_LEN) == -E_NOTFOUND) {
SAFE_FREE(ident);
return NULL;
}
SAFE_FREE(ident);
/* the session is invalid */
if (s->data == NULL) {
dissect_wipe_session(PACKET, DISSECT_CODE(dissector_imap));
return NULL;
}
if (!strcmp(s->data, "AUTH")) {
char *user;
int i;
DEBUG_MSG("\tDissector_imap AUTHENTICATE LOGIN USER");
/* username is encoded in base64 */
i = base64decode((const char*)ptr, &user);
SAFE_FREE(s->data);
/* store the username in the session */
SAFE_CALLOC(s->data, strlen("AUTH USER ") + i + 1, sizeof(char) );
snprintf(s->data, strlen("AUTH USER ") + i + 1, "AUTH USER %s", user);
SAFE_FREE(user);
/* pass is in the next packet */
return NULL;
}
if (!strncmp(s->data, "AUTH USER", 9)) {
char *pass;
DEBUG_MSG("\tDissector_imap AUTHENTICATE LOGIN PASS");
//SAFE_CALLOC(pass, strlen((const char*)ptr), sizeof(char));
/* password is encoded in base64 */
base64decode((const char *)ptr, &pass);
/* fill the structure */
PACKET->DISSECTOR.user = strdup(s->data + strlen("AUTH USER "));
PACKET->DISSECTOR.pass = strdup(pass);
SAFE_FREE(pass);
/* destroy the session */
dissect_wipe_session(PACKET, DISSECT_CODE(dissector_imap));
/* print the message */
DISSECT_MSG("IMAP : %s:%d -> USER: %s PASS: %s\n", ip_addr_ntoa(&PACKET->L3.dst, tmp),
ntohs(PACKET->L4.dst),
PACKET->DISSECTOR.user,
PACKET->DISSECTOR.pass);
return NULL;
}
if (!strcmp(s->data, "PLAIN")) {
char *cred;
char *cred_end;
char *p;
int i;
DEBUG_MSG("\tDissector_imap AUTHENTICATE PLAIN USER/PASS");
/* password is encoded in base64 */
i = base64decode((const char *)ptr, &cred);
p = cred;
cred_end = cred+i;
if (p > cred_end) {
SAFE_FREE(cred);
dissect_wipe_session(PACKET, DISSECT_CODE(dissector_imap));
return NULL;
}
/* move to the username right after the first \0 */
while(*p && p!=cred_end) p++;
if (p!=cred_end) p++;
/* fill the structure */
PACKET->DISSECTOR.user = strdup(p);
/* now find the password right after the first \0 in cred */
while(*p && p!=cred_end) p++;
if (p!=cred_end) p++;
PACKET->DISSECTOR.pass = strdup(p);
SAFE_FREE(cred);
/* destroy the session */
dissect_wipe_session(PACKET, DISSECT_CODE(dissector_imap));
/* print the message */
DISSECT_MSG("IMAP : %s:%d -> USER: %s PASS: %s\n", ip_addr_ntoa(&PACKET->L3.dst, tmp),
ntohs(PACKET->L4.dst),
PACKET->DISSECTOR.user,
PACKET->DISSECTOR.pass);
return NULL;
}
return NULL;
}
/* EOF */
// vim:ts=3:expandtab
|