码迷,mamicode.com
首页 > 其他好文 > 详细

FILEBUFFER TO IMAGEBUFFER

时间:2019-10-06 09:15:15      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:optional   ima   sign   space   fse   nal   signature   turn   c++   

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <bits/stdc++.h>
 3 #include <windows.h>
 4 #include <winnt.h>
 5 #define FILE_PATH1 "*************"
 6 #define FILE_PATH2 "*************"
 7 using namespace std;
 8 
 9 int main(void) {
10     int size, j;
11     FILE* p1 = fopen(FILE_PATH1, "rb");
12     //把硬盘里的内容读到内存里
13     fseek(p1, 0, SEEK_END);
14     size = ftell(p1);
15     rewind(p1);
16     char* buffer = (char*)malloc(size);
17     fread(buffer, 1, size, p1);
18     //cout << size << endl;
19 
20     PIMAGE_DOS_HEADER PDOS = (PIMAGE_DOS_HEADER)buffer;
21     //得到NT头
22     PIMAGE_NT_HEADERS32 PNT = (PIMAGE_NT_HEADERS32)((char*)PDOS + PDOS->e_lfanew);
23     //printf("%p\n", PNT->Signature);
24     //得到FILE头
25     PIMAGE_FILE_HEADER PFILE = (PIMAGE_FILE_HEADER)((char*)PNT + sizeof(PNT->Signature));
26     //得到OPTIONAL头
27     PIMAGE_OPTIONAL_HEADER32 POPTIONAL = (PIMAGE_OPTIONAL_HEADER32)((char*)PFILE + sizeof(IMAGE_FILE_HEADER));
28 
29     FILE* p2 = fopen(FILE_PATH2, "wb");
30     
31     //将头和节表写入文件中
32     fwrite(buffer, 1, POPTIONAL->SizeOfHeaders, p2); 
33     //将内存补齐对好
34     j = 0;
35     while ((POPTIONAL->SizeOfHeaders + j) % POPTIONAL->SectionAlignment != 0) {
36         fprintf(p2, "%c", 0x00);
37         j++;
38     }
39 
40     //fwrite(PDOS, 1, POPTIONAL->SizeOfHeaders, p2);
41     fclose(p2);
42     p2 = fopen(FILE_PATH2, "ab");
43     //获得节表
44     PIMAGE_SECTION_HEADER PSECTION = (PIMAGE_SECTION_HEADER)((char*)POPTIONAL + PFILE->SizeOfOptionalHeader);
45     //printf("%p\n", buffer + PSECTION->PointerToRawData);
46     for (int i = 0; i < PFILE->NumberOfSections; i++) {    
47         
48         //fflush(p2);
49         //写入节区的内容
50         //fwrite((char*)buffer +(int) PSECTION->PointerToRawData, 1, PSECTION->SizeOfRawData, p2);
51         //fwrite(buffer + PSECTION->PointerToRawData, 1, PSECTION->SizeOfRawData, p2);
52         //for (j = 0; j < 2; j++) printf("%p\n", * (buffer + PSECTION->PointerToRawData + j));
53         //for (j = 0; j < PSECTION->SizeOfRawData; j++) fprintf(p2, "%x", * (buffer + PSECTION->PointerToRawData + j));
54 
55         for (j = 0; j < PSECTION->SizeOfRawData; j++) fwrite(buffer + PSECTION->PointerToRawData + j, 1, 1, p2);
56         //cout << j << endl;
57 
58 
59         //printf("%p    %p\n", buffer + PSECTION->PointerToRawData, PSECTION->PointerToRawData);
60         //内存对齐
61         j = 0;
62         while ((PSECTION->VirtualAddress + PSECTION->SizeOfRawData + j) % POPTIONAL->SectionAlignment != 0) {
63             fprintf(p2, "%c", 0x00);
64             j++;
65         }
66         //cout << i << endl;
67         //指向下一个节表
68         if (i < PFILE->NumberOfSections - 1) {
69             PSECTION = (PIMAGE_SECTION_HEADER)((char*)PSECTION + 0x28);
70         }
71     }
72 
73     return 0;
74 }

 

FILEBUFFER TO IMAGEBUFFER

标签:optional   ima   sign   space   fse   nal   signature   turn   c++   

原文地址:https://www.cnblogs.com/zhangzixu/p/11626413.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!