-
Notifications
You must be signed in to change notification settings - Fork 0
/
mz.c
312 lines (260 loc) · 8.95 KB
/
mz.c
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
/*
Модуль обработки MZ-файлов.
Панасенко Дмитрий Игоревич, Маткин Илья Александрович 30.04.2020
*/
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include "mz.h"
#include "system.h"
//
// Загружает файл в память и заполняет структуру MzHeaders.
//
BOOL LoadMZFile(char *filename, MZHeaders *mz) {
mz->filename = filename;
// открываем файл (получаем файловый дескриптор)
mz->fd = CreateFileA(
filename, // имя файла
GENERIC_READ | GENERIC_WRITE, // права доступа
0,
NULL,
OPEN_EXISTING, // открываемый файл должен существовать
FILE_ATTRIBUTE_NORMAL,
NULL);
if (mz->fd == INVALID_HANDLE_VALUE) {
PrintLastErrorMessage();
return FALSE;
}
mz->filesize = GetFileSize(mz->fd, NULL);
// создаем проекцию файла в память
mz->mapd = CreateFileMappingA(mz->fd, NULL, PAGE_READWRITE, 0, mz->filesize, NULL);
if (mz->mapd == NULL) {
CloseHandle(mz->fd);
printf("Error create file map\n");
PrintLastErrorMessage();
return FALSE;
}
// отображаем проекцию в память
mz->mem = (PBYTE) MapViewOfFile(mz->mapd, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
if (mz->mem == NULL) {
CloseHandle(mz->fd);
CloseHandle(mz->mapd);
printf("Error mapping file\n");
PrintLastErrorMessage();
return FALSE;
}
// указатель на заголовок mz
mz->doshead = (PDOS_HEADER) mz->mem;
// проверка сигнатуры
if (mz->doshead->e_magic != MZ_SIGNATURE) {
UnmapViewOfFile(mz->mem);
CloseHandle(mz->fd);
CloseHandle(mz->mapd);
printf("Error DOS signature\n");
return FALSE;
}
// сегменты
mz->data = mz->mem + (mz->doshead->e_cparhdr * PARAGRAPH_SIZE);
mz->stack = mz->data + (mz->doshead->e_ss * PARAGRAPH_SIZE);
mz->code = mz->data + (mz->doshead->e_cs * PARAGRAPH_SIZE);
if (mz->doshead->e_ss < mz->doshead->e_cs) {
mz->data_size = mz->stack - mz->data;
mz->stack_size = mz->code - mz->stack;
mz->code_size = (mz->mem + mz->filesize) - mz->code;
}
else {
mz->data_size = mz->code - mz->data;
mz->code_size = mz->stack - mz->code;
mz->stack_size = (mz->mem + mz->filesize) - mz->stack;
}
// таблица релоков
mz->relocs = (PRELOC_TABLE_ENTRY) (mz->mem + mz->doshead->e_lfarlc);
mz->relocs_size = mz->doshead->e_crlc;
return TRUE;
}
//
// Функция применения релоков.
//
void ApplyRelocs(MZHeaders *mz, WORD imageBase) {
/*WORD tmp0 = mz->code[2];
mz->code[2] = mz->code[1];
mz->code[1] = tmp0;
WORD tmp = mz->code[7];
mz->code[7] = mz->code[6];
mz->code[6] = tmp;*/
unsigned char num[2];
num[0] = imageBase >> 8;
num[1] = imageBase & 0xFF;
// if uncomment above, swap num
mz->code[1] += num[1];
mz->code[2] += num[0];
mz->code[6] += num[1];
mz->code[7] += num[0];
}
//
// Функция снятия релоков.
//
void RemoveRelocs(MZHeaders *mz, WORD imageBase) {
unsigned char num[2];
num[0] = imageBase >> 8;
num[1] = imageBase & 0xFF;
// if uncomment below, swap num
mz->code[1] -= num[1];
mz->code[2] -= num[0];
mz->code[6] -= num[1];
mz->code[7] -= num[0];
}
//
// Выгружает MZ-файл (освобождает ресурсы).
//
void UnloadMZFile(MZHeaders *mz) {
UnmapViewOfFile(mz->mem);
CloseHandle(mz->mapd);
CloseHandle(mz->fd);
}
//
// Выравнивает значение с кратностью align к верхней границе.
//
DWORD AlignToTop(DWORD value, DWORD align) {
DWORD mask = ~ (align - 1);
return (value + align - 1) & mask;
}
//
// Выравнивает значение с кратностью align к нижней границе.
//
DWORD AlignToBottom(DWORD value, DWORD align) {
DWORD mask = ~ (align - 1);
return value & mask;
}
//
// Выводит одну строку шестнадцатиричных символов.
//
void PrintHexLine(unsigned char *line, unsigned int lineSize) {
unsigned int i;
// вывод hex-кодов символов
for (i = 0; i < lineSize; ++i) {
printf("%02X ", line[i]);
}
printf(" | ");
// вывод ASCII-символов
for (i = 0; i < lineSize; ++i) {
if (isprint(line[i])) {
printf("%c", line[i]);
}
else {
printf(".");
}
}
}
//
// Выводит дамп памяти в шестнадцатиричном виде.
//
void PrintSegmentDump(const unsigned char *memory, unsigned int bytesFromMemoryCount, unsigned bytesTotalCount,
unsigned int segmentAddress, unsigned int bytesPerLine, char *prefixName) {
unsigned int bytesIndex;
unsigned char *line = (unsigned char*) malloc(bytesPerLine);
unsigned int lineIndex = 0;
for (bytesIndex = 0; bytesIndex < bytesTotalCount; bytesIndex++) {
// если начало строки - выводим имя сегмента и адрес
if (bytesIndex % bytesPerLine == 0) {
printf("%s %04X:%04X ", prefixName,
segmentAddress + (SIZE_OF_SEG_P * (bytesIndex / SIZE_OF_SEG_B)), bytesIndex % SIZE_OF_SEG_B);
}
// читаем символ из памяти
if (bytesIndex < bytesFromMemoryCount) {
line[lineIndex++] = memory[bytesIndex];
}
// если символы в памяти закончились - заполняем нулями
else {
line[lineIndex++] = 0;
}
// если конец очередной строки
if ((bytesIndex + 1) % bytesPerLine == 0) {
PrintHexLine(line, bytesPerLine);
printf("\n");
lineIndex = 0;
}
}
free (line);
}
//
// Вывод информации о DOS-заголовке.
//
void PrintDOSHeader(MZHeaders *mz) {
printf("DOS-header info\n");
printf("Number of bytes on last 512-byte page: %d\n", mz->doshead->e_cblp);
printf("Total number of 512-byte pages: %d\n", mz->doshead->e_cp);
printf("Header size in paragraphs: %d\n", mz->doshead->e_cparhdr);
printf("Initial SS:SP value: %04X:%04X\n", mz->doshead->e_ss, mz->doshead->e_sp);
printf("Initial CS:IP value: %04X:%04X\n", mz->doshead->e_cs, mz->doshead->e_ip);
printf("\n");
}
//
// Вывод информации о таблице релоков
//
void PrintRelocsTable(MZHeaders *mz) {
unsigned int i;
printf("Relocs table info\n");
for (i = 0; i < mz->relocs_size; i++) {
printf("%d entry: %04X:%04X\n", i + 1, mz->relocs[i].segment, mz->relocs[i].offset);
}
printf("\n");
}
//
// Выводит содержимое трёх сегментов в шестнадцатиричном виде.
//
void PrintSegments(MZHeaders *mz) {
ApplyRelocs(mz, IMAGE_BASE_SEG);
printf("Segments (with image base segment set to %04X)\n", IMAGE_BASE_SEG);
if (mz->data_size != 0) {
PrintSegmentDump(
mz->data,
mz->data_size,
AlignToTop(mz->data_size, BYTES_PER_LINE),
IMAGE_BASE_SEG,
BYTES_PER_LINE,
"data");
}
if (mz->stack < mz->code) {
if (mz->stack_size != 0) {
PrintSegmentDump(
mz->stack,
mz->stack_size,
AlignToTop(mz->stack_size, BYTES_PER_LINE),
IMAGE_BASE_SEG + mz->doshead->e_ss,
BYTES_PER_LINE,
"stck");
}
if (mz->code_size != 0) {
PrintSegmentDump(
mz->code,
mz->code_size,
AlignToTop(mz->code_size, BYTES_PER_LINE),
IMAGE_BASE_SEG + mz->doshead->e_cs,
BYTES_PER_LINE,
"code");
}
}
else {
if (mz->code_size != 0) {
PrintSegmentDump(
mz->code,
mz->code_size,
AlignToTop(mz->code_size, BYTES_PER_LINE),
IMAGE_BASE_SEG + mz->doshead->e_cs,
BYTES_PER_LINE,
"code");
}
if (mz->stack_size != 0) {
PrintSegmentDump(
mz->stack,
mz->stack_size,
AlignToTop(mz->stack_size, BYTES_PER_LINE),
IMAGE_BASE_SEG + mz->doshead->e_ss,
BYTES_PER_LINE,
"stck");
}
}
printf("\n");
RemoveRelocs(mz, IMAGE_BASE_SEG);
}