-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathJdkSecurityBypass.java
More file actions
159 lines (136 loc) · 7.42 KB
/
Copy pathJdkSecurityBypass.java
File metadata and controls
159 lines (136 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sun.misc.Unsafe;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
public class JdkSecurityBypass{
private static Unsafe getUnsafe() {
Unsafe unsafe = null;
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
} catch (Exception e) {
throw new AssertionError(e);
}
return unsafe;
}
private static Method getMethod(Class clazz,String methodName,Class[] params) {
Method method = null;
while (clazz!=null){
try {
method = clazz.getDeclaredMethod(methodName,params);
break;
}catch (NoSuchMethodException e){
clazz = clazz.getSuperclass();
}
}
return method;
}
public static byte[] readInputStream(InputStream inputStream) {
byte[] temp = new byte[4096];
int readOneNum = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
while ((readOneNum = inputStream.read(temp)) != -1) {
bos.write(temp, 0, readOneNum);
}
inputStream.close();
}catch (Exception e){
}
return bos.toByteArray();
}
public void bypassModule(){
try {
Unsafe unsafe = getUnsafe();
Class InstrumentationImplClass = Class.forName("sun.instrument.InstrumentationImpl");
Class currentClass = this.getClass();
Constructor InstrumentationImplConstructor = null;
try {
InstrumentationImplConstructor = InstrumentationImplClass.getDeclaredConstructor(new Class[]{long.class,boolean.class,boolean.class});
InstrumentationImplConstructor.setAccessible(true);
Instrumentation instrumentationInstance= (Instrumentation) InstrumentationImplConstructor.newInstance(0,true,true);
System.out.println(String.format("<JDK16 无需bypass instrumentationInstance:%s", instrumentationInstance));
}catch (Exception e) {
Method getModuleMethod = getMethod(Class.class, "getModule", new Class[0]);
if (getModuleMethod != null) {
Object oldModule = getModuleMethod.invoke(currentClass, new Object[]{});
Object targetModule = getModuleMethod.invoke(InstrumentationImplClass, new Object[]{});
unsafe.getAndSetObject(currentClass, unsafe.objectFieldOffset(Class.class.getDeclaredField("module")), targetModule);
InstrumentationImplConstructor = InstrumentationImplClass.getDeclaredConstructor(new Class[]{long.class,boolean.class,boolean.class});
InstrumentationImplConstructor.setAccessible(true);
Instrumentation instrumentationInstance= (Instrumentation) InstrumentationImplConstructor.newInstance(0,true,true);
if (instrumentationInstance != null) {
System.out.println(String.format("Bypass Jdk Security Module instrumentationInstance:%s Successfully!", instrumentationInstance));
unsafe.getAndSetObject(currentClass, unsafe.objectFieldOffset(Class.class.getDeclaredField("module")), oldModule);
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
private void removeClassCache(Unsafe unsafe,Class clazz){
try {
Class ClassAnonymousClass = unsafe.defineAnonymousClass(clazz,readInputStream(Class.class.getResourceAsStream("Class.class")),null);
Field reflectionDataField = ClassAnonymousClass.getDeclaredField("reflectionData");
unsafe.putObject(clazz,unsafe.objectFieldOffset(reflectionDataField),null);
}catch (Exception e){
//e.printStackTrace();
}
}
public void bypassReflectionFilter(){
try {
Unsafe unsafe = getUnsafe();
Class classClass = Class.class;
try {
System.out.println(String.format("没有Reflection Filter ClassLoader :%s", classClass.getDeclaredField("classLoader")));
}catch (Exception e){
try {
Class reflectionClass=Class.forName("jdk.internal.reflect.Reflection");
byte[] classBuffer = readInputStream(reflectionClass.getResourceAsStream("Reflection.class"));
Class reflectionAnonymousClass = unsafe.defineAnonymousClass(reflectionClass,classBuffer,null);
Field fieldFilterMapField=reflectionAnonymousClass.getDeclaredField("fieldFilterMap");
Field methodFilterMapField=reflectionAnonymousClass.getDeclaredField("methodFilterMap");
if(fieldFilterMapField.getType().isAssignableFrom(HashMap.class)){
unsafe.putObject(reflectionClass,unsafe.staticFieldOffset(fieldFilterMapField),new HashMap());
}
if(methodFilterMapField.getType().isAssignableFrom(HashMap.class)){
unsafe.putObject(reflectionClass,unsafe.staticFieldOffset(methodFilterMapField),new HashMap());
}
removeClassCache(unsafe,classClass);
System.out.println(String.format("Bypass Jdk Reflection Filter Successfully! ClassLoader :%s", classClass.getDeclaredField("classLoader")));
}catch (ClassNotFoundException e2){
try {
Class reflectionClass=Class.forName("sun.reflect.Reflection");
byte[] classBuffer = readInputStream(reflectionClass.getResourceAsStream("Reflection.class"));
Class reflectionAnonymousClass = unsafe.defineAnonymousClass(reflectionClass,classBuffer,null);
Field fieldFilterMapField=reflectionAnonymousClass.getDeclaredField("fieldFilterMap");
Field methodFilterMapField=reflectionAnonymousClass.getDeclaredField("methodFilterMap");
if(fieldFilterMapField.getType().isAssignableFrom(HashMap.class)){
unsafe.putObject(reflectionClass,unsafe.staticFieldOffset(fieldFilterMapField),new HashMap());
}
if(methodFilterMapField.getType().isAssignableFrom(HashMap.class)){
unsafe.putObject(reflectionClass,unsafe.staticFieldOffset(methodFilterMapField),new HashMap());
}
removeClassCache(unsafe,classClass);
System.out.println(String.format("Bypass Jdk Reflection Filter Successfully! ClassLoader :%s", classClass.getDeclaredField("classLoader")));
}catch (Exception e3){
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
//绕过Java 反射过滤获取ClassLoader私有字段
new JdkSecurityBypass().bypassReflectionFilter();
//绕过Jdk Module访问限制 可以访问任意类(即使是未声明导出) 任意私有方法
new JdkSecurityBypass().bypassModule();
}
}