工做以前的几年一直都在搞计算机安全/病毒相关的东西(纯学习,不做恶),其中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