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
0722234f
Commit
0722234f
authored
Feb 15, 2019
by
swift_gan
Committed by
swift_gan
Feb 15, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tweak api
parent
6bce5cfd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
24 deletions
+45
-24
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+3
-5
SandHook.java
hooklib/src/main/java/com/swift/sandhook/SandHook.java
+41
-19
HookWrapper.java
...src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
+1
-0
No files found.
hooklib/src/main/cpp/sandhook.cpp
View file @
0722234f
...
...
@@ -184,7 +184,7 @@ bool doHookWithInline(JNIEnv* env,
}
extern
"C"
JNIEXPORT
j
boolean
JNICALL
JNIEXPORT
j
int
JNICALL
Java_com_swift_sandhook_SandHook_hookMethod
(
JNIEnv
*
env
,
jclass
type
,
jobject
originMethod
,
jobject
hookMethod
,
jobject
backupMethod
,
jint
hookMode
)
{
...
...
@@ -232,11 +232,9 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
label_hook
:
if
(
isInlineHook
&&
trampolineManager
.
canSafeInline
(
origin
))
{
LOGD
(
"method hook by inline!"
);
return
static_cast
<
jboolean
>
(
doHookWithInline
(
env
,
origin
,
hook
,
backup
));
return
doHookWithInline
(
env
,
origin
,
hook
,
backup
)
?
INLINE
:
-
1
;
}
else
{
LOGW
(
"method hook by replacement!"
);
return
static_cast
<
jboolean
>
(
doHookWithReplacement
(
env
,
origin
,
hook
,
backup
));
return
doHookWithReplacement
(
env
,
origin
,
hook
,
backup
)
?
REPLACE
:
-
1
;
}
}
...
...
hooklib/src/main/java/com/swift/sandhook/SandHook.java
View file @
0722234f
...
...
@@ -24,6 +24,11 @@ public class SandHook {
SandHook
.
hookModeCallBack
=
hookModeCallBack
;
}
private
static
HookResultCallBack
hookResultCallBack
;
public
static
void
setHookResultCallBack
(
HookResultCallBack
hookResultCallBack
)
{
SandHook
.
hookResultCallBack
=
hookResultCallBack
;
}
public
static
Class
artMethodClass
;
public
static
Field
nativePeerField
;
...
...
@@ -67,23 +72,20 @@ public class SandHook {
}
public
static
synchronized
void
hook
(
HookWrapper
.
HookEntity
entity
)
throws
HookErrorException
{
if
(
entity
.
target
!=
null
&&
entity
.
hook
!=
null
)
{
if
(
globalHookEntityMap
.
containsKey
(
entity
.
target
))
throw
new
HookErrorException
(
"method <"
+
entity
.
target
.
getName
()
+
"> has been hooked!"
);
if
(!
SandHook
.
hook
(
entity
.
target
,
entity
.
hook
,
entity
.
backup
))
{
throw
new
HookErrorException
(
"hook method <"
+
entity
.
target
.
getName
()
+
"> error in native!"
);
}
Log
.
d
(
"SandHook"
,
"method <"
+
entity
.
target
.
getName
()
+
"> hook success!"
);
globalHookEntityMap
.
put
(
entity
.
target
,
entity
);
if
(
entity
.
backup
!=
null
)
{
globalBackupMap
.
put
(
entity
.
backup
,
entity
);
}
}
}
public
static
boolean
hook
(
Member
target
,
Method
hook
,
Method
backup
)
{
if
(
entity
==
null
)
throw
new
HookErrorException
(
"null entity"
);
Member
target
=
entity
.
target
;
Method
hook
=
entity
.
hook
;
Method
backup
=
entity
.
backup
;
if
(
target
==
null
||
hook
==
null
)
return
false
;
throw
new
HookErrorException
(
"null input"
);
if
(
globalHookEntityMap
.
containsKey
(
entity
.
target
))
throw
new
HookErrorException
(
"method <"
+
entity
.
target
.
getName
()
+
"> has been hooked!"
);
resolveStaticMethod
(
target
);
if
(
backup
!=
null
)
{
resolveStaticMethod
(
backup
);
...
...
@@ -98,7 +100,7 @@ public class SandHook {
mode
=
hookModeCallBack
.
hookMode
(
target
);
}
boolean
res
;
int
res
;
if
(
mode
!=
HookMode
.
AUTO
)
{
res
=
hookMethod
(
target
,
hook
,
backup
,
mode
);
}
else
{
...
...
@@ -106,10 +108,25 @@ public class SandHook {
res
=
hookMethod
(
target
,
hook
,
backup
,
hookMode
==
null
?
HookMode
.
AUTO
:
hookMode
.
value
());
}
if
(
res
&&
backup
!=
null
)
{
if
(
res
>
0
&&
backup
!=
null
)
{
backup
.
setAccessible
(
true
);
}
return
res
;
entity
.
hookMode
=
res
;
if
(
hookResultCallBack
!=
null
)
{
hookResultCallBack
.
hookResult
(
res
>
0
,
entity
);
}
if
(
res
<=
0
)
throw
new
HookErrorException
(
"hook method <"
+
entity
.
target
.
toString
()
+
"> error in native!"
);
globalHookEntityMap
.
put
(
entity
.
target
,
entity
);
if
(
entity
.
backup
!=
null
)
{
globalBackupMap
.
put
(
entity
.
backup
,
entity
);
}
Log
.
d
(
"SandHook"
,
"method <"
+
entity
.
target
.
toString
()
+
"> hook <"
+
(
res
==
HookMode
.
INLINE
?
"inline"
:
"replacement"
)
+
"> success!"
);
}
public
static
void
ensureBackupDeclaringClass
(
Method
backupMethod
)
{
...
...
@@ -226,7 +243,7 @@ public class SandHook {
//default on!
public
static
native
void
setInlineSafeCheck
(
boolean
check
);
private
static
native
boolean
hookMethod
(
Member
originMethod
,
Method
hookMethod
,
Method
backupMethod
,
int
hookMode
);
private
static
native
int
hookMethod
(
Member
originMethod
,
Method
hookMethod
,
Method
backupMethod
,
int
hookMode
);
public
static
native
void
ensureMethodCached
(
Method
hook
,
Method
backup
);
...
...
@@ -237,4 +254,9 @@ public class SandHook {
int
hookMode
(
Member
originMethod
);
}
@FunctionalInterface
public
interface
HookResultCallBack
{
void
hookResult
(
boolean
success
,
HookWrapper
.
HookEntity
hookEntity
);
}
}
hooklib/src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
View file @
0722234f
...
...
@@ -273,6 +273,7 @@ public class HookWrapper {
public
boolean
hookIsStub
=
false
;
public
Class
[]
pars
;
public
int
hookMode
;
public
HookEntity
(
Member
target
)
{
this
.
target
=
target
;
...
...
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