Basic IMAP StartTLS analyzer.

Parses certificates out of imap connections using StartTLS. Aborts
processing if StartTLS is not found.
This commit is contained in:
Johanna Amann 2015-07-22 10:35:49 -07:00
parent 871b340ade
commit 4a5737708c
17 changed files with 331 additions and 0 deletions

View file

@ -0,0 +1,11 @@
include(BroPlugin)
include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
bro_plugin_begin(Bro IMAP)
bro_plugin_cc(Plugin.cc)
bro_plugin_cc(IMAP.cc)
bro_plugin_pac(imap.pac imap-analyzer.pac imap-protocol.pac)
bro_plugin_end()

View file

@ -0,0 +1,86 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include "IMAP.h"
#include "analyzer/protocol/tcp/TCP_Reassembler.h"
#include "analyzer/Manager.h"
using namespace analyzer::imap;
IMAP_Analyzer::IMAP_Analyzer(Connection* conn)
: tcp::TCP_ApplicationAnalyzer("IMAP", conn)
{
interp = new binpac::IMAP::IMAP_Conn(this);
had_gap = false;
tls_active = false;
}
IMAP_Analyzer::~IMAP_Analyzer()
{
delete interp;
}
void IMAP_Analyzer::Done()
{
tcp::TCP_ApplicationAnalyzer::Done();
interp->FlowEOF(true);
interp->FlowEOF(false);
}
void IMAP_Analyzer::EndpointEOF(bool is_orig)
{
tcp::TCP_ApplicationAnalyzer::EndpointEOF(is_orig);
interp->FlowEOF(is_orig);
}
void IMAP_Analyzer::DeliverStream(int len, const u_char* data, bool orig)
{
tcp::TCP_ApplicationAnalyzer::DeliverStream(len, data, orig);
if ( tls_active )
{
// If TLS has been initiated, forward to child and abort further
// processing
ForwardStream(len, data, orig);
return;
}
assert(TCP());
if ( TCP()->IsPartial() )
return;
if ( had_gap )
// If only one side had a content gap, we could still try to
// deliver data to the other side if the script layer can
// handle this.
return;
try
{
interp->NewData(orig, data, data + len);
}
catch ( const binpac::Exception& e )
{
ProtocolViolation(fmt("Binpac exception: %s", e.c_msg()));
}
}
void IMAP_Analyzer::Undelivered(uint64 seq, int len, bool orig)
{
tcp::TCP_ApplicationAnalyzer::Undelivered(seq, len, orig);
had_gap = true;
interp->NewGap(orig, len);
}
void IMAP_Analyzer::StartTLS()
{
// StartTLS was called. This means we saw a client starttls followed
// by a server proceed. From here on, everything should be a binary
// TLS datastream.
tls_active = true;
Analyzer* ssl = analyzer_mgr->InstantiateAnalyzer("SSL", Conn());
if ( ssl )
AddChildAnalyzer(ssl);
}

View file

@ -0,0 +1,38 @@
// See the file "COPYING" in the main distribution directory for copyright.
#ifndef ANALYZER_PROTOCOL_IMAP_IMAP_H
#define ANALYZER_PROTOCOL_IMAP_IMAP_H
#include "analyzer/protocol/tcp/TCP.h"
#include "imap_pac.h"
namespace analyzer { namespace imap {
class IMAP_Analyzer : public tcp::TCP_ApplicationAnalyzer {
public:
IMAP_Analyzer(Connection* conn);
virtual ~IMAP_Analyzer();
virtual void Done();
virtual void DeliverStream(int len, const u_char* data, bool orig);
virtual void Undelivered(uint64 seq, int len, bool orig);
// Overriden from tcp::TCP_ApplicationAnalyzer.
virtual void EndpointEOF(bool is_orig);
void StartTLS();
static analyzer::Analyzer* Instantiate(Connection* conn)
{ return new IMAP_Analyzer(conn); }
protected:
binpac::IMAP::IMAP_Conn* interp;
bool had_gap;
bool tls_active;
};
} } // namespace analyzer::*
#endif /* ANALYZER_PROTOCOL_IMAP_IMAP_H */

View file

@ -0,0 +1,26 @@
// See the file in the main distribution directory for copyright.
#include "plugin/Plugin.h"
#include "IMAP.h"
namespace plugin {
namespace Bro_IMAP {
class Plugin : public plugin::Plugin {
public:
plugin::Configuration Configure()
{
AddComponent(new ::analyzer::Component("IMAP", ::analyzer::imap::IMAP_Analyzer::Instantiate));
plugin::Configuration config;
config.name = "Bro::IMAP";
config.description = "IMAP analyzer StartTLS only";
return config;
}
} plugin;
}
}

View file

@ -0,0 +1,57 @@
refine connection IMAP_Conn += {
%member{
string client_starttls_id;
%}
%init{
%}
function proc_imap_token(is_orig: bool, tag: bytestring, command: bytestring): bool
%{
string commands = std_str(command);
std::transform(commands.begin(), commands.end(), commands.begin(), ::tolower);
string tags = std_str(tag);
//printf("imap %s %s\n", commands.c_str(), tags.c_str());
if ( !is_orig && tags == "*" && commands == "ok" )
bro_analyzer()->ProtocolConfirmation();
if ( is_orig && ( command == "capability" || commands == "starttls" ) )
bro_analyzer()->ProtocolConfirmation();
if ( command == "authenticate" || command == "login" || command == "examine" || command == "create" || command == "list" || command == "fetch" )
{
bro_analyzer()->ProtocolConfirmation();
// Handshake has passed the phase where we should see StartTLS. Simply skip from hereon...
bro_analyzer()->SetSkip(true);
return true;
}
if ( is_orig && commands == "starttls" )
{
if ( !client_starttls_id.empty() )
reporter->Weird(bro_analyzer()->Conn(), "IMAP: client sent duplicate StartTLS");
client_starttls_id = tags;
}
if ( !is_orig && !client_starttls_id.empty() && tags == client_starttls_id )
{
if ( commands == "ok" )
bro_analyzer()->StartTLS();
else
reporter->Weird(bro_analyzer()->Conn(), "IMAP: server refused StartTLS");
}
return true;
%}
};
refine typeattr IMAP_TOKEN += &let {
proc: bool = $context.connection.proc_imap_token(is_orig, tag, command);
};

View file

@ -0,0 +1,17 @@
type TAG = RE/[[:alnum:][:punct:]]+/;
type CONTENT = RE/[^\r\n]*/;
type SPACING = RE/[ ]+/;
type OPTIONALSPACING = RE/[ ]*/;
type NEWLINE = RE/[\r\n]+/;
type IMAP_PDU(is_orig: bool) = IMAP_TOKEN(is_orig)[] &until($input.length() == 0);
type IMAP_TOKEN(is_orig: bool) = record {
tag : TAG;
: SPACING;
command: TAG;
: OPTIONALSPACING;
tagcontent: CONTENT;
: NEWLINE;
};

View file

@ -0,0 +1,35 @@
# binpac file for the IMAP analyzer.
# Note that we currently do not even try to parse the protocol
# completely -- this is only supposed to be able to parse imap
# till StartTLS does (or does not) kick in.
%include binpac.pac
%include bro.pac
%extern{
namespace analyzer { namespace imap { class IMAP_Analyzer; } }
namespace binpac { namespace IMAP { class IMAP_Conn; } }
typedef analyzer::imap::IMAP_Analyzer* IMAPAnalyzer;
#include "IMAP.h"
%}
extern type IMAPAnalyzer;
analyzer IMAP withcontext {
connection: IMAP_Conn;
flow: IMAP_Flow;
};
connection IMAP_Conn(bro_analyzer: IMAPAnalyzer) {
upflow = IMAP_Flow(true);
downflow = IMAP_Flow(false);
};
%include imap-protocol.pac
flow IMAP_Flow(is_orig: bool) {
datagram = IMAP_PDU(is_orig) withcontext(connection, this);
};
%include imap-analyzer.pac