-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin2exe.c
150 lines (122 loc) · 3 KB
/
bin2exe.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#define EXTRA_BTTES_OFFSET 2
#define NUM_PAGES_OFFSET 4
#define INITIAL_IP_OFFSET 20
#define DEFAULT_IP 0x0000
static void writeLe16(unsigned char *dst, int offset, int value)
{
dst[offset] = value;
dst[offset+1] = value >> 8;
}
static void printHelp(void)
{
printf("Usage: ./bin2exe [options] input_file output_file.exe\n");
printf(" -i offset Set initial instruction pointer (IP). Default: %d\n", DEFAULT_IP);
printf(" -v Enable verbose output\n");
}
int main(int argc, char **argv)
{
FILE *infptr, *outfptr;
const char *infile, *outfile;
long insize;
int initial_ip = DEFAULT_IP;
int verbose = 0;
int opt;
int pages, extra_bytes;
unsigned char *binary;
unsigned char exe_header[32] = {
'M','Z', // SIG
0,0, // Extra bytes
0,0, // Pages
0,0, // Relocation items (none)
2,0, // Header size (in paragraphs. 32 bytes)
0x00, 0x10, // minimum paragraphs
0x00, 0x10, // required paragraphs
0x00, 0x00, // initial SS
0xFF, 0xFF, // initial SP
0x00, 0x00, // checksum
0x00, 0x00, // Initial IP
0x00, 0x00, // initial CS
0x00, 0x00, // relocation segment offset
0x00, 0x00, // overlay
0x00, 0x00, // overlay information
};
while (-1 != (opt = getopt(argc, argv, "hi:v"))) {
switch (opt)
{
case 'h':
printHelp();
return 0;
case 'i':
initial_ip = strtol(optarg, NULL, 0);
break;
case 'v':
verbose = 1;
break;
case '?':
break;
default:
fprintf(stderr, "unknown option. try -h\n");
return -1;
}
}
if (argc - optind < 2) {
fprintf(stderr, "An input and output file must be specified. See -h\n");
return -1;
}
infile = argv[optind];
outfile = argv[optind+1];
if (verbose) {
printf("Input file: %s\n", infile);
printf("Output file: %s\n", outfile);
printf("initial ip: $%04x\n", initial_ip);
}
infptr = fopen(infile, "rb");
if (!infptr) {
perror(infile);
return -1;
}
fseek(infptr, 0, SEEK_END);
insize = ftell(infptr);
if (insize > 0x10000) {
fprintf(stderr, "Input file too large (> 64k)\n");
fclose(infptr);
return -1;
}
binary = malloc(insize);
if (!binary) {
perror("could not allocate memory to load file\n");
fclose(infptr);
return -1;
}
fseek(infptr, 0, SEEK_SET);
if (fread(binary, insize, 1, infptr) != 1) {
perror("cound not read input file\n");
free(binary);
fclose(infptr);
return -1;
}
fclose(infptr);
outfptr = fopen(outfile, "wb");
if (!outfptr) {
perror(outfile);
free(binary);
return -1;
}
// update the header
pages = (insize + 511) / 512;
extra_bytes = insize - (insize / 512) * 512;
if (verbose) {
printf("Size: %ld bytes (%d pages + %d byte(s))\n", insize, pages, extra_bytes);
}
writeLe16(exe_header, INITIAL_IP_OFFSET, initial_ip);
writeLe16(exe_header, EXTRA_BTTES_OFFSET, extra_bytes);
writeLe16(exe_header, NUM_PAGES_OFFSET, pages);
fwrite(exe_header, sizeof(exe_header), 1, outfptr);
fwrite(binary, insize, 1, outfptr);
fclose(outfptr);
free(binary);
return 0;
}