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, const 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

◆ AppLayerProtoDetectCtx

The app layer protocol detection context.

◆ AppLayerProtoDetectCtxIpproto

◆ AppLayerProtoDetectPMCtx

◆ AppLayerProtoDetectPMSignature

◆ AppLayerProtoDetectPPTestDataElement

◆ AppLayerProtoDetectPPTestDataIPProto

◆ AppLayerProtoDetectPPTestDataPort

◆ AppLayerProtoDetectProbingParser

◆ AppLayerProtoDetectProbingParserElement

◆ AppLayerProtoDetectProbingParserPort

Function Documentation

◆ AppLayerProtoDetectConfProtoDetectionEnabled()

int AppLayerProtoDetectConfProtoDetectionEnabled ( const char *  ipproto,
const char *  alproto 
)

◆ AppLayerProtoDetectDeSetup()

int AppLayerProtoDetectDeSetup ( void  )

Cleans up the app layer protocol detection phase.

Todo:
incomplete. Need more work.

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

References FLOW_PROTO_DEFAULT, PatIntId, and SCEnter.

Referenced by AppLayerDeSetup().

Here is the caller graph for this function:

◆ AppLayerProtoDetectDestroyCtxThread()

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 2009 of file app-layer-detect-proto.c.

References FLOW_PROTO_DEFAULT, and SCEnter.

Referenced by AppLayerDestroyCtxThread().

Here is the caller graph for this function:

◆ AppLayerProtoDetectGetCtxThread()

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 1955 of file app-layer-detect-proto.c.

References alpd_tctx, FLOW_PROTO_DEFAULT, PatIntId, and SCEnter.

Referenced by AppLayerGetCtxThread(), and LLVMFuzzerTestOneInput().

Here is the caller graph for this function:

◆ AppLayerProtoDetectGetProto()

AppProto AppLayerProtoDetectGetProto ( AppLayerProtoDetectThreadCtx tctx,
Flow f,
const 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 1491 of file app-layer-detect-proto.c.

References ALPROTO_MAX, ALPROTO_UNKNOWN, FLOW_IS_PM_DONE, SCEnter, SCLogDebug, and STREAM_TOSERVER.

Referenced by LLVMFuzzerTestOneInput().

Here is the caller graph for this function:

◆ AppLayerProtoDetectGetProtoByName()

AppProto AppLayerProtoDetectGetProtoByName ( const char *  alproto_name)

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

References ALPROTO_MAX, and SCEnter.

Referenced by AppLayerGetProtoByName().

Here is the caller graph for this function:

◆ AppLayerProtoDetectGetProtoName()

const char* AppLayerProtoDetectGetProtoName ( AppProto  alproto)

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

Referenced by AppLayerGetProtoName().

Here is the caller graph for this function:

◆ AppLayerProtoDetectPMRegisterPatternCI()

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 1750 of file app-layer-detect-proto.c.

References SCEnter.

◆ AppLayerProtoDetectPMRegisterPatternCS()

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 1723 of file app-layer-detect-proto.c.

References SCEnter.

Referenced by AppLayerParserRegisterProtocolParsers().

Here is the caller graph for this function:

◆ AppLayerProtoDetectPMRegisterPatternCSwPP()

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 1736 of file app-layer-detect-proto.c.

References SCEnter.

◆ AppLayerProtoDetectPPParseConfPorts()

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 
)
Return values
bool0 if no config was found, 1 if config was found

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

References AppLayerProtoDetectPPRegister(), ConfGetNode(), ConfNodeLookupChild(), SC_ERR_FATAL, SCEnter, SCLogDebug, SCLogError, SCReturnInt, STREAM_TOCLIENT, STREAM_TOSERVER, and ConfNode_::val.

Here is the call graph for this function:

◆ AppLayerProtoDetectPPRegister()

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

Parameters
directionSTREAM_TOSERVER or STREAM_TOCLIENT for dp or sp

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

References DetectPortParse(), head, DetectPort_::port, DetectPort_::port2, and SCEnter.

Referenced by AppLayerProtoDetectPPParseConfPorts(), RegisterDNP3Parsers(), RegisterENIPTCPParsers(), RegisterENIPUDPParsers(), RegisterModbusParsers(), RegisterTemplateParsers(), and RegisterTFTPParsers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppLayerProtoDetectPrepareState()

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 1567 of file app-layer-detect-proto.c.

References FLOW_PROTO_DEFAULT, and SCEnter.

Referenced by AppLayerSetup().

Here is the caller graph for this function:

◆ AppLayerProtoDetectRegisterProtocol()

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 1826 of file app-layer-detect-proto.c.

References SCEnter.

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

Here is the caller graph for this function:

◆ AppLayerProtoDetectReset()

◆ AppLayerProtoDetectSetup()

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 1765 of file app-layer-detect-proto.c.

References SCEnter.

Referenced by AppLayerSetup(), and LLVMFuzzerTestOneInput().

Here is the caller graph for this function:

◆ AppLayerProtoDetectSupportedAppProtocols()

void AppLayerProtoDetectSupportedAppProtocols ( AppProto alprotos)

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

References ALPROTO_MAX, and SCEnter.

Referenced by AppLayerListSupportedProtocols(), and AppLayerRegisterThreadCounters().

Here is the caller graph for this function:

◆ AppLayerProtoDetectSupportedIpprotos()

void AppLayerProtoDetectSupportedIpprotos ( AppProto  alproto,
uint8_t *  ipprotos 
)

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

References SCEnter.

◆ AppLayerProtoDetectUnittestCtxBackup()

void AppLayerProtoDetectUnittestCtxBackup ( void  )

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

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

References SCEnter.

◆ AppLayerProtoDetectUnittestCtxRestore()

void AppLayerProtoDetectUnittestCtxRestore ( void  )

Restores back the internal context used by the app layer proto detection module, that was previously backed up by calling AppLayerProtoDetectUnittestCtxBackup().

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

References SCEnter.

◆ AppLayerProtoDetectUnittestsRegister()

void AppLayerProtoDetectUnittestsRegister ( void  )

Register unittests for app layer proto detection module.

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

References SCEnter, and UtRegisterTest().

Here is the call graph for this function:

◆ AppLayerRegisterExpectationProto()

void AppLayerRegisterExpectationProto ( uint8_t  proto,
AppProto  alproto 
)

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

References expectation_proto, proto, SC_ERR_NOT_SUPPORTED, and SCLogError.

◆ AppLayerRequestProtocolChange()

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 1850 of file app-layer-detect-proto.c.

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

Referenced by AppLayerRequestProtocolTLSUpgrade().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ AppLayerRequestProtocolTLSUpgrade()

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 1865 of file app-layer-detect-proto.c.

References ALPROTO_TLS, and AppLayerRequestProtocolChange().

Here is the call graph for this function:

Variable Documentation

◆ expectation_proto

uint8_t expectation_proto[ALPROTO_MAX]

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

Referenced by AppLayerRegisterExpectationProto().