-
Notifications
You must be signed in to change notification settings - Fork 3
/
toggle-pie.mm
400 lines (344 loc) · 11.8 KB
/
toggle-pie.mm
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
////////////////////////////////////////////////////////////////////////////////
// HEADERS //
////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <mach-o/loader.h>
////////////////////////////////////////////////////////////////////////////////
// ENUMS //
////////////////////////////////////////////////////////////////////////////////
typedef enum ARCH_ENUM
{
ARCH_32_BIT,
ARCH_64_BIT
} ARCH;
////////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS //
////////////////////////////////////////////////////////////////////////////////
// For pretty printing of bytes.
void hexify(unsigned char *data, uint32_t size)
{
while(size--)
{
printf("%02x", *data++);
}
}
// For pretty printing of header flags.
void print_header_flags(mach_header *header_32, mach_header_64 *header_64)
{
if (header_32)
{
hexify((unsigned char *)&header_32->flags, sizeof(header_32->flags));
printf("\n");
}
else if (header_64)
{
hexify((unsigned char *)&header_64->flags, sizeof(header_64->flags));
printf("\n");
}
}
// Helper for getting file size.
// Note: Assumes the file is open for reading and doesn't close it.
size_t get_file_size(FILE *input_file)
{
// Declarations.
size_t file_size;
// Seek to the end of the file and count the
// number of bytes from the start.
fseek(input_file, 0, SEEK_END);
file_size = ftell(input_file);
rewind(input_file);
// Return.
return file_size;
}
// Helper for reading files into a buffer. Returns 0 if successful, 1 otherwise.
// Note: Does NOT close the passed file, assumes you have read privileges.
int read_file_into_buffer(FILE *input_file, char file_buffer[])
{
// Declarations.
size_t file_size;
// Get the file size.
file_size = get_file_size(input_file);
// Copy the file into the buffer.
if (fread(file_buffer, sizeof(char), file_size, input_file) !=
file_size)
{
return 1;
}
// Successful.
return 0;
}
// Helper for backing up files. Return the writeable file handler for the file
// that is to be edited. Also loads the contents of the file into the buffer.
FILE * backup_file(char *file_name, char* &file_buffer)
{
// Declarations.
FILE *original_file;
FILE *backup_file;
char backup_suffix[5] = ".bak";
char backup_file_name[strlen(file_name) + strlen(backup_suffix) + 1];
size_t file_size;
// Construct new backup filename by appending the suffix.
strlcpy(backup_file_name, file_name, sizeof(backup_file_name));
strlcat(backup_file_name, backup_suffix, sizeof(backup_file_name));
// Open the file for editing.
original_file = fopen(file_name, "r+");
if (!original_file)
{
printf("[ERROR] Unable to open the binary file.\n");
return NULL;
}
// Get file size.
file_size = get_file_size(original_file);
// Open the backup file.
backup_file = fopen(backup_file_name, "w");
if (!backup_file)
{
printf("[ERROR] Could not open the backup file.\n");
return NULL;
}
// Allocate space for the buffer.
file_buffer = (char *)calloc(1, file_size + 1);
// Copy the file into the temporary copy buffer.
if (read_file_into_buffer(original_file, file_buffer))
{
printf("[ERROR] Could not read the binary file into memory.\n");
return NULL;
}
// Write the copy buffer to the backup file.
if (fwrite(file_buffer, sizeof(char), file_size, backup_file) != file_size)
{
// Couldn't write the correct number of bytes.
printf("[ERROR] Could not copy file.\n");
return NULL;
}
// Close the backup file.
fclose(backup_file);
// Return the file handler for the original file.
// Note: this handler is open for editing.
return original_file;
}
// Helper for toggling the PIE bit.
void toggle_pie_bit_helper(uint32_t &flags)
{
// Toggles the right bit.
flags ^= 1 << 21;
}
// Helper for flipping the PIE bit in the given binary file buffer
// for the selected architecture. The binary file is expected to be loaded into
// the passed buffer.
// Returns:
// 0 - Successfully flipped the PIE bit.
// 1 - Couldn't find the magic number.
// 2 - Some other error.
int flip_pie(FILE *binary_file, char binary_file_buffer[], ARCH arch)
{
// Declarations.
struct mach_header header_32;
struct mach_header_64 header_64;
int header_size;
uint32_t magic;
unsigned char leading_byte;
char *leading_byte_location;
size_t file_size;
// Check the buffer.
if (!binary_file_buffer)
{
printf("[ERROR] Buffer is not loaded with the binary file data.\n");
return 2;
}
// Set the magic number and leading byte for the selected architecture.
magic = (arch == ARCH_32_BIT) ? MH_MAGIC : MH_MAGIC_64;
leading_byte = magic & 0xff;
// Set the header size.
header_size =
(arch == ARCH_32_BIT) ? sizeof(mach_header) : sizeof(mach_header_64);
// Get file size.
file_size = get_file_size(binary_file);
// Look for the magic number in the buffer.
leading_byte_location = binary_file_buffer;
do
{
// Determine the amount of space not looked at in the file yet.
unsigned long remaining_space =
binary_file_buffer +
file_size -
leading_byte_location;
// If there isn't enough space for the header struct to fit in the
// file, break.
if (remaining_space < header_size)
{
break;
}
// Look for the beginning of the magic number.
leading_byte_location =
(char *)memchr(leading_byte_location, leading_byte, remaining_space);
// Break if couldn't find the magic number.
if (!leading_byte_location)
{
break;
}
// Found leading byte. See if the four
// bytes match the magic number.
if (memcmp(leading_byte_location, &magic, 4) == 0)
{
// Found the location of the magic number!
printf("Original Mach-O header: ");
hexify((unsigned char *)leading_byte_location, header_size);
printf("\n");
// Seek to the header location in the binary file.
fseek(
binary_file,
leading_byte_location - binary_file_buffer,
SEEK_SET
);
// Read in the header structure from the file.
if (arch == ARCH_32_BIT)
{
if(fread(&header_32, header_size, 1, binary_file) == 0)
{
printf("[ERROR] Could not read header from binary file.\n");
return 2;
}
// Print out the original flags.
printf("Original Mach-O header flags: ");
print_header_flags(&header_32, NULL);
// Flip the PIE bit.
printf("Flipping the PIE...\n");
toggle_pie_bit_helper(header_32.flags);
// Print out the new flags.
printf("New Mach-O header flags: ");
print_header_flags(&header_32, NULL);
}
else
{
if(fread(&header_64, header_size, 1, binary_file) == 0)
{
printf("[ERROR] Could not read header from binary file.\n");
return 2;
}
// Print out the original flags.
printf("Original Mach-O header flags: ");
print_header_flags(NULL, &header_64);
// Flip the PIE bit.
printf("Flipping the PIE...\n");
toggle_pie_bit_helper(header_64.flags);
// Print out the new flags.
printf("New Mach-O header flags: ");
print_header_flags(NULL, &header_64);
}
// Seek back to the header location in the binary file.
fseek(
binary_file,
leading_byte_location - binary_file_buffer,
SEEK_SET
);
// Write the new header to the binary file.
if (arch == ARCH_32_BIT)
{
if(fwrite(&header_32, sizeof(char), sizeof(mach_header), binary_file) == 0)
{
printf("[ERROR] Could not write the new header to the binary file.\n");
return 2;
}
}
else
{
if(fwrite(&header_64, sizeof(char), sizeof(mach_header_64), binary_file) == 0)
{
printf("[ERROR] Could not write the new header to the binary file.\n");
return 2;
}
}
// Success!
return 0;
}
else
{
// Didn't find the leading byte. Increment by one.
leading_byte_location += 1;
}
} while (true);
// Couldn't find the magic number and associated header.
return 1;
}
// Helper for performing the PIE flip step.
void perform_pie_flip
(
FILE *binary_file,
char binary_file_buffer[],
ARCH arch,
int step_number
)
{
// Declarations.
int pie_flip_result;
char bit_string_32[7] = "32-bit";
char bit_string_64[7] = "64-bit";
char *bit_string;
// Set the bit string.
bit_string = (arch == ARCH_32_BIT) ? bit_string_32 : bit_string_64;
// Show step beginning.
printf("[STEP %d] Flip the %s PIE...\n", step_number, bit_string);
// Perform the flip!
pie_flip_result = flip_pie(binary_file, binary_file_buffer, arch);
// Parse the flip result.
if (pie_flip_result == 0)
{
printf(
"[STEP %d] Successfully flipped the %s PIE.\n",
step_number,
bit_string
);
}
else if (pie_flip_result == 1)
{
printf(
"[STEP %d] Could not find the %s Mach-O header.\n",
step_number,
bit_string
);
printf(" This is expected if the binary isn't compiled for\n");
printf(" a %s architecture.\n", bit_string);
}
else
{
printf(
"[STEP %d] Bad things happened trying to flip the PIE.\n",
step_number
);
}
}
////////////////////////////////////////////////////////////////////////////////
// MAIN //
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv, char **envp)
{
// Declarations.
FILE *binary_file;
char *binary_file_buffer;
// Check if the correct number of arguments were given.
if (argc <= 1)
{
printf("Usage: %s <path_to_binary>\n", argv[0]);
return EXIT_FAILURE;
}
// Now backup the file before doing anything.
printf("[STEP 1] Backing up the binary file...\n");
binary_file = backup_file(argv[1], binary_file_buffer);
if (!binary_file)
{
printf("[ERROR] Exiting because could not back up the binary file.\n");
return EXIT_FAILURE;
}
printf("[STEP 1] Binary file successfully backed up to %s.bak\n", argv[1]);
printf("\n");
// Flip the 32-bit PIE.
perform_pie_flip(binary_file, binary_file_buffer, ARCH_32_BIT, 2);
printf("\n");
// Flip the 64-bit PIE.
perform_pie_flip(binary_file, binary_file_buffer, ARCH_64_BIT, 3);
// Close the binary file.
fclose(binary_file);
// Shouldn't get here! Failure.
return EXIT_SUCCESS;
}