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
5725154d
Commit
5725154d
authored
Jan 28, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add plugin hooker sample
parent
ca8d08d1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
14 deletions
+62
-14
MainActivity.java
app/src/main/java/com/swift/sandhook/MainActivity.java
+7
-0
MyApp.java
app/src/main/java/com/swift/sandhook/MyApp.java
+2
-1
PluginHooker.java
...rc/main/java/com/swift/sandhook/hookers/PluginHooker.java
+32
-0
HookWrapper.java
...src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
+21
-13
No files found.
app/src/main/java/com/swift/sandhook/MainActivity.java
View file @
5725154d
...
...
@@ -82,6 +82,8 @@ public class MainActivity extends AppCompatActivity {
}).
start
();
inter
.
dosth
();
testPluginHook
(
str
);
}
public
static
Field
getField
(
Class
topClass
,
String
fieldName
)
throws
NoSuchFieldException
{
...
...
@@ -120,6 +122,11 @@ public class MainActivity extends AppCompatActivity {
return
a
+
b
;
}
public
int
testPluginHook
(
TestClass
testClass
)
{
Log
.
e
(
"MainActivity"
,
"call testPluginHook origin"
);
return
testClass
.
a
;
}
@Override
public
boolean
onOptionsItemSelected
(
MenuItem
item
)
{
// Handle action bar item clicks here. The action bar will
...
...
app/src/main/java/com/swift/sandhook/MyApp.java
View file @
5725154d
...
...
@@ -32,7 +32,8 @@ public class MyApp extends Application {
DexClassLoader
dexClassLoader
=
new
DexClassLoader
(
"/sdcard/hookers-debug.apk"
,
getCacheDir
().
getAbsolutePath
(),
null
,
classLoader
);
Class
absHookerClass
=
Class
.
forName
(
"com.swift.sandhook.hookers.AbsHooker"
,
true
,
dexClassLoader
);
SandHook
.
addHookClass
(
absHookerClass
);
Class
pluginHookerClass
=
Class
.
forName
(
"com.swift.sandhook.hookers.PluginHooker"
,
true
,
dexClassLoader
);
SandHook
.
addHookClass
(
getClassLoader
(),
absHookerClass
,
pluginHookerClass
);
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
HookErrorException
e
)
{
...
...
hookers/src/main/java/com/swift/sandhook/hookers/PluginHooker.java
0 → 100644
View file @
5725154d
package
com
.
swift
.
sandhook
.
hookers
;
import
android.util.Log
;
import
com.swift.sandhook.annotation.HookMethod
;
import
com.swift.sandhook.annotation.HookMethodBackup
;
import
com.swift.sandhook.annotation.HookReflectClass
;
import
com.swift.sandhook.annotation.MethodReflectParams
;
import
java.lang.reflect.Method
;
@HookReflectClass
(
"com.swift.sandhook.MainActivity"
)
public
class
PluginHooker
{
@HookMethodBackup
(
"testPluginHook"
)
@MethodReflectParams
(
"com.swift.sandhook.test.TestClass"
)
static
Method
backup
;
@HookMethod
(
"testPluginHook"
)
@MethodReflectParams
(
"com.swift.sandhook.test.TestClass"
)
public
static
int
testPluginHook
(
Object
thiz
,
Object
test
)
{
Log
.
e
(
"PluginHooker"
,
"testPluginHook be hooked"
);
return
testPluginHookBackup
(
thiz
,
test
);
}
@HookMethodBackup
(
"testPluginHook"
)
@MethodReflectParams
(
"com.swift.sandhook.test.TestClass"
)
public
static
int
testPluginHookBackup
(
Object
thiz
,
Object
test
)
{
return
testPluginHookBackup
(
thiz
,
test
);
}
}
hooklib/src/main/java/com/swift/sandhook/wrapper/HookWrapper.java
View file @
5725154d
...
...
@@ -37,8 +37,8 @@ public class HookWrapper {
Class
targetHookClass
=
getTargetHookClass
(
classLoader
,
clazz
);
if
(
targetHookClass
==
null
)
throw
new
HookErrorException
(
"error hook wrapper class :"
+
clazz
.
getName
());
Map
<
Member
,
HookEntity
>
hookEntityMap
=
getHookMethods
(
targetHookClass
,
clazz
);
fillBackupMethod
(
clazz
,
hookEntityMap
);
Map
<
Member
,
HookEntity
>
hookEntityMap
=
getHookMethods
(
classLoader
,
targetHookClass
,
clazz
);
fillBackupMethod
(
cla
ssLoader
,
cla
zz
,
hookEntityMap
);
for
(
HookEntity
entity:
hookEntityMap
.
values
())
{
if
(
entity
.
target
!=
null
&&
entity
.
hook
!=
null
)
{
SandHook
.
hook
(
entity
.
target
,
entity
.
hook
,
entity
.
backup
);
...
...
@@ -47,7 +47,7 @@ public class HookWrapper {
}
}
private
static
void
fillBackupMethod
(
Class
<?>
clazz
,
Map
<
Member
,
HookEntity
>
hookEntityMap
)
{
private
static
void
fillBackupMethod
(
Class
Loader
classLoader
,
Class
<?>
clazz
,
Map
<
Member
,
HookEntity
>
hookEntityMap
)
{
Field
[]
fields
=
clazz
.
getDeclaredFields
();
if
(
fields
==
null
||
fields
.
length
==
0
)
return
;
...
...
@@ -62,7 +62,7 @@ public class HookWrapper {
if
(
hookMethodBackup
==
null
)
continue
;
for
(
HookEntity
hookEntity:
hookEntityMap
.
values
())
{
if
(
TextUtils
.
equals
(
hookEntity
.
target
.
getName
(),
hookMethodBackup
.
value
())
&&
hookEntity
.
backup
!=
null
&&
samePars
(
field
,
hookEntity
.
pars
))
{
if
(
TextUtils
.
equals
(
hookEntity
.
target
.
getName
(),
hookMethodBackup
.
value
())
&&
hookEntity
.
backup
!=
null
&&
samePars
(
classLoader
,
field
,
hookEntity
.
pars
))
{
field
.
setAccessible
(
true
);
try
{
field
.
set
(
null
,
hookEntity
.
backup
);
...
...
@@ -74,7 +74,7 @@ public class HookWrapper {
}
}
private
static
Map
<
Member
,
HookEntity
>
getHookMethods
(
Class
targetHookClass
,
Class
<?>
hookWrapperClass
)
throws
HookErrorException
{
private
static
Map
<
Member
,
HookEntity
>
getHookMethods
(
Class
Loader
classLoader
,
Class
targetHookClass
,
Class
<?>
hookWrapperClass
)
throws
HookErrorException
{
Map
<
Member
,
HookEntity
>
hookEntityMap
=
new
HashMap
<>();
Method
[]
methods
=
hookWrapperClass
.
getDeclaredMethods
();
if
(
methods
==
null
&&
methods
.
length
==
0
)
...
...
@@ -87,7 +87,7 @@ public class HookWrapper {
Class
[]
pars
;
if
(
hookMethodAnno
!=
null
)
{
methodName
=
hookMethodAnno
.
value
();
pars
=
parseMethodPars
(
method
);
pars
=
parseMethodPars
(
classLoader
,
method
);
try
{
if
(
methodName
.
equals
(
"<init>"
))
{
foundMethod
=
targetHookClass
.
getConstructor
(
pars
);
...
...
@@ -106,7 +106,7 @@ public class HookWrapper {
entity
.
hook
=
method
;
}
else
if
(
hookMethodBackupAnno
!=
null
)
{
methodName
=
hookMethodBackupAnno
.
value
();
pars
=
parseMethodPars
(
method
);
pars
=
parseMethodPars
(
classLoader
,
method
);
try
{
if
(
methodName
.
equals
(
"<init>"
))
{
foundMethod
=
targetHookClass
.
getConstructor
(
pars
);
...
...
@@ -131,7 +131,7 @@ public class HookWrapper {
return
hookEntityMap
;
}
private
static
Class
[]
parseMethodPars
(
Method
method
)
throws
HookErrorException
{
private
static
Class
[]
parseMethodPars
(
ClassLoader
classLoader
,
Method
method
)
throws
HookErrorException
{
MethodParams
methodParams
=
method
.
getAnnotation
(
MethodParams
.
class
);
MethodReflectParams
methodReflectParams
=
method
.
getAnnotation
(
MethodReflectParams
.
class
);
if
(
methodParams
!=
null
)
{
...
...
@@ -142,7 +142,11 @@ public class HookWrapper {
Class
[]
pars
=
new
Class
[
methodReflectParams
.
value
().
length
];
for
(
int
i
=
0
;
i
<
methodReflectParams
.
value
().
length
;
i
++)
{
try
{
pars
[
i
]
=
Class
.
forName
(
methodReflectParams
.
value
()[
i
]);
if
(
classLoader
==
null
)
{
pars
[
i
]
=
Class
.
forName
(
methodReflectParams
.
value
()[
i
]);
}
else
{
pars
[
i
]
=
Class
.
forName
(
methodReflectParams
.
value
()[
i
],
true
,
classLoader
);
}
}
catch
(
ClassNotFoundException
e
)
{
throw
new
HookErrorException
(
"hook method pars error: "
+
method
.
getName
(),
e
);
}
...
...
@@ -153,7 +157,7 @@ public class HookWrapper {
}
}
private
static
Class
[]
parseMethodPars
(
Field
field
)
throws
HookErrorException
{
private
static
Class
[]
parseMethodPars
(
ClassLoader
classLoader
,
Field
field
)
throws
HookErrorException
{
MethodParams
methodParams
=
field
.
getAnnotation
(
MethodParams
.
class
);
MethodReflectParams
methodReflectParams
=
field
.
getAnnotation
(
MethodReflectParams
.
class
);
if
(
methodParams
!=
null
)
{
...
...
@@ -164,7 +168,11 @@ public class HookWrapper {
Class
[]
pars
=
new
Class
[
methodReflectParams
.
value
().
length
];
for
(
int
i
=
0
;
i
<
methodReflectParams
.
value
().
length
;
i
++)
{
try
{
pars
[
i
]
=
Class
.
forName
(
methodReflectParams
.
value
()[
i
]);
if
(
classLoader
==
null
)
{
pars
[
i
]
=
Class
.
forName
(
methodReflectParams
.
value
()[
i
]);
}
else
{
pars
[
i
]
=
Class
.
forName
(
methodReflectParams
.
value
()[
i
],
true
,
classLoader
);
}
}
catch
(
ClassNotFoundException
e
)
{
throw
new
HookErrorException
(
"hook method pars error: "
+
field
.
getName
(),
e
);
}
...
...
@@ -176,9 +184,9 @@ public class HookWrapper {
}
private
static
boolean
samePars
(
Field
field
,
Class
[]
par
)
{
private
static
boolean
samePars
(
ClassLoader
classLoader
,
Field
field
,
Class
[]
par
)
{
try
{
Class
[]
parsOnField
=
parseMethodPars
(
field
);
Class
[]
parsOnField
=
parseMethodPars
(
classLoader
,
field
);
if
(
par
==
null
)
par
=
new
Class
[
0
];
if
(
parsOnField
==
null
)
...
...
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