Browse code

added alignment functionality in rebuildpe

Kevin Lin authored on 2014/12/06 04:32:05
Showing 3 changed files
... ...
@@ -891,7 +891,7 @@ int unmew11(char *src, int off, int ssize, int dsize, uint32_t base, uint32_t va
891 891
 		section[0].raw = 0; section[0].rva = vadd;
892 892
 		section[0].rsz = section[0].vsz = dsize;
893 893
 	}
894
-	if (!cli_rebuildpe(src, section, i, base, entry_point - base, 0, 0, filedesc))
894
+	if (!cli_rebuildpe_align(src, section, i, base, entry_point - base, 0, 0, filedesc, 0x1000))
895 895
 	{
896 896
 		cli_dbgmsg("MEW: Rebuilding failed\n");
897 897
 		free(section);
... ...
@@ -121,6 +121,11 @@ struct IMAGE_PE_HEADER {
121 121
 
122 122
 int cli_rebuildpe(char *buffer, struct cli_exe_section *sections, int sects, uint32_t base, uint32_t ep, uint32_t ResRva, uint32_t ResSize, int file)
123 123
 {
124
+  return cli_rebuildpe_align(buffer, sections, sects, base, ep, ResRva, ResSize, file, 0);
125
+}
126
+
127
+int cli_rebuildpe_align(char *buffer, struct cli_exe_section *sections, int sects, uint32_t base, uint32_t ep, uint32_t ResRva, uint32_t ResSize, int file, uint32_t align)
128
+{
124 129
   uint32_t datasize=0, rawbase=PESALIGN(0x148+0x80+0x28*sects, 0x200);
125 130
   char *pefile=NULL, *curpe;
126 131
   struct IMAGE_PE_HEADER *fakepe;
... ...
@@ -131,8 +136,12 @@ int cli_rebuildpe(char *buffer, struct cli_exe_section *sections, int sects, uin
131 131
   if(sects+gotghost > 96)
132 132
     return 0;
133 133
 
134
-  for (i=0; i < sects; i++)
135
-    datasize+=PESALIGN(sections[i].rsz, 0x200);
134
+  if (!align)
135
+    for (i=0; i < sects; i++)
136
+      datasize+=PESALIGN(sections[i].rsz, 0x200);
137
+  else
138
+    for (i=0; i < sects; i++)
139
+      datasize+=PESALIGN(PESALIGN(sections[i].rsz, align), 0x200);
136 140
 
137 141
   if(datasize > CLI_MAX_ALLOCATION)
138 142
     return 0;
... ...
@@ -163,10 +172,17 @@ int cli_rebuildpe(char *buffer, struct cli_exe_section *sections, int sects, uin
163 163
 
164 164
     for (i=0; i < sects; i++) {
165 165
       snprintf(curpe, 8, ".clam%.2d", i+1);
166
-      cli_writeint32(curpe+8, sections[i].vsz);
167
-      cli_writeint32(curpe+12, sections[i].rva);
168
-      cli_writeint32(curpe+16, sections[i].rsz);
169
-      cli_writeint32(curpe+20, rawbase);
166
+      if (!align) {
167
+        cli_writeint32(curpe+8, sections[i].vsz);
168
+        cli_writeint32(curpe+12, sections[i].rva);
169
+        cli_writeint32(curpe+16, sections[i].rsz);
170
+        cli_writeint32(curpe+20, rawbase);
171
+      } else {
172
+        cli_writeint32(curpe+8, PESALIGN(sections[i].vsz, align));
173
+        cli_writeint32(curpe+12, PESALIGN(sections[i].rva, align));
174
+        cli_writeint32(curpe+16, PESALIGN(sections[i].rsz, align));
175
+        cli_writeint32(curpe+20, rawbase);
176
+      }
170 177
       /* already zeroed
171 178
       cli_writeint32(curpe+24, 0);
172 179
       cli_writeint32(curpe+28, 0);
... ...
@@ -174,9 +190,14 @@ int cli_rebuildpe(char *buffer, struct cli_exe_section *sections, int sects, uin
174 174
       */
175 175
       cli_writeint32(curpe+0x24, 0xffffffff);
176 176
       memcpy(pefile+rawbase, buffer+sections[i].raw, sections[i].rsz);
177
-      rawbase+=PESALIGN(sections[i].rsz, 0x200);
178 177
       curpe+=40;
179
-      datasize+=PESALIGN(sections[i].vsz, 0x1000);
178
+      if (!align) {
179
+        rawbase+=PESALIGN(sections[i].rsz, 0x200);
180
+        datasize+=PESALIGN(sections[i].vsz, 0x1000);
181
+      } else {
182
+        rawbase+=PESALIGN(PESALIGN(sections[i].rsz, align), 0x200);
183
+        datasize+=PESALIGN(PESALIGN(sections[i].vsz, align), 0x1000);
184
+      }
180 185
     }
181 186
     fakepe->SizeOfImage = EC32(datasize);
182 187
   } else {
... ...
@@ -25,5 +25,6 @@
25 25
 #include "execs.h"
26 26
 
27 27
 int cli_rebuildpe(char *, struct cli_exe_section *, int, uint32_t, uint32_t, uint32_t, uint32_t, int);
28
+int cli_rebuildpe_align(char *, struct cli_exe_section *, int, uint32_t, uint32_t, uint32_t, uint32_t, int, uint32_t);
28 29
 
29 30
 #endif