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
8c3f0c1d
Commit
8c3f0c1d
authored
Jan 21, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix code
parent
c976181d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
30 additions
and
96 deletions
+30
-96
art.h
app/src/main/cpp/casts/art/art.h
+1
-0
cast_art_method.h
app/src/main/cpp/casts/cast_art_method.h
+13
-4
native-lib.cpp
app/src/main/cpp/native-lib.cpp
+5
-37
MainActivity.java
app/src/main/java/com/swift/sandhook/MainActivity.java
+4
-54
SandHook.java
app/src/main/java/com/swift/sandhook/SandHook.java
+7
-1
No files found.
app/src/main/cpp/casts/art/art.h
View file @
8c3f0c1d
...
...
@@ -41,6 +41,7 @@ public:
class
ArtMethod
{
public
:
uint16_t
palceholder
[
32
];
};
}
...
...
app/src/main/cpp/casts/cast_art_method.h
View file @
8c3f0c1d
...
...
@@ -6,6 +6,7 @@
#define SANDHOOK_CAST_ART_METHOD_H
#include "../includes/cast.h"
#include "../trampoline/trampoline_manager.h"
namespace
SandHook
{
...
...
@@ -17,7 +18,7 @@ namespace SandHook {
int
offset
=
0
;
Size
addr
=
getAddressFromJava
(
jniEnv
,
"com/swift/sandhook/SandHookMethodResolver"
,
"resolvedMethodsAddress"
);
if
(
addr
!=
0
)
{
offset
=
findOffset
(
&
p
,
getParentSize
(),
2
,
reinterpret_cast
<
void
*>
(
addr
)
);
offset
=
findOffset
(
&
p
,
getParentSize
(),
2
,
addr
);
if
(
offset
>=
0
)
{
return
static_cast
<
Size
>
(
offset
);
}
...
...
@@ -68,9 +69,9 @@ namespace SandHook {
class
CastAccessFlag
:
public
IMember
<
art
::
mirror
::
ArtMethod
,
uint32_t
>
{
protected
:
Size
calOffset
(
JNIEnv
*
jniEnv
,
art
::
mirror
::
ArtMethod
p
)
override
{
int
offset
=
findOffset
(
&
p
,
getParentSize
(),
sizeof
(
uint32_t
),
524313
);
int
offset
=
findOffset
(
&
p
,
getParentSize
(),
2
,
(
uint32_t
)
524313
);
if
(
offset
<
0
)
throw
getParentSize
()
+
1
;
return
getParentSize
()
+
1
;
else
return
static_cast
<
size_t
>
(
offset
);
}
...
...
@@ -105,8 +106,11 @@ namespace SandHook {
static
ArrayMember
<
art
::
mirror
::
ArtMethod
>*
dexCacheResolvedMethods
;
static
IMember
<
art
::
mirror
::
ArtMethod
,
uint32_t
>*
dexMethodIndex
;
static
IMember
<
art
::
mirror
::
ArtMethod
,
uint32_t
>*
accessFlag
;
static
Code
quickToInterpreterBridge
;
static
void
init
(
JNIEnv
*
env
)
{
static
void
init
(
JNIEnv
*
env
,
int
sdk
)
{
SDK_INT
=
sdk
;
//init ArtMethodSize
jclass
sizeTestClass
=
env
->
FindClass
(
"com/swift/sandhook/ArtMethodSizeTest"
);
Size
artMethod1
=
(
Size
)
env
->
GetStaticMethodID
(
sizeTestClass
,
"method1"
,
"()V"
);
...
...
@@ -132,6 +136,10 @@ namespace SandHook {
dexMethodIndex
=
new
CastDexMethodIndex
();
dexMethodIndex
->
init
(
env
,
m1
,
size
);
jclass
neverCallTestClass
=
env
->
FindClass
(
"com/swift/sandhook/ClassNeverCall"
);
art
::
mirror
::
ArtMethod
*
neverCall
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCall"
,
"()V"
));
quickToInterpreterBridge
=
reinterpret_cast
<
Code
>
(
entryPointQuickCompiled
->
get
(
*
neverCall
));
// //test
// art::mirror::ArtMethod** mArray = reinterpret_cast<art::mirror::ArtMethod**>(m1.dex_cache_resolved_methods_);
//
...
...
@@ -152,6 +160,7 @@ namespace SandHook {
ArrayMember
<
art
::
mirror
::
ArtMethod
>*
CastArtMethod
::
dexCacheResolvedMethods
=
nullptr
;
IMember
<
art
::
mirror
::
ArtMethod
,
uint32_t
>*
CastArtMethod
::
dexMethodIndex
=
nullptr
;
IMember
<
art
::
mirror
::
ArtMethod
,
uint32_t
>*
CastArtMethod
::
accessFlag
=
nullptr
;
Code
CastArtMethod
::
quickToInterpreterBridge
=
nullptr
;
}
...
...
app/src/main/cpp/native-lib.cpp
View file @
8c3f0c1d
#include <jni.h>
#include <string>
#include "casts/cast_art_method.h"
#include "./trampoline/trampoline_manager.h"
extern
"C"
JNIEXPORT
jstring
JNICALL
Java_com_swift_sandhook_MainActivity_stringFromJNI
(
JNIEnv
*
env
,
jobject
instance
,
jobject
method
/* this */
)
{
std
::
string
hello
=
"Hello from C++"
;
// art::mirror::ArtMethod* artMethod = (art::mirror::ArtMethod*) env->FromReflectedMethod(method);
return
env
->
NewStringUTF
(
hello
.
c_str
());
}
extern
"C"
JNIEXPORT
jint
JNICALL
Java_com_swift_sandhook_MainActivity_calArtSize
(
JNIEnv
*
env
,
jobject
instance
,
jobject
method1
,
jobject
method2
)
{
// Size artMethod1 = (Size) env->FromReflectedMethod(method1);
// Size artMethod2 = (Size) env->FromReflectedMethod(method2);
//
// Size size = artMethod2 - artMethod1;
//
// art::mirror::ArtMethod* methodTest = reinterpret_cast<art::mirror::ArtMethod*>(artMethod1);
//
// Size addr1 = reinterpret_cast<size_t>(&methodTest->ptr_sized_fields_.entry_point_from_quick_compiled_code_);
// Size addr2 = size + artMethod1 - BYTE_POINT;
return
0
;
}
extern
"C"
void
INLINE_HOOK_TRAMPOLINE
();
extern
"C"
JNIEXPORT
void
JNICALL
Java_com_swift_sandhook_
MainActivity_initHook
(
JNIEnv
*
env
,
jobject
instance
)
{
JNIEXPORT
jboolean
JNICALL
Java_com_swift_sandhook_
SandHook_initNative
(
JNIEnv
*
env
,
jclass
type
,
jint
sdk
)
{
INLINE_HOOK_TRAMPOLINE
();
// TODO
SandHook
::
CastArtMethod
::
init
(
env
,
sdk
);
SandHook
::
CastArtMethod
::
init
(
env
)
;
return
JNI_TRUE
;
}
\ No newline at end of file
app/src/main/java/com/swift/sandhook/MainActivity.java
View file @
8c3f0c1d
...
...
@@ -20,9 +20,7 @@ import java.lang.reflect.Method;
public
class
MainActivity
extends
AppCompatActivity
{
// Used to load the 'native-lib' library on application startup.
static
{
System
.
loadLibrary
(
"native-lib"
);
}
@Override
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
...
...
@@ -40,6 +38,8 @@ public class MainActivity extends AppCompatActivity {
}
});
SandHook
.
init
();
try
{
HookWrapper
.
addHookClass
(
ActivityHooker
.
class
);
}
catch
(
HookErrorException
e
)
{
...
...
@@ -48,47 +48,6 @@ public class MainActivity extends AppCompatActivity {
// Example of a call to a native method
TextView
tv
=
(
TextView
)
findViewById
(
R
.
id
.
sample_text
);
try
{
Method
method1
=
ArtMethodSizeTest
.
class
.
getDeclaredMethod
(
"method1"
);
Method
method2
=
ArtMethodSizeTest
.
class
.
getDeclaredMethod
(
"method2"
);
Field
field
=
Class
.
class
.
getDeclaredField
(
"dexCache"
);
field
.
setAccessible
(
true
);
Object
dexCache
=
field
.
get
(
ArtMethodSizeTest
.
class
);
Field
resolvedMethodsField
=
dexCache
.
getClass
().
getDeclaredField
(
"resolvedMethods"
);
resolvedMethodsField
.
setAccessible
(
true
);
// long[] methods = (long[]) resolvedMethodsField.get(dexCache);
//
//
// Field dexMethodIndexField = getField(Method.class, "dexMethodIndex");
// dexMethodIndexField.setAccessible(true);
// int dexMethodIndex = (int) dexMethodIndexField.get(method1);
//
// Field artMethodField = getField(Method.class, "artMethod");
// artMethodField.setAccessible(true);
// long artMethod = (long) artMethodField.get(method1);
//
// methods[dexMethodIndex] = artMethod;
//SandHook.init();
//initHook();
tv
.
setText
(
""
+
calArtSize
(
method1
,
method2
));
}
catch
(
NoSuchMethodException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchFieldException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalAccessException
e
)
{
e
.
printStackTrace
();
}
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
Log
.
e
(
"Thread_Local"
,
"thread_local_i = "
+
calArtSize
(
null
,
null
));
}
}).
start
();
}
}
...
...
@@ -124,14 +83,5 @@ public class MainActivity extends AppCompatActivity {
return
super
.
onOptionsItemSelected
(
item
);
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public
native
String
stringFromJNI
(
Method
method
);
public
native
int
calArtSize
(
Method
method1
,
Method
method2
);
public
native
void
initHook
();
}
app/src/main/java/com/swift/sandhook/SandHook.java
View file @
8c3f0c1d
package
com
.
swift
.
sandhook
;
import
android.os.Build
;
import
android.support.annotation.NonNull
;
import
android.support.annotation.Nullable
;
...
...
@@ -17,11 +18,15 @@ public class SandHook {
public
static
int
testAccessFlag
;
static
{
System
.
loadLibrary
(
"native-lib"
);
}
public
static
boolean
init
()
{
initTestOffset
();
SandHookMethodResolver
.
init
();
return
true
;
return
initNative
(
Build
.
VERSION
.
SDK_INT
)
;
}
public
static
void
hook
(
@NonNull
Method
target
,
@NonNull
Method
hook
,
@Nullable
Method
backup
)
{
...
...
@@ -97,5 +102,6 @@ public class SandHook {
throw
new
NoSuchFieldException
(
fieldName
);
}
private
static
native
boolean
initNative
(
int
sdk
);
}
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