gpt4 book ai didi

java - 调用通过 ByteBuddy 检测的 java 代理时出现异常

转载 作者:行者123 更新时间:2023-11-30 07:01:42 24 4
gpt4 key购买 nike

我正在尝试重新创建 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 类。

我还尝试通过将 mainClass 节添加到 securityfixer-example 的 list 中来将其作为可执行 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必须在 java-agents-experiments\securityfixer\agent\target 内以及生成的 securityfixer-agent-1.0-SNAPSHOT.jar因为后者取决于前者。这是通过在 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!

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

关于java - 调用通过 ByteBuddy 检测的 java 代理时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40795399/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com