Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5727d49

Browse filesBrowse files
committed
Create code
1 parent d13e537 commit 5727d49
Copy full SHA for 5727d49

File tree

Expand file treeCollapse file tree

17 files changed

+405
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

17 files changed

+405
-0
lines changed
Open diff view settings
Collapse file

‎6.JavaAgent/AgentMain/code/AgentMainTest/.idea/.gitignore‎

Copy file name to clipboardExpand all lines: 6.JavaAgent/AgentMain/code/AgentMainTest/.idea/.gitignore
+8Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎6.JavaAgent/AgentMain/code/AgentMainTest/.idea/compiler.xml‎

Copy file name to clipboardExpand all lines: 6.JavaAgent/AgentMain/code/AgentMainTest/.idea/compiler.xml
+13Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎6.JavaAgent/AgentMain/code/AgentMainTest/.idea/jarRepositories.xml‎

Copy file name to clipboardExpand all lines: 6.JavaAgent/AgentMain/code/AgentMainTest/.idea/jarRepositories.xml
+20Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎6.JavaAgent/AgentMain/code/AgentMainTest/.idea/libraries/testJar.xml‎

Copy file name to clipboardExpand all lines: 6.JavaAgent/AgentMain/code/AgentMainTest/.idea/libraries/testJar.xml
+9Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎6.JavaAgent/AgentMain/code/AgentMainTest/.idea/misc.xml‎

Copy file name to clipboardExpand all lines: 6.JavaAgent/AgentMain/code/AgentMainTest/.idea/misc.xml
+14Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎6.JavaAgent/AgentMain/code/AgentMainTest/.idea/uiDesigner.xml‎

Copy file name to clipboardExpand all lines: 6.JavaAgent/AgentMain/code/AgentMainTest/.idea/uiDesigner.xml
+124Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>AgentMainTest</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<maven.compiler.source>8</maven.compiler.source>
14+
<maven.compiler.target>8</maven.compiler.target>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.sunn</groupId>
19+
<artifactId>tools</artifactId>
20+
<version>1.8.0</version>
21+
<scope>system</scope>
22+
<systemPath>D:/JDKV/jdk8u301/lib/tools.jar</systemPath>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.javassist</groupId>
26+
<artifactId>javassist</artifactId>
27+
<version>3.21.0-GA</version>
28+
</dependency>
29+
30+
31+
</dependencies>
32+
33+
<build>
34+
35+
<pluginManagement>
36+
<plugins>
37+
<plugin>
38+
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-jar-plugin</artifactId>
41+
<version>2.2</version>
42+
<configuration>
43+
<archive>
44+
<manifestEntries>
45+
<!--改这个为代理类-->
46+
<Agent-Class>AgentMain</Agent-Class>
47+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
48+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
49+
</manifestEntries>
50+
</archive>
51+
<skip>true</skip>
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</pluginManagement>
56+
</build>
57+
58+
</project>
Collapse file
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.lang.instrument.ClassDefinition;
2+
import java.lang.instrument.Instrumentation;
3+
import java.lang.instrument.UnmodifiableClassException;
4+
5+
public class AgentMain {
6+
public static void agentmain(String agentArgs, Instrumentation inst) throws ClassNotFoundException, UnmodifiableClassException, InterruptedException {
7+
inst.addTransformer(new Transformer (), true);
8+
Class[] loadedClass = inst.getAllLoadedClasses();
9+
for (Class clazz : loadedClass){
10+
String className = clazz.getName();
11+
if (inst.isModifiableClass(clazz)){
12+
if (className.equals("TransClass")){
13+
try {
14+
inst.retransformClasses(clazz);
15+
} catch (UnmodifiableClassException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
}
21+
}
22+
}
23+
}
Collapse file
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import com.sun.tools.attach.VirtualMachine;
2+
import com.sun.tools.attach.VirtualMachineDescriptor;
3+
4+
import java.util.List;
5+
6+
public class AttachTest {
7+
// 一个运行 Attach API 的线程子类
8+
// 每隔半秒时间检查一次所有的 Java 虚拟机
9+
static class AttachThread extends Thread {
10+
11+
private final List<VirtualMachineDescriptor> listBefore;
12+
13+
private final String jar;
14+
15+
AttachThread(String attachJar, List<VirtualMachineDescriptor> vms) {
16+
listBefore = vms; // 记录程序启动时的 VM 集合
17+
jar = attachJar;
18+
}
19+
20+
public void run() {
21+
VirtualMachine vm = null;
22+
List<VirtualMachineDescriptor> listAfter = null;
23+
try {
24+
int count = 0;
25+
while (true) {
26+
listAfter = VirtualMachine.list();
27+
for (VirtualMachineDescriptor vmd : listAfter) {
28+
29+
if (vmd.displayName().equals("Test")) {
30+
System.out.println("进程ID:" + vmd.id() + ",进程名称:" + vmd.displayName());
31+
System.out.println("捕捉到Test进程,准备Hook");
32+
vm = VirtualMachine.attach(vmd.id());
33+
break;
34+
}
35+
}
36+
Thread.sleep(500);
37+
count++;
38+
if (null != vm || count >= 10) {
39+
break;
40+
}
41+
}
42+
vm.loadAgent(jar);
43+
vm.detach();
44+
} catch (Exception e) {
45+
46+
}
47+
}
48+
}
49+
public static void main(String[] args) throws InterruptedException {
50+
new AttachThread("E:\\AgentMainTest\\target\\AgentMainTest-1.0-SNAPSHOT.jar", VirtualMachine.list()).start();
51+
}
52+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 程序每隔半秒钟输出一次返回值(显示出返回值从 1 变成 2)
2+
public class Test {
3+
public static void main(String[] args) throws InterruptedException {
4+
System.out.println(new TransClass().getNumber());
5+
int count = 0;
6+
while (true) {
7+
Thread.sleep(500);
8+
count++;
9+
int number = new TransClass().getNumber();
10+
System.out.println(number);
11+
if (3 == number || count >= 10) {
12+
break;
13+
}
14+
}
15+
}
16+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.