This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
config.cpp
337 lines (295 loc) · 10.9 KB
/
config.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Copyright (c) 2015 Piotr Stolarz
dumpext: PE files fix, dump & analysis WinDbg extension
Distributed under the GNU General Public License (the License)
see accompanying file LICENSE for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
*/
#include "common.h"
#include "config.h"
static char prop_file[MAX_PATH+1];
const char *PROP_FILE = &prop_file[0];
static char home_path[MAX_PATH+1];
const char *HOME_PATH = &home_path[0];
/* Sections & parameters names
*/
const char *PROP_SECT_OPTH = "optional_header";
const char *PROP_OPTH_ENTRY_POINT = "AddressOfEntryPoint";
const char *PROP_OPTH_BASE_CODE = "BaseOfCode";
const char *PROP_OPTH_BASE_DATA = "BaseOfData";
const char *PROP_SECT_DIRS = "directories";
const char *PROP_DIRS_EXP_RVA = "ExportTab.rva";
const char *PROP_DIRS_EXP_SZ = "ExportTab.size";
const char *PROP_DIRS_IDT_RVA = "ImportTab.rva";
const char *PROP_DIRS_IDT_SZ = "ImportTab.size";
const char *PROP_DIRS_RSRC_RVA = "ResourceTab.rva";
const char *PROP_DIRS_RSRC_SZ = "ResourceTab.size";
const char *PROP_DIRS_EXPT_RVA = "ExceptionTab.rva";
const char *PROP_DIRS_EXPT_SZ = "ExceptionTab.size";
const char *PROP_DIRS_CERT_RVA = "CertificateTab.rva";
const char *PROP_DIRS_CERT_SZ = "CertificateTab.szie";
const char *PROP_DIRS_RELOC_RVA = "BaseRelocTab.rva";
const char *PROP_DIRS_RELOC_SZ = "BaseRelocTab.size";
const char *PROP_DIRS_DBG_RVA = "Debug.rva";
const char *PROP_DIRS_DBG_SZ = "Debug.size";
const char *PROP_DIRS_ARCH_RVA = "Architecture.rva";
const char *PROP_DIRS_ARCH_SZ = "Architecture.size";
const char *PROP_DIRS_GPTR_RVA = "GlobalPtr.rva";
const char *PROP_DIRS_GPTR_SZ = "GlobalPtr.size";
const char *PROP_DIRS_TLS_RVA = "TLSTab.rva";
const char *PROP_DIRS_TLS_SZ = "TLSTab.size";
const char *PROP_DIRS_CFG_RVA = "LoadConfigTab.rva";
const char *PROP_DIRS_CFG_SZ = "LoadConfigTab.size";
const char *PROP_DIRS_BOUND_RVA = "BoundImportTab.rva";
const char *PROP_DIRS_BOUND_SZ = "BoundImportTab.size";
const char *PROP_DIRS_IAT_RVA = "IAT.rva";
const char *PROP_DIRS_IAT_SZ = "IAT.size";
const char *PROP_DIRS_DELAY_RVA = "DelayImportDesc.rva";
const char *PROP_DIRS_DELAY_SZ = "DelayImportDesc.size";
const char *PROP_DIRS_CLR_RVA = "CLRRuntimeHeader.rva";
const char *PROP_DIRS_CLR_SZ = "CLRRuntimeHeader.size";
const char *PROP_SECT_SECTS = "sections";
const char *PROP_SECTS_DEL_TRAILING_SECS = "RemoveTrailingSections";
const char *PROP_SECTS_NAME = "Name";
const char *PROP_SECTS_CHARACTER = "Characteristics";
const char *PROP_SECTS_VSZ = "VirtualSize";
const char *PROP_SECTS_RVA = "VirtualAddress";
const char *PROP_SECTS_RSZ = "SizeOfRawData";
const char *PROP_SECTS_DMPCONT = "DumpedContent";
const char *PROP_SECT_IMPFIX = "imports_fix";
const char *PROP_IMPFIX_NO_PADD_NAMES = "NoPaddNames";
const char *PROP_IMPFIX_NO_ILTS = "NoILTs";
const char *PROP_IMPFIX_HN_TAB_RVA = "FollowIDT.HintNameTab.rva";
const char *PROP_IMPFIX_NAME_TAB_RVA = "FollowIDT.NameTab.rva";
const char *PROP_SECT_DUMP = "dump_pe";
const char *PROP_DUMP_OUTPUT = "OutFile";
const char *PROP_DUMP_SET_CRC = "SetCrc";
const char *PROP_DUMP_SAVE_HDR_SPACE = "SaveHeaderSpace";
const char *PROP_DUMP_BIND_IMPORTS = "BindImports";
const char *PROP_SECT_CONFLSPEC = "mod_conflicts_reslv";
const char *PROP_SECT_IMPSPEC = "imports";
const char *PROP_IMPSPEC_IAT_RVA = "iat_rva";
const char *PROP_SECT_RSRCFIX = "rsrc_fix";
const char *PROP_RSRCFIX_RECOVER = "RecoverRsrc";
const char *PROP_RSRCFIX_RSRC_RVA = PROP_DIRS_RSRC_RVA;
const char *PROP_RSRCFIX_PADD = "PaddRsrc";
const char *PROP_RSRCFIX_TMPFILE = "TmpRsrcFile";
const char *PROP_RSRCFIX_KEEP_TMPFILE = "KeepTmpRsrcFile";
/* Special parameters values & default values
*/
const char *OUT_DUMP_DEF_FILE = "dump.out";
const char *OUT_TMP_RSRC_DEF_FILE = "$rsrc.tmp"; /* in the tmp dir */
const char *IDT_AFTER_IAT = "after_iat";
const char *RSZ_AS_VSZ = "vsize";
const char *RSZ_AUTO = "auto";
const char *DMPCONT_MEM = "memory";
const char *DMPCONT_ZEROS = "zeros";
const char *DMPCONT_FILE = "file:";
/* "set" values hash tab */
static const str_num_t setvals_ht[] =
{
{"no", set_no},
{"as_original", set_as_original},
{"always", set_always},
{NULL}
};
const str_num_t *SETVALS_HT = &setvals_ht[0];
/* "rsrc recovery" values hash tab */
static const str_num_t rsrcrvvals_ht[] =
{
{"no", rsrcrv_no},
{"yes", rsrcrv_yes},
{"detect", rsrcrv_detect},
{NULL}
};
const str_num_t *RSRCRVVALS_HT = &rsrcrvvals_ht[0];
/* "rsrc padding" values hash tab */
static const str_num_t paddvals_ht[] =
{
{"no", padd_no},
{"word", padd_w},
{"dword", padd_dw},
{"auto", padd_auto},
{NULL}
};
const str_num_t *PADDVALS_HT = &paddvals_ht[0];
/* "section characteristics" values hash tab */
static const str_num_t secchrvals_ht[] =
{
{"no_pad", IMAGE_SCN_TYPE_NO_PAD},
{"code", IMAGE_SCN_CNT_CODE},
{"init_data", IMAGE_SCN_CNT_INITIALIZED_DATA},
{"uninit_data", IMAGE_SCN_CNT_UNINITIALIZED_DATA},
{"lnk_other", IMAGE_SCN_LNK_OTHER},
{"lnk_info", IMAGE_SCN_LNK_INFO},
{"lnk_remove", IMAGE_SCN_LNK_REMOVE},
{"lnk_comdat", IMAGE_SCN_LNK_COMDAT},
{"no_defer_spec_exc", IMAGE_SCN_NO_DEFER_SPEC_EXC},
{"gp_refer", IMAGE_SCN_GPREL},
{"purgable", IMAGE_SCN_MEM_PURGEABLE},
{"locked", IMAGE_SCN_MEM_LOCKED},
{"preload", IMAGE_SCN_MEM_PRELOAD},
{"algn_bit1", IMAGE_SCN_ALIGN_1BYTES},
{"algn_bit2", IMAGE_SCN_ALIGN_2BYTES},
{"algn_bit3", IMAGE_SCN_ALIGN_8BYTES},
{"algn_bit4", IMAGE_SCN_ALIGN_128BYTES},
{"lnk_nreloc_ovfl", IMAGE_SCN_LNK_NRELOC_OVFL},
{"discardable", IMAGE_SCN_MEM_DISCARDABLE},
{"not_cached", IMAGE_SCN_MEM_NOT_CACHED},
{"not_paged", IMAGE_SCN_MEM_NOT_PAGED},
{"shared", IMAGE_SCN_MEM_SHARED},
{"exec", IMAGE_SCN_MEM_EXECUTE},
{"read", IMAGE_SCN_MEM_READ},
{"write", IMAGE_SCN_MEM_WRITE},
{NULL}
};
const str_num_t *SECCHRVALS_HT = &secchrvals_ht[0];
/* "file header characteristics" values hash tab */
static const str_num_t flchrvals_ht[] =
{
{"reloc_strip", IMAGE_FILE_RELOCS_STRIPPED},
{"exe", IMAGE_FILE_EXECUTABLE_IMAGE},
{"lines_strip", IMAGE_FILE_LINE_NUMS_STRIPPED},
{"syms_strip", IMAGE_FILE_LOCAL_SYMS_STRIPPED},
{"aggr_ws_trim", IMAGE_FILE_AGGRESIVE_WS_TRIM},
{"big_addr_aware", IMAGE_FILE_LARGE_ADDRESS_AWARE},
{"little_end", IMAGE_FILE_BYTES_REVERSED_LO},
{"32_bit", IMAGE_FILE_32BIT_MACHINE},
{"dbg_strip", IMAGE_FILE_DEBUG_STRIPPED},
{"remov_media", IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP},
{"net_media", IMAGE_FILE_NET_RUN_FROM_SWAP},
{"system", IMAGE_FILE_SYSTEM},
{"dll", IMAGE_FILE_DLL},
{"uniproc", IMAGE_FILE_UP_SYSTEM_ONLY},
{"big_end", IMAGE_FILE_BYTES_REVERSED_HI},
{NULL}
};
const str_num_t *FLCHRVALS_HT = &flchrvals_ht[0];
/* "dll characteristics" values hash tab */
static const str_num_t dllchrvals_ht[] =
{
{"dyn_base", IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE},
{"force_integr", IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY},
{"nx_compat", IMAGE_DLLCHARACTERISTICS_NX_COMPAT},
{"no_isolation", IMAGE_DLLCHARACTERISTICS_NO_ISOLATION},
{"no_seh", IMAGE_DLLCHARACTERISTICS_NO_SEH},
{"no_bind", IMAGE_DLLCHARACTERISTICS_NO_BIND},
{"wdm", IMAGE_DLLCHARACTERISTICS_WDM_DRIVER},
{"term_aware", IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE},
{NULL}
};
const str_num_t *DLLCHRVALS_HT = &dllchrvals_ht[0];
/* "memory info" values hash tab */
static const str_num_t meminfovals_ht[] =
{
{"PAGE_NOACCESS", PAGE_NOACCESS},
{"PAGE_READONLY", PAGE_READONLY},
{"PAGE_READWRITE", PAGE_READWRITE},
{"PAGE_WRITECOPY", PAGE_WRITECOPY},
{"PAGE_EXECUTE", PAGE_EXECUTE},
{"PAGE_EXECUTE_READ", PAGE_EXECUTE_READ},
{"PAGE_EXECUTE_READWRITE", PAGE_EXECUTE_READWRITE},
{"PAGE_EXECUTE_WRITECOPY", PAGE_EXECUTE_WRITECOPY},
{"PAGE_GUARD", PAGE_GUARD},
{"PAGE_NOCACHE", PAGE_NOCACHE},
{"PAGE_WRITECOMBINE", PAGE_WRITECOMBINE},
{"MEM_COMMIT", MEM_COMMIT},
{"MEM_RESERVE", MEM_RESERVE},
{"MEM_DECOMMIT", MEM_DECOMMIT},
{"MEM_RELEASE", MEM_RELEASE},
{"MEM_FREE", MEM_FREE},
{"MEM_PRIVATE", MEM_PRIVATE},
{"MEM_MAPPED", MEM_MAPPED},
{"MEM_RESET", MEM_RESET},
{"MEM_TOP_DOWN", MEM_TOP_DOWN},
{"MEM_WRITE_WATCH", MEM_WRITE_WATCH},
{"MEM_PHYSICAL", MEM_PHYSICAL},
{"MEM_ROTATE", MEM_ROTATE},
{"MEM_LARGE_PAGES", MEM_LARGE_PAGES},
{"MEM_4MB_PAGES", MEM_4MB_PAGES},
{"SEC_IMAGE", SEC_IMAGE},
{"SEC_PROTECTED_IMAGE", SEC_PROTECTED_IMAGE},
{"SEC_RESERVE", SEC_RESERVE},
{"SEC_COMMIT", SEC_COMMIT},
{"SEC_NOCACHE", SEC_NOCACHE},
{"SEC_WRITECOMBINE", SEC_WRITECOMBINE},
{NULL}
};
const str_num_t *MEMINFOVALS_HT = &meminfovals_ht[0];
/* exported; see header for details */
DWORD get_ht_num(const str_num_t *ht, const char *str, DWORD def_val)
{
DWORD ret = def_val;
if (str) {
for (; ht->str; ht++) {
if (!strcmpi(ht->str, str)) { ret = ht->num; break; }
}
}
return ret;
}
/* exported; see header for details */
const char *get_ht_str(const str_num_t *ht, DWORD num, const char *def_val)
{
const char *ret = def_val;
for (; ht->str; ht++) {
if (ht->num == num) { ret = ht->str; break; }
}
return ret;
}
/* exported; see header for details */
void print_flags(const str_num_t *ht, DWORD flags, UINT bits)
{
if (flags) {
char sep=' ';
dbgprintf(" ;");
for (DWORD bt=1, i=0; i<bits; bt=bt<<1, i++) {
if (flags & bt) {
const char *flg_str = get_ht_str(ht, bt, NULL);
if (flg_str) { dbgprintf("%c%s", sep, flg_str); sep='|'; }
}
}
}
dbgprintf("\n");
}
/* exported; see header for details */
DWORD parse_flags(const str_num_t *ht, char *pc_flags)
{
DWORD flags=0;
for (; *pc_flags; pc_flags++)
{
if (isspace(*pc_flags) || *pc_flags=='|') continue;
char *pc_end=strchr(pc_flags, '|'), *pc_cont=NULL;
if (!pc_end) pc_end=pc_flags+strlen(pc_flags);
else pc_cont=pc_end;
for (pc_end--; pc_flags<pc_end && isspace(*pc_end); pc_end--);
*(++pc_end)=0;
DWORD flag=get_ht_num(ht, pc_flags, 0);
if (!flag) flag=strtoul(pc_flags, NULL, 0);
flags|=flag;
if (pc_cont) pc_flags=pc_cont;
else break;
}
return flags;
}
/* exported; see header for details */
void init_config(HINSTANCE hinstDLL)
{
char dll_fname[MAX_PATH+1];
/* extract home dir */
if (GetModuleFileName(hinstDLL, dll_fname, sizeof(dll_fname)) > 0)
{
*(name_from_path(dll_fname))=0;
strcpy(home_path, dll_fname);
} else
*home_path=0;
_snprintf(prop_file, sizeof(prop_file), "%s%s", home_path, "dumpext.conf");
prop_file[sizeof(prop_file)-1] = 0;
}
/* exported; see header for details */
void set_prop_file(const char* pc_prop_file)
{
strncpy(prop_file, pc_prop_file, sizeof(prop_file));
prop_file[sizeof(prop_file)-1]=0;
}