Commit 75a51504 authored by Robert David Graham's avatar Robert David Graham
Browse files

SMTP STARTTLS

parent 52224c06
Loading
Loading
Loading
Loading
+10 −2
Changes for src/proto-banner1.c: 10 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include "proto-ssl.h"
#include "proto-ssh.h"
#include "proto-ftp.h"
#include "proto-smtp.h"
#include "proto-vnc.h"
#include "masscan-app.h"
#include <ctype.h>
@@ -90,7 +91,7 @@ banner1_parse(
             * pattern */
            switch (proto) {
            case PROTO_FTP:
                if (patterns[x].extra == 0) {
                if (patterns[x].extra == 1) {
                    if (tcb_state->port == 25 || tcb_state->port == 587)
                        proto = PROTO_SMTP;
                }
@@ -136,10 +137,17 @@ banner1_parse(
                             banout,
                             more);
            break;
    case PROTO_SMTP:
            banner_smtp.parse(   banner1,
                             banner1->http_fields,
                             tcb_state,
                             px, length,
                             banout,
                             more);
            break;
            
    case PROTO_SSH1:
    case PROTO_SSH2:
    case PROTO_SMTP:
    case PROTO_POP3:
    case PROTO_IMAP4:
        /* generic text-based parser
+7
Changes for src/proto-banner1.h: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -98,6 +98,12 @@ struct FTPSTUFF {
    unsigned is_last:1;
};


struct SMTPSTUFF {
    unsigned code;
    unsigned is_last:1;
};

struct ProtocolState {
    unsigned state;
    unsigned remaining;
@@ -111,6 +117,7 @@ struct ProtocolState {
        struct SSLRECORD ssl;
        struct VNCSTUFF vnc;
        struct FTPSTUFF ftp;
        struct SMTPSTUFF smtp;
    } sub;
};

+172
Changes for src/proto-smtp.c: 172 added lines, 0 removed lines.
Original line number Diff line number Diff line
#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 = port;
                        state = 0;
                        
                        more->payload = banner_ssl.hello;
                        more->length = 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,
};
+7
Changes for src/proto-smtp.h: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
#ifndef PROTO_SMTP_H
#define PROTO_SMTP_H
#include "proto-banner1.h"

extern const struct ProtocolParserStream banner_smtp;

#endif
+6
Changes for xcode4/masscan.xcodeproj/project.pbxproj: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
		11126597197A086B00DC5987 /* out-unicornscan.c in Sources */ = {isa = PBXBuildFile; fileRef = 11126596197A086B00DC5987 /* out-unicornscan.c */; };
		11420DD319A2D47A00DB5BFE /* proto-vnc.c in Sources */ = {isa = PBXBuildFile; fileRef = 11420DD219A2D47A00DB5BFE /* proto-vnc.c */; };
		11420DD819A8160500DB5BFE /* proto-ftp.c in Sources */ = {isa = PBXBuildFile; fileRef = 11420DD719A8160500DB5BFE /* proto-ftp.c */; };
		11420DDB19A84A9F00DB5BFE /* proto-smtp.c in Sources */ = {isa = PBXBuildFile; fileRef = 11420DDA19A84A9E00DB5BFE /* proto-smtp.c */; };
		115C0CAB18035BC5004E6CD7 /* proto-netbios.c in Sources */ = {isa = PBXBuildFile; fileRef = 115C0CA518035BC5004E6CD7 /* proto-netbios.c */; };
		115C0CAC18035BC5004E6CD7 /* proto-ssl.c in Sources */ = {isa = PBXBuildFile; fileRef = 115C0CA718035BC5004E6CD7 /* proto-ssl.c */; };
		11623F6A191E0DB00075EEE6 /* out-certs.c in Sources */ = {isa = PBXBuildFile; fileRef = 11623F69191E0DB00075EEE6 /* out-certs.c */; };
@@ -102,6 +103,8 @@
		11420DD519A2D48C00DB5BFE /* proto-vnc.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "proto-vnc.h"; sourceTree = "<group>"; };
		11420DD619A815CD00DB5BFE /* proto-ftp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "proto-ftp.h"; sourceTree = "<group>"; };
		11420DD719A8160500DB5BFE /* proto-ftp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "proto-ftp.c"; sourceTree = "<group>"; };
		11420DD919A844B000DB5BFE /* proto-smtp.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "proto-smtp.h"; sourceTree = "<group>"; };
		11420DDA19A84A9E00DB5BFE /* proto-smtp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "proto-smtp.c"; sourceTree = "<group>"; };
		115C0CA318035BC5004E6CD7 /* out-record.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "out-record.h"; sourceTree = "<group>"; };
		115C0CA418035BC5004E6CD7 /* proto-dns-parse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "proto-dns-parse.h"; sourceTree = "<group>"; };
		115C0CA518035BC5004E6CD7 /* proto-netbios.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "proto-netbios.c"; sourceTree = "<group>"; };
@@ -407,6 +410,8 @@
				11420DD519A2D48C00DB5BFE /* proto-vnc.h */,
				11420DD619A815CD00DB5BFE /* proto-ftp.h */,
				11420DD719A8160500DB5BFE /* proto-ftp.c */,
				11420DD919A844B000DB5BFE /* proto-smtp.h */,
				11420DDA19A84A9E00DB5BFE /* proto-smtp.c */,
			);
			name = src;
			path = ../src;
@@ -536,6 +541,7 @@
				11126597197A086B00DC5987 /* out-unicornscan.c in Sources */,
				11420DD319A2D47A00DB5BFE /* proto-vnc.c in Sources */,
				11420DD819A8160500DB5BFE /* proto-ftp.c in Sources */,
				11420DDB19A84A9F00DB5BFE /* proto-smtp.c in Sources */,
			);
			runOnlyForDeploymentPostprocessing = 0;
		};