01
Java Agent 简介
Manifest-Version: 1.0 # 用来定义manifest文件的版本
Archiver-Version: Plexus Archiver # 详见 http://codehaus-plexus.github.io/plexus-archiver/
Built-By: penson # 构建者
Created-By: Apache Maven 3.5.0 # # 声明该文件的生成者,一般该属性是由 jar 命令行工具生成的
Build-Jdk: 1.8.0_162 # 基于构建的 JDK 版本
02
premain方法
import java.lang.instrument.Instrumentation;
public class Demo {
public static void premain(String agentArgs, Instrumentation inst) throws Exception{
System.out.println(agentArgs);
for(int i=0;i<5;i++){
System.out.println("premain 方法被调用");
}
}
}
Manifest-Version: 1.0
Premain-Class: Demo
jar cvfm agent.jar agent.mf Demo.class
public class Test {
public static void main(String[] args) {
System.out.println("this is test main");
}
}
Manifest-Version: 1.0
Main-Class: Test
jar cvfm Test.jar agent.mf Test.class
java -javaagent:agent.jar[=options] -jar Test.jar
agent.jar=penson -jar Test.jar :
Instrumentation
import java.lang.instrument.Instrumentation;
import DefineTransformer;
public class Demo2 {
public static void premain(String agentArgs, Instrumentation inst) throws Exception{
System.out.println(agentArgs);
for(int i=0;i<5;i++){
System.out.println("premain 方法被调用了");
}
// 注册 DefineTransformer
inst.addTransformer(new DefineTransformer(),true);
}
}
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
// 每当类被加载,就会调用 transform 函数
public class DefineTransformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
System.out.println(className);
return new byte[0];
}
}
Manifest-Version: 1.0
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Premain-Class: Demo2
jar cvfm agent.jar agent.mf Demo2.class DefineTransformer.class
java -javaagent:agent.jar=penson -jar Test.jar
03
agentmain 方法
-
必须要实现 agentmain 方法。
VirtualMachine
VirtualMachineDescriptor
import java.lang.instrument.Instrumentation;
public class Demo3 {
public static void agentmain(String agentArgs, Instrumentation ins) {
ins.addTransformer(new DefineTransformer(),true);
}
}
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
// 每当类被加载,就会调用 transform 函数
public class DefineTransformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
System.out.println(className);
return classfileBuffer;
}
}
Manifest-Version: 1.0
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Agent-Class: Demo3
jar cvfm agent.jar agent.mf Demo2.class DefineTransformer.class
package com.test.agentlearn_agentmain;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import java.util.List;
public class TestDemo {
public static void main(String[] args) throws Exception{
String path = "/Users/penson/Desktop/网安/网络安全学习/java安全/java-agent内存马/src/main/java/com/test/agentlearn_agentmain/agent.jar";
Listlist = VirtualMachine.list();
for (VirtualMachineDescriptor v:list){
System.out.println(v.displayName());
if (v.displayName().contains("TestDemo")){
// 将 jvm 虚拟机的 pid 号传入 attach 来进行远程连接
System.out.println(v.id());
VirtualMachine vm = VirtualMachine.attach(v.id());
// 将我们的 agent.jar 发送给
vm.loadAgent(path);
vm.detach();
}
}
}
}
— 往期回顾 —
原文始发于微信公众号(安恒信息安全服务):九维团队-红队(突破)| 关于文件落地型java-agent内存马探讨(一)