suricata应用层协议解析

                                              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中注册的应用层协议函数。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章