Suricata之源代码(一)

本文深入剖析Suricata的源代码,首先介绍了Suricata的工作流程,强调DetectEngineCtx结构体的重要性,将其比喻为系统的核心。接着详细讲解了从main函数开始的启动过程,特别是RunModeIpsNFQRegister函数在IPS模式下的功能,包括ReceiveNFQ、Decode、StreamTcp、Detect和Verdict等模块的连接。文章通过代码实例展示了如何将各个模块串联起来,并以多线程方式处理网络数据包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        第一次系统性的写blog,写的不好,请大家多多包涵。

        在介绍Suricata源代码之前,大致介绍一下Suricata的工作流程。在suricata中主要使用了回调函数将所有的模块连接起来的。最后是通过DetectEngineCtx *global_de_ctx这个结构体启动起来的。整个的启动过程我用鞭炮来进行比喻,回调函数就好像鞭炮的引线一样,将所有的小的鞭炮连接起来,连接起来之后如果要放鞭炮就的要使用火柴将引线点燃。所以我将global_de_ctx比喻为火柴。在suricata中也是这样,最后通过global_de_ctx将suricata运行起来的。就好比是global_de_ctx打通了suricata的任督二脉。

DetectEngineCtx结构体在detect.h中。

typedef structDetectEngineCtx_ {

    uint8_t flags;

    int failure_fatal;

 

    Signature *sig_list;

    uint32_t sig_cnt;

 

    /* version of the srep data */

    uint32_t srep_version;

 

    Signature **sig_array;

    uint32_t sig_array_size; /* size in bytes*/

    uint32_t sig_array_len;  /* size in array members */

 

    uint32_t signum;

 

    /* used by the signature ordering module */

    struct SCSigOrderFunc_ *sc_sig_order_funcs;

    struct SCSigSignatureWrapper_*sc_sig_sig_wrapper;

 

    /*hash table used for holding the classification config info */

    HashTable *class_conf_ht;

    /* hash table used for holding thereference config info */

    HashTable *reference_conf_ht;

 

    /* main sigs */

    DetectEngineLookupFlowflow_gh[FLOW_STATES];

 

    uint32_t mpm_unique, mpm_reuse, mpm_none,

        mpm_uri_unique, mpm_uri_reuse,mpm_uri_none;

    uint32_t gh_unique, gh_reuse;

 

    uint32_t mpm_max_patcnt, mpm_min_patcnt,mpm_tot_patcnt,

        mpm_uri_max_patcnt, mpm_uri_min_patcnt,mpm_uri_tot_patcnt;

 

    /* init phase vars */

    HashListTable *sgh_hash_table;

 

    …….

}DetectEngineCtx;

下面还有很长没有贴出来,这个结构体非常的大。想想它确实是应该这么大的。毕竟它就是suricata的心脏嘛!

Suricata是c语言开发的,而c语言的起始点是从main()函数开始的,为了好找到入口点,所以在写代码的时候就规定了从main函数开始。而suricata的入口main函数是在suricata.c文件中。

int main(intargc, char **argv)

{

    int opt;

    char pcap_dev[128];

    char *sig_file = NULL;

    int sig_file_exclusive = FALSE;

    int conf_test = 0;

    char *pid_filename = NULL;

#ifdef UNITTESTS

    char *regex_arg = NULL;

#endif

…….

Suricata中的main函数接下来所做的事情是:

/* initializethe logging subsys */

    SCLogInitLogModule(NULL);//初始化日志系统

 

    if(SCSetThreadName("Suricata-Main") < 0) {//给主线程设置线程名称

    &nb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值