工做以前的幾年一直都在搞計算機安全/病毒相關的東西(純學習,不做惡),其中PE文件格式是必須知識。有些PE文件,好比驅動,系統會在加載時對checksum進行校驗,確保驅動文件的完整性。關於PE文件如何校驗,網上有不少資料能夠學習,這裏有一篇文章《An Analysis of the Windows PE Checksum Algorithm》是對WINDOWS API CheckSumMappedFile進行逆向分析的。文章的結尾提到WINDOWS的這個校驗和算法和IP協議的校驗和算法相似,IP的校驗和算法實現是RFC1071,若是對其餘的校驗和算法感興趣,能夠閱讀WIKI的《Error Detection and correction》。html
可是CheckSumMappedFile的實現略顯複雜,不夠直觀,後來讀WRK的代碼時,發現了WINDOWS內核在加載驅動時實現的校驗和算法要簡潔直觀不少,分享給你們:c++
uint32_t calc_checksum(uint32_t checksum, void *data, int length) { if (length && data != nullptr) { uint32_t sum = 0; do { sum = *(uint16_t *)data + checksum; checksum = (uint16_t)sum + (sum >> 16); data = (char *)data + 2; } while (--length); } return checksum + (checksum >> 16); } uint32_t generate_pe_checksum(void *file_base, uint32_t file_size) { uint32_t file_checksum = 0; PIMAGE_NT_HEADERS nt_headers = ImageNtHeader(file_base); if (nt_headers) { uint32_t header_size = (uintptr_t)nt_headers - (uintptr_t)file_base + ((uintptr_t)&nt_headers->OptionalHeader.CheckSum - (uintptr_t)nt_headers); uint32_t remain_size = (file_size - header_size - 4) >> 1; void *remain = &nt_headers->OptionalHeader.Subsystem; uint32_t header_checksum = calc_checksum(0, file_base, header_size >> 1); file_checksum = calc_checksum(header_checksum, remain, remain_size); if (file_size & 1){ file_checksum += (uint16_t)*((char *)file_base + file_size - 1); } } return (file_size + file_checksum); }
華麗的分割線=======================================================算法
crc32.h安全
#ifndef CRC_32_H #define CRC_32_H #ifdef __cplusplus extern "C" { #endif void init_crc_table(void); unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size); int calc_img_crc(const char *in_file, unsigned int *img_crc); #ifdef __cplusplus } #endif #endif
crc32.capp
#include <stdlib.h> /***************************************************** ** Name : crc32.c ** Author : ** Version : 1.0 ** Date : ** Description : CRC32 Checking ******************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #define BUFSIZE 1024*4 static unsigned int crc_table[256]; const static char * program_name = "crc32"; /* **初始化crc表,生成32位大小的crc表 */ void init_crc_table(void) { unsigned int c; unsigned int i, j; for (i = 0; i < 256; i++) { c = (unsigned int)i; for (j = 0; j < 8; j++) { if (c & 1) c = 0xedb88320L ^ (c >> 1); else c = c >> 1; } crc_table[i] = c; } } /*計算buffer的crc校驗碼*/ unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size) { unsigned int i; for (i = 0; i < size; i++) { crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8); } return crc ; } /* **計算大文件的CRC校驗碼:crc32函數,是對一個buffer進行處理, **但若是一個文件相對較大,顯然不能直接讀取到內存當中 **因此只能將文件分段讀取出來進行crc校驗, **而後循環將上一次的crc校驗碼再傳遞給新的buffer校驗函數, **到最後,生成的crc校驗碼就是該文件的crc校驗碼. */ int calc_img_crc(const char *in_file, unsigned int *img_crc) { int fd; int nread; int ret; unsigned char buf[BUFSIZE]; /*第一次傳入的值須要固定,若是發送端使用該值計算crc校驗碼, **那麼接收端也一樣須要使用該值進行計算*/ unsigned int crc = 0xffffffff; fd = open(in_file, O_RDONLY); if (fd < 0) { printf("%d:open %s.\n", __LINE__, strerror(errno)); return -1; } while ((nread = read(fd, buf, BUFSIZE)) > 0) { crc = crc32(crc, buf, nread); } *img_crc = crc; close(fd); if (nread < 0) { printf("%d:read %s.\n", __LINE__, strerror(errno)); return -1; } return 0; } /* int main(int argc, char **argv) { int ret; unsigned int img_crc; const char *in_file = argv[1]; if (argc < 2) { exit(1); } init_crc_table(); ret = calc_img_crc(in_file, &img_crc); if (ret < 0) { exit(1); } printf("The crc of %s is:%u\n", in_file, img_crc); return 0; } */
對文件進行校驗:函數
init_crc_table(); unsigned int bin_crc; calc_img_crc(argv[1],&bin_crc);
對buf 中的數據校驗:學習
init_crc_table(); unsigned int binCrcNew = 0xFFFFFFFF; binCrcNew = crc32(binCrcNew, (unsigned char*)fwBuff, binLen);
Makefile:ui
all: encryptBIN encryptBIN:encryptBIN.cpp crc32.c gcc -c crc32.c -o crc32.o g++ -c encryptBIN.cpp -o encryptBIN.o gcc crc32.o encryptBIN.o -lstdc++ -o encryptBIN clean: rm -rf *.o encryptBIN
http://www.cnblogs.com/concurrency/p/3926698.htmlspa
https://blog.csdn.net/liukang325/article/details/41745237.net