mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Basic IMAP StartTLS analyzer.
Parses certificates out of imap connections using StartTLS. Aborts processing if StartTLS is not found.
This commit is contained in:
parent
871b340ade
commit
4a5737708c
17 changed files with 331 additions and 0 deletions
|
@ -44,6 +44,7 @@
|
|||
@load base/protocols/dns
|
||||
@load base/protocols/ftp
|
||||
@load base/protocols/http
|
||||
@load base/protocols/imap
|
||||
@load base/protocols/irc
|
||||
@load base/protocols/krb
|
||||
@load base/protocols/modbus
|
||||
|
|
5
scripts/base/protocols/imap/README
Normal file
5
scripts/base/protocols/imap/README
Normal file
|
@ -0,0 +1,5 @@
|
|||
Support for the Internet Message Access Protocol (IMAP).
|
||||
|
||||
Note that currently the IMAP analyzer only supports analyzing IMAP sessions
|
||||
until they do or do not switch to TLS using StartTLS. Hence, we do not get
|
||||
mails from IMAP sessions, only X509 certificates.
|
2
scripts/base/protocols/imap/__load__.bro
Normal file
2
scripts/base/protocols/imap/__load__.bro
Normal file
|
@ -0,0 +1,2 @@
|
|||
@load ./main
|
||||
|
11
scripts/base/protocols/imap/main.bro
Normal file
11
scripts/base/protocols/imap/main.bro
Normal file
|
@ -0,0 +1,11 @@
|
|||
|
||||
module IMAP;
|
||||
|
||||
const ports = { 143/tcp };
|
||||
redef likely_server_ports += { ports };
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
Analyzer::register_for_ports(Analyzer::ANALYZER_IMAP, ports);
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ add_subdirectory(gtpv1)
|
|||
add_subdirectory(http)
|
||||
add_subdirectory(icmp)
|
||||
add_subdirectory(ident)
|
||||
add_subdirectory(imap)
|
||||
add_subdirectory(interconn)
|
||||
add_subdirectory(irc)
|
||||
add_subdirectory(krb)
|
||||
|
|
11
src/analyzer/protocol/imap/CMakeLists.txt
Normal file
11
src/analyzer/protocol/imap/CMakeLists.txt
Normal 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()
|
||||
|
86
src/analyzer/protocol/imap/IMAP.cc
Normal file
86
src/analyzer/protocol/imap/IMAP.cc
Normal 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);
|
||||
}
|
38
src/analyzer/protocol/imap/IMAP.h
Normal file
38
src/analyzer/protocol/imap/IMAP.h
Normal 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 */
|
26
src/analyzer/protocol/imap/Plugin.cc
Normal file
26
src/analyzer/protocol/imap/Plugin.cc
Normal 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;
|
||||
|
||||
}
|
||||
}
|
57
src/analyzer/protocol/imap/imap-analyzer.pac
Normal file
57
src/analyzer/protocol/imap/imap-analyzer.pac
Normal 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);
|
||||
};
|
||||
|
17
src/analyzer/protocol/imap/imap-protocol.pac
Normal file
17
src/analyzer/protocol/imap/imap-protocol.pac
Normal 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;
|
||||
};
|
||||
|
35
src/analyzer/protocol/imap/imap.pac
Normal file
35
src/analyzer/protocol/imap/imap.pac
Normal 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
|
|
@ -0,0 +1,10 @@
|
|||
#separator \x09
|
||||
#set_separator ,
|
||||
#empty_field (empty)
|
||||
#unset_field -
|
||||
#path conn
|
||||
#open 2015-07-22-17-31-02
|
||||
#fields ts uid id.orig_h id.orig_p id.resp_h id.resp_p proto service duration orig_bytes resp_bytes conn_state local_orig local_resp missed_bytes history orig_pkts orig_ip_bytes resp_pkts resp_ip_bytes tunnel_parents
|
||||
#types time string addr port addr port enum string interval count count string bool bool count string count count count count set[string]
|
||||
1437584567.812552 CXWv6p3arKYeMETxOg 192.168.17.53 49640 212.227.17.186 143 tcp ssl,imap 2.827002 540 5653 SF - - 0 ShAdDafFr 18 1284 14 6225 (empty)
|
||||
#close 2015-07-22-17-31-02
|
|
@ -0,0 +1,10 @@
|
|||
#separator \x09
|
||||
#set_separator ,
|
||||
#empty_field (empty)
|
||||
#unset_field -
|
||||
#path ssl
|
||||
#open 2015-07-22-17-31-02
|
||||
#fields ts uid id.orig_h id.orig_p id.resp_h id.resp_p version cipher curve server_name resumed last_alert next_protocol established cert_chain_fuids client_cert_chain_fuids subject issuer client_subject client_issuer
|
||||
#types time string addr port addr port string string string string bool string string bool vector[string] vector[string] string string string string
|
||||
1437584568.570497 CXWv6p3arKYeMETxOg 192.168.17.53 49640 212.227.17.186 143 TLSv12 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 secp256r1 - F - - T FOWmhO3rUj3SEB5RTb,FjH9n52SzEIJ9UoVK9,FisDHa396LIaZadgG9 (empty) CN=imap.gmx.net,emailAddress=server-certs@1und1.de,L=Montabaur,ST=Rhineland-Palatinate,O=1&1 Mail & Media GmbH,C=DE CN=TeleSec ServerPass DE-1,street=Untere Industriestr. 20,L=Netphen,postalCode=57250,ST=NRW,OU=T-Systems Trust Center,O=T-Systems International GmbH,C=DE - -
|
||||
#close 2015-07-22-17-31-02
|
|
@ -0,0 +1,12 @@
|
|||
#separator \x09
|
||||
#set_separator ,
|
||||
#empty_field (empty)
|
||||
#unset_field -
|
||||
#path x509
|
||||
#open 2015-07-22-17-31-02
|
||||
#fields ts id certificate.version certificate.serial certificate.subject certificate.issuer certificate.not_valid_before certificate.not_valid_after certificate.key_alg certificate.sig_alg certificate.key_type certificate.key_length certificate.exponent certificate.curve san.dns san.uri san.email san.ip basic_constraints.ca basic_constraints.path_len
|
||||
#types time string count string string string time time string string string count string string vector[string] vector[string] vector[string] vector[addr] bool count
|
||||
1437584568.769690 FOWmhO3rUj3SEB5RTb 3 339D9ED8E73927C9 CN=imap.gmx.net,emailAddress=server-certs@1und1.de,L=Montabaur,ST=Rhineland-Palatinate,O=1&1 Mail & Media GmbH,C=DE CN=TeleSec ServerPass DE-1,street=Untere Industriestr. 20,L=Netphen,postalCode=57250,ST=NRW,OU=T-Systems Trust Center,O=T-Systems International GmbH,C=DE 1384251451.000000 1479427199.000000 rsaEncryption sha1WithRSAEncryption rsa 2048 65537 - imap.gmx.net,imap.gmx.de - - - F -
|
||||
1437584568.769690 FjH9n52SzEIJ9UoVK9 3 21B6777E8CBD0EA8 CN=TeleSec ServerPass DE-1,street=Untere Industriestr. 20,L=Netphen,postalCode=57250,ST=NRW,OU=T-Systems Trust Center,O=T-Systems International GmbH,C=DE CN=Deutsche Telekom Root CA 2,OU=T-TeleSec Trust Center,O=Deutsche Telekom AG,C=DE 1362146309.000000 1562716740.000000 rsaEncryption sha1WithRSAEncryption rsa 2048 65537 - - - - - T 0
|
||||
1437584568.769690 FisDHa396LIaZadgG9 3 26 CN=Deutsche Telekom Root CA 2,OU=T-TeleSec Trust Center,O=Deutsche Telekom AG,C=DE CN=Deutsche Telekom Root CA 2,OU=T-TeleSec Trust Center,O=Deutsche Telekom AG,C=DE 931522260.000000 1562716740.000000 rsaEncryption sha1WithRSAEncryption rsa 2048 65537 - - - - - T 5
|
||||
#close 2015-07-22-17-31-02
|
BIN
testing/btest/Traces/tls/imap-starttls.pcap
Normal file
BIN
testing/btest/Traces/tls/imap-starttls.pcap
Normal file
Binary file not shown.
9
testing/btest/scripts/base/protocols/imap/starttls.test
Normal file
9
testing/btest/scripts/base/protocols/imap/starttls.test
Normal file
|
@ -0,0 +1,9 @@
|
|||
# @TEST-EXEC: bro -b -C -r $TRACES/tls/imap-starttls.pcap %INPUT
|
||||
# @TEST-EXEC: btest-diff conn.log
|
||||
# @TEST-EXEC: btest-diff ssl.log
|
||||
# @TEST-EXEC: btest-diff x509.log
|
||||
|
||||
@load base/protocols/ssl
|
||||
@load base/protocols/conn
|
||||
@load base/frameworks/dpd
|
||||
@load base/protocols/imap
|
Loading…
Add table
Add a link
Reference in a new issue