通过 ByteBuddy 检测的 java 代理调用异常

Exception on invocation of java agent instrumented via ByteBuddy

我正在尝试重新创建 Will blog post 中描述的行为,但在尝试 运行 时遇到以下异常:

$ java -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar -jar example/target/securi 
tyfixer-example-1.0-SNAPSHOT.jar                                                                                                                           
Exception in thread "main" java.lang.NoClassDefFoundError: net/bytebuddy/implementation/Implementation$Context$Factory                                     
        at java.lang.Class.getDeclaredMethods0(Native Method)                                                                                             
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)                                                                                       
        at java.lang.Class.getDeclaredMethod(Unknown Source)                                                                                               
        at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)                                                                       
        at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)                                                                     
Caused by: java.lang.ClassNotFoundException: net.bytebuddy.implementation.Implementation$Context$Factory                                                   
        at java.net.URLClassLoader.findClass(Unknown Source)                                                                                               
        at java.lang.ClassLoader.loadClass(Unknown Source)                                                                                                 
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)                                                                                     
        at java.lang.ClassLoader.loadClass(Unknown Source)                                                                                                 
        ... 5 more                                                                                                                                         
FATAL ERROR in native method: processing of -javaagent failed 

structure 如 Will 的博客中所述 - 3 个独立的 jar,一个带有代理,一个带有拦截器,一个带有 Main class。

我还尝试 运行 通过将 mainClass 节添加到 securityfixer-example 的清单中,将其作为可执行 jar,但这似乎完全绕过了检测:

$ java -jar example/target/securityfixer-example-1.0-SNAPSHOT.jar -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar 
Security manager is set! 
ATTACK SUCCEEDED: Security manager was reset! 

我可能在这里遗漏了什么?提前致谢。

以下设置似乎有效:

byte-buddy-1.0.0.jar 必须与生成的 securityfixer-agent-1.0-SNAPSHOT.jar 一起在 java-agents-experiments\securityfixer\agent\target 内,因为后者依赖于前者。这是通过在 securityfixer-agent/pom.xml 中包含以下执行复制的插件来实现的:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                            <includeScope>runtime</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

以及 <Boot-Class-Path> 中对上述节生成的工件的以下引用:

        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Can-Redefine-Classes>true</Can-Redefine-Classes>
                        <Can-Retransform-Classes>true</Can-Retransform-Classes>
                        <Agent-Class>com.excelsiorsoft.securityfixer.agent.SecurityFixerAgent</Agent-Class>
                        <Premain-Class>com.excelsiorsoft.securityfixer.agent.SecurityFixerAgent</Premain-Class>
                        <Boot-Class-Path>byte-buddy-1.0.0.jar</Boot-Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>

所以不必要的依赖项(例如 securityfixer-bootstrap)不会被上述插件与 byte-buddy-1.0.0.jar 一起复制,我需要将它们的范围更改为 providedmaven-dependency-plugin 似乎跳过了将该范围的依赖项复制到其目标文件夹。

为了能够 运行 它作为可执行 jar,我们需要将 <mainClass> 节添加到 securityfixer-example/pom.xml:

        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>securityfixer.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

.

$ java -javaagent:agent/target/securityfixer-agent-1.0-SNAPSHOT.jar=bootstrap/target/securityfixer-bootstrap-1.0-SNAPSHOT.jar -jar example/target/securit yfixer-example-1.0-SNAPSHOT.jar


Security manager is set!    
ATTACK FAILED: SecurityManager cannot be reset!

随时发表评论 - 也许有更优雅的解决方案。谢谢!