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
5bd6ce01
Commit
5bd6ce01
authored
Feb 03, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reconst
parent
a482565f
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
196 additions
and
101 deletions
+196
-101
CMakeLists.txt
hooklib/CMakeLists.txt
+1
-0
art_method.cpp
hooklib/src/main/cpp/art/art_method.cpp
+84
-8
art_method.h
hooklib/src/main/cpp/includes/art_method.h
+1
-1
cast.h
hooklib/src/main/cpp/includes/cast.h
+1
-1
hide_api.h
hooklib/src/main/cpp/includes/hide_api.h
+19
-0
utils.h
hooklib/src/main/cpp/includes/utils.h
+3
-3
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+1
-1
hide_api.cpp
hooklib/src/main/cpp/utils/hide_api.cpp
+36
-0
hide_api.h
hooklib/src/main/cpp/utils/hide_api.h
+0
-40
offset.cpp
hooklib/src/main/cpp/utils/offset.cpp
+3
-3
utils.cpp
hooklib/src/main/cpp/utils/utils.cpp
+47
-44
No files found.
hooklib/CMakeLists.txt
View file @
5bd6ce01
...
...
@@ -21,6 +21,7 @@ add_library( # Sets the name of the library.
src/main/cpp/trampoline/trampoline.cpp
src/main/cpp/trampoline/trampoline_manager.cpp
src/main/cpp/utils/fake_dlfcn.cpp
src/main/cpp/utils/hide_api.cpp
src/main/cpp/utils/utils.cpp
src/main/cpp/utils/offset.cpp
src/main/cpp/casts/cast_art_method.cpp
...
...
hooklib/src/main/cpp/art/art_method.cpp
View file @
5bd6ce01
...
...
@@ -4,37 +4,113 @@
#include <cstdint>
#include "../includes/art_method.h"
#include "../includes/cast_art_method.h"
#include "../includes/hide_api.h"
using
namespace
art
::
mirror
;
using
namespace
SandHook
;
void
ArtMethod
::
setStatic
()
{
};
void
ArtMethod
::
tryDisableInline
()
{
if
(
SDK_INT
<
ANDROID_O
)
return
;
uint32_t
accessFlag
=
getAccessFlags
();
accessFlag
&=
~
0x08000000
;
setAccessFlags
(
accessFlag
);
}
void
ArtMethod
::
disableInterpreterForO
()
{
uint32_t
accessFlag
=
getAccessFlags
();
accessFlag
|=
0x0100
;
setAccessFlags
(
accessFlag
);
}
void
ArtMethod
::
disableCompilable
()
{
uint32_t
accessFlag
=
getAccessFlags
();
if
(
SDK_INT
>=
ANDROID_O2
)
{
accessFlag
|=
0x02000000
;
accessFlag
|=
0x00800000
;
}
else
{
accessFlag
|=
0x01000000
;
}
setAccessFlags
(
accessFlag
);
}
bool
ArtMethod
::
isAbstract
()
{
uint32_t
accessFlags
=
getAccessFlags
();
return
((
accessFlags
&
0x0400
)
!=
0
);
}
bool
ArtMethod
::
isNative
()
{
uint32_t
accessFlags
=
getAccessFlags
();
return
((
accessFlags
&
0x0100
)
!=
0
);
}
bool
ArtMethod
::
isCompiled
()
{
return
getQuickCodeEntry
()
==
SandHook
::
CastArtMethod
::
quickToInterpreterBridge
;
}
void
ArtMethod
::
setAccessFlags
(
uint32_t
flags
)
{
CastArtMethod
::
accessFlag
->
set
(
this
,
flags
);
}
void
ArtMethod
::
setPrivate
()
{
uint32_t
accessFlag
=
getAccessFlags
();
accessFlag
&=
~
0x1
;
accessFlag
|=
0x2
;
setAccessFlags
(
accessFlag
);
}
void
ArtMethod
::
setStatic
()
{
uint32_t
accessFlag
=
getAccessFlags
();
accessFlag
|=
0x0008
;
setAccessFlags
(
accessFlag
);
};
uint32_t
ArtMethod
::
getAccessFlags
()
{
return
CastArtMethod
::
accessFlag
->
get
(
this
);
}
uint32_t
ArtMethod
::
getDexMethodIndex
()
{
return
CastArtMethod
::
dexMethodIndex
->
get
(
this
);
}
void
*
ArtMethod
::
getQuickCodeEntry
()
{
return
CastArtMethod
::
entryPointQuickCompiled
->
get
(
this
);
}
void
*
ArtMethod
::
getInterpreterCodeEntry
()
{
return
CastArtMethod
::
entryPointFormInterpreter
->
get
(
this
);
}
void
ArtMethod
::
setQuickCodeEntry
(
void
*
entry
)
{
CastArtMethod
::
entryPointQuickCompiled
->
set
(
this
,
entry
);
}
void
ArtMethod
::
setJniCodeEntry
(
void
*
entry
)
{
}
void
ArtMethod
::
setInterpreterCodeEntry
(
void
*
entry
)
{
CastArtMethod
::
entryPointFormInterpreter
->
set
(
this
,
entry
);
}
void
ArtMethod
::
setDexCacheResolveItem
(
uint32_t
index
,
void
*
item
)
{
CastArtMethod
::
dexCacheResolvedMethods
->
setElement
(
this
,
index
,
item
);
}
bool
ArtMethod
::
compile
()
{
// if (isCompiled())
// return true;
// Size threadId = getAddressFromJavaByCallMethod(env, "com/swift/sandhook/SandHook", "getThreadId");
// if (threadId == 0)
// return false;
// return compileMethod(this, ) && isCompiled();
}
void
ArtMethod
::
flushCache
()
{
}
void
ArtMethod
::
backup
(
ArtMethod
*
backup
)
{
}
\ No newline at end of file
hooklib/src/main/cpp/includes/art_method.h
View file @
5bd6ce01
...
...
@@ -57,7 +57,7 @@ public:
void
setQuickCodeEntry
(
void
*
entry
);
void
setJniCodeEntry
(
void
*
entry
);
void
setInterpreterCodeEntry
(
void
*
entry
);
void
setDexCacheResolveItem
(
ArtMethod
*
method
,
uint32_t
index
);
void
setDexCacheResolveItem
(
uint32_t
index
,
void
*
item
);
void
*
getQuickCodeEntry
();
void
*
getInterpreterCodeEntry
();
...
...
hooklib/src/main/cpp/includes/cast.h
View file @
5bd6ce01
...
...
@@ -62,7 +62,7 @@ namespace SandHook {
template
<
typename
T
>
int
findOffset
(
void
*
start
,
size_t
len
,
size_t
step
,
T
value
)
{
if
(
NULL
==
start
)
{
if
(
nullptr
==
start
)
{
return
-
1
;
}
...
...
hooklib/src/main/cpp/includes/hide_api.h
0 → 100644
View file @
5bd6ce01
//
// Created by swift on 2019/1/21.
//
#ifndef SANDHOOK_HIDE_API_H
#define SANDHOOK_HIDE_API_H
#include <jni.h>
#include "../utils/fake_dlfcn.h"
#include "dlfcn.h"
extern
"C"
{
void
initHideApi
(
JNIEnv
*
env
,
int
SDK_VERSION
);
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
);
}
#endif //SANDHOOK_HIDE_API_H
hooklib/src/main/cpp/includes/utils.h
View file @
5bd6ce01
...
...
@@ -13,11 +13,11 @@
extern
"C"
{
Size
getAddressFromJava
(
JNIEnv
*
env
,
c
har
*
className
,
char
*
fieldName
);
Size
getAddressFromJava
(
JNIEnv
*
env
,
c
onst
char
*
className
,
const
char
*
fieldName
);
Size
getAddressFromJavaByCallMethod
(
JNIEnv
*
env
,
c
har
*
className
,
char
*
methodName
);
Size
getAddressFromJavaByCallMethod
(
JNIEnv
*
env
,
c
onst
char
*
className
,
const
char
*
methodName
);
jint
getIntFromJava
(
JNIEnv
*
env
,
c
har
*
className
,
char
*
fieldName
);
jint
getIntFromJava
(
JNIEnv
*
env
,
c
onst
char
*
className
,
const
char
*
fieldName
);
bool
munprotect
(
size_t
addr
,
size_t
len
);
...
...
hooklib/src/main/cpp/sandhook.cpp
View file @
5bd6ce01
#include <jni.h>
#include "includes/cast_art_method.h"
#include "includes/trampoline_manager.h"
#include "
./util
s/hide_api.h"
#include "
include
s/hide_api.h"
SandHook
::
TrampolineManager
trampolineManager
;
...
...
hooklib/src/main/cpp/utils/hide_api.cpp
0 → 100644
View file @
5bd6ce01
//
// Created by swift on 2019/1/21.
//
#include "../includes/hide_api.h"
extern
"C"
{
void
*
(
*
jitLoad
)(
bool
*
)
=
nullptr
;
void
*
jitCompilerHandle
=
nullptr
;
bool
(
*
jitCompileMethod
)(
void
*
,
void
*
,
void
*
,
bool
)
=
nullptr
;
void
initHideApi
(
JNIEnv
*
env
,
int
SDK_VERSION
)
{
if
(
SDK_VERSION
>=
24
)
{
void
*
jit_lib
;
if
(
sizeof
(
void
*
)
==
8
)
{
jit_lib
=
fake_dlopen
(
"/system/lib64/libart-compiler.so"
,
RTLD_NOW
);
}
else
{
jit_lib
=
fake_dlopen
(
"/system/lib/libart-compiler.so"
,
RTLD_NOW
);
}
jitCompileMethod
=
(
bool
(
*
)(
void
*
,
void
*
,
void
*
,
bool
))
fake_dlsym
(
jit_lib
,
"jit_compile_method"
);
jitLoad
=
reinterpret_cast
<
void
*
(
*
)(
bool
*
)
>
(
fake_dlsym
(
jit_lib
,
"jit_load"
));
bool
generate_debug_info
=
false
;
jitCompilerHandle
=
(
jitLoad
)(
&
generate_debug_info
);
}
}
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
)
{
if
(
jitCompileMethod
==
nullptr
)
{
return
false
;
}
return
jitCompileMethod
(
jitCompilerHandle
,
artMethod
,
thread
,
false
);
}
}
hooklib/src/main/cpp/utils/hide_api.h
deleted
100644 → 0
View file @
a482565f
//
// Created by swift on 2019/1/21.
//
#ifndef SANDHOOK_HIDE_API_H
#define SANDHOOK_HIDE_API_H
#include <jni.h>
#include "fake_dlfcn.h"
#include "dlfcn.h"
void
*
(
*
jitLoad
)(
bool
*
)
=
nullptr
;
void
*
jitCompilerHandle
=
nullptr
;
bool
(
*
jitCompileMethod
)(
void
*
,
void
*
,
void
*
,
bool
)
=
nullptr
;
void
initHideApi
(
JNIEnv
*
env
,
int
SDK_VERSION
)
{
if
(
SDK_VERSION
>=
24
)
{
void
*
jit_lib
;
if
(
sizeof
(
void
*
)
==
8
)
{
jit_lib
=
fake_dlopen
(
"/system/lib64/libart-compiler.so"
,
RTLD_NOW
);
}
else
{
jit_lib
=
fake_dlopen
(
"/system/lib/libart-compiler.so"
,
RTLD_NOW
);
}
jitCompileMethod
=
(
bool
(
*
)(
void
*
,
void
*
,
void
*
,
bool
))
fake_dlsym
(
jit_lib
,
"jit_compile_method"
);
jitLoad
=
reinterpret_cast
<
void
*
(
*
)(
bool
*
)
>
(
fake_dlsym
(
jit_lib
,
"jit_load"
));
bool
generate_debug_info
=
false
;
jitCompilerHandle
=
(
jitLoad
)(
&
generate_debug_info
);
}
}
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
)
{
if
(
jitCompileMethod
==
nullptr
)
{
return
false
;
}
return
jitCompileMethod
(
jitCompilerHandle
,
artMethod
,
thread
,
false
);
}
#endif //SANDHOOK_HIDE_API_H
hooklib/src/main/cpp/utils/offset.cpp
View file @
5bd6ce01
...
...
@@ -9,7 +9,7 @@ namespace SandHook {
template
<
typename
T
>
int
Offset
::
findOffset
(
void
*
start
,
size_t
len
,
size_t
step
,
T
value
)
{
if
(
NULL
==
start
)
{
if
(
nullptr
==
start
)
{
return
-
1
;
}
...
...
@@ -25,7 +25,7 @@ namespace SandHook {
template
<
typename
T
>
int
Offset
::
findOffsetWithCB1
(
void
*
start
,
size_t
len
,
size_t
step
,
bool
func
(
int
,
T
))
{
if
(
NULL
==
start
)
{
if
(
nullptr
==
start
)
{
return
-
1
;
}
...
...
@@ -41,7 +41,7 @@ namespace SandHook {
template
<
typename
T
>
int
Offset
::
findOffsetWithCB2
(
void
*
start1
,
void
*
start2
,
size_t
len
,
size_t
step
,
bool
func
(
T
,
T
))
{
if
(
NULL
==
start1
||
NULL
==
start2
)
{
if
(
nullptr
==
start1
||
nullptr
==
start2
)
{
return
-
1
;
}
...
...
hooklib/src/main/cpp/utils/utils.cpp
View file @
5bd6ce01
...
...
@@ -4,57 +4,60 @@
#include "../includes/utils.h"
extern
"C"
{
Size
getAddressFromJava
(
JNIEnv
*
env
,
char
*
className
,
char
*
fieldName
)
{
jclass
clazz
=
env
->
FindClass
(
className
);
if
(
clazz
==
NULL
)
{
printf
(
"find class error !"
);
return
0
;
Size
getAddressFromJava
(
JNIEnv
*
env
,
const
char
*
className
,
const
char
*
fieldName
)
{
jclass
clazz
=
env
->
FindClass
(
className
);
if
(
clazz
==
NULL
)
{
printf
(
"find class error !"
);
return
0
;
}
jfieldID
id
=
env
->
GetStaticFieldID
(
clazz
,
fieldName
,
"J"
);
if
(
id
==
NULL
)
{
printf
(
"find field error !"
);
return
0
;
}
return
env
->
GetStaticLongField
(
clazz
,
id
);
}
jfieldID
id
=
env
->
GetStaticFieldID
(
clazz
,
fieldName
,
"J"
);
if
(
id
==
NULL
)
{
printf
(
"find field error !"
);
return
0
;
}
return
env
->
GetStaticLongField
(
clazz
,
id
);
}
Size
getAddressFromJavaByCallMethod
(
JNIEnv
*
env
,
char
*
className
,
char
*
methodName
)
{
jclass
clazz
=
env
->
FindClass
(
className
);
if
(
clazz
==
NULL
)
{
printf
(
"find class error !"
);
return
0
;
Size
getAddressFromJavaByCallMethod
(
JNIEnv
*
env
,
const
char
*
className
,
const
char
*
methodName
)
{
jclass
clazz
=
env
->
FindClass
(
className
);
if
(
clazz
==
NULL
)
{
printf
(
"find class error !"
);
return
0
;
}
jmethodID
id
=
env
->
GetStaticMethodID
(
clazz
,
methodName
,
"()J"
);
if
(
id
==
NULL
)
{
printf
(
"find field error !"
);
return
0
;
}
return
env
->
CallStaticLongMethodA
(
clazz
,
id
,
nullptr
);
}
jmethodID
id
=
env
->
GetStaticMethodID
(
clazz
,
methodName
,
"()J"
);
if
(
id
==
NULL
)
{
printf
(
"find field error !"
);
return
0
;
}
return
env
->
CallStaticLongMethodA
(
clazz
,
id
,
nullptr
);
}
jint
getIntFromJava
(
JNIEnv
*
env
,
char
*
className
,
char
*
fieldName
)
{
jclass
clazz
=
env
->
FindClass
(
className
);
if
(
clazz
==
NULL
)
{
printf
(
"find class error !"
);
return
0
;
jint
getIntFromJava
(
JNIEnv
*
env
,
const
char
*
className
,
const
char
*
fieldName
)
{
jclass
clazz
=
env
->
FindClass
(
className
);
if
(
clazz
==
NULL
)
{
printf
(
"find class error !"
);
return
0
;
}
jfieldID
id
=
env
->
GetStaticFieldID
(
clazz
,
fieldName
,
"I"
);
if
(
id
==
NULL
)
{
printf
(
"find field error !"
);
return
0
;
}
return
env
->
GetStaticIntField
(
clazz
,
id
);
}
jfieldID
id
=
env
->
GetStaticFieldID
(
clazz
,
fieldName
,
"I"
);
if
(
id
==
NULL
)
{
printf
(
"find field error !"
);
return
0
;
}
return
env
->
GetStaticIntField
(
clazz
,
id
);
}
bool
munprotect
(
size_t
addr
,
size_t
len
)
{
long
pagesize
=
sysconf
(
_SC_PAGESIZE
);
unsigned
alignment
=
(
unsigned
)
((
unsigned
long
long
)
addr
%
pagesize
);
int
i
=
mprotect
((
void
*
)
(
addr
-
alignment
),
(
size_t
)
(
alignment
+
len
),
PROT_READ
|
PROT_WRITE
|
PROT_EXEC
);
if
(
i
==
-
1
)
{
return
false
;
bool
munprotect
(
size_t
addr
,
size_t
len
)
{
long
pagesize
=
sysconf
(
_SC_PAGESIZE
);
unsigned
alignment
=
(
unsigned
)
((
unsigned
long
long
)
addr
%
pagesize
);
int
i
=
mprotect
((
void
*
)
(
addr
-
alignment
),
(
size_t
)
(
alignment
+
len
),
PROT_READ
|
PROT_WRITE
|
PROT_EXEC
);
if
(
i
==
-
1
)
{
return
false
;
}
return
true
;
}
return
true
;
}
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