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
e75ab9c9
Commit
e75ab9c9
authored
Jan 23, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add thumb code
parent
22ec6bcb
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
124 additions
and
9 deletions
+124
-9
native-lib.cpp
app/src/main/cpp/native-lib.cpp
+4
-4
arm32.S
app/src/main/cpp/trampoline/arch/arm32.S
+44
-3
base.h
app/src/main/cpp/trampoline/arch/base.h
+12
-0
trampoline.cpp
app/src/main/cpp/trampoline/trampoline.cpp
+24
-0
trampoline.h
app/src/main/cpp/trampoline/trampoline.h
+19
-0
trampoline_manager.cpp
app/src/main/cpp/trampoline/trampoline_manager.cpp
+10
-1
trampoline_manager.h
app/src/main/cpp/trampoline/trampoline_manager.h
+10
-0
SandHook.java
app/src/main/java/com/swift/sandhook/SandHook.java
+1
-1
No files found.
app/src/main/cpp/native-lib.cpp
View file @
e75ab9c9
...
...
@@ -125,10 +125,10 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
bool
isInterpreter
=
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
get
(
origin
)
==
SandHook
::
CastArtMethod
::
quickToInterpreterBridge
;
#if defined(__arm__)
doHookWithReplacement
(
origin
,
hook
,
backup
);
return
JNI_TRUE
;
#endif
//
#if defined(__arm__)
//
doHookWithReplacement(origin, hook, backup);
//
return JNI_TRUE;
//
#endif
if
(
isInterpreter
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
...
...
app/src/main/cpp/trampoline/arch/arm32.S
View file @
e75ab9c9
...
...
@@ -4,7 +4,7 @@
#define Reg0 ip
//need restore
#define Reg
1 r11
#define Reg
T ip
#define RegMethod r0
FUNCTION_START(REPLACEMENT_HOOK_TRAMPOLINE)
...
...
@@ -42,7 +42,8 @@ origin_code:
ldr Reg0, addr_origin_art_method
add Reg0, Reg0, #8
ldr Reg0, [Reg0]
add pc, Reg0, SIZE_JUMP
add Reg0, Reg0, SIZE_JUMP
mov pc, Reg0
addr_origin_art_method:
.long 0
offset_entry_code2:
...
...
@@ -52,7 +53,6 @@ addr_hook_art_method:
FUNCTION_END(INLINE_HOOK_TRAMPOLINE)
FUNCTION_START(CALL_ORIGIN_TRAMPOLINE)
ldr RegMethod, origin_art_method
ldr pc, addr_origin
...
...
@@ -62,4 +62,45 @@ addr_origin:
.long 0
FUNCTION_END(CALL_ORIGIN_TRAMPOLINE)
//thumb-2
FUNCTION_START_T(DIRECT_JUMP_TRAMPOLINE_T)
ldr pc, addr_target_t
addr_target_t:
.long 0
FUNCTION_END(DIRECT_JUMP_TRAMPOLINE_T)
FUNCTION_START_T(INLINE_HOOK_TRAMPOLINE_T)
ldr RegT, addr_origin_art_method_t
cmp RegMethod, RegT
bne origin_code_t
ldr RegMethod, addr_hook_art_method_t
ldr RegT, offset_entry_code2_t
add RegT, RegMethod, RegT
ldr pc, [RegT]
origin_code_t:
.long 0
.long 0
ldr RegT, addr_origin_art_method_t
add RegT, RegT, #8
ldr RegT, [RegT]
add RegT, RegT, SIZE_JUMP
mov pc, RegT
addr_origin_art_method_t:
.long 0
offset_entry_code2_t:
.long 0
addr_hook_art_method_t:
.long 0
FUNCTION_END(INLINE_HOOK_TRAMPOLINE_T)
FUNCTION_START_T(CALL_ORIGIN_TRAMPOLINE_T)
ldr RegMethod, origin_art_method_t
ldr pc, addr_origin_t
origin_art_method_t:
.long 0
addr_origin_t:
.long 0
FUNCTION_END(CALL_ORIGIN_TRAMPOLINE_T)
#endif
\ No newline at end of file
app/src/main/cpp/trampoline/arch/base.h
View file @
e75ab9c9
...
...
@@ -11,6 +11,14 @@
.global x; \
x: \
#define FUNCTION_START_T(x) \
.syntax unified; \
.text; \
.align 4; \
.thumb_func; \
.global x; \
x: \
#define FUNCTION_END(x) .size x, .-x
#define REPLACEMENT_HOOK_TRAMPOLINE replacement_hook_trampoline
...
...
@@ -18,4 +26,8 @@ x: \
#define DIRECT_JUMP_TRAMPOLINE direct_jump_trampoline
#define CALL_ORIGIN_TRAMPOLINE call_origin_trampoline
#define INLINE_HOOK_TRAMPOLINE_T inline_hook_trampoline_t
#define DIRECT_JUMP_TRAMPOLINE_T direct_jump_trampoline_t
#define CALL_ORIGIN_TRAMPOLINE_T call_origin_trampoline_t
#endif //SANDHOOK_BASE_H
app/src/main/cpp/trampoline/trampoline.cpp
View file @
e75ab9c9
...
...
@@ -24,7 +24,15 @@ namespace SandHook {
}
Code
templateCode
()
override
{
#if defined(__arm__)
if
(
isThumCode
())
{
return
reinterpret_cast
<
Code
>
(
DIRECT_JUMP_TRAMPOLINE_T
);
}
else
{
return
reinterpret_cast
<
Code
>
(
DIRECT_JUMP_TRAMPOLINE
);
}
#else
return
reinterpret_cast
<
Code
>
(
DIRECT_JUMP_TRAMPOLINE
);
#endif
}
};
...
...
@@ -84,7 +92,15 @@ namespace SandHook {
}
Code
templateCode
()
override
{
#if defined(__arm__)
if
(
isThumCode
())
{
return
reinterpret_cast
<
Code
>
(
INLINE_HOOK_TRAMPOLINE_T
);
}
else
{
return
reinterpret_cast
<
Code
>
(
INLINE_HOOK_TRAMPOLINE
);
}
#else
return
reinterpret_cast
<
Code
>
(
INLINE_HOOK_TRAMPOLINE
);
#endif
}
};
...
...
@@ -105,7 +121,15 @@ namespace SandHook {
}
Code
templateCode
()
override
{
#if defined(__arm__)
if
(
isThumCode
())
{
return
reinterpret_cast
<
Code
>
(
CALL_ORIGIN_TRAMPOLINE_T
);
}
else
{
return
reinterpret_cast
<
Code
>
(
CALL_ORIGIN_TRAMPOLINE
);
}
#else
return
reinterpret_cast
<
Code
>
(
CALL_ORIGIN_TRAMPOLINE
);
#endif
}
};
...
...
app/src/main/cpp/trampoline/trampoline.h
View file @
e75ab9c9
...
...
@@ -83,6 +83,13 @@ extern "C" void INLINE_HOOK_TRAMPOLINE();
extern
"C"
void
REPLACEMENT_HOOK_TRAMPOLINE
();
extern
"C"
void
CALL_ORIGIN_TRAMPOLINE
();
#if defined(__arm__)
extern
"C"
void
DIRECT_JUMP_TRAMPOLINE_T
();
extern
"C"
void
INLINE_HOOK_TRAMPOLINE_T
();
extern
"C"
void
CALL_ORIGIN_TRAMPOLINE_T
();
#endif
namespace
SandHook
{
//deal with little or big edn
...
...
@@ -105,6 +112,15 @@ namespace SandHook {
codeLen
=
codeLength
();
tempCode
=
templateCode
();
}
void
setThumb
(
bool
thumb
)
{
isThumb
=
thumb
;
}
bool
isThumCode
()
{
return
isThumb
;
}
void
setExecuteSpace
(
Code
start
)
{
code
=
start
;
memcpy
(
code
,
tempCode
,
codeLen
);
...
...
@@ -133,9 +149,11 @@ namespace SandHook {
void
clone
(
Code
dest
)
{
memcpy
(
dest
,
code
,
codeLen
);
}
Code
getCode
()
{
return
code
;
}
Size
getCodeLen
()
{
return
codeLen
;
}
...
...
@@ -166,6 +184,7 @@ namespace SandHook {
Code
code
;
Code
tempCode
;
Size
codeLen
;
bool
isThumb
=
false
;
};
}
...
...
app/src/main/cpp/trampoline/trampoline_manager.cpp
View file @
e75ab9c9
...
...
@@ -80,7 +80,10 @@ namespace SandHook {
Code
originEntry
;
//生成二段跳板
inlineHookTrampoline
=
new
InlineHookTrampoline
;
inlineHookTrampoline
=
new
InlineHookTrampoline
();
#if defined(__arm__)
checkThumbCode
(
inlineHookTrampoline
,
getEntryCode
(
originMethod
));
#endif
inlineHookTrampoline
->
init
();
inlineHookTrampolineSpace
=
allocExecuteSpace
(
inlineHookTrampoline
->
getCodeLen
());
if
(
inlineHookTrampolineSpace
==
0
)
...
...
@@ -94,6 +97,9 @@ namespace SandHook {
//注入 EntryCode
directJumpTrampoline
=
new
DirectJumpTrampoline
();
#if defined(__arm__)
checkThumbCode
(
directJumpTrampoline
,
getEntryCode
(
originMethod
));
#endif
directJumpTrampoline
->
init
();
originEntry
=
getEntryCode
(
originMethod
);
if
(
!
memUnprotect
(
reinterpret_cast
<
Size
>
(
originEntry
),
directJumpTrampoline
->
getCodeLen
()))
{
...
...
@@ -106,6 +112,9 @@ namespace SandHook {
//备份原始方法
if
(
backupMethod
!=
nullptr
)
{
callOriginTrampoline
=
new
CallOriginTrampoline
();
#if defined(__arm__)
checkThumbCode
(
callOriginTrampoline
,
getEntryCode
(
originMethod
));
#endif
callOriginTrampoline
->
init
();
callOriginTrampolineSpace
=
allocExecuteSpace
(
callOriginTrampoline
->
getCodeLen
());
if
(
callOriginTrampolineSpace
==
0
)
...
...
app/src/main/cpp/trampoline/trampoline_manager.h
View file @
e75ab9c9
...
...
@@ -63,6 +63,16 @@ namespace SandHook {
return
entryCode
;
}
bool
isThumbCode
(
Size
codeAddr
)
{
return
(
codeAddr
&
(
Size
)
1
)
==
(
Size
)
1
;
}
void
checkThumbCode
(
Trampoline
*
trampoline
,
Code
code
)
{
#if defined(__arm__)
trampoline
->
setThumb
(
isThumbCode
(
reinterpret_cast
<
Size
>
(
code
)));
#endif
}
private
:
...
...
app/src/main/java/com/swift/sandhook/SandHook.java
View file @
e75ab9c9
...
...
@@ -56,10 +56,10 @@ public class SandHook {
}
private
static
void
resolveBackupMethod
(
Method
method
)
{
//ignore result, just call to trigger resolve
try
{
method
.
invoke
(
null
,
null
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
...
...
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