Commit b685a174 authored by swift_gan's avatar swift_gan Committed by swift_gan

[SingleInstHook]tweak code

parent 7a2b7b8f
......@@ -5,7 +5,6 @@ import android.app.Application;
import android.os.Build;
import android.util.Log;
import com.swift.sandhook.nativehook.NativeHook;
import com.swift.sandhook.test.TestClass;
import com.swift.sandhook.testHookers.ActivityHooker;
import com.swift.sandhook.testHookers.CtrHook;
......@@ -37,8 +36,6 @@ public class MyApp extends Application {
SandHookConfig.SDK_INT = 29;
}
NativeHook.test();
SandHook.disableVMInline();
SandHook.tryDisableProfile(getPackageName());
SandHook.disableDex2oatInline(false);
......
......@@ -90,7 +90,7 @@ ElfImg::ElfImg(const char *elf) {
}
//load module rang
baseInRam = getModuleBase(elf);
baseInRam = GetModuleBase(elf);
}
ElfImg::~ElfImg() {
......@@ -105,7 +105,7 @@ ElfImg::~ElfImg() {
}
}
Elf_Addr ElfImg::getSymbOffset(const char *name) {
Elf_Addr ElfImg::GetSymOffset(const char *name) {
Elf_Addr _offset = 0;
//search dynmtab
......@@ -139,8 +139,8 @@ Elf_Addr ElfImg::getSymbOffset(const char *name) {
return 0;
}
Elf_Addr ElfImg::getSymbAddress(const char *name) {
Elf_Addr offset = getSymbOffset(name);
Elf_Addr ElfImg::GetSymAddress(const char *name) {
Elf_Addr offset = GetSymOffset(name);
if (offset > 0 && baseInRam != nullptr) {
return static_cast<Elf_Addr>((size_t) baseInRam + offset - bias);
} else {
......@@ -148,7 +148,7 @@ Elf_Addr ElfImg::getSymbAddress(const char *name) {
}
}
void *ElfImg::getModuleBase(const char *name) {
void *ElfImg::GetModuleBase(const char *name) {
FILE *maps;
char buff[256];
off_t load_addr;
......
......@@ -44,11 +44,11 @@ namespace SandHook {
ElfImg(const char* elf);
Elf_Addr getSymbOffset(const char* name);
Elf_Addr GetSymOffset(const char *name);
static void* getModuleBase(const char* name);
static void* GetModuleBase(const char *name);
Elf_Addr getSymbAddress(const char* name);
Elf_Addr GetSymAddress(const char *name);
~ElfImg();
......
......@@ -3,8 +3,6 @@
//
#include <jni.h>
#include <sys/mman.h>
#include <log.h>
#include "sandhook_native.h"
#include "hook.h"
#include "elf.h"
......@@ -12,139 +10,10 @@
using namespace SandHook::Hook;
using namespace SandHook::Elf;
int m1 = 5;
int m3 = 1036;
int m2 = 1035;
int m4 = 5;
void (*dosth3Backup)(int, int) = nullptr;
void (*dosth4Backup)() = nullptr;
void (*dosth4Backup2)() = nullptr;
void (*innerSuspendVM)() = nullptr;
static void (*heapPreForkBackup)(void *) = nullptr;
static void myHeapPreFork(void *heap) {
heapPreForkBackup(heap);
}
void SuspendVMReplace() {
LOGE("VM Suspend!");
innerSuspendVM();
}
void do2() {
int a = 1 + 1;
int b = 1 + 1;
int c = 1 + 1;
int d = 1 + 1;
}
void do3(int x, int y) {
do2();
int a = 1 + 1;
int b = 1 + 1;
int c = 1 + 1;
int d = a + b + c;
LOGE("do3 = %d", y);
}
void do5() {
LOGE("x = %d", 5);
}
void do4() {
int a = 1 + 1;
int b = a + 1;
int d = a + 1;
int e = a + 1;
LOGE("x = %d", 7);
}
void do4replace() {
int a = 1 + 1;
int b = 1 + 1;
int c = 1 + 1;
int d = 1 + 1;
dosth4Backup();
}
void do4replace2() {
int a = 1 + 1;
int c = 1 + 1;
int d = 1 + 1;
dosth4Backup2();
}
void do3replace(int x, int y) {
int a = 1 + 1;
int b = 1 + 1;
int c = 1 + 1;
int d = 1 + 1;
dosth3Backup(x, y);
}
void do1() {
do2();
}
void breakCallback(REG regs[]) {
LOGE("breakCallback = %d SP = %d", regs[0], regs);
}
class Visitor : public InstVisitor {
bool Visit(Unit<Base> *unit, void *pc) override {
BaseInst* instruction = reinterpret_cast<BaseInst *>(unit);
instruction->Assemble();
return true;
}
};
extern "C"
JNIEXPORT void JNICALL
Java_com_swift_sandhook_nativehook_NativeHook_test(JNIEnv *env, jclass jclass1) {
dosth4Backup = reinterpret_cast<void (*)()>(InlineHook::instance->SingleInstHook(
reinterpret_cast<void *>(do4),
reinterpret_cast<void *>(do4replace)));
do4();
if (sizeof(void*) == 8) {
heapPreForkBackup = reinterpret_cast<void (*)(void *)>(SandInlineHookSym("/system/lib64/libart.so", "_ZN3art2gc4Heap13PreZygoteForkEv",
reinterpret_cast<void *>(myHeapPreFork)));
innerSuspendVM = reinterpret_cast<void (*)()>(SandInlineHookSym("/system/lib64/libart.so", "_ZN3art3Dbg9SuspendVMEv",
reinterpret_cast<void *>(SuspendVMReplace)));
} else {
heapPreForkBackup = reinterpret_cast<void (*)(void *)>(SandInlineHookSym("/system/lib/libart.so", "_ZN3art2gc4Heap13PreZygoteForkEv",
reinterpret_cast<void *>(myHeapPreFork)));
innerSuspendVM = reinterpret_cast<void (*)()>(SandInlineHookSym("/system/lib/libart.so", "_ZN3art3Dbg9SuspendVMEv",
reinterpret_cast<void *>(SuspendVMReplace)));
}
void* do3P = reinterpret_cast<void *>(do3);
InlineHook::instance->BreakPoint(reinterpret_cast<void *>((Addr)do3), breakCallback);
LOGE("ok");
do3(100, 2);
}
extern "C"
EXPORT void* SandGetSym(const char* so, const char* symb) {
ElfImg elfImg(so);
return reinterpret_cast<void *>(elfImg.getSymbAddress(symb));
return reinterpret_cast<void *>(elfImg.GetSymAddress(symb));
}
extern "C"
......@@ -155,13 +24,28 @@ EXPORT void* SandInlineHook(void* origin, void* replace) {
extern "C"
EXPORT void* SandInlineHookSym(const char* so, const char* symb, void* replace) {
ElfImg elfImg(so);
void* origin = reinterpret_cast<void *>(elfImg.getSymbAddress(symb));
void* origin = reinterpret_cast<void *>(elfImg.GetSymAddress(symb));
if (origin == nullptr)
return nullptr;
return InlineHook::instance->Hook(origin, replace);
}
extern "C"
EXPORT void* SandSingleInstHook(void* origin, void* replace) {
return InlineHook::instance->SingleInstHook(origin, replace);
}
extern "C"
EXPORT void* SandSingleInstHookSym(const char* so, const char* symb, void* replace) {
ElfImg elfImg(so);
void* origin = reinterpret_cast<void *>(elfImg.GetSymAddress(symb));
if (origin == nullptr)
return nullptr;
return InlineHook::instance->SingleInstHook(origin, replace);
}
extern "C"
EXPORT bool SandBreakpoint(void* origin, void (*callback)(REG[])) {
return InlineHook::instance->BreakPoint(origin, callback);
......
......@@ -17,5 +17,11 @@ EXPORT void* SandInlineHook(void* origin, void* replace);
extern "C"
EXPORT void* SandInlineHookSym(const char* so, const char* symb, void* replace);
extern "C"
EXPORT void* SandSingleInstHook(void* origin, void* replace);
extern "C"
EXPORT void* SandSingleInstHookSym(const char* so, const char* symb, void* replace);
extern "C"
EXPORT bool SandBreakpoint(void* origin, void (*callback)(REG[]));
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment