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
8b9a58a3
Commit
8b9a58a3
authored
Sep 23, 2019
by
swift_gan
Committed by
ganyao114
Sep 23, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FixTemp: filter case "bx pc"
parent
e93f103d
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
82 additions
and
22 deletions
+82
-22
MainActivity.java
app/src/main/java/com/swift/sandhook/MainActivity.java
+5
-1
PendingHookHandler.java
.../src/main/java/com/swift/sandhook/PendingHookHandler.java
+1
-2
SandHook.java
hooklib/src/main/java/com/swift/sandhook/SandHook.java
+1
-1
HookWrapper.java
...src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
+1
-0
hook_arm32.cpp
nativehook/src/main/cpp/archs/arm/arm32/hook/hook_arm32.cpp
+37
-6
code_relocate_arm32.cpp
...main/cpp/archs/arm/arm32/relocate/code_relocate_arm32.cpp
+19
-9
code_relocate_arm32.h
...c/main/cpp/archs/arm/arm32/relocate/code_relocate_arm32.h
+2
-2
exception.h
nativehook/src/main/cpp/includes/exception.h
+16
-1
No files found.
app/src/main/java/com/swift/sandhook/MainActivity.java
View file @
8b9a58a3
...
...
@@ -85,7 +85,11 @@ public class MainActivity extends AppCompatActivity {
testPluginHook
(
str
);
MyApp
.
initedTest
=
true
;
PendingHookTest
.
test
();
try
{
PendingHookTest
.
test
();
}
catch
(
Throwable
e
)
{
}
}
public
static
Field
getField
(
Class
topClass
,
String
fieldName
)
throws
NoSuchFieldException
{
...
...
hooklib/src/main/java/com/swift/sandhook/PendingHookHandler.java
View file @
8b9a58a3
package
com
.
swift
.
sandhook
;
import
android.util.Log
;
import
com.swift.sandhook.wrapper.HookErrorException
;
import
com.swift.sandhook.wrapper.HookWrapper
;
...
...
@@ -47,6 +45,7 @@ public class PendingHookHandler {
for
(
HookWrapper
.
HookEntity
entity:
entities
)
{
HookLog
.
w
(
"do pending hook for method: "
+
entity
.
target
.
toString
());
try
{
entity
.
initClass
=
false
;
SandHook
.
hook
(
entity
);
}
catch
(
HookErrorException
e
)
{
HookLog
.
e
(
"Pending Hook Error!"
,
e
);
...
...
hooklib/src/main/java/com/swift/sandhook/SandHook.java
View file @
8b9a58a3
...
...
@@ -91,7 +91,7 @@ public class SandHook {
if
(
SandHookConfig
.
delayHook
&&
PendingHookHandler
.
canWork
()
&&
ClassStatusUtils
.
isStaticAndNoInited
(
entity
.
target
))
{
PendingHookHandler
.
addPendingHook
(
entity
);
return
;
}
else
{
}
else
if
(
entity
.
initClass
)
{
resolveStaticMethod
(
target
);
}
...
...
hooklib/src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
View file @
8b9a58a3
...
...
@@ -409,6 +409,7 @@ public class HookWrapper {
public
boolean
hookIsStub
=
false
;
public
boolean
resolveDexCache
=
true
;
public
boolean
backupIsStub
=
true
;
public
boolean
initClass
=
true
;
public
Class
[]
pars
;
public
int
hookMode
;
...
...
nativehook/src/main/cpp/archs/arm/arm32/hook/hook_arm32.cpp
View file @
8b9a58a3
...
...
@@ -57,7 +57,11 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) {
//build backup method
CodeRelocateA32
relocate
=
CodeRelocateA32
(
assembler_backup
);
backup
=
relocate
.
Relocate
(
origin
,
code_container_inline
->
Size
(),
nullptr
);
try
{
backup
=
relocate
.
Relocate
(
origin
,
code_container_inline
->
Size
(),
nullptr
);
}
catch
(
ErrorCodeException
e
)
{
return
nullptr
;
}
#define __ assembler_backup.
Label
*
origin_addr_label
=
new
Label
();
ALIGN_FOR_LDR
...
...
@@ -99,7 +103,11 @@ bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) {
//build backup method
CodeRelocateA32
relocate
=
CodeRelocateA32
(
assembler_backup
);
backup
=
relocate
.
Relocate
(
origin
,
change_mode
?
(
4
*
2
+
2
)
:
(
4
*
2
),
nullptr
);
try
{
backup
=
relocate
.
Relocate
(
origin
,
change_mode
?
(
4
*
2
+
2
)
:
(
4
*
2
),
nullptr
);
}
catch
(
ErrorCodeException
e
)
{
return
nullptr
;
}
#define __ assembler_backup.
Label
*
origin_addr_label
=
new
Label
();
ALIGN_FOR_LDR
...
...
@@ -161,14 +169,33 @@ void *InlineHookArm32Android::SingleInstHook(void *origin, void *replace) {
#undef __
//build backup method
bool
rec_to_thumb
=
true
;
CodeRelocateA32
relocate
=
CodeRelocateA32
(
assembler_backup
);
backup
=
relocate
.
Relocate
(
origin
,
code_container_inline
->
Size
(),
nullptr
);
try
{
backup
=
relocate
.
Relocate
(
origin
,
code_container_inline
->
Size
(),
nullptr
);
}
catch
(
ErrorCodeException
e
)
{
if
(
e
.
Code
()
==
ERROR_SWITCH_TO_ARM32
)
{
//to arm32 mode
rec_to_thumb
=
false
;
assembler_backup
.
AllocBufferFirst
(
4
*
4
);
backup
=
assembler_backup
.
GetPC
();
}
else
{
return
nullptr
;
}
}
Addr
rec_addr
=
reinterpret_cast
<
Addr
>
(
origin_code
);
if
(
rec_to_thumb
)
{
rec_addr
+=
relocate
.
cur_offset
;
rec_addr
=
reinterpret_cast
<
Addr
>
(
GetThumbPC
(
reinterpret_cast
<
void
*>
(
rec_addr
)));
}
else
{
rec_addr
+=
4
;
}
#define __ assembler_backup.
Label
*
origin_addr_label
=
new
Label
();
Label
*
origin_addr_label
=
new
Label
();
ALIGN_FOR_LDR
__
Ldr
(
PC
,
origin_addr_label
);
__
Emit
(
origin_addr_label
);
__
Emit
(
(
Addr
)
GetThumbPC
(
reinterpret_cast
<
void
*>
(
reinterpret_cast
<
Addr
>
(
origin_code
)
+
relocate
.
cur_offset
))
);
__
Emit
(
rec_addr
);
__
Finish
();
#undef __
...
...
@@ -208,7 +235,11 @@ bool InlineHookArm32Android::SingleBreakPoint(void *point, BreakCallback callbac
//build backup method
CodeRelocateA32
relocate
=
CodeRelocateA32
(
assembler_backup
);
backup
=
relocate
.
Relocate
(
point
,
code_container_inline
->
Size
(),
nullptr
);
try
{
backup
=
relocate
.
Relocate
(
point
,
code_container_inline
->
Size
(),
nullptr
);
}
catch
(
ErrorCodeException
e
)
{
return
nullptr
;
}
#define __ assembler_backup.
Label
*
origin_addr_label
=
new
Label
();
ALIGN_FOR_LDR
...
...
nativehook/src/main/cpp/archs/arm/arm32/relocate/code_relocate_arm32.cpp
View file @
8b9a58a3
...
...
@@ -11,17 +11,17 @@ using namespace SandHook::AsmA32;
using
namespace
SandHook
::
Utils
;
using
namespace
SandHook
::
Decoder
;
#define __ assembler
A
32->
#define __ assembler
_a
32->
#define IMPL_RELOCATE(T, X) void CodeRelocateA32::relocate_##T##_##X (INST_##T(X)* inst, void* to
P
c) throw(ErrorCodeException)
#define IMPL_RELOCATE(T, X) void CodeRelocateA32::relocate_##T##_##X (INST_##T(X)* inst, void* to
_p
c) throw(ErrorCodeException)
#define CASE(T, X) \
case ENUM_VALUE(InstCode##T, InstCode##T::X): \
relocate_##T##_##X(reinterpret_cast<INST_##T(X)*>(instruction), to
P
c); \
relocate_##T##_##X(reinterpret_cast<INST_##T(X)*>(instruction), to
_p
c); \
break;
CodeRelocateA32
::
CodeRelocateA32
(
AssemblerA32
&
assembler
)
:
CodeRelocate
(
assembler
.
code_container
)
{
this
->
assembler
A
32
=
&
assembler
;
this
->
assembler
_a
32
=
&
assembler
;
}
bool
CodeRelocateA32
::
Visit
(
BaseUnit
*
unit
,
void
*
pc
)
{
...
...
@@ -33,7 +33,7 @@ bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) {
return
true
;
}
void
*
CodeRelocateA32
::
Relocate
(
void
*
startPc
,
Addr
len
,
void
*
to
P
c
=
nullptr
)
throw
(
ErrorCodeException
)
{
void
*
CodeRelocateA32
::
Relocate
(
void
*
startPc
,
Addr
len
,
void
*
to
_p
c
=
nullptr
)
throw
(
ErrorCodeException
)
{
AutoLock
autoLock
(
relocate_lock
);
start_addr
=
reinterpret_cast
<
Addr
>
(
startPc
);
if
(
IsThumbCode
(
start_addr
))
{
...
...
@@ -43,7 +43,7 @@ void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) t
cur_offset
=
0
;
__
AllocBufferFirst
(
static_cast
<
U32
>
(
len
*
8
));
void
*
curPc
=
__
GetPC
();
if
(
to
P
c
==
nullptr
)
{
if
(
to
_p
c
==
nullptr
)
{
Disassembler
::
Get
()
->
Disassemble
(
startPc
,
len
,
*
this
,
true
);
}
else
{
//TODO
...
...
@@ -51,7 +51,7 @@ void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) t
return
curPc
;
}
void
*
CodeRelocateA32
::
Relocate
(
BaseInst
*
instruction
,
void
*
to
P
c
)
throw
(
ErrorCodeException
)
{
void
*
CodeRelocateA32
::
Relocate
(
BaseInst
*
instruction
,
void
*
to
_p
c
)
throw
(
ErrorCodeException
)
{
void
*
cur_pc
=
__
GetPC
();
//insert later AddBind labels
...
...
@@ -141,8 +141,18 @@ IMPL_RELOCATE(T16, B) {
}
IMPL_RELOCATE
(
T16
,
BX_BLX
)
{
__
Emit
(
reinterpret_cast
<
BaseInst
*>
(
inst
));
inst
->
Ref
();
if
(
*
inst
->
rm
==
PC
)
{
//maybe mode switch!
if
(
IsThumbCode
(
reinterpret_cast
<
Addr
>
(
inst
->
GetPC
())))
{
__
Nop16
();
}
else
{
//switch to arm mode
throw
ErrorCodeException
(
ERROR_SWITCH_TO_ARM32
,
"switch to arm32! not impl!"
);
}
}
else
{
__
Emit
(
reinterpret_cast
<
BaseInst
*>
(
inst
));
inst
->
Ref
();
}
}
IMPL_RELOCATE
(
T16
,
CBZ_CBNZ
)
{
...
...
nativehook/src/main/cpp/archs/arm/arm32/relocate/code_relocate_arm32.h
View file @
8b9a58a3
...
...
@@ -22,7 +22,7 @@ namespace SandHook {
public
:
CodeRelocateA32
(
AssemblerA32
&
assembler
);
void
*
Relocate
(
BaseInst
*
instruction
,
void
*
to
P
c
)
throw
(
ErrorCodeException
)
override
;
void
*
Relocate
(
BaseInst
*
instruction
,
void
*
to
_p
c
)
throw
(
ErrorCodeException
)
override
;
void
*
Relocate
(
void
*
startPc
,
Addr
len
,
void
*
toPc
)
throw
(
ErrorCodeException
)
override
;
...
...
@@ -48,7 +48,7 @@ namespace SandHook {
private
:
AssemblerA32
*
assembler
A
32
;
AssemblerA32
*
assembler
_a
32
;
};
}
...
...
nativehook/src/main/cpp/includes/exception.h
View file @
8b9a58a3
...
...
@@ -3,22 +3,37 @@
//
#pragma once
#include <log.h>
#include "exception"
namespace
SandHook
{
namespace
Asm
{
constexpr
int
ERROR_DEFAULT
=
0
;
constexpr
int
ERROR_SWITCH_TO_ARM32
=
1
;
class
ErrorCodeException
:
public
std
::
exception
{
public
:
ErrorCodeException
(
const
char
*
what_
)
:
what_
(
what_
)
{}
ErrorCodeException
(
const
char
*
what
)
:
code_
(
ERROR_DEFAULT
),
what_
(
what
)
{
LOGE
(
"ErrorCodeException: %s"
,
what
);
}
ErrorCodeException
(
const
int
code
,
const
char
*
what
)
:
code_
(
code
),
what_
(
what
)
{
LOGE
(
"ErrorCodeException: %s"
,
what
);
}
const
char
*
what
()
const
noexcept
override
{
return
what_
;
}
const
int
Code
()
const
{
return
code_
;
}
private
:
const
int
code_
;
const
char
*
what_
;
};
...
...
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