编辑代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>

// 函数声明
int is_http_format(const unsigned char *pPayload);
char* extract_cookie(const unsigned char *pPayload);
int check_cookie_data(const char *pPayload);

int main() {
    // 原始数据
    unsigned char pPayload[] = {
        0x50, 0x4f, 0x53, 0x54, 0x20, 0x2f, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a,
        0x48, 0x6f, 0x73, 0x74, 0x3a, 0x20, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x34, 0x37, 0x2e, 0x31, 0x36, 0x3a, 0x38, 0x30, 0x0d, 0x0a,
        0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x3a, 0x20, 0x34, 0x31, 0x36, 0x0d, 0x0a,
        0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x2d, 0x77, 0x77, 0x77, 0x2d, 0x66, 0x6f, 0x72, 0x6d, 0x2d, 0x75, 0x72, 0x6c, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x64, 0x3b, 0x20, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x0d, 0x0a,
        0x43, 0x6f, 0x6f, 0x6b, 0x69, 0x65, 0x3a, 0x20, 0x58, 0x67, 0x61, 0x70, 0x47, 0x33, 0x45, 0x76, 0x3d, 0x56, 0x4c, 0x6f, 0x70, 0x6d, 0x54, 0x58, 0x2b, 0x63, 0x67, 0x4e, 0x48, 0x4e, 0x51, 0x59, 0x2b, 0x72, 0x76, 0x70, 0x50, 0x71, 0x2b, 0x43, 0x52, 0x68, 0x66, 0x31, 0x72, 0x74, 0x2b, 0x66, 0x7a, 0x42, 0x6c, 0x49, 0x37, 0x2b, 0x67, 0x56, 0x7a, 0x2b, 0x56, 0x4b, 0x75, 0x69, 0x2b, 0x4e, 0x6e, 0x5a, 0x2b, 0x63, 0x74, 0x59, 0x2e, 0x2b, 0x4f, 0x44, 0x36, 0x59, 0x2b, 0x69, 0x61, 0x4d, 0x2b, 0x36, 0x62, 0x57, 0x75, 0x32, 0x2b, 0x25, 0x33, 0x41, 0x30, 0x63, 0x39, 0x68, 0x2b, 0x36, 0x74, 0x56, 0x73, 0x49, 0x2b, 0x5a, 0x6d, 0x4e, 0x25, 0x32, 0x43, 0x5a, 0x34, 0x66, 0x2b, 0x50, 0x6b, 0x76, 0x35, 0x2b, 0x63, 0x32, 0x79, 0x41, 0x48, 0x56, 0x4e, 0x2e, 0x2b, 0x47, 0x6c, 0x72, 0x70, 0x25, 0x32, 0x43, 0x2b, 0x38, 0x34, 0x61, 0x2b, 0x47, 0x50, 0x75, 0x2b, 0x47, 0x38, 0x35, 0x31, 0x25, 0x33, 0x41, 0x2b, 0x75, 0x4f, 0x6c, 0x7a, 0x72, 0x69, 0x2b, 0x35, 0x57, 0x70, 0x6a, 0x31, 0x2b, 0x5a, 0x78, 0x63, 0x4a, 0x2b, 0x49, 0x4c, 0x56, 0x56, 0x46, 0x62, 0x2e, 0x2b, 0x49, 0x44, 0x51, 0x31, 0x6f, 0x50, 0x6d, 0x2b, 0x36, 0x34, 0x64, 0x44, 0x45, 0x4b, 0x37, 0x2b, 0x54, 0x3b, 0x20, 0x4a, 0x46, 0x79, 0x4a, 0x76, 0x30, 0x44, 0x68, 0x3d, 0x65, 0x4a, 0x41, 0x52, 0x53, 0x6d, 0x2b, 0x68, 0x6d, 0x30, 0x73, 0x4a, 0x63, 0x43, 0x77, 0x2b, 0x2b, 0x2b, 0x2b, 0x2b, 0x0d, 0x0a,
        0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 0x70, 0x0d, 0x0a,
        0x0d, 0x0a
    };

    int result = check_cookie_data((const char*)pPayload);
    if (result) {
        printf("所有条件满足,返回1\n");
    } else {
        printf("条件不满足,返回0\n");
    }

    return 0;
}

// 判断是否为HTTP格式
int is_http_format(const unsigned char *pPayload) {
    const char *http_methods[] = {"GET ", "POST ", "PUT ", "DELETE ", "HEAD ", "OPTIONS ", "PATCH ", NULL};
    for (int i = 0; http_methods[i] != NULL; ++i) {
        if (strncmp((const char *)pPayload, http_methods[i], strlen(http_methods[i])) == 0) {
            return 1;
        }
    }
    return 0;
}

// 提取Cookie
char* extract_cookie(const unsigned char *pPayload) {
    const char *headers_end = strstr((const char *)pPayload, "\r\n\r\n");
    if (!headers_end) {
        return NULL;
    }

    size_t headers_len = headers_end - (const char *)pPayload;
    char headers[headers_len + 1];
    strncpy(headers, (const char *)pPayload, headers_len);
    headers[headers_len] = '\0';

    printf("headers: %.*s\n", (int)sizeof(headers), headers);

    regex_t regex;
    regmatch_t match[2];
    // const char *pattern = "(?i)Cookie:\\s*(.*?)\\r\\n";
    const char *pattern = "Cookie:\s*(.*?)\r\n";
    int reti = regcomp(&regex, pattern, REG_EXTENDED | REG_ICASE);
    if (reti) {
        fprintf(stderr, "Could not compile regex\n");
        return NULL;
    }

    reti = regexec(&regex, headers, 2, match, 0);
    if (!reti) {
        size_t start = match[1].rm_so;
        size_t end = match[1].rm_eo;
        size_t cookie_len = end - start;
        char *cookie = malloc(cookie_len + 1);
        if (cookie) {
            strncpy(cookie, headers + start, cookie_len);
            cookie[cookie_len] = '\0';
            regfree(&regex);
            printf("Extracted Cookie: %s\n", cookie); // 打印提取的Cookie
            return cookie;
        }
    }
    regfree(&regex);
    return NULL;
}

// 检查Cookie数据
int check_cookie_data(const char *pPayload) {
    if (!is_http_format((const unsigned char *)pPayload)) {
        printf("不是HTTP格式的数据\n");
        return 0;
    }

    char *cookie = extract_cookie((const unsigned char *)pPayload);
    if (!cookie) {
        printf("未找到Cookie\n");
        return 0;
    }

    printf("Cookie: %s\n", cookie);

    char *cookie_data1 = strchr(cookie, '=');
    if (!cookie_data1) {
        free(cookie);
        printf("未找到cookie_data1\n");
        return 0;
    }
    cookie_data1++; // Move past the '='

    char *end_of_cookie_data1 = strchr(cookie_data1, ';');
    if (!end_of_cookie_data1) {
        free(cookie);
        printf("未找到cookie_data1结束符\n");
        return 0;
    }
    *end_of_cookie_data1 = '\0'; // Null-terminate cookie_data1

    char *cookie_data2 = strchr(end_of_cookie_data1 + 1, '=');
    if (!cookie_data2) {
        free(cookie);
        printf("未找到cookie_data2\n");
        return 0;
    }
    cookie_data2++; // Move past the '='

    char *end_of_cookie_data2 = strchr(cookie_data2, ';');
    if (end_of_cookie_data2) {
        *end_of_cookie_data2 = '\0'; // Null-terminate cookie_data2
    }

    printf("cookie_data1: %s\n", cookie_data1);
    printf("cookie_data2: %s\n", cookie_data2);

    if (*cookie_data1 && *cookie_data2) {
        free(cookie);
        return 1;
    }

    free(cookie);
    return 0;
}