Commit af878e60 authored by swift_gan's avatar swift_gan

tweak code

parent 217cb801
//
// Created by 甘尧 on 2019/1/12.
//
#include "../includes/cast.h"
#include "art/art_6_0.h"
namespace SandHook {
class CastDexCacheResolvedMethods : public ArrayMember<art::mirror::ArtMethod> {
protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
if (SDK_INT >= ANDROID_P)
return getParentSize() + 1;
int offset = 0;
Size addr = getAddressFromJava(jniEnv, "com/swift/sandhook/SandHookMethodResolver", "resolvedMethodsAddress");
if (addr != 0) {
offset = findOffset(&p, getParentSize(), 2, reinterpret_cast<void *>(addr));
if (offset >= 0) {
return static_cast<Size>(offset);
}
}
return getParentSize() + 1;
}
Size calElementSize(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
return getParentSize();
}
};
class CastEntryPointFormInterpreter : public IMember<art::mirror::ArtMethod, void*> {
protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
if (SDK_INT >= ANDROID_L2 && SDK_INT <= ANDROID_M)
return getParentSize() - 3 * BYTE_POINT;
else if (SDK_INT <= ANDROID_L)
return getParentSize() - 4 * 8 - 4 * 4;
else
return getParentSize() + 1;
}
};
class CastEntryPointQuickCompiled : public IMember<art::mirror::ArtMethod, void*> {
protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
if (SDK_INT >= ANDROID_L2) {
return getParentSize() - BYTE_POINT;
} else {
return getParentSize() - 8 * 2 - 4 * 4;
}
}
};
class CastEntryPointFromJni : public IMember<art::mirror::ArtMethod, void*> {
protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
if (SDK_INT >= ANDROID_L2 && SDK_INT <= ANDROID_N) {
return getParentSize() - 2 * BYTE_POINT;
} else {
return getParentSize() - 8 * 2 - 4 * 4;
}
}
};
class CastAccessFlag : public IMember<art::mirror::ArtMethod, uint32_t> {
protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
int offset = findOffset(&p, getParentSize(), sizeof(uint32_t), 524313);
if (offset < 0)
throw getParentSize() + 1;
else
return static_cast<size_t>(offset);
}
};
class CastDexMethodIndex : public IMember<art::mirror::ArtMethod, uint32_t> {
protected:
Size calOffset(JNIEnv *jniEnv, art::mirror::ArtMethod p) override {
if (SDK_INT >= ANDROID_P)
return getParentSize() + 1;
int offset = 0;
jint index = getIntFromJava(jniEnv, "com/swift/sandhook/SandHookMethodResolver", "dexMethodIndex");
if (index != 0) {
offset = findOffset(&p, getParentSize(), 2, static_cast<uint32_t>(index));
if (offset >= 0) {
return static_cast<Size>(offset);
}
}
return getParentSize() + 1;
}
};
class cast_art_method {
public:
static Size size;
static IMember<art::mirror::ArtMethod, void*>* entryPointQuickCompiled;
static IMember<art::mirror::ArtMethod, void*>* entryPointFormInterpreter;
static ArrayMember<art::mirror::ArtMethod>* dexCacheResolvedMethods;
static IMember<art::mirror::ArtMethod, uint32_t>* dexMethodIndex;
static IMember<art::mirror::ArtMethod, uint32_t>* accessFlag;
static void init(JNIEnv *env) {
//init ArtMethodSize
jclass sizeTestClass = env->FindClass("com/swift/sandhook/ArtMethodSizeTest");
Size artMethod1 = (Size) env->GetStaticMethodID(sizeTestClass, "method1", "()V");
Size artMethod2 = (Size) env->GetStaticMethodID(sizeTestClass, "method2", "()V");
size = artMethod2 - artMethod1;
art::mirror::ArtMethod m1 = *reinterpret_cast<art::mirror::ArtMethod *>(artMethod1);
art::mirror::ArtMethod m2 = *reinterpret_cast<art::mirror::ArtMethod *>(artMethod2);
//init Members
entryPointQuickCompiled = new CastEntryPointQuickCompiled();
entryPointQuickCompiled->init(env, m1, size);
accessFlag = new CastAccessFlag();
accessFlag->init(env, m1, size);
entryPointFormInterpreter = new CastEntryPointFormInterpreter();
entryPointFormInterpreter->init(env, m1, size);
dexCacheResolvedMethods = new CastDexCacheResolvedMethods();
dexCacheResolvedMethods->init(env, m1, size);
dexMethodIndex = new CastDexMethodIndex();
dexMethodIndex->init(env, m1, size);
// //test
// art::mirror::ArtMethod** mArray = reinterpret_cast<art::mirror::ArtMethod**>(m1.dex_cache_resolved_methods_);
//
// art::mirror::ArtMethod m1B = *mArray[m1.dex_method_index_];
// art::mirror::ArtMethod m1C = *mArray[m2.dex_method_index_];
}
static void copy(art::mirror::ArtMethod* from, art::mirror::ArtMethod* to) {
memcpy(to, from, size);
}
};
Size cast_art_method::size = 0;
IMember<art::mirror::ArtMethod, void*>* cast_art_method::entryPointQuickCompiled = nullptr;
IMember<art::mirror::ArtMethod, void*>* cast_art_method::entryPointFormInterpreter = nullptr;
ArrayMember<art::mirror::ArtMethod>* cast_art_method::dexCacheResolvedMethods = nullptr;
IMember<art::mirror::ArtMethod, uint32_t>* cast_art_method::dexMethodIndex = nullptr;
IMember<art::mirror::ArtMethod, uint32_t>* cast_art_method::accessFlag = nullptr;
}
//
// Created by SwiftGan on 2019/1/14.
//
#ifndef SANDHOOK_IMETHODRESOLVER_H
#define SANDHOOK_IMETHODRESOLVER_H
#endif //SANDHOOK_IMETHODRESOLVER_H
//
// Created by 甘尧 on 2019/1/12.
//
#include <stdint.h>
#include <string.h>
#include "arch.h"
#include "utils.h"
#define ANDROID_L 21
#define ANDROID_L2 22
#define ANDROID_M 23
#define ANDROID_N 24
#define ANDROID_N2 25
#define ANDROID_O 26
#define ANDROID_O2 27
#define ANDROID_P 28
#ifndef SANDHOOK_ICAST_H
#define SANDHOOK_ICAST_H
namespace SandHook {
static int SDK_INT = 0;
template <typename T>
class cast {
public:
cast(T t) {
this->origin = t;
};
virtual Size getSize() { return sizeof(T); };
private:
T origin;
};
template <typename PType, typename MType>
class IMember {
public:
virtual void init(JNIEnv *jniEnv, PType p, Size size) {
this->parentSize = size;
offset = calOffset(jniEnv, p);
}
virtual Size getOffset() {
return offset;
}
virtual Size getParentSize() {
return parentSize;
}
virtual MType get(PType p) {
if (offset > parentSize)
return NULL;
return *reinterpret_cast<MType*>(&p + getOffset());
};
virtual void set(PType p, MType t) {
if (offset > parentSize)
return;
memcpy(&p + getOffset(), &t, sizeof(MType));
};
private:
Size offset = 0;
protected:
Size parentSize = 0;
virtual Size calOffset(JNIEnv *jniEnv, PType p) = 0;
};
template<typename PType>
class ArrayMember : IMember<PType, void*> {
public:
virtual void init(JNIEnv *jniEnv, PType p, Size parentSize) override : IMember::init(jniEnv, p, parentSize) {
elementSize = calElementSize(jniEnv, p);
}
virtual Size getElementSize() {
return elementSize;
}
virtual void* arrayStart(PType parent) {
return get(parent);
}
Size getParentSize() : IMember::getParentSize() = default;
public:
virtual void setElement(PType parent, int position, void* elementPoint) {
void* array = arrayStart(parent);
memcpy(array + position * getElementSize(), elementPoint, getElementSize());
}
private:
Size elementSize = 0;
protected:
virtual Size calElementSize(JNIEnv *jniEnv, PType p) = 0;
};
}
#endif //SANDHOOK_ICAST_H
\ No newline at end of file
#include <jni.h>
#include <string>
#include "casts/CastArtMethod.h"
#include "casts/cast_art_method.h"
extern "C" JNIEXPORT jstring
......@@ -34,6 +34,6 @@ JNIEXPORT void JNICALL
Java_com_swift_sandhook_MainActivity_initHook(JNIEnv *env, jobject instance) {
SandHook::CastArtMethod::init(env);
SandHook::cast_art_method::init(env);
}
\ 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