forked from peterfillmore/removePIE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeASLR.c
79 lines (65 loc) · 2.44 KB
/
removeASLR.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
#include <stdio.h>
#include <stdlib.h>
#include "xnu-definitions.h"
void hexlify(unsigned char *data, uint32_t size) {
while (size--) {
printf("%02x", *data++);
}
}
int main(int argc, char *argv[]) {
FILE *editedFilePtr;
struct mach_header currentHeader;
// error cases
if (argc < 1) {
puts("please enter the filename binary in the format ./removeASLR filename");
return EXIT_FAILURE;
}
if ((editedFilePtr = fopen(argv[1], "rb+")) == NULL) {
printf("error, unable to open file %s\n", argv[1]);
return EXIT_FAILURE;
}
if ((fread(¤tHeader.magic, sizeof(int32_t), 1, editedFilePtr)) == 0) {
puts("error reading magic constant in file");
return EXIT_FAILURE;
}
// check magic
if (currentHeader.magic == MH_MAGIC || currentHeader.magic == MH_MAGIC_64) {
puts("loading header...");
fseek(editedFilePtr, 0, SEEK_SET);
if ((fread(¤tHeader, sizeof(currentHeader), 1, editedFilePtr)) == 0) {
printf("error reading mach-o header");
return EXIT_FAILURE;
}
puts("looks ok");
printf("mach-o header: ");
hexlify((unsigned char *) ¤tHeader, sizeof(currentHeader));
// everything ok. create a backup
puts("\n\nbacking up application binary...");
char cpCommand[1000];
sprintf(cpCommand, "cp %s %s.bak", argv[1], argv[1]);
system(cpCommand);
puts("done");
// modify mach-o header
printf("\noriginal flags:\t");
hexlify((unsigned char *) ¤tHeader.flags, sizeof(currentHeader.flags));
printf("\ndisabling ASLR...\n");
currentHeader.flags &= ~MH_PIE;
printf("new flags:\t");
hexlify((unsigned char *) ¤tHeader.flags, sizeof(currentHeader.flags));
fseek(editedFilePtr, 0, SEEK_SET);
if ((fwrite(¤tHeader, sizeof(char), 28, editedFilePtr)) == 0) {
printf("error writing to application file");
}
printf("\n\nASLR has been disabled for %s!\n", argv[1]);
// exit and close memory
fclose(editedFilePtr);
return EXIT_SUCCESS;
} else if (currentHeader.magic == MH_CIGAM || currentHeader.magic == MH_CIGAM_64) // big endian
{
puts("file is big endian, not an iOS binary!");
return EXIT_FAILURE;
} else {
puts("file is not a Mach-O binary!");
return EXIT_FAILURE;
}
}