/***************************************************************************** * @Author : Songsk * @Date : 2020-11-10 * @LastEditors : songshuaikang@comleader.com.cn * @Description : Sync Time with Schedule. *******************************************************************************/ #include #include #include #include #include #define uint unsigned int #define uchar unsigned char typedef struct frame_head_t { uchar dmac[6]; uchar smac[6]; // uchar dot1q[4]; uchar protocol[2]; uchar padding; uchar len; }FRAME_HEAD_T; typedef struct timeval TS_TO_PROXY_T; #define SNAP_LEN 65536 #define _ENCRYPT(a,b,c) (a=(b^c)) #define _DECRYPT _ENCRYPT #define _CHECKSUM(_checksum, _cipher, _key, _len)\ {\ uint _m_i = 0;\ uint sum = 0;\ for(_m_i=0; _m_i< _len; _m_i++)\ sum = sum + (0xffffffff & _cipher[_m_i]) + (0xffffffff & _key[_m_i]);\ _checksum = 0xff - (0xff & (sum)) - ((sum) >> 8);\ } #define ENCRYPT_FUNC(_cipher, _plain, _key, _len)\ {\ int _m_i = 0;\ for(_m_i=0; _m_i< _len; _m_i++)\ _ENCRYPT(_cipher[_m_i], _plain[_m_i], _key[_m_i]);\ } #define DECRYPT_FUNC(_plain, _cipher, _key, _len)\ {\ int _m_i = 0;\ for(_m_i=0; _m_i< _len; _m_i++)\ _DECRYPT( _plain[_m_i],_cipher[_m_i], _key[_m_i]);\ } #define dPrint(fmt, ...) do{fprintf(stderr, "[%s:%d] " fmt "\r\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0) #define HexPrint(_buf, _len) \ {\ int _m_i = 0;\ char *_m_buf = (char *)(_buf);\ int _m_len = (int)(_len);\ printf("[%s:%d] \r\n", __FUNCTION__, __LINE__);\ printf("*****************************\n");\ for(_m_i = 0; _m_i < _m_len; _m_i++)\ {\ printf("\033[32m%02x \033[0m", _m_buf[_m_i] & 0xff);\ if(!((_m_i+1) % 10)) printf("\n");\ }\ printf("\nsize = %d\n*****************************\n", _m_len);\ } /************************************************ * Add By Tianlun Song in Nov 11, 2020. * Title: 设置操作系统时间 * Argu Type: struct timeval * Usage: * set_sys_time(time); **************************************************/ void set_sys_time(TS_TO_PROXY_T *time) { // char strTime[26] = {0}; //printf("%d.%d\n", time->tv_sec, time->tv_usec); printf("The time before set:"); fflush(stdout); system("date"); settimeofday(time,NULL); printf("The time after set :"); fflush(stdout); system("date"); } void dispatcher_handler(u_char *temp1, const struct pcap_pkthdr *header, const u_char *pkt_data) { // HexPrint((const char *)pkt_data, header->caplen); FRAME_HEAD_T head; TS_TO_PROXY_T time; memset(&head, 0, sizeof(head)); memset(&time, 0, sizeof(time)); memcpy(&head, pkt_data, sizeof(head)); // printf("head info: protocol is [0x%X 0x%X], padding is [0x%X], len is [0x%X]\n", // head.protocol[0],head.protocol[1], head.padding, head.len ); if(head.protocol[1] == 0x89) { uchar plantext[head.len],ciphertext[head.len],key[head.len]; memset(plantext, 0, head.len); memset(ciphertext, 0, head.len); memset(key, 0, head.len); head.padding -= 4; memcpy(ciphertext, pkt_data + head.padding, head.len); memcpy(key, pkt_data + head.padding + head.len, head.len); //printf("ciphertext:[0x%x]\tkey:[0x%x]\n",ciphertext, key); DECRYPT_FUNC(plantext, ciphertext, key, head.len); //printf("plaintext:[0x%x]\n",plantext); memcpy(&time, plantext, sizeof(time)); set_sys_time(&time); } } int main() { pcap_t *handle; char err_buf[PCAP_ERRBUF_SIZE]; char dev_str[64]; struct bpf_program fp; strcpy(dev_str, "linux_dal"); printf("Sniff Device is %s\n", dev_str); /* open capture device */ handle = pcap_open_live(dev_str, SNAP_LEN, 1, 1000, err_buf); if (handle == NULL) { fprintf(stderr, "Couldn't open device %s: %s\n", dev_str, err_buf); return 0; } /* compile the filter expression */ if (pcap_compile(handle, &fp, "ether proto 0x889", 0, 24) == -1) { fprintf(stderr, "Couldn't parse filter %s: %s\n", "***", pcap_geterr(handle)); return 0; } /* apply the compiled filter */ if (pcap_setfilter(handle, &fp) == -1) { fprintf(stderr, "Couldn't install filter %s: %s\n", "***", pcap_geterr(handle)); return 0; } pcap_loop(handle, -1, dispatcher_handler, NULL); /* cleanup */ pcap_freecode(&fp); pcap_close(handle); dPrint("Capture complete."); return 0; }