Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
SandHook
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
SandHook
Commits
331f9130
Commit
331f9130
authored
Mar 13, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tweak code
parent
891bd37b
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
121 additions
and
51 deletions
+121
-51
art_method.cpp
hooklib/src/main/cpp/art/art_method.cpp
+9
-0
cast_art_method.cpp
hooklib/src/main/cpp/casts/cast_art_method.cpp
+6
-0
art_method.h
hooklib/src/main/cpp/includes/art_method.h
+1
-0
cast_art_method.h
hooklib/src/main/cpp/includes/cast_art_method.h
+1
-0
dlfcn_nougat.h
hooklib/src/main/cpp/includes/dlfcn_nougat.h
+2
-0
hide_api.h
hooklib/src/main/cpp/includes/hide_api.h
+2
-0
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+23
-0
dlfcn_nougat.cpp
hooklib/src/main/cpp/utils/dlfcn_nougat.cpp
+20
-0
hide_api.cpp
hooklib/src/main/cpp/utils/hide_api.cpp
+51
-51
ClassNeverCall.java
hooklib/src/main/java/com/swift/sandhook/ClassNeverCall.java
+5
-0
SandHook.java
hooklib/src/main/java/com/swift/sandhook/SandHook.java
+1
-0
No files found.
hooklib/src/main/cpp/art/art_method.cpp
View file @
331f9130
...
@@ -145,6 +145,15 @@ bool ArtMethod::compile(JNIEnv* env) {
...
@@ -145,6 +145,15 @@ bool ArtMethod::compile(JNIEnv* env) {
return
compileMethod
(
this
,
reinterpret_cast
<
void
*>
(
threadId
))
&&
isCompiled
();
return
compileMethod
(
this
,
reinterpret_cast
<
void
*>
(
threadId
))
&&
isCompiled
();
}
}
bool
ArtMethod
::
deCompile
()
{
if
(
!
isCompiled
())
return
true
;
if
(
CastArtMethod
::
beAot
)
return
false
;
setQuickCodeEntry
(
isNative
()
?
CastArtMethod
::
genericJniStub
:
CastArtMethod
::
quickToInterpreterBridge
);
flushCache
();
}
void
ArtMethod
::
flushCache
()
{
void
ArtMethod
::
flushCache
()
{
flushCacheExt
(
reinterpret_cast
<
Size
>
(
this
),
size
());
flushCacheExt
(
reinterpret_cast
<
Size
>
(
this
),
size
());
}
}
...
...
hooklib/src/main/cpp/casts/cast_art_method.cpp
View file @
331f9130
...
@@ -182,6 +182,11 @@ namespace SandHook {
...
@@ -182,6 +182,11 @@ namespace SandHook {
jclass
neverCallTestClass
=
env
->
FindClass
(
"com/swift/sandhook/ClassNeverCall"
);
jclass
neverCallTestClass
=
env
->
FindClass
(
"com/swift/sandhook/ClassNeverCall"
);
art
::
mirror
::
ArtMethod
*
neverCall
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
art
::
mirror
::
ArtMethod
*
neverCall
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCall"
,
"()V"
));
neverCallTestClass
,
"neverCall"
,
"()V"
));
art
::
mirror
::
ArtMethod
*
neverCall2
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCall2"
,
"()V"
));
beAot
=
entryPointQuickCompiled
->
get
(
neverCall
)
!=
entryPointQuickCompiled
->
get
(
neverCall2
);
quickToInterpreterBridge
=
entryPointQuickCompiled
->
get
(
neverCall
);
quickToInterpreterBridge
=
entryPointQuickCompiled
->
get
(
neverCall
);
art
::
mirror
::
ArtMethod
*
neverCallStatic
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetStaticMethodID
(
art
::
mirror
::
ArtMethod
*
neverCallStatic
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetStaticMethodID
(
...
@@ -208,5 +213,6 @@ namespace SandHook {
...
@@ -208,5 +213,6 @@ namespace SandHook {
void
*
CastArtMethod
::
quickToInterpreterBridge
=
nullptr
;
void
*
CastArtMethod
::
quickToInterpreterBridge
=
nullptr
;
void
*
CastArtMethod
::
genericJniStub
=
nullptr
;
void
*
CastArtMethod
::
genericJniStub
=
nullptr
;
void
*
CastArtMethod
::
staticResolveStub
=
nullptr
;
void
*
CastArtMethod
::
staticResolveStub
=
nullptr
;
bool
CastArtMethod
::
beAot
=
false
;
}
}
\ No newline at end of file
hooklib/src/main/cpp/includes/art_method.h
View file @
331f9130
...
@@ -73,6 +73,7 @@ public:
...
@@ -73,6 +73,7 @@ public:
void
*
getDeclaringClassPtr
();
void
*
getDeclaringClassPtr
();
bool
compile
(
JNIEnv
*
env
);
bool
compile
(
JNIEnv
*
env
);
bool
deCompile
();
void
flushCache
();
void
flushCache
();
void
backup
(
ArtMethod
*
backup
);
void
backup
(
ArtMethod
*
backup
);
...
...
hooklib/src/main/cpp/includes/cast_art_method.h
View file @
331f9130
...
@@ -22,6 +22,7 @@ namespace SandHook {
...
@@ -22,6 +22,7 @@ namespace SandHook {
static
void
*
quickToInterpreterBridge
;
static
void
*
quickToInterpreterBridge
;
static
void
*
genericJniStub
;
static
void
*
genericJniStub
;
static
void
*
staticResolveStub
;
static
void
*
staticResolveStub
;
static
bool
beAot
;
static
void
init
(
JNIEnv
*
env
);
static
void
init
(
JNIEnv
*
env
);
static
void
copy
(
art
::
mirror
::
ArtMethod
*
from
,
art
::
mirror
::
ArtMethod
*
to
);
static
void
copy
(
art
::
mirror
::
ArtMethod
*
from
,
art
::
mirror
::
ArtMethod
*
to
);
...
...
hooklib/src/main/cpp/includes/dlfcn_nougat.h
View file @
331f9130
...
@@ -10,6 +10,8 @@ void *fake_dlopen(const char *filename, int flags);
...
@@ -10,6 +10,8 @@ void *fake_dlopen(const char *filename, int flags);
void
*
fake_dlsym
(
void
*
handle
,
const
char
*
name
);
void
*
fake_dlsym
(
void
*
handle
,
const
char
*
name
);
const
char
*
fake_dlerror
();
const
char
*
fake_dlerror
();
void
*
getSymCompat
(
const
char
*
filename
,
const
char
*
name
);
}
}
#endif //DLFCN_NOUGAT_H
#endif //DLFCN_NOUGAT_H
hooklib/src/main/cpp/includes/hide_api.h
View file @
331f9130
...
@@ -31,6 +31,8 @@ extern "C" {
...
@@ -31,6 +31,8 @@ extern "C" {
bool
disableJitInline
(
art
::
CompilerOptions
*
compilerOptions
);
bool
disableJitInline
(
art
::
CompilerOptions
*
compilerOptions
);
void
*
getInterpreterBridge
(
bool
isNative
);
}
}
#endif //SANDHOOK_HIDE_API_H
#endif //SANDHOOK_HIDE_API_H
hooklib/src/main/cpp/sandhook.cpp
View file @
331f9130
...
@@ -242,6 +242,29 @@ Java_com_swift_sandhook_SandHook_compileMethod(JNIEnv *env, jclass type, jobject
...
@@ -242,6 +242,29 @@ Java_com_swift_sandhook_SandHook_compileMethod(JNIEnv *env, jclass type, jobject
}
}
extern
"C"
JNIEXPORT
jboolean
JNICALL
Java_com_swift_sandhook_SandHook_deCompileMethod
(
JNIEnv
*
env
,
jclass
type
,
jobject
member
)
{
if
(
member
==
NULL
)
return
JNI_FALSE
;
art
::
mirror
::
ArtMethod
*
method
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
FromReflectedMethod
(
member
));
if
(
method
==
nullptr
)
return
JNI_FALSE
;
if
(
method
->
isCompiled
())
{
SandHook
::
StopTheWorld
stopTheWorld
;
if
(
SDK_INT
>=
ANDROID_N
)
{
method
->
disableCompilable
();
}
return
static_cast
<
jboolean
>
(
method
->
deCompile
());
}
else
{
return
JNI_TRUE
;
}
}
extern
"C"
extern
"C"
JNIEXPORT
jobject
JNICALL
JNIEXPORT
jobject
JNICALL
Java_com_swift_sandhook_SandHook_getObjectNative
(
JNIEnv
*
env
,
jclass
type
,
jlong
thread
,
Java_com_swift_sandhook_SandHook_getObjectNative
(
JNIEnv
*
env
,
jclass
type
,
jlong
thread
,
...
...
hooklib/src/main/cpp/utils/dlfcn_nougat.cpp
View file @
331f9130
...
@@ -30,6 +30,8 @@
...
@@ -30,6 +30,8 @@
#include <sys/mman.h>
#include <sys/mman.h>
#include <elf.h>
#include <elf.h>
#include <android/log.h>
#include <android/log.h>
#include <dlfcn.h>
#include "../includes/arch.h"
#define TAG_NAME "nougat_dlfcn"
#define TAG_NAME "nougat_dlfcn"
...
@@ -52,6 +54,8 @@
...
@@ -52,6 +54,8 @@
#define Elf_Sym Elf32_Sym
#define Elf_Sym Elf32_Sym
#endif
#endif
extern
int
SDK_INT
;
struct
ctx
{
struct
ctx
{
void
*
load_addr
;
void
*
load_addr
;
...
@@ -237,4 +241,20 @@ void *fake_dlsym(void *handle, const char *name) {
...
@@ -237,4 +241,20 @@ void *fake_dlsym(void *handle, const char *name) {
const
char
*
fake_dlerror
()
{
const
char
*
fake_dlerror
()
{
return
NULL
;
return
NULL
;
}
}
void
*
getSymCompat
(
const
char
*
filename
,
const
char
*
name
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
void
*
handle
=
fake_dlopen
(
filename
,
RTLD_NOW
);
if
(
handle
)
{
return
fake_dlsym
(
handle
,
name
);
}
}
else
{
void
*
handle
=
dlopen
(
filename
,
RTLD_LAZY
|
RTLD_GLOBAL
);
if
(
handle
)
{
return
dlsym
(
handle
,
name
);
}
}
}
}
}
\ No newline at end of file
hooklib/src/main/cpp/utils/hide_api.cpp
View file @
331f9130
...
@@ -20,19 +20,30 @@ extern "C" {
...
@@ -20,19 +20,30 @@ extern "C" {
art
::
jit
::
JitCompiler
**
globalJitCompileHandlerAddr
=
nullptr
;
art
::
jit
::
JitCompiler
**
globalJitCompileHandlerAddr
=
nullptr
;
void
*
(
*
get
)(
bool
*
)
=
nullptr
;
void
*
(
*
getQuickToInterpreterBridge
)(
void
*
)
=
nullptr
;
void
*
(
*
getQuickGenericJniStub
)(
void
*
)
=
nullptr
;
void
initHideApi
(
JNIEnv
*
env
)
{
void
initHideApi
(
JNIEnv
*
env
)
{
//init compile
if
(
SDK_INT
>=
ANDROID_N
)
{
const
char
*
art_lib_path
;
void
*
jit_lib
;
const
char
*
jit_lib_path
;
if
(
BYTE_POINT
==
8
)
{
if
(
BYTE_POINT
==
8
)
{
jit_lib
=
fake_dlopen
(
"/system/lib64/libart-compiler.so"
,
RTLD_NOW
);
art_lib_path
=
"/system/lib64/libart.so"
;
jit_lib_path
=
"/system/lib64/libart-compiler.so"
;
}
else
{
}
else
{
jit_lib
=
fake_dlopen
(
"/system/lib/libart-compiler.so"
,
RTLD_NOW
);
art_lib_path
=
"/system/lib/libart.so"
;
jit_lib_path
=
"/system/lib/libart-compiler.so"
;
}
}
jitCompileMethod
=
(
bool
(
*
)(
void
*
,
void
*
,
void
*
,
bool
))
fake_dlsym
(
jit_lib
,
"jit_compile_method"
);
jitLoad
=
reinterpret_cast
<
void
*
(
*
)(
bool
*
)
>
(
fake_dlsym
(
jit_lib
,
"jit_load"
));
//init compile
if
(
SDK_INT
>=
ANDROID_N
)
{
jitCompileMethod
=
reinterpret_cast
<
bool
(
*
)(
void
*
,
void
*
,
void
*
,
bool
)
>
(
getSymCompat
(
jit_lib_path
,
"jit_compile_method"
));
jitLoad
=
reinterpret_cast
<
void
*
(
*
)(
bool
*
)
>
(
getSymCompat
(
jit_lib_path
,
"jit_load"
));
bool
generate_debug_info
=
false
;
bool
generate_debug_info
=
false
;
jitCompilerHandle
=
(
jitLoad
)(
&
generate_debug_info
);
jitCompilerHandle
=
(
jitLoad
)(
&
generate_debug_info
);
...
@@ -43,56 +54,34 @@ extern "C" {
...
@@ -43,56 +54,34 @@ extern "C" {
}
}
}
}
//init suspend
//init suspend
void
*
art_lib
;
innerSuspendVM
=
reinterpret_cast
<
void
(
*
)()
>
(
getSymCompat
(
art_lib_path
,
const
char
*
art_lib_path
;
if
(
BYTE_POINT
==
8
)
{
art_lib_path
=
"/system/lib64/libart.so"
;
}
else
{
art_lib_path
=
"/system/lib/libart.so"
;
}
if
(
SDK_INT
>=
ANDROID_N
)
{
art_lib
=
fake_dlopen
(
art_lib_path
,
RTLD_NOW
);
if
(
art_lib
>
0
)
{
innerSuspendVM
=
reinterpret_cast
<
void
(
*
)()
>
(
fake_dlsym
(
art_lib
,
"_ZN3art3Dbg9SuspendVMEv"
));
"_ZN3art3Dbg9SuspendVMEv"
));
innerResumeVM
=
reinterpret_cast
<
void
(
*
)()
>
(
fake_dlsym
(
art_lib
,
innerResumeVM
=
reinterpret_cast
<
void
(
*
)()
>
(
getSymCompat
(
art_lib_path
,
"_ZN3art3Dbg8ResumeVMEv"
));
"_ZN3art3Dbg8ResumeVMEv"
));
}
}
else
{
art_lib
=
dlopen
(
art_lib_path
,
RTLD_NOW
);
if
(
art_lib
>
0
)
{
innerSuspendVM
=
reinterpret_cast
<
void
(
*
)()
>
(
dlsym
(
art_lib
,
"_ZN3art3Dbg9SuspendVMEv"
));
innerResumeVM
=
reinterpret_cast
<
void
(
*
)()
>
(
dlsym
(
art_lib
,
"_ZN3art3Dbg8ResumeVMEv"
));
}
}
//init for getObject & JitCompiler
//init for getObject & JitCompiler
const
char
*
add_weak_ref_sym
;
if
(
SDK_INT
<
ANDROID_M
)
{
if
(
SDK_INT
<
ANDROID_M
)
{
void
*
handle
=
dlopen
(
"libart.so"
,
RTLD_LAZY
|
RTLD_GLOBAL
);
add_weak_ref_sym
=
"_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE"
;
addWeakGlobalRef
=
(
jobject
(
*
)(
JavaVM
*
,
void
*
,
void
*
))
dlsym
(
handle
,
"_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE"
);
}
else
if
(
SDK_INT
<
ANDROID_N
)
{
}
else
if
(
SDK_INT
<
ANDROID_N
)
{
void
*
handle
=
dlopen
(
"libart.so"
,
RTLD_LAZY
|
RTLD_GLOBAL
);
add_weak_ref_sym
=
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
;
addWeakGlobalRef
=
(
jobject
(
*
)(
JavaVM
*
,
void
*
,
void
*
))
dlsym
(
handle
,
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
);
}
else
{
}
else
{
void
*
handle
;
add_weak_ref_sym
=
SDK_INT
<=
ANDROID_N2
if
(
BYTE_POINT
==
8
)
{
handle
=
fake_dlopen
(
"/system/lib64/libart.so"
,
RTLD_NOW
);
}
else
{
handle
=
fake_dlopen
(
"/system/lib/libart.so"
,
RTLD_NOW
);
}
const
char
*
addWeakGloablReferenceSymbol
=
SDK_INT
<=
25
?
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
?
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
:
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadENS_6ObjPtrINS_6mirror6ObjectEEE"
;
:
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadENS_6ObjPtrINS_6mirror6ObjectEEE"
;
addWeakGlobalRef
=
(
jobject
(
*
)(
JavaVM
*
,
void
*
,
void
*
))
fake_dlsym
(
handle
,
}
addWeakGloablReferenceSymbol
);
addWeakGlobalRef
=
reinterpret_cast
<
jobject
(
*
)(
JavaVM
*
,
void
*
,
void
*
)
>
(
getSymCompat
(
art_lib_path
,
add_weak_ref_sym
));
//try disable inline !
if
(
SDK_INT
>=
ANDROID_N
)
{
globalJitCompileHandlerAddr
=
reinterpret_cast
<
art
::
jit
::
JitCompiler
**>
(
fake_dlsym
(
handle
,
"_ZN3art3jit3Jit20jit_compiler_handle_E"
));
globalJitCompileHandlerAddr
=
reinterpret_cast
<
art
::
jit
::
JitCompiler
**>
(
getSymCompat
(
art_lib_path
,
"_ZN3art3jit3Jit20jit_compiler_handle_E"
));
getQuickGenericJniStub
=
reinterpret_cast
<
void
*
(
*
)(
void
*
)
>
(
getSymCompat
(
art_lib_path
,
"_ZNK3art11ClassLinker29GetRuntimeQuickGenericJniStubEv"
));
getQuickToInterpreterBridge
=
reinterpret_cast
<
void
*
(
*
)(
void
*
)
>
(
getSymCompat
(
art_lib_path
,
"_ZNK3art9OatHeader27GetQuickToInterpreterBridgeEv"
));
}
}
}
}
...
@@ -170,5 +159,16 @@ extern "C" {
...
@@ -170,5 +159,16 @@ extern "C" {
}
}
}
}
void
*
getInterpreterBridge
(
bool
isNative
)
{
if
(
isNative
)
{
if
(
getQuickGenericJniStub
==
nullptr
||
getQuickGenericJniStub
<=
0
)
return
nullptr
;
return
getQuickGenericJniStub
(
nullptr
);
}
else
{
//no implement
return
nullptr
;
}
}
}
}
hooklib/src/main/java/com/swift/sandhook/ClassNeverCall.java
View file @
331f9130
package
com
.
swift
.
sandhook
;
package
com
.
swift
.
sandhook
;
import
android.util.Log
;
public
class
ClassNeverCall
{
public
class
ClassNeverCall
{
private
void
neverCall
()
{}
private
void
neverCall
()
{}
private
void
neverCall2
()
{
Log
.
e
(
"ClassNeverCall"
,
"ClassNeverCall2"
);
}
private
static
void
neverCallStatic
()
{}
private
static
void
neverCallStatic
()
{}
private
native
void
neverCallNative
();
private
native
void
neverCallNative
();
}
}
hooklib/src/main/java/com/swift/sandhook/SandHook.java
View file @
331f9130
...
@@ -308,6 +308,7 @@ public class SandHook {
...
@@ -308,6 +308,7 @@ public class SandHook {
public
static
native
void
ensureMethodCached
(
Method
hook
,
Method
backup
);
public
static
native
void
ensureMethodCached
(
Method
hook
,
Method
backup
);
public
static
native
boolean
compileMethod
(
Member
member
);
public
static
native
boolean
compileMethod
(
Member
member
);
public
static
native
boolean
deCompileMethod
(
Member
member
);
public
static
native
boolean
canGetObject
();
public
static
native
boolean
canGetObject
();
public
static
native
Object
getObjectNative
(
long
thread
,
long
address
);
public
static
native
Object
getObjectNative
(
long
thread
,
long
address
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment