zoukankan      html  css  js  c++  java
  • hex2pcap

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct
    {
        unsigned int magic_number;
        unsigned short version_major;
        unsigned short version_minor;
        unsigned int thiszone;
        unsigned int sigfigs;
        unsigned int snaplen;
        unsigned int network;
    }pcap_hdr_t;
    
    typedef struct
    {
        unsigned int ts_sec;
        unsigned int ts_usec;
        unsigned int incl_len;
        unsigned int orig_len;
    }pkt_hdr_t;
    
    FILE          *pcap_fd     = NULL;
    unsigned char input_buf[4096]  = {0};
    unsigned char output_buf[2048] ={0};
    int           input_len = 0, output_len = 0;
    
    int hexdump(unsigned char *name, unsigned char *data, unsigned short len)
    {
        int i, j, p, ret, tmp_len = 0;
        static unsigned char dump_buf[1024] = {0};
        printf("hexdump(%s len=%d)
    ", name, len);
        memset(dump_buf, 0x00, sizeof(dump_buf));
    
        for (i = 0, j = 0, p = 0; i < len; i++)
        {
            ret = sprintf( (dump_buf + p), " %02x", *(data + i) );
            p += ret;
            j++;
            if(j >= 16)
            {
                printf("%s
    ", dump_buf);
                j = 0;
                p = 0;
            }
        }
        if(p > 0)
            printf("%s
    ", dump_buf);
        return 0;
    }
    
    int file_size(FILE *fd)
    {
        int cur, len;
        cur = ftell(fd);
        fseek(fd, 0, SEEK_END);
        len = ftell(fd);
        fseek(fd,cur, SEEK_SET);
        return len;
    }
    
    int wpkt(unsigned char *buf, int size)
    {
        int ret;
        ret = fwrite(buf, size, 1, pcap_fd);
        if(ret != 1)
            printf("fwrite %d %d err 
    ", size, ret);
        return 0;
    }
    
    int pcap_deinit(void)
    {
        fclose(pcap_fd);
        return 0;
    }
    
    int eth_pcap(unsigned char *pkt, int len)
    {
        pkt_hdr_t pkt_hdr = {0};
        
        pkt_hdr.incl_len = pkt_hdr.orig_len = len;
        wpkt((unsigned char *)(&pkt_hdr), sizeof(pkt_hdr));
        wpkt(pkt, len);
        return 0;
    }
    
    int pcap_init(int pkt_type, unsigned char *file)
    {
        pcap_hdr_t       pcap = {0};
            
        pcap_fd = fopen(file, "wb+");
        if(NULL == pcap_fd)
        {
            printf("create %s err
    ", file);
            return -1;
        }
        printf("create %s file
    ", file);
        pcap.magic_number = 0xa1b2c3d4;
        pcap.snaplen = 0x900;
        pcap.version_major = 2;
        pcap.version_minor = 4;    
        pcap.network = pkt_type;//105,1    
        wpkt((unsigned char *)(&pcap), sizeof(pcap));
        return 0;
    }
    
    int main(int argc, char *argv[])
    {    
        FILE *fd;
        unsigned char ch, *token, *delim = " ,";
        int i, ret,pkt_type;
        
        argc--;
        argv++;
        /*hex2pkt -mac -i frame.hex -o mac.pcap*/
        do
        {
            if( 0 == strcmp(argv[0], "-eth"))
            {
                pkt_type = 1;
            }    
            else if(0 == strcmp(argv[0], "-mac"))
            {
                pkt_type = 105;
            }
            else if(0 == strcmp(argv[0], "-i"))
            {
                argv++;
                argc--;
                fd = fopen(argv[0], "rb");
                printf("-i %s
    ", argv[0]);
                input_len = file_size(fd);
                ret = fread( input_buf, input_len, 1, fd );
                if(1 != ret)
                    printf("fread err ret  %d
    ", ret);
                printf("input_buf(len=%d):
    %s
    ", input_len, input_buf);
                fclose(fd);
            }
            else if(0 == strcmp(argv[0], "-o"))
            {
                argv++;
                argc--;
                for( token = strtok(input_buf, delim), output_len=0; token != NULL; token = strtok(NULL, delim) )
                {
                    sscanf(token, "%x", &output_buf[output_len++]);
                }
                hexdump("output", output_buf, output_len);
                pcap_init(pkt_type, argv[0]);
                eth_pcap(output_buf, output_len);
                pcap_deinit();    
            }
            else
            {
                printf("unknow cmd %s
    ", argv[0]);
            }
            argv++;
            argc--;
        }
        while(argc);
        
        return 0;
    }
  • 相关阅读:
    javascript和C#比较
    前端模块管理器简介
    javascript中的splice方法介绍&示例
    javascript中数组揭秘
    17款code review工具
    IIS ip访问限制插件
    iis 限制动态IP地址访问次数
    AWS云使用100条宝贵经验分享
    C# 开源框架(整理)
    如何获取Azure AD tenant的tenant Id?
  • 原文地址:https://www.cnblogs.com/to7str/p/5487434.html
Copyright © 2011-2022 走看看