1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
| bool PatchReses(char *source,char *target)
{
HANDLE src=CreateFileA(source,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
_IMAGE_NT_HEADERS *srcimage=new _IMAGE_NT_HEADERS;
_IMAGE_NT_HEADERS *targetimage=new _IMAGE_NT_HEADERS;
DWORD srcressz=0;
char *srcreses;
if(src!=INVALID_HANDLE_VALUE)
{
LARGE_INTEGER SZ;
ZeroMemory(&SZ,sizeof(SZ));
GetFileSizeEx(src,&SZ);
char *buf=new char[SZ.QuadPart+1024];
memset(buf,NULL,SZ.QuadPart+1024);
DWORD rb;
ReadFile(src,buf,SZ.QuadPart,&rb,0);
CloseHandle(src);
_IMAGE_DOS_HEADER stub;
memcpy(&stub,buf,sizeof(_IMAGE_DOS_HEADER));
buf+=stub.e_lfanew;
memcpy(srcimage,buf,sizeof(_IMAGE_NT_HEADERS));
_IMAGE_SECTION_HEADER *sections=new _IMAGE_SECTION_HEADER[srcimage->FileHeader.NumberOfSections];
buf+=sizeof(_IMAGE_NT_HEADERS);
memcpy(sections,buf,(srcimage->FileHeader.NumberOfSections*sizeof(_IMAGE_SECTION_HEADER)));
buf-=sizeof(_IMAGE_NT_HEADERS);
buf-=stub.e_lfanew;
for(int i=0;i<srcimage->FileHeader.NumberOfSections;i++)
{
//printf("%.8s\n",sections[i].Name);
if(srcimage->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress==sections[i].VirtualAddress)
{
srcreses=new char[sections[i].SizeOfRawData];
buf+=sections[i].PointerToRawData;
memcpy(srcreses,buf,sections[i].SizeOfRawData);
buf-=sections[i].PointerToRawData;
srcressz=sections[i].SizeOfRawData;
}
}
delete []buf;
}
else
{
printf("Error open source exe\n");
return(false);
}
HANDLE tg=CreateFileA(target,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(tg!=INVALID_HANDLE_VALUE)
{
LARGE_INTEGER SZ;
ZeroMemory(&SZ,sizeof(SZ));
GetFileSizeEx(tg,&SZ);
char *buf=new char[SZ.QuadPart+1024];
memset(buf,NULL,SZ.QuadPart+1024);
DWORD rb;
ReadFile(tg,buf,SZ.QuadPart,&rb,0);
CloseHandle(tg);
_IMAGE_DOS_HEADER stub;
memcpy(&stub,buf,sizeof(_IMAGE_DOS_HEADER));
buf+=stub.e_lfanew;
memcpy(targetimage,buf,sizeof(_IMAGE_NT_HEADERS));
_IMAGE_SECTION_HEADER *sections=new _IMAGE_SECTION_HEADER[targetimage->FileHeader.NumberOfSections];
buf+=sizeof(_IMAGE_NT_HEADERS);
memcpy(sections,buf,(targetimage->FileHeader.NumberOfSections*sizeof(_IMAGE_SECTION_HEADER)));
buf-=sizeof(_IMAGE_NT_HEADERS);
buf-=stub.e_lfanew;
DWORD resesDelta=0;
for(int i=0;i<targetimage->FileHeader.NumberOfSections;i++)
{
if(targetimage->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress==sections[i].VirtualAddress)
{
resesDelta=srcressz-sections[i].SizeOfRawData;
}
}
char *newexe=new char[SZ.QuadPart+resesDelta];
for(int i=0;i<targetimage->FileHeader.NumberOfSections;i++)
{
if(targetimage->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress==sections[i].VirtualAddress)
{
DWORD irdsz=sizeof(_IMAGE_RESOURCE_DIRECTORY),irdEsz=sizeof(_IMAGE_RESOURCE_DIRECTORY_ENTRY),resdatsz=sizeof(_IMAGE_RESOURCE_DATA_ENTRY);
DWORD acum=irdsz;
_IMAGE_RESOURCE_DATA_ENTRY *dataE=new _IMAGE_RESOURCE_DATA_ENTRY;
//printf("%d",irdsz);
srcreses+=irdsz;
for(;;)
{
ZeroMemory(dataE,resdatsz);
srcreses+=irdEsz;
memcpy(dataE,srcreses,resdatsz);//тут ошибка,ругается что srcreses кривой указатель
acum=acum+irdEsz+resdatsz;
dataE->OffsetToData=/*dataE.Size+*/acum+sections[i].PointerToRawData;//calculate new RVA
memcpy(srcreses,dataE,resdatsz);
srcreses+=(resdatsz+dataE->Size);
acum=acum+resdatsz+dataE->Size;
if(acum==srcressz){break;}
}
srcreses-=srcressz;
printf("!");
////////////////////////////////////////////////////////////
memcpy(newexe,buf,sections[i].PointerToRawData);
newexe+=sections[i].PointerToRawData;
memcpy(newexe,srcreses,srcressz);
newexe+=srcressz;
DWORD totalseek=0;
_IMAGE_SECTION_HEADER *fixedsections=new _IMAGE_SECTION_HEADER[targetimage->FileHeader.NumberOfSections];
memcpy(fixedsections,sections,(targetimage->FileHeader.NumberOfSections*sizeof(_IMAGE_SECTION_HEADER)));
fixedsections[i].SizeOfRawData=srcressz;
i++;
for(int j=i;j<targetimage->FileHeader.NumberOfSections;j++)
{
buf+=sections[j].PointerToRawData;
memcpy(newexe,buf,sections[j].SizeOfRawData);
fixedsections[j].PointerToRawData=sections[j].PointerToRawData+resesDelta;
fixedsections[j].VirtualAddress=sections[j].VirtualAddress+resesDelta;
newexe+=sections[j].SizeOfRawData;
totalseek=totalseek+sections[j].SizeOfRawData;
}
newexe-=totalseek;
newexe-=sections[i-1].PointerToRawData;
newexe-=srcressz;
_IMAGE_DOS_HEADER stubN;
memcpy(&stubN,newexe,sizeof(_IMAGE_DOS_HEADER));
newexe+=stubN.e_lfanew;
newexe+=sizeof(_IMAGE_NT_HEADERS);
memcpy(newexe,fixedsections,(targetimage->FileHeader.NumberOfSections*sizeof(_IMAGE_SECTION_HEADER)));
newexe-=stubN.e_lfanew+sizeof(_IMAGE_NT_HEADERS);
HANDLE out=CreateFileA(target,GENERIC_WRITE,FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
DWORD wb;
WriteFile(out,newexe,SZ.QuadPart+resesDelta,&wb,0);
CloseHandle(out);
break;
}
}
}
else
{
printf("Error open target exe");
}
delete srcimage;
delete targetimage;
delete []srcreses;
return(true);
} |