-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmkpoly.c
88 lines (69 loc) · 1.56 KB
/
mkpoly.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
#include "mkpoly.h"
int main(int argc, char* argv[])
{
if (argc < 5)
{
help();
return 1;
}
FILE* source = fopen(argv[1], "rb");
if (!source)
{
fprintf(stderr, "failed to open %s\n", argv[1]);
return 2;
}
size_t size;
fseek(source, 0, SEEK_END);
size = ftell(source);
fseek(source, 0, SEEK_SET);
uint8_t *bin = malloc(size);
if (!bin)
{
fprintf(stderr, "failed to allocate binary data");
fclose(source);
return 3;
}
if (!fread(bin, size, 1, source))
{
fprintf(stderr, "binary file reading failed\n");
fclose(source);
free(bin);
return 4;
}
fclose(source);
size_t coff = strtol(argv[2], NULL, 16);
size_t csize = strtol(argv[3], NULL, 16);
size_t eoff = strtol(argv[4], NULL, 16);
if (polyeng(bin, coff, csize, eoff))
{
fprintf(stderr, "polymorphic engine error\n");
free(bin);
return 5;
}
char filename[FILENAME_MAX];
snprintf(filename, FILENAME_MAX, "%s.crypt", argv[1]);
FILE *dest = fopen(filename, "wb");
if (!dest)
{
fprintf(stderr, "failed to open %s\n", filename);
free(bin);
return 6;
}
if (!fwrite(bin, size, 1, dest))
{
fprintf(stderr, "failed to write polymorphic code into %s\n", filename);
free(bin);
return 7;
}
free(bin);
return 0;
}
void help()
{
printf("usage: mkpoly ");
printf("<source> <crypt-off> <crypt-size> <engine-off>\n");
printf("<source> the filename of the binary file\n");
printf("<crypt-off> the offset of the section to crypt\n");
printf("<crypt-size> the size of the section to crypt\n");
printf("<engine-off> the offset where to place the decryptor\n");
}