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
d1c1b69e
Commit
d1c1b69e
authored
Jan 30, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add hookmode modifier
parent
adf97741
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
14 deletions
+50
-14
HookMode.java
...src/main/java/com/swift/sandhook/annotation/HookMode.java
+17
-0
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+29
-11
SandHook.java
hooklib/src/main/java/com/swift/sandhook/SandHook.java
+4
-3
No files found.
annotation/src/main/java/com/swift/sandhook/annotation/HookMode.java
0 → 100644
View file @
d1c1b69e
package
com
.
swift
.
sandhook
.
annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
({
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
HookMode
{
int
AUTO
=
0
;
int
INLINE
=
1
;
int
REPLACE
=
2
;
int
value
()
default
AUTO
;
}
hooklib/src/main/cpp/sandhook.cpp
View file @
d1c1b69e
...
...
@@ -7,6 +7,12 @@ SandHook::TrampolineManager trampolineManager;
int
SDK_INT
=
0
;
enum
HookMode
{
AUTO
=
0
,
INLINE
=
1
,
REPLACE
=
2
};
extern
"C"
JNIEXPORT
jboolean
JNICALL
Java_com_swift_sandhook_SandHook_initNative
(
JNIEnv
*
env
,
jclass
type
,
jint
sdk
)
{
...
...
@@ -151,26 +157,38 @@ bool doHookWithInline(JNIEnv* env,
extern
"C"
JNIEXPORT
jboolean
JNICALL
Java_com_swift_sandhook_SandHook_hookMethod
(
JNIEnv
*
env
,
jclass
type
,
jobject
originMethod
,
jobject
hookMethod
,
jobject
backupMethod
)
{
jobject
hookMethod
,
jobject
backupMethod
,
jint
hookMode
)
{
// TODO
art
::
mirror
::
ArtMethod
*
origin
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
FromReflectedMethod
(
originMethod
));
art
::
mirror
::
ArtMethod
*
hook
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
FromReflectedMethod
(
hookMethod
));
art
::
mirror
::
ArtMethod
*
backup
=
backupMethod
==
NULL
?
nullptr
:
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
FromReflectedMethod
(
backupMethod
));
// if (backup != nullptr) {
// memcpy(backup, origin, SandHook::CastArtMethod::size);
// }
bool
isInterpreter
=
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
get
(
origin
)
==
SandHook
::
CastArtMethod
::
quickToInterpreterBridge
;
bool
idJNi
=
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
get
(
origin
)
==
SandHook
::
CastArtMethod
::
genericJniStub
;
int
mode
=
reinterpret_cast
<
int
>
(
hookMode
);
if
(
mode
==
INLINE
)
{
if
(
isInterpreter
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
Size
threadId
=
getAddressFromJavaByCallMethod
(
env
,
"com/swift/sandhook/SandHook"
,
"getThreadId"
);
if
(
compileMethod
(
origin
,
reinterpret_cast
<
void
*>
(
threadId
))
&&
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
get
(
origin
)
!=
SandHook
::
CastArtMethod
::
quickToInterpreterBridge
)
{
return
static_cast
<
jboolean
>
(
doHookWithInline
(
env
,
origin
,
hook
,
backup
));
}
else
{
return
static_cast
<
jboolean
>
(
doHookWithReplacement
(
origin
,
hook
,
backup
));
}
}
}
else
{
return
static_cast
<
jboolean
>
(
doHookWithInline
(
env
,
origin
,
hook
,
backup
));
}
}
else
if
(
mode
==
REPLACE
)
{
return
static_cast
<
jboolean
>
(
doHookWithReplacement
(
origin
,
hook
,
backup
));
}
// #if defined(__arm__)
// doHookWithReplacement(origin, hook, backup);
// return JNI_TRUE;
// #endif
if
(
isAbsMethod
(
origin
))
{
if
(
SDK_INT
>=
ANDROID_P
&&
BYTE_POINT
==
4
)
{
return
static_cast
<
jboolean
>
(
doHookWithReplacement
(
origin
,
hook
,
backup
));
}
else
if
(
isAbsMethod
(
origin
))
{
return
static_cast
<
jboolean
>
(
doHookWithReplacement
(
origin
,
hook
,
backup
));
}
else
if
(
isInterpreter
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
...
...
hooklib/src/main/java/com/swift/sandhook/SandHook.java
View file @
d1c1b69e
...
...
@@ -2,10 +2,10 @@ package com.swift.sandhook;
import
android.os.Build
;
import
com.swift.sandhook.annotation.HookMode
;
import
com.swift.sandhook.wrapper.HookErrorException
;
import
com.swift.sandhook.wrapper.HookWrapper
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Member
;
import
java.lang.reflect.Method
;
...
...
@@ -62,7 +62,8 @@ public class SandHook {
if
(
target
instanceof
Method
)
{
((
Method
)
target
).
setAccessible
(
true
);
}
boolean
res
=
hookMethod
(
target
,
hook
,
backup
);
HookMode
hookMode
=
hook
.
getAnnotation
(
HookMode
.
class
);
boolean
res
=
hookMethod
(
target
,
hook
,
backup
,
hookMode
==
null
?
HookMode
.
AUTO
:
hookMode
.
value
());
if
(
res
&&
backup
!=
null
)
{
backup
.
setAccessible
(
true
);
}
...
...
@@ -160,7 +161,7 @@ public class SandHook {
private
static
native
boolean
initNative
(
int
sdk
);
private
static
native
boolean
hookMethod
(
Member
originMethod
,
Method
hookMethod
,
Method
backupMethod
);
private
static
native
boolean
hookMethod
(
Member
originMethod
,
Method
hookMethod
,
Method
backupMethod
,
int
hookMode
);
public
static
native
void
ensureMethodCached
(
Method
hook
,
Method
backup
);
...
...
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