从C中从HTTP标头中提取数据

发布于 2025-02-01 10:35:36 字数 1062 浏览 6 评论 0原文

我想使用C从HTTP标头中提取一些数据来执行一些检查。这是我用来提取数据的代码。在这里,我只是想检查sec-websocket-key是否为13。

我已经使用libpcap将PCAP文件读为命令行参数。

    const u_char *payload;
    payload = packet+total_headers_size
    const u_char *token;
    token = strtok(payload, "\r\n\r\n");
    while (token)
    {
        printf("Current token: %s\n", token);
        const u_char *buff;
        sprintf(buff,"%s",token);
        /*if(strcmp(token,"HTTP/1.1 101 Switching Protocols")==0)
        {
            printf("Protocol switched\n");   
        }*/
        if(strcmp(buff,"Sec-WebSocket-Version: 13")==0)
        {
            printf("Correct Version\n");
        }
        //printf("%d\n",sec);
        token = strtok(NULL, "\n");
    }

变量有效载荷包含HTTP标头请求,并且我基于\ r \ n \ r \ r \ n将标头拆分,但是我没有得到所需的输出。 total_headers_size是以太网标头LEN,IP标头LEN和TCP标头LEN的总和。

I want to extract some data from an HTTP header using C to perform some checks. This is the code that I am using to extract the data. Here I am just trying to check whether the Sec-WebSocket-Key is 13 or not.

Sample HTTP header

I have used libpcap to read the pcap file as a command line argument.

    const u_char *payload;
    payload = packet+total_headers_size
    const u_char *token;
    token = strtok(payload, "\r\n\r\n");
    while (token)
    {
        printf("Current token: %s\n", token);
        const u_char *buff;
        sprintf(buff,"%s",token);
        /*if(strcmp(token,"HTTP/1.1 101 Switching Protocols")==0)
        {
            printf("Protocol switched\n");   
        }*/
        if(strcmp(buff,"Sec-WebSocket-Version: 13")==0)
        {
            printf("Correct Version\n");
        }
        //printf("%d\n",sec);
        token = strtok(NULL, "\n");
    }

The variable payload contains the HTTP header request and I have split the header based on \r\n\r\n, but I am not getting the desired output. Total_headers_size is the sum of the Ethernet header len, IP header len, and TCP header len.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文