Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Substrate native hooking for arm32, x86 and x86_64 #243

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion edxp-core/jni/main/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ LOCAL_SRC_FILES:= \
yahfa/trampoline.c \
java_hook/java_hook.cpp \
inject/framework_hook.cpp \
inject/config_manager.cpp
inject/config_manager.cpp \
Substrate/SubstrateDebug.cpp \
Substrate/SubstrateHook.cpp \
Substrate/SubstratePosixMemory.cpp \
Substrate/hde64.c \

include $(BUILD_SHARED_LIBRARY)
38 changes: 38 additions & 0 deletions edxp-core/jni/main/Substrate/Buffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* Cydia Substrate - Powerful Code Insertion Platform
* Copyright (C) 2008-2011 Jay Freeman (saurik)
*/

/* GNU Lesser General Public License, Version 3 {{{ */
/*
* Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */

#ifndef SUBSTRATE_BUFFER_HPP
#define SUBSTRATE_BUFFER_HPP

#include <string.h>

template <typename Type_>
_disused static _finline void MSWrite(uint8_t *&buffer, Type_ value) {
*reinterpret_cast<Type_ *>(buffer) = value;
buffer += sizeof(Type_);
}

_disused static _finline void MSWrite(uint8_t *&buffer, uint8_t *data, size_t size) {
memcpy(buffer, data, size);
buffer += size;
}

#endif//SUBSTRATE_BUFFER_HPP
152 changes: 152 additions & 0 deletions edxp-core/jni/main/Substrate/CydiaSubstrate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* Cydia Substrate - Powerful Code Insertion Platform
* Copyright (C) 2008-2011 Jay Freeman (saurik)
*/

/* GNU Lesser General Public License, Version 3 {{{ */
/*
* Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */

#ifndef SUBSTRATE_H_
#define SUBSTRATE_H_

#ifdef __APPLE__
#ifdef __cplusplus
extern "C" {
#endif
#include <mach-o/nlist.h>
#ifdef __cplusplus
}
#endif

#include <objc/runtime.h>
#include <objc/message.h>
#endif

#include <dlfcn.h>
#include <stdlib.h>

#define _finline \
inline __attribute__((__always_inline__))
#define _disused \
__attribute__((__unused__))

#define _extern \
extern "C" __attribute__((__visibility__("default")))

#ifdef __cplusplus
#define _default(value) = value
#else
#define _default(value)
#endif

#ifdef __cplusplus
extern "C" {
#endif

bool MSHookProcess(pid_t pid, const char *library);

typedef const void *MSImageRef;

MSImageRef MSGetImageByName(const char *file);
void *MSFindSymbol(MSImageRef image, const char *name);

void MSHookFunction(void *symbol, void *replace, void **result);

#ifdef __APPLE__
#ifdef __arm__
__attribute__((__deprecated__))
IMP MSHookMessage(Class _class, SEL sel, IMP imp, const char *prefix _default(NULL));
#endif
void MSHookMessageEx(Class _class, SEL sel, IMP imp, IMP *result);
#endif

#ifdef SubstrateInternal
typedef void *SubstrateAllocatorRef;
typedef struct __SubstrateProcess *SubstrateProcessRef;
typedef struct __SubstrateMemory *SubstrateMemoryRef;

SubstrateProcessRef SubstrateProcessCreate(SubstrateAllocatorRef allocator, pid_t pid);
void SubstrateProcessRelease(SubstrateProcessRef process);

SubstrateMemoryRef SubstrateMemoryCreate(SubstrateAllocatorRef allocator, SubstrateProcessRef process, void *data, size_t size);
void SubstrateMemoryRelease(SubstrateMemoryRef memory);
#endif

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

#ifdef SubstrateInternal
struct SubstrateHookMemory {
SubstrateMemoryRef handle_;

SubstrateHookMemory(SubstrateProcessRef process, void *data, size_t size) :
handle_(SubstrateMemoryCreate(NULL, NULL, data, size))
{
}

~SubstrateHookMemory() {
if (handle_ != NULL)
SubstrateMemoryRelease(handle_);
}
};
#endif


template<typename Type_>
static inline void MSHookFunction(Type_ *symbol, Type_ *replace, Type_ **result) {
MSHookFunction(
reinterpret_cast<void *>(symbol),
reinterpret_cast<void *>(replace),
reinterpret_cast<void **>(result)
);
}

template<typename Type_>
static inline void MSHookFunction(Type_ *symbol, Type_ *replace) {
return MSHookFunction(symbol, replace, reinterpret_cast<Type_ **>(NULL));
}

template<typename Type_>
static inline void MSHookSymbol(Type_ *&value, const char *name, MSImageRef image = NULL) {
value = reinterpret_cast<Type_ *>(MSFindSymbol(image, name));
}

template<typename Type_>
static inline void MSHookFunction(const char *name, Type_ *replace, Type_ **result = NULL) {
Type_ *symbol;
MSHookSymbol(symbol, name);
return MSHookFunction(symbol, replace, result);
}

#endif

#define MSHook(type, name, args...) \
_disused static type (*_ ## name)(args); \
static type $ ## name(args)

#ifdef __cplusplus
#define MSHake(name) \
&$ ## name, &_ ## name
#else
#define MSHake(name) \
&$ ## name, (void **) &_ ## name
#endif


#endif//SUBSTRATE_H_
67 changes: 67 additions & 0 deletions edxp-core/jni/main/Substrate/SubstrateARM.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Cydia Substrate - Powerful Code Insertion Platform
* Copyright (C) 2008-2011 Jay Freeman (saurik)
*/

/* GNU Lesser General Public License, Version 3 {{{ */
/*
* Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */

#ifndef SUBSTRATE_ARM_HPP
#define SUBSTRATE_ARM_HPP

enum A$r {
A$r0, A$r1, A$r2, A$r3,
A$r4, A$r5, A$r6, A$r7,
A$r8, A$r9, A$r10, A$r11,
A$r12, A$r13, A$r14, A$r15,
A$sp = A$r13,
A$lr = A$r14,
A$pc = A$r15
};

enum A$c {
A$eq, A$ne, A$cs, A$cc,
A$mi, A$pl, A$vs, A$vc,
A$hi, A$ls, A$ge, A$lt,
A$gt, A$le, A$al,
A$hs = A$cs,
A$lo = A$cc
};

template<class T> static T xabs(T _Val);

#define A$mrs_rm_cpsr(rd) /* mrs rd, cpsr */ \
(0xe10f0000 | ((rd) << 12))
#define A$msr_cpsr_f_rm(rm) /* msr cpsr_f, rm */ \
(0xe128f000 | (rm))
#define A$ldr_rd_$rn_im$(rd, rn, im) /* ldr rd, [rn, #im] */ \
(0xe5100000 | ((im) < 0 ? 0 : 1 << 23) | ((rn) << 16) | ((rd) << 12) | xabs(im))
#define A$str_rd_$rn_im$(rd, rn, im) /* sr rd, [rn, #im] */ \
(0xe5000000 | ((im) < 0 ? 0 : 1 << 23) | ((rn) << 16) | ((rd) << 12) | xabs(im))
#define A$sub_rd_rn_$im(rd, rn, im) /* sub, rd, rn, #im */ \
(0xe2400000 | ((rn) << 16) | ((rd) << 12) | (im & 0xff))
#define A$blx_rm(rm) /* blx rm */ \
(0xe12fff30 | (rm))
#define A$mov_rd_rm(rd, rm) /* mov rd, rm */ \
(0xe1a00000 | ((rd) << 12) | (rm))
#define A$ldmia_sp$_$rs$(rs) /* ldmia sp!, {rs} */ \
(0xe8b00000 | (A$sp << 16) | (rs))
#define A$stmdb_sp$_$rs$(rs) /* stmdb sp!, {rs} */ \
(0xe9200000 | (A$sp << 16) | (rs))
#define A$stmia_sp$_$r0$ 0xe8ad0001 /* stmia sp!, {r0} */
#define A$bx_r0 0xe12fff10 /* bx r0 */

#endif//SUBSTRATE_ARM_HPP
96 changes: 96 additions & 0 deletions edxp-core/jni/main/Substrate/SubstrateDebug.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Cydia Substrate - Powerful Code Insertion Platform
* Copyright (C) 2008-2011 Jay Freeman (saurik)
*/

/* GNU Lesser General Public License, Version 3 {{{ */
/*
* Substrate is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Substrate is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Substrate. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */

#include "SubstrateHook.h"
#include "SubstrateDebug.hpp"

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

_extern bool MSDebug;
bool MSDebug = false;

static char _MSHexChar(uint8_t value) {
return value < 0x20 || value >= 0x80 ? '.' : value;
}

#define HexWidth_ 16
#define HexDepth_ 4

void MSLogHexEx(const void *vdata, size_t size, size_t stride, const char *mark) {
const uint8_t *data((const uint8_t *) vdata);

size_t i(0), j;

char d[256];
size_t b(0);
d[0] = '\0';

while (i != size) {
if (i % HexWidth_ == 0) {
if (mark != NULL)
b += sprintf(d + b, "\n[%s] ", mark);
b += sprintf(d + b, "0x%.3zx:", i);
}

b += sprintf(d + b, " ");

for (size_t q(0); q != stride; ++q)
b += sprintf(d + b, "%.2x", data[i + stride - q - 1]);

i += stride;

for (size_t q(1); q != stride; ++q)
b += sprintf(d + b, " ");

if (i % HexDepth_ == 0)
b += sprintf(d + b, " ");

if (i % HexWidth_ == 0) {
b += sprintf(d + b, " ");
for (j = i - HexWidth_; j != i; ++j)
b += sprintf(d + b, "%c", _MSHexChar(data[j]));

lprintf("%s", d);
b = 0;
d[0] = '\0';
}
}

if (i % HexWidth_ != 0) {
for (j = i % HexWidth_; j != HexWidth_; ++j)
b += sprintf(d + b, " ");
for (j = 0; j != (HexWidth_ - i % HexWidth_ + HexDepth_ - 1) / HexDepth_; ++j)
b += sprintf(d + b, " ");
b += sprintf(d + b, " ");
for (j = i / HexWidth_ * HexWidth_; j != i; ++j)
b += sprintf(d + b, "%c", _MSHexChar(data[j]));

lprintf("%s", d);
b = 0;
d[0] = '\0';
}
}

void MSLogHex(const void *vdata, size_t size, const char *mark) {
return MSLogHexEx(vdata, size, 1, mark);
}
Loading