suricata應用層協議解析
1. 協議註冊
應用層協議保存在全局變量static AppLayerParserCtx alp_ctx;
typedef struct AppLayerParserCtx_ {
AppLayerParserProtoCtx ctxs[FLOW_PROTO_MAX][ALPROTO_MAX];
} AppLayerParserCtx;
AppLayerParserProtoCtx是一個二維數組,橫座標是三層協議,縱座標是應用層協議。
typedef struct AppLayerParserProtoCtx_{
/* 0 - to_server, 1 - to_client. */
AppLayerParserFPtr Parser[2];
......
} AppLayerParserProtoCtx;
函數指針AppLayerParserFPtr對應協議的解析函數。
AppLayerParserRegisterParser註冊應用層協議函數,給Parser賦值協議處理函數。
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto,
uint8_t direction,
AppLayerParserFPtr Parser)
{
SCEnter();
alp_ctx.ctxs[FlowGetProtoMapping(ipproto)][alproto].
Parser[(direction & STREAM_TOSERVER) ? 0 : 1] = Parser;
SCReturnInt(0);
}
2. 協議處理流程
2.1. 處理函數註冊
RegisterAllModules註冊所有模塊,調用TmModuleFlowWorkerRegister註冊流量模塊,將流量模塊保存到全局數組TmModule tmm_modules[TMM_SIZE];
模塊結構體TmModule_如下所示
typedef struct TmModule_ {
const char *name;
/** thread handling */
TmEcode (*ThreadInit)(ThreadVars *, const void *, void **);//線程初始化
void (*ThreadExitPrintStats)(ThreadVars *, void *);
TmEcode (*ThreadDeinit)(ThreadVars *, void *);
/** the packet processing function */
TmEcode (*Func)(ThreadVars *, Packet *, void *, PacketQueue *, PacketQueue *); //模塊處理函數
TmEcode (*PktAcqLoop)(ThreadVars *, void *, void *);
/** terminates the capture loop in PktAcqLoop */
TmEcode (*PktAcqBreakLoop)(ThreadVars *, void *);
TmEcode (*Management)(ThreadVars *, void *);
/** global Init/DeInit */
TmEcode (*Init)(void); //全局初始化模塊函數
TmEcode (*DeInit)(void);
void (*RegisterTests)(void);
uint8_t cap_flags; /**< Flags to indicate the capability requierment of
the given TmModule */
/* Other flags used by the module */
uint8_t flags;
} TmModule;
2.2.流量處理
--->FlowWorker
---> AppLayerHandleUdp
-----> p->Parser()
FlowWorker根據tcp還是udp調用不同的函數,Udp調用AppLayerHandleUdp處理。然後調用AppLayerParserParse處理應用層協議,例如dns協議、http協議,p->parser根據應用層協議號調用AppLayerParserProtoCtx中註冊的應用層協議函數。