使用Agent破解Java应用更好机器无法启动

公司之前由于各方面原因,在一个项目上没有采用自研,而是外购系统,且由于费用问题没有采购源码,无奈由于客户需求不确定,以及定制化需求多,后续系统方基本不支撑了。正好其他项目交付了,因此公司立项准备自研开发一套系统。开发系统免不了要参考之前采购的这套系统,因此从客户现场将这套系统部署包拷贝回来,准备在公司测试环境部署一套。部署才发现系统和机器码绑定了,换了电脑部署应用启动不了,好在项目也Java开发,这种情况下直接反编译看看。

image.png

应用启动类调用StartValidation#isOk然后调用validateByMacV2方法。

到这就比较好处理了,编写一个Agent,使用ByteBuddy字节码操作库,在JVM启动时动态修改目标类的字节码,拦截isOk方法的执行,直接返回true,从而跳过原有的验证逻辑即可。

package com.zrkj.agent;  

import net.bytebuddy.agent.builder.AgentBuilder;  
import net.bytebuddy.implementation.MethodDelegation;  
import net.bytebuddy.implementation.bind.annotation.RuntimeType;  
import net.bytebuddy.implementation.bind.annotation.AllArguments;  
import net.bytebuddy.matcher.ElementMatchers;  
import net.bytebuddy.description.type.TypeDescription;  
import net.bytebuddy.dynamic.DynamicType;  
import net.bytebuddy.utility.JavaModule;  

import java.lang.instrument.Instrumentation;  
import java.security.ProtectionDomain;  

/**  
 * @author tcxurun  
 * @version 1.0  
 * @description  
 * @date 2025/8/12  
 */public class ValidationAgent {  
    // JVM启动时加载Agent的入口方法  
    public static void premain(String agentArgs, Instrumentation inst) {  
        System.out.println("=== ValidationAgent 开始加载 ===");  
        System.out.println("Agent参数: " + agentArgs);  
        System.out.println("Instrumentation: " + inst);  

        // 构建Agent,拦截目标类和方法  
        new AgentBuilder.Default()  
                .with(new AgentBuilder.Listener.Adapter() {  
                    @Override  
                    public void onTransformation(TypeDescription typeDescription,   
                                               ClassLoader classLoader,   
                                               JavaModule module,   
                                               boolean loaded,   
                                               DynamicType dynamicType) {  
                        System.out.println("=== 拦截到类: " + typeDescription.getName() + " ===");  
                        System.out.println("ClassLoader: " + classLoader);  
                        System.out.println("已加载: " + loaded);  
                    }  

                    @Override  
                    public void onError(String typeName,   
                                      ClassLoader classLoader,   
                                      JavaModule module,   
                                      boolean loaded,   
                                      Throwable throwable) {  
                        System.out.println("=== 拦截错误: " + typeName + " ===");  
                        throwable.printStackTrace();  
                    }  
                })  
                // 匹配多个不同包名下的StartValidation类  
                .type(ElementMatchers.nameEndsWith(".StartValidation"))  
                // 匹配目标方法:isOk(String)  
                .transform(new AgentBuilder.Transformer() {  
                    @Override  
                    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,   
                                                           TypeDescription typeDescription,   
                                                           ClassLoader classLoader,   
                                                           JavaModule module,   
                                                           ProtectionDomain protectionDomain) {  
                        System.out.println("=== 开始转换类: " + typeDescription.getName() + " ===");  
                        return builder.method(ElementMatchers.named("isOk")  
                                        .and(ElementMatchers.takesArguments(String.class)))  
                                // 使用MethodDelegation修改方法实现  
                                .intercept(MethodDelegation.to(ValidationInterceptor.class));  
                    }  
                })  
                .installOn(inst);  

        System.out.println("=== ValidationAgent 加载完成 ===");  
    }  

    // 用于拦截方法的类  
    public static class ValidationInterceptor {  

        @RuntimeType  
        public static boolean intercept(@AllArguments Object[] args) {  
            System.out.println("=== Agent拦截到isOk方法 ===");  
            if (args != null && args.length > 0) {  
                System.out.println("=== 方法参数: " + args[0] + " ===");  
            }  
            System.out.println("=== 直接返回true,跳过原始验证逻辑 ===");  
            // 直接返回true,跳过原方法执行  
            return true;  
        }  
    }  
}

使用方法

在启动目标应用程序时,添加-javaagent参数:

java -javaagent:target/ValidationSkippingAgent-1.0-SNAPSHOT-jar-with-dependencies.jar -jar your-application.jar

或者:

java -javaagent:target/ValidationSkippingAgent-1.0-SNAPSHOT-jar-with-dependencies.jar -cp your-classpath com.your.MainClass
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇