forked from Y4tacker/JavaSec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyAgent.java
More file actions
49 lines (44 loc) · 1.81 KB
/
MyAgent.java
File metadata and controls
49 lines (44 loc) · 1.81 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
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;
import java.util.List;
public class MyAgent {
public static void agentmain(String agentArgs, Instrumentation inst){
inst.addTransformer(new MyTransformer("E:\\LicenceCrack\\target\\classes\\CrackLicenseTest.class"),true);
Class[] loadedClass = inst.getAllLoadedClasses();
for (Class clazz : loadedClass){
String className = clazz.getName();
if (inst.isModifiableClass(clazz)){
if (className.equals("CrackLicenseTest")){
try {
inst.retransformClasses(clazz);
} catch (UnmodifiableClassException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws Exception {
int flag = 0;
while (true) {
List<VirtualMachineDescriptor> list = VirtualMachine.list();
for (VirtualMachineDescriptor desc : list) {
if ("CrackLicenseTest".equals(desc.displayName())) {
System.out.println("获取到注入进程ID:" + desc.id() + ",进程名称:" + desc.displayName());
VirtualMachine vm = VirtualMachine.attach(desc.id());
String agentPath = "E:\\LicenceCrack\\target\\LicenceCrack-1.0-SNAPSHOT.jar";
// 注入Agent到目标JVM
vm.loadAgent(agentPath);
vm.detach();
flag = 1;
break;
}
}
if (flag==1){
break;
}
}
}
}