suricata
app-layer-detect-proto.c File Reference
#include "suricata-common.h"
#include "debug.h"
#include "decode.h"
#include "threads.h"
#include "threadvars.h"
#include "tm-threads.h"
#include "detect.h"
#include "detect-engine-port.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-content.h"
#include "detect-engine-mpm.h"
#include "detect-engine-state.h"
#include "util-print.h"
#include "util-pool.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "flow.h"
#include "flow-util.h"
#include "flow-private.h"
#include "stream-tcp-private.h"
#include "stream-tcp-reassemble.h"
#include "stream-tcp.h"
#include "stream.h"
#include "app-layer.h"
#include "app-layer-protos.h"
#include "app-layer-parser.h"
#include "app-layer-detect-proto.h"
#include "app-layer-expectation.h"
#include "conf.h"
#include "util-memcmp.h"
#include "util-spm.h"
#include "util-debug.h"
#include "runmodes.h"
#include "app-layer-htp.h"
Include dependency graph for app-layer-detect-proto.c:

Go to the source code of this file.

Data Structures

struct  AppLayerProtoDetectProbingParserElement_
 
struct  AppLayerProtoDetectProbingParserPort_
 
struct  AppLayerProtoDetectProbingParser_
 
struct  AppLayerProtoDetectPMSignature_
 
struct  AppLayerProtoDetectPMCtx_
 
struct  AppLayerProtoDetectCtxIpproto_
 
struct  AppLayerProtoDetectCtx_
 The app layer protocol detection context. More...
 
struct  AppLayerProtoDetectThreadCtx_
 The app layer protocol detection thread context. More...
 
struct  AppLayerProtoDetectPPTestDataElement_
 
struct  AppLayerProtoDetectPPTestDataPort_
 
struct  AppLayerProtoDetectPPTestDataIPProto_
 

Typedefs

typedef struct AppLayerProtoDetectProbingParserElement_ AppLayerProtoDetectProbingParserElement
 
typedef struct AppLayerProtoDetectProbingParserPort_ AppLayerProtoDetectProbingParserPort
 
typedef struct AppLayerProtoDetectProbingParser_ AppLayerProtoDetectProbingParser
 
typedef struct AppLayerProtoDetectPMSignature_ AppLayerProtoDetectPMSignature
 
typedef struct AppLayerProtoDetectPMCtx_ AppLayerProtoDetectPMCtx
 
typedef struct AppLayerProtoDetectCtxIpproto_ AppLayerProtoDetectCtxIpproto
 
typedef struct AppLayerProtoDetectCtx_ AppLayerProtoDetectCtx
 The app layer protocol detection context. More...
 
typedef struct AppLayerProtoDetectPPTestDataElement_ AppLayerProtoDetectPPTestDataElement
 
typedef struct AppLayerProtoDetectPPTestDataPort_ AppLayerProtoDetectPPTestDataPort
 
typedef struct AppLayerProtoDetectPPTestDataIPProto_ AppLayerProtoDetectPPTestDataIPProto
 

Functions

AppProto AppLayerProtoDetectGetProto (AppLayerProtoDetectThreadCtx *tctx, Flow *f, uint8_t *buf, uint32_t buflen, uint8_t ipproto, uint8_t direction, bool *reverse_flow)
 Returns the app layer protocol given a buffer. More...
 
int AppLayerProtoDetectPrepareState (void)
 Prepares the internal state for protocol detection. This needs to be called once all the patterns and probing parser ports have been registered. More...
 
void AppLayerProtoDetectPPRegister (uint8_t ipproto, const char *portstr, AppProto alproto, uint16_t min_depth, uint16_t max_depth, uint8_t direction, ProbingParserFPtr ProbingParser1, ProbingParserFPtr ProbingParser2)
 register parser at a port More...
 
int AppLayerProtoDetectPPParseConfPorts (const char *ipproto_name, uint8_t ipproto, const char *alproto_name, AppProto alproto, uint16_t min_depth, uint16_t max_depth, ProbingParserFPtr ProbingParserTs, ProbingParserFPtr ProbingParserTc)
 
int AppLayerProtoDetectPMRegisterPatternCS (uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
 Registers a case-sensitive pattern for protocol detection. More...
 
int AppLayerProtoDetectPMRegisterPatternCSwPP (uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction, ProbingParserFPtr PPFunc, uint16_t pp_min_depth, uint16_t pp_max_depth)
 
int AppLayerProtoDetectPMRegisterPatternCI (uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
 Registers a case-insensitive pattern for protocol detection. More...
 
int AppLayerProtoDetectSetup (void)
 The first function to be called. This initializes a global protocol detection context. More...
 
int AppLayerProtoDetectDeSetup (void)
 Cleans up the app layer protocol detection phase. More...
 
void AppLayerProtoDetectRegisterProtocol (AppProto alproto, const char *alproto_name)
 Registers a protocol for protocol detection phase. More...
 
void AppLayerRequestProtocolChange (Flow *f, uint16_t dp, AppProto expect_proto)
 request applayer to wrap up this protocol and rerun protocol detection. More...
 
void AppLayerRequestProtocolTLSUpgrade (Flow *f)
 request applayer to wrap up this protocol and rerun protocol detection with expectation of TLS. Used by STARTTLS. More...
 
void AppLayerProtoDetectReset (Flow *f)
 Reset proto detect for flow. More...
 
int AppLayerProtoDetectConfProtoDetectionEnabled (const char *ipproto, const char *alproto)
 Given a protocol name, checks if proto detection is enabled in the conf file. More...
 
AppLayerProtoDetectThreadCtxAppLayerProtoDetectGetCtxThread (void)
 Inits and returns an app layer protocol detection thread context. More...
 
void AppLayerProtoDetectDestroyCtxThread (AppLayerProtoDetectThreadCtx *alpd_tctx)
 Destroys the app layer protocol detection thread context. More...
 
void AppLayerProtoDetectSupportedIpprotos (AppProto alproto, uint8_t *ipprotos)
 
AppProto AppLayerProtoDetectGetProtoByName (const char *alproto_name)
 
const char * AppLayerProtoDetectGetProtoName (AppProto alproto)
 
void AppLayerProtoDetectSupportedAppProtocols (AppProto *alprotos)
 
void AppLayerRegisterExpectationProto (uint8_t proto, AppProto alproto)
 
void AppLayerProtoDetectUnittestCtxBackup (void)
 Backs up the internal context used by the app layer proto detection module. More...
 
void AppLayerProtoDetectUnittestCtxRestore (void)
 Restores back the internal context used by the app layer proto detection module, that was previously backed up by calling AppLayerProtoDetectUnittestCtxBackup(). More...
 
void AppLayerProtoDetectUnittestsRegister (void)
 Register unittests for app layer proto detection module. More...
 

Variables

uint8_t expectation_proto [ALPROTO_MAX]
 

Detailed Description

Typedef Documentation

The app layer protocol detection context.

Function Documentation

int AppLayerProtoDetectConfProtoDetectionEnabled ( const char *  ipproto,
const char *  alproto 
)
void AppLayerProtoDetectDestroyCtxThread ( AppLayerProtoDetectThreadCtx tctx)

Destroys the app layer protocol detection thread context.

Parameters
tctxPointer to the app layer protocol detection thread context.

Definition at line 2005 of file app-layer-detect-proto.c.

References AppLayerProtoDetectCtx_::ctx_ipp, AppLayerProtoDetectCtxIpproto_::ctx_pm, MpmTableElmt_::DestroyThreadCtx, FLOW_PROTO_DEFAULT, AppLayerProtoDetectPMCtx_::mpm_ctx, mpm_table, AppLayerProtoDetectThreadCtx_::mpm_tctx, MpmCtx_::mpm_type, AppLayerProtoDetectThreadCtx_::pmq, PmqFree(), SCEnter, SCFree, SCReturn, AppLayerProtoDetectThreadCtx_::spm_thread_ctx, and SpmDestroyThreadCtx().

Referenced by AppLayerDestroyCtxThread(), AppLayerProtoDetectGetCtxThread(), and AppLayerProtoDetectUnittestCtxRestore().

Here is the call graph for this function:

Here is the caller graph for this function:

AppLayerProtoDetectThreadCtx* AppLayerProtoDetectGetCtxThread ( void  )

Inits and returns an app layer protocol detection thread context.

Parameters
ctxPointer to the app layer protocol detection context.
Return values
Pointerto the thread context, on success; NULL, on failure.

Definition at line 1951 of file app-layer-detect-proto.c.

References AppLayerProtoDetectDestroyCtxThread(), AppLayerProtoDetectCtx_::ctx_ipp, AppLayerProtoDetectCtxIpproto_::ctx_pm, FLOW_PROTO_DEFAULT, MpmTableElmt_::InitThreadCtx, AppLayerProtoDetectPMCtx_::max_pat_id, AppLayerProtoDetectPMCtx_::mpm_ctx, mpm_table, AppLayerProtoDetectThreadCtx_::mpm_tctx, MpmCtx_::mpm_type, PatIntId, AppLayerProtoDetectThreadCtx_::pmq, PmqSetup(), SCEnter, SCMalloc, SCReturnPtr, AppLayerProtoDetectCtx_::spm_global_thread_ctx, AppLayerProtoDetectThreadCtx_::spm_thread_ctx, and SpmMakeThreadCtx().

Referenced by AppLayerGetCtxThread(), and AppLayerProtoDetectUnittestCtxRestore().

Here is the call graph for this function:

Here is the caller graph for this function:

AppProto AppLayerProtoDetectGetProto ( AppLayerProtoDetectThreadCtx tctx,
Flow f,
uint8_t *  buf,
uint32_t  buflen,
uint8_t  ipproto,
uint8_t  direction,
bool *  reverse_flow 
)

Returns the app layer protocol given a buffer.

Parameters
tctxPointer to the app layer protocol detection thread context.
fPointer to the flow.
bufThe buffer to be inspected.
buflenThe length of the above buffer.
ipprotoThe ip protocol.
directionThe direction bitfield - STREAM_TOSERVER/STREAM_TOCLIENT.
[out]reverse_flowtrue if flow is detected to be reversed
Return values
Theapp layer protocol.

Definition at line 1484 of file app-layer-detect-proto.c.

References AppLayerProtoDetectProbingParserElement_::alproto, ALPROTO_DCERPC, ALPROTO_MAX, ALPROTO_UNKNOWN, FLOW_IS_PE_DONE, FLOW_IS_PM_DONE, FLOW_IS_PP_DONE, AppLayerProtoDetectProbingParser_::next, SCEnter, SCLogDebug, SCReturn, SCReturnUInt, and STREAM_TOCLIENT.

Referenced by AppLayerHandleUdp(), and AppLayerIncTxCounter().

Here is the caller graph for this function:

AppProto AppLayerProtoDetectGetProtoByName ( const char *  alproto_name)

Definition at line 2042 of file app-layer-detect-proto.c.

References ALPROTO_MAX, AppLayerProtoDetectCtx_::alproto_names, ALPROTO_UNKNOWN, SCEnter, SCMemcmp, and SCReturnCT.

Referenced by AppLayerGetProtoByName().

Here is the caller graph for this function:

const char* AppLayerProtoDetectGetProtoName ( AppProto  alproto)

Definition at line 2059 of file app-layer-detect-proto.c.

References AppLayerProtoDetectProbingParserElement_::alproto, and AppLayerProtoDetectCtx_::alproto_names.

Referenced by AppLayerGetProtoName().

Here is the caller graph for this function:

int AppLayerProtoDetectPMRegisterPatternCI ( uint8_t  ipproto,
AppProto  alproto,
const char *  pattern,
uint16_t  depth,
uint16_t  offset,
uint8_t  direction 
)

Registers a case-insensitive pattern for protocol detection.

Definition at line 1743 of file app-layer-detect-proto.c.

References SCEnter, and SCReturnInt.

Referenced by AppLayerHtpPrintStats(), and SMTPStateAlloc().

Here is the caller graph for this function:

int AppLayerProtoDetectPMRegisterPatternCS ( uint8_t  ipproto,
AppProto  alproto,
const char *  pattern,
uint16_t  depth,
uint16_t  offset,
uint8_t  direction 
)

Registers a case-sensitive pattern for protocol detection.

Definition at line 1716 of file app-layer-detect-proto.c.

References SCEnter, and SCReturnInt.

Referenced by AppLayerParserRegisterProtocolParsers(), AppLayerProtoDetectUnittestCtxRestore(), DCERPCCleanup(), and SSLVersionToString().

Here is the caller graph for this function:

int AppLayerProtoDetectPMRegisterPatternCSwPP ( uint8_t  ipproto,
AppProto  alproto,
const char *  pattern,
uint16_t  depth,
uint16_t  offset,
uint8_t  direction,
ProbingParserFPtr  PPFunc,
uint16_t  pp_min_depth,
uint16_t  pp_max_depth 
)

Definition at line 1729 of file app-layer-detect-proto.c.

References SCEnter, and SCReturnInt.

int AppLayerProtoDetectPPParseConfPorts ( const char *  ipproto_name,
uint8_t  ipproto,
const char *  alproto_name,
AppProto  alproto,
uint16_t  min_depth,
uint16_t  max_depth,
ProbingParserFPtr  ProbingParserTs,
ProbingParserFPtr  ProbingParserTc 
)
void AppLayerProtoDetectPPRegister ( uint8_t  ipproto,
const char *  portstr,
AppProto  alproto,
uint16_t  min_depth,
uint16_t  max_depth,
uint8_t  direction,
ProbingParserFPtr  ProbingParser1,
ProbingParserFPtr  ProbingParser2 
)
int AppLayerProtoDetectPrepareState ( void  )

Prepares the internal state for protocol detection. This needs to be called once all the patterns and probing parser ports have been registered.

Definition at line 1560 of file app-layer-detect-proto.c.

References AppLayerProtoDetectCtx_::ctx_ipp, AppLayerProtoDetectCtxIpproto_::ctx_pm, AppLayerProtoDetectCtx_::ctx_pp, FLOW_PROTO_DEFAULT, AppLayerProtoDetectPMCtx_::max_sig_id, SCEnter, SCLogDebugEnabled(), and SCReturnInt.

Referenced by AppLayerProtoDetectUnittestCtxRestore(), and AppLayerSetup().

Here is the call graph for this function:

Here is the caller graph for this function:

void AppLayerProtoDetectRegisterProtocol ( AppProto  alproto,
const char *  alproto_name 
)

Registers a protocol for protocol detection phase.

This is the first function to be called after calling the setup function, AppLayerProtoDetectSetup(), before calling any other app layer functions, AppLayerParser or AppLayerProtoDetect, alike. With this function you are associating/registering a string that can be used by users to write rules, i.e. you register the http protocol for protocol detection using AppLayerProtoDetectRegisterProtocol(ctx, ALPROTO_HTTP, "http"), following which you can write rules like - alert http any any -> any any (sid:1;) which basically matches on the HTTP protocol.

Parameters
alprotoThe protocol.
alproto_strThe string to associate with the above "alproto". Please send a static string that won't be destroyed post making this call, since this function won't create a copy of the received argument.
Return values
0On success; -1 On failure.

Definition at line 1819 of file app-layer-detect-proto.c.

References AppLayerProtoDetectProbingParserElement_::alproto, AppLayerProtoDetectCtx_::alproto_names, SCEnter, and SCReturn.

Referenced by AppLayerParserRegisterProtocolParsers(), AppLayerRegisterProtocolDetection(), RegisterDCERPCParsers(), RegisterDCERPCUDPParsers(), RegisterDNP3Parsers(), RegisterENIPTCPParsers(), RegisterENIPUDPParsers(), RegisterFTPParsers(), RegisterHTPParsers(), RegisterModbusParsers(), RegisterNFSTCPParsers(), RegisterNFSUDPParsers(), RegisterSMBParsers(), RegisterSMTPParsers(), RegisterSSHParsers(), RegisterSSLParsers(), RegisterTemplateParsers(), and RegisterTFTPParsers().

Here is the caller graph for this function:

void AppLayerProtoDetectReset ( Flow f)
int AppLayerProtoDetectSetup ( void  )

The first function to be called. This initializes a global protocol detection context.

Return values
0On succcess;
-1On failure.

Definition at line 1758 of file app-layer-detect-proto.c.

References AppLayerExpectationSetup(), AppLayerProtoDetectCtx_::ctx_ipp, AppLayerProtoDetectCtxIpproto_::ctx_pm, FLOW_PROTO_DEFAULT, AppLayerProtoDetectPMCtx_::mpm_ctx, MpmInitCtx(), PatternMatchDefaultMatcher(), SC_ERR_FATAL, SCEnter, SCLogError, SCReturnInt, SinglePatternMatchDefaultMatcher(), AppLayerProtoDetectCtx_::spm_global_thread_ctx, and SpmInitGlobalThreadCtx().

Referenced by AppLayerProtoDetectUnittestCtxRestore(), AppLayerSetup(), and RegisterAllModules().

Here is the call graph for this function:

Here is the caller graph for this function:

void AppLayerProtoDetectSupportedAppProtocols ( AppProto alprotos)
void AppLayerProtoDetectSupportedIpprotos ( AppProto  alproto,
uint8_t *  ipprotos 
)

Definition at line 2031 of file app-layer-detect-proto.c.

References SCEnter, and SCReturn.

Referenced by SigMatchList2DataArray(), and SigMatchListSMBelongsTo().

Here is the caller graph for this function:

void AppLayerProtoDetectUnittestCtxBackup ( void  )

Backs up the internal context used by the app layer proto detection module.

Definition at line 2112 of file app-layer-detect-proto.c.

References SCEnter, and SCReturn.

Referenced by AppLayerProtoDetectUnittestCtxRestore().

Here is the caller graph for this function:

void AppLayerProtoDetectUnittestsRegister ( void  )

Register unittests for app layer proto detection module.

Definition at line 3614 of file app-layer-detect-proto.c.

References SCEnter, SCReturn, and UtRegisterTest().

Here is the call graph for this function:

void AppLayerRegisterExpectationProto ( uint8_t  proto,
AppProto  alproto 
)

Definition at line 2093 of file app-layer-detect-proto.c.

References AppLayerProtoDetectProbingParserElement_::alproto, proto, SC_ERR_NOT_SUPPORTED, and SCLogError.

Referenced by RegisterFTPParsers().

Here is the caller graph for this function:

void AppLayerRequestProtocolChange ( Flow f,
uint16_t  dp,
AppProto  expect_proto 
)

request applayer to wrap up this protocol and rerun protocol detection.

When this is called, the old session is reset unconditionally. A 'detect/log' flush packet is generated for both direction before the reset, so allow for final detection and logging.

Parameters
fflow to act on
dpdestination port to use in protocol detection. Set to 443 for start tls, set to the HTTP uri port for CONNECT and set to 0 to not use it.
expect_protoexpected protocol. AppLayer event will be set if detected protocol differs from this.

Definition at line 1843 of file app-layer-detect-proto.c.

References Flow_::alproto_expect, FlowSetChangeProtoFlag(), and Flow_::protodetect_dp.

Referenced by AppLayerRequestProtocolTLSUpgrade(), and HTPFreeConfig().

Here is the call graph for this function:

Here is the caller graph for this function:

void AppLayerRequestProtocolTLSUpgrade ( Flow f)

request applayer to wrap up this protocol and rerun protocol detection with expectation of TLS. Used by STARTTLS.

Sets detection port to 443 to make port based TLS detection work for SMTP, FTP etc as well.

Parameters
fflow to act on

Definition at line 1858 of file app-layer-detect-proto.c.

References ALPROTO_TLS, and AppLayerRequestProtocolChange().

Referenced by SMTPProcessDataChunk().

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

uint8_t expectation_proto[ALPROTO_MAX]

Definition at line 2080 of file app-layer-detect-proto.c.