2020-09-25 16:43:53 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <libcore/libcore.h>
|
|
|
|
|
2020-11-02 20:27:41 +00:00
|
|
|
#define PATCH_PRINTF(file, line, start, str) if (file != NULL) fprintf(stderr, "[PATCH]: (%s:%i) Patching (0x%04x) - "str": 0x%02x 0x%02x 0x%02x 0x%02x\n", file, line, (uint32_t) start, data[0], data[1], data[2], data[3]);
|
2020-10-01 15:08:46 +00:00
|
|
|
|
2020-10-03 16:57:58 +00:00
|
|
|
#define ORIGINAL_SIZE 8
|
2020-10-01 15:08:46 +00:00
|
|
|
|
2020-10-04 00:30:15 +00:00
|
|
|
void *_overwrite(const char *file, int line, void *start, void *target) {
|
2020-10-01 15:08:46 +00:00
|
|
|
void *original = malloc(ORIGINAL_SIZE);
|
|
|
|
memcpy(original, start, ORIGINAL_SIZE);
|
2020-09-25 16:43:53 +00:00
|
|
|
|
2020-10-03 16:57:58 +00:00
|
|
|
int thumb = ((uint32_t) start) & 1;
|
|
|
|
unsigned char *patch_data;
|
2020-09-25 16:43:53 +00:00
|
|
|
if (thumb) {
|
2020-10-03 16:57:58 +00:00
|
|
|
unsigned char patch_data_temp[4] = {0xdf, 0xf8, 0x00, 0xf0};
|
|
|
|
patch_data = patch_data_temp;
|
2020-09-25 16:43:53 +00:00
|
|
|
} else {
|
2020-10-03 16:57:58 +00:00
|
|
|
unsigned char patch_data_temp[4] = {0x04, 0xf0, 0x1f, 0xe5};
|
|
|
|
patch_data = patch_data_temp;
|
2020-09-25 16:43:53 +00:00
|
|
|
}
|
2020-10-01 15:08:46 +00:00
|
|
|
|
2020-10-03 16:57:58 +00:00
|
|
|
_patch(file, line, start, patch_data);
|
2020-10-29 16:14:51 +00:00
|
|
|
_patch_address(file, line, start + 4, target);
|
2020-10-01 15:08:46 +00:00
|
|
|
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
|
|
|
void revert_overwrite(void *start, void *original) {
|
2020-10-03 16:57:58 +00:00
|
|
|
unsigned char *data = (unsigned char *) start;
|
|
|
|
int thumb = ((uint32_t) start) & 1;
|
|
|
|
if (thumb) {
|
|
|
|
data--;
|
|
|
|
}
|
2020-10-01 15:08:46 +00:00
|
|
|
|
2020-10-03 16:57:58 +00:00
|
|
|
// Store Current Value In Temp
|
2020-10-01 15:08:46 +00:00
|
|
|
void *temp = malloc(ORIGINAL_SIZE);
|
|
|
|
memcpy(temp, data, ORIGINAL_SIZE);
|
2020-10-03 16:57:58 +00:00
|
|
|
|
|
|
|
// Insert Original Value
|
|
|
|
_patch(NULL, -1, start, original);
|
|
|
|
_patch(NULL, -1, start + 4, original + 4);
|
2020-10-04 20:45:00 +00:00
|
|
|
|
2020-10-03 16:57:58 +00:00
|
|
|
// Complete Memory Swap
|
2020-10-01 15:08:46 +00:00
|
|
|
memcpy(original, temp, ORIGINAL_SIZE);
|
|
|
|
free(temp);
|
2020-09-25 16:43:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 00:30:15 +00:00
|
|
|
void _patch(const char *file, int line, void *start, unsigned char patch[]) {
|
2020-10-03 16:57:58 +00:00
|
|
|
size_t page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
uintptr_t end = ((uintptr_t) start) + 4;
|
|
|
|
uintptr_t page_start = ((uintptr_t) start) & -page_size;
|
|
|
|
mprotect((void *) page_start, end - page_start, PROT_READ | PROT_WRITE);
|
|
|
|
|
|
|
|
unsigned char *data = (unsigned char *) start;
|
|
|
|
int thumb = ((uint32_t) start) & 1;
|
|
|
|
if (thumb) {
|
|
|
|
data--;
|
|
|
|
}
|
|
|
|
|
|
|
|
PATCH_PRINTF(file, line, start, "original");
|
|
|
|
|
2020-09-25 16:43:53 +00:00
|
|
|
memcpy(data, patch, 4);
|
2020-10-03 16:57:58 +00:00
|
|
|
|
|
|
|
PATCH_PRINTF(file, line, start, "result");
|
|
|
|
|
|
|
|
mprotect((void *) page_start, end - page_start, PROT_READ | PROT_EXEC);
|
2020-10-16 22:23:39 +00:00
|
|
|
|
2020-10-26 19:58:28 +00:00
|
|
|
// Clear ARM Instruction Cache
|
2020-10-17 21:18:18 +00:00
|
|
|
__clear_cache(start, (void *) end);
|
2020-09-25 16:43:53 +00:00
|
|
|
}
|
2020-10-02 23:28:31 +00:00
|
|
|
|
2020-10-04 00:30:15 +00:00
|
|
|
void _patch_address(const char *file, int line, void *start, void *target) {
|
2020-10-03 00:07:08 +00:00
|
|
|
uint32_t addr = (uint32_t) target;
|
|
|
|
unsigned char patch_data[4] = {addr & 0xff, (addr >> 8) & 0xff, (addr >> 16) & 0xff, (addr >> 24) & 0xff};
|
2020-10-03 16:57:58 +00:00
|
|
|
_patch(file, line, start, patch_data);
|
2020-10-02 23:28:31 +00:00
|
|
|
}
|