mirror of
https://github.com/zeek/zeek.git
synced 2025-10-10 02:28:21 +00:00

This is a larger internal change that moves the analyzer infrastructure to a more flexible model where the available analyzers don't need to be hardcoded at compile time anymore. While currently they actually still are, this will in the future enable external analyzer plugins. For now, it does already add the capability to dynamically enable/disable analyzers from script-land, replacing the old Analyzer::Available() methods. There are three major parts going into this: - A new plugin infrastructure in src/plugin. This is independent of analyzers and will eventually support plugins for other parts of Bro as well (think: readers and writers). The goal is that plugins can be alternatively compiled in statically or loadead dynamically at runtime from a shared library. While the latter isn't there yet, there'll be almost no code change for a plugin to make it dynamic later (hopefully :) - New analyzer infrastructure in src/analyzer. I've moved a number of analyzer-related classes here, including Analyzer and DPM; the latter now renamed to Analyzer::Manager. More will move here later. Currently, there's only one plugin here, which provides *all* existing analyzers. We can modularize this further in the future (or not). - A new script interface in base/framework/analyzer. I think that this will eventually replace the dpm framework, but for now that's still there as well, though some parts have moved over. I've also remove the dpd_config table; ports are now configured via the analyzer framework. For exmaple, for SSH: const ports = { 22/tcp } &redef; event bro_init() &priority=5 { ... Analyzer::register_for_ports(Analyzer::ANALYZER_SSH, ports); } As you can see, the old ANALYZER_SSH constants have more into an enum in the Analyzer namespace. This is all hardly tested right now, and not everything works yet. There's also a lot more cleanup to do (moving more classes around; removing no longer used functionality; documenting script and C++ interfaces; regression tests). But it seems to generally work with a small trace at least. The debug stream "dpm" shows more about the loaded/enabled analyzers. A new option -N lists loaded plugins and what they provide (including those compiled in statically; i.e., right now it outputs all the analyzers). This is all not cast-in-stone yet, for some things we need to see if they make sense this way. Feedback welcome.
541 lines
11 KiB
C++
541 lines
11 KiB
C++
// See the file "COPYING" in the main distribution directory for copyright.
|
|
|
|
#include "config.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "NetVar.h"
|
|
#include "NetbiosSSN.h"
|
|
#include "Sessions.h"
|
|
#include "Event.h"
|
|
|
|
double netbios_ssn_session_timeout = 15.0;
|
|
|
|
#define MAKE_INT16(dest, src) dest = *src; dest <<=8; src++; dest |= *src; src++;
|
|
|
|
NetbiosSSN_RawMsgHdr::NetbiosSSN_RawMsgHdr(const u_char*& data, int& len)
|
|
{
|
|
type = *data; ++data, --len;
|
|
flags = *data; ++data, --len;
|
|
length = *data; ++data, --len;
|
|
|
|
length <<= 8;
|
|
length |= *data;
|
|
++data, --len;
|
|
}
|
|
|
|
NetbiosDGM_RawMsgHdr::NetbiosDGM_RawMsgHdr(const u_char*& data, int& len)
|
|
{
|
|
type = *data; ++data, --len;
|
|
flags = *data; ++data, --len;
|
|
|
|
MAKE_INT16(id, data); len -= 2;
|
|
|
|
srcip = *data++ << 24;
|
|
srcip |= *data++ << 16;
|
|
srcip |= *data++ << 8;
|
|
srcip |= *data++;
|
|
len -=4;
|
|
|
|
MAKE_INT16(srcport, data); len -= 2;
|
|
MAKE_INT16(length, data); len -= 2;
|
|
MAKE_INT16(offset, data);; len -= 2;
|
|
}
|
|
|
|
|
|
NetbiosSSN_Interpreter::NetbiosSSN_Interpreter(analyzer::Analyzer* arg_analyzer,
|
|
SMB_Session* arg_smb_session)
|
|
{
|
|
analyzer = arg_analyzer;
|
|
smb_session = arg_smb_session;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseMessage(unsigned int type, unsigned int flags,
|
|
const u_char* data, int len, int is_query)
|
|
{
|
|
if ( netbios_session_message )
|
|
{
|
|
val_list* vl = new val_list;
|
|
vl->append(analyzer->BuildConnVal());
|
|
vl->append(new Val(is_query, TYPE_BOOL));
|
|
vl->append(new Val(type, TYPE_COUNT));
|
|
vl->append(new Val(len, TYPE_COUNT));
|
|
analyzer->ConnectionEvent(netbios_session_message, vl);
|
|
}
|
|
|
|
switch ( type ) {
|
|
case NETBIOS_SSN_MSG:
|
|
return ParseSessionMsg(data, len, is_query);
|
|
|
|
case NETBIOS_SSN_REQ:
|
|
return ParseSessionReq(data, len, is_query);
|
|
|
|
case NETBIOS_SSN_POS_RESP:
|
|
return ParseSessionPosResp(data, len, is_query);
|
|
|
|
case NETBIOS_SSN_NEG_RESP:
|
|
return ParseSessionNegResp(data, len, is_query);
|
|
|
|
case NETBIOS_SSN_RETARG_RESP:
|
|
return ParseRetArgResp(data, len, is_query);
|
|
|
|
case NETBIOS_SSN_KEEP_ALIVE:
|
|
return ParseKeepAlive(data, len, is_query);
|
|
|
|
case NETBIOS_DGM_DIRECT_UNIQUE:
|
|
case NETBIOS_DGM_DIRECT_GROUP:
|
|
case NETBIOS_DGM_BROADCAST:
|
|
return ParseBroadcast(data, len, is_query);
|
|
|
|
case NETBIOS_DGM_ERROR:
|
|
case NETBIOS_DGG_QUERY_REQ:
|
|
case NETBIOS_DGM_POS_RESP:
|
|
case NETBIOS_DGM_NEG_RESP:
|
|
return ParseDatagram(data, len, is_query);
|
|
|
|
default:
|
|
analyzer->Weird(fmt("unknown_netbios_type: 0x%x", type));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseDatagram(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
if ( smb_session )
|
|
{
|
|
smb_session->Deliver(is_query, len, data);
|
|
return 0;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseBroadcast(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
// FIND THE NUL-TERMINATED NAME STRINGS HERE!
|
|
// Not sure what's in them, so we don't keep them currently.
|
|
|
|
BroString* srcname = new BroString((char*) data);
|
|
data += srcname->Len()+1;
|
|
len -= srcname->Len();
|
|
|
|
BroString* dstname = new BroString((char*) data);
|
|
data += dstname->Len()+1;
|
|
len -= dstname->Len();
|
|
|
|
if ( smb_session )
|
|
{
|
|
smb_session->Deliver(is_query, len, data);
|
|
return 0;
|
|
}
|
|
|
|
delete srcname;
|
|
delete dstname;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseMessageTCP(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
NetbiosSSN_RawMsgHdr hdr(data, len);
|
|
|
|
if ( hdr.length > unsigned(len) )
|
|
analyzer->Weird(fmt("excess_netbios_hdr_len (%d > %d)",
|
|
hdr.length, len));
|
|
|
|
else if ( hdr.length < unsigned(len) )
|
|
{
|
|
analyzer->Weird("deficit_netbios_hdr_len");
|
|
len = hdr.length;
|
|
}
|
|
|
|
return ParseMessage(hdr.type, hdr.flags, data, len, is_query);
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseMessageUDP(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
|
|
NetbiosDGM_RawMsgHdr hdr(data, len);
|
|
|
|
if ( unsigned(hdr.length-14) > unsigned(len) )
|
|
analyzer->Weird(fmt("excess_netbios_hdr_len (%d > %d)",
|
|
hdr.length, len));
|
|
|
|
else if ( hdr.length < unsigned(len) )
|
|
{
|
|
analyzer->Weird(fmt("deficit_netbios_hdr_len (%d < %d)",
|
|
hdr.length, len));
|
|
len = hdr.length;
|
|
}
|
|
|
|
return ParseMessage(hdr.type, hdr.flags, data, len, is_query);
|
|
}
|
|
|
|
|
|
int NetbiosSSN_Interpreter::ParseSessionMsg(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
if ( len < 4 || strncmp((const char*) data, "\xffSMB", 4) )
|
|
{
|
|
// This should be an event, too.
|
|
analyzer->Weird("netbios_raw_session_msg");
|
|
Event(netbios_session_raw_message, data, len, is_query);
|
|
return 0;
|
|
}
|
|
|
|
if ( smb_session )
|
|
{
|
|
smb_session->Deliver(is_query, len, data);
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
analyzer->Weird("no_smb_session_using_parsesambamsg");
|
|
data += 4;
|
|
len -= 4;
|
|
return ParseSambaMsg(data, len, is_query);
|
|
}
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseSambaMsg(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ConvertName(const u_char* name, int name_len,
|
|
u_char*& xname, int& xlen)
|
|
{
|
|
// Taken from tcpdump's smbutil.c.
|
|
|
|
xname = 0;
|
|
|
|
if ( name_len < 1 )
|
|
return 0;
|
|
|
|
int len = (*name++) / 2;
|
|
xlen = len;
|
|
|
|
if ( len > 30 || len < 1 || name_len < len )
|
|
return 0;
|
|
|
|
u_char* convert_name = new u_char[len + 1];
|
|
*convert_name = 0;
|
|
xname = convert_name;
|
|
|
|
while ( len-- )
|
|
{
|
|
if ( name[0] < 'A' || name[0] > 'P' ||
|
|
name[1] < 'A' || name[1] > 'P' )
|
|
{
|
|
*convert_name = 0;
|
|
return 0;
|
|
}
|
|
|
|
*convert_name = ((name[0] - 'A') << 4) + (name[1] - 'A');
|
|
name += 2;
|
|
++convert_name;
|
|
}
|
|
|
|
*convert_name = 0;
|
|
|
|
return 1;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseSessionReq(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
if ( ! is_query )
|
|
analyzer->Weird("netbios_server_session_request");
|
|
|
|
u_char* xname;
|
|
int xlen;
|
|
|
|
if ( ConvertName(data, len, xname, xlen) )
|
|
Event(netbios_session_request, xname, xlen);
|
|
|
|
delete xname;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseSessionPosResp(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
if ( is_query )
|
|
analyzer->Weird("netbios_client_session_reply");
|
|
|
|
Event(netbios_session_accepted, data, len);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseSessionNegResp(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
if ( is_query )
|
|
analyzer->Weird("netbios_client_session_reply");
|
|
|
|
Event(netbios_session_rejected, data, len);
|
|
|
|
#if 0
|
|
case 0x80:
|
|
printf("Not listening on called name\n");
|
|
break;
|
|
case 0x81:
|
|
printf("Not listening for calling name\n");
|
|
break;
|
|
case 0x82:
|
|
printf("Called name not present\n");
|
|
break;
|
|
case 0x83:
|
|
printf("Called name present, but insufficient resources\n");
|
|
break;
|
|
default:
|
|
printf("Unspecified error 0x%X\n",ecode);
|
|
break;
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseRetArgResp(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
if ( is_query )
|
|
analyzer->Weird("netbios_client_session_reply");
|
|
|
|
Event(netbios_session_ret_arg_resp, data, len);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int NetbiosSSN_Interpreter::ParseKeepAlive(const u_char* data, int len,
|
|
int is_query)
|
|
{
|
|
Event(netbios_session_keepalive, data, len);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void NetbiosSSN_Interpreter::Event(EventHandlerPtr event, const u_char* data,
|
|
int len, int is_orig)
|
|
{
|
|
if ( ! event )
|
|
return;
|
|
|
|
val_list* vl = new val_list;
|
|
vl->append(analyzer->BuildConnVal());
|
|
if ( is_orig >= 0 )
|
|
vl->append(new Val(is_orig, TYPE_BOOL));
|
|
vl->append(new StringVal(new BroString(data, len, 0)));
|
|
|
|
analyzer->ConnectionEvent(event, vl);
|
|
}
|
|
|
|
|
|
Contents_NetbiosSSN::Contents_NetbiosSSN(Connection* conn, bool orig,
|
|
NetbiosSSN_Interpreter* arg_interp)
|
|
: TCP_SupportAnalyzer("CONTENTS_NETBIOSSSN", conn, orig)
|
|
{
|
|
interp = arg_interp;
|
|
type = flags = msg_size = 0;
|
|
msg_buf = 0;
|
|
buf_n = msg_size = 0;
|
|
state = NETBIOS_SSN_TYPE;
|
|
}
|
|
|
|
Contents_NetbiosSSN::~Contents_NetbiosSSN()
|
|
{
|
|
delete [] msg_buf;
|
|
}
|
|
|
|
void Contents_NetbiosSSN::Flush()
|
|
{
|
|
if ( buf_n > 0 )
|
|
{ // Deliver partial message.
|
|
interp->ParseMessage(type, flags, msg_buf, buf_n, IsOrig());
|
|
msg_size = 0;
|
|
}
|
|
}
|
|
|
|
void Contents_NetbiosSSN::DeliverStream(int len, const u_char* data, bool orig)
|
|
{
|
|
TCP_SupportAnalyzer::DeliverStream(len, data, orig);
|
|
|
|
if ( state == NETBIOS_SSN_TYPE )
|
|
{
|
|
type = *data;
|
|
state = NETBIOS_SSN_FLAGS;
|
|
|
|
++data;
|
|
--len;
|
|
|
|
if ( len == 0 )
|
|
return;
|
|
}
|
|
|
|
if ( state == NETBIOS_SSN_FLAGS )
|
|
{
|
|
flags = *data;
|
|
state = NETBIOS_SSN_LEN_HI;
|
|
|
|
++data;
|
|
--len;
|
|
|
|
if ( len == 0 )
|
|
return;
|
|
}
|
|
|
|
if ( state == NETBIOS_SSN_LEN_HI )
|
|
{
|
|
msg_size = (*data) << 8;
|
|
state = NETBIOS_SSN_LEN_LO;
|
|
|
|
++data;
|
|
--len;
|
|
|
|
if ( len == 0 )
|
|
return;
|
|
}
|
|
|
|
if ( state == NETBIOS_SSN_LEN_LO )
|
|
{
|
|
msg_size += *data;
|
|
state = NETBIOS_SSN_BUF;
|
|
|
|
buf_n = 0;
|
|
|
|
if ( msg_buf )
|
|
{
|
|
if ( buf_len < msg_size )
|
|
{
|
|
delete [] msg_buf;
|
|
buf_len = msg_size;
|
|
msg_buf = new u_char[buf_len];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
buf_len = msg_size;
|
|
if ( buf_len > 0 )
|
|
msg_buf = new u_char[buf_len];
|
|
}
|
|
|
|
++data;
|
|
--len;
|
|
|
|
if ( len == 0 && msg_size != 0 )
|
|
return;
|
|
}
|
|
|
|
if ( state != NETBIOS_SSN_BUF )
|
|
Conn()->Internal("state inconsistency in Contents_NetbiosSSN::Deliver");
|
|
|
|
int n;
|
|
for ( n = 0; buf_n < msg_size && n < len; ++n )
|
|
msg_buf[buf_n++] = data[n];
|
|
|
|
if ( buf_n < msg_size )
|
|
// Haven't filled up the message buffer yet, no more to do.
|
|
return;
|
|
|
|
(void) interp->ParseMessage(type, flags, msg_buf, msg_size, IsOrig());
|
|
buf_n = 0;
|
|
|
|
state = NETBIOS_SSN_TYPE;
|
|
|
|
if ( n < len )
|
|
// More data to munch on.
|
|
DeliverStream(len - n, data + n, orig);
|
|
}
|
|
|
|
NetbiosSSN_Analyzer::NetbiosSSN_Analyzer(Connection* conn)
|
|
: TCP_ApplicationAnalyzer("NETBIOS", conn)
|
|
{
|
|
smb_session = new SMB_Session(this);
|
|
interp = new NetbiosSSN_Interpreter(this, smb_session);
|
|
orig_netbios = resp_netbios = 0;
|
|
did_session_done = 0;
|
|
|
|
if ( Conn()->ConnTransport() == TRANSPORT_TCP )
|
|
{
|
|
orig_netbios = new Contents_NetbiosSSN(conn, true, interp);
|
|
resp_netbios = new Contents_NetbiosSSN(conn, false, interp);
|
|
AddSupportAnalyzer(orig_netbios);
|
|
AddSupportAnalyzer(resp_netbios);
|
|
}
|
|
else
|
|
{
|
|
ADD_ANALYZER_TIMER(&NetbiosSSN_Analyzer::ExpireTimer,
|
|
network_time + netbios_ssn_session_timeout, 1,
|
|
TIMER_NB_EXPIRE);
|
|
}
|
|
}
|
|
|
|
NetbiosSSN_Analyzer::~NetbiosSSN_Analyzer()
|
|
{
|
|
delete interp;
|
|
delete smb_session;
|
|
}
|
|
|
|
void NetbiosSSN_Analyzer::Done()
|
|
{
|
|
TCP_ApplicationAnalyzer::Done();
|
|
interp->Timeout();
|
|
|
|
if ( Conn()->ConnTransport() == TRANSPORT_UDP && ! did_session_done )
|
|
Event(udp_session_done);
|
|
else
|
|
interp->Timeout();
|
|
}
|
|
|
|
void NetbiosSSN_Analyzer::EndpointEOF(bool orig)
|
|
{
|
|
TCP_ApplicationAnalyzer::EndpointEOF(orig);
|
|
|
|
(orig ? orig_netbios : resp_netbios)->Flush();
|
|
}
|
|
|
|
void NetbiosSSN_Analyzer::ConnectionClosed(TCP_Endpoint* endpoint,
|
|
TCP_Endpoint* peer, int gen_event)
|
|
{
|
|
TCP_ApplicationAnalyzer::ConnectionClosed(endpoint, peer, gen_event);
|
|
|
|
// Question: Why do we flush *both* endpoints upon connection close?
|
|
// orig_netbios->Flush();
|
|
// resp_netbios->Flush();
|
|
}
|
|
|
|
void NetbiosSSN_Analyzer::DeliverPacket(int len, const u_char* data, bool orig,
|
|
int seq, const IP_Hdr* ip, int caplen)
|
|
{
|
|
TCP_ApplicationAnalyzer::DeliverPacket(len, data, orig, seq, ip, caplen);
|
|
|
|
if ( orig )
|
|
interp->ParseMessageUDP(data, len, 1);
|
|
else
|
|
interp->ParseMessageUDP(data, len, 0);
|
|
}
|
|
|
|
void NetbiosSSN_Analyzer::ExpireTimer(double t)
|
|
{
|
|
// The - 1.0 in the following is to allow 1 second for the
|
|
// common case of a single request followed by a single reply,
|
|
// so we don't needlessly set the timer twice in that case.
|
|
if ( terminating ||
|
|
network_time - Conn()->LastTime() >=
|
|
netbios_ssn_session_timeout - 1.0 )
|
|
{
|
|
Event(connection_timeout);
|
|
sessions->Remove(Conn());
|
|
}
|
|
else
|
|
ADD_ANALYZER_TIMER(&NetbiosSSN_Analyzer::ExpireTimer,
|
|
t + netbios_ssn_session_timeout,
|
|
1, TIMER_NB_EXPIRE);
|
|
}
|