suricata smtp协议解析源码注释五

本文详细介绍了Suricata中SMTP协议解析的过程,特别是MIME头部字段解析的函数ProcessMimeHeaders和FindMimeHeader。这两个函数用于查找和存储MIME头部字段,如content-type,并分析其重要信息,如文本格式、编码类型、附件、boundary和嵌套格式。FindMimeHeader函数通过查找冒号来区分字段名和值,处理多行字段值并设置解析状态标志。
摘要由CSDN通过智能技术生成

一。MIME头部字段解析函数ProcessMimeHeaders

SMTPParse-> SMTPProcessRequest-> SMTPProcessCommandDATA-> MimeDecParseLine-> ProcessMimeEntity-> ProcessMimeHeaders

主要完成以下几个工作:

1。调研函数FindMimeHeader根据冒号查找头部字段name和value,如content-type等等,并对其进行存储。

2。解析完头部字段后,分析头部字段的重要内容,如文本格式,编码类型,是否包含附件,是否有boundary,是否嵌套格式等,并根据这些信息设置相应的标志位,在解析body数据时使用。

参数:buf 行数据,len:行数据长度    
static int ProcessMimeHeaders(const uint8_t *buf, uint32_t len,
        MimeDecParseState *state)
{
    int ret = MIME_DEC_OK;
    MimeDecField *field;
    uint8_t *bptr = NULL, *rptr = NULL;
    uint32_t blen = 0;
    MimeDecEntity *entity = (MimeDecEntity *) state->stack->top->data;

    //调用函数FindMimeHeader解析mime头部字段,并存储到mime_state中,
    //解析完成后设置HEADER_DONE标志,如果成功解析到name则设置HEADER_START标志
    //该函数解析完头部字段后,所有字段存放在MimeDecField 类型的链表中
    /* Look for mime header in current line */
    ret = FindMimeHeader(buf, len, state);
    if (ret != MIME_DEC_OK) {
        SCLogDebug("Error: FindMimeHeader() function failed: %d", ret);
        return ret;
    }

    //下边的函数MimeDecFindField主要是变量头部字段链表,查找指定的字段名称,返回那个节点指针

    //如果解析标志为HEADER_DONE则头部解析完成,分析头部重要字段如:content-type
    //content-transer-code,content-dispositon,查找关键字符串如,"message/"
    //"boundary"等,设置相关的编码标志
    /* Post-processing after all headers done */
    if (state->state_flag == HEADER_DONE) {
        //查找编码并设置编码标志
        /* First determine encoding by looking at Content-Transfer-Encoding */
        field = MimeDecFindField(entity, CTNT_TRAN_STR);
        if (field != NULL) {
            /* Look for base64 */
            if (FindBuffer(field->value, field->value_len, (const uint8_t *)BASE64_STR, strlen(BASE64_STR))) {
                SCLogDebug("Base64 encoding found");
                entity->ctnt_flags |= CTNT_IS_BASE64;
            } else if (FindBuffer(field->value, field->value_len, (const uint8_t *)QP_STR, strlen(QP_STR))) {
                /* Look for quoted-printable */
                SCLogDebug("quoted-printable encoding found");
                entity->ctnt_flags |= CTNT_IS_QP;
            }
        }

        //查找是否有附件并设置附件标志
        /* Check for file attachment in content disposition */
        field = MimeDecFindField(entity, CTNT_DISP_STR);
        if (field != NULL) {
            bptr = FindMimeHeaderToken(field, "filename=", TOK_END_STR, &blen);
            if (bptr != NULL) {
                SCLogDebug("File attachment found in disposition");
                entity->ctnt_flags |= CTNT_IS_ATTACHMENT;

                /* Copy over using dynamic memory */
                entity->filename = SCMalloc(blen);
                if (unlikely(entity->filename == NULL)) {
                   SCLogError(SC_ERR_MEM_ALLOC, "memory allocation failed");
                    return MIME_DEC_ERR_MEM;
                }
                memcpy(entity->filename, bptr, blen);
                entity->filename_len = blen;
            }
        }

        //分析content-type的内容,查找boundary,message/等
        /* Check for boundary, encapsulated message, and file name in Content-Type */
        field = MimeDecFindField(entity, CTNT_TYPE_STR);
        if (field != NULL) {
            //如果找到boundary字符串,表示当前信件体包含子信件体,设置变量found_child为1
            //解析body数据时会判断这个变量
            /* Check if child e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

种菜的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值