java - 尝试使用 Java 代理运行 WebGoat 时崩溃

标签 java maven spring-boot javaagents

我目前正在学习如何使用用于监控的 Java 代理启动 Web 应用程序。

我选择的 Web 应用程序是 WebGoat ,并按照 WebGoat 的自述文件中所述,使用 java -jar webgoat-server-8.0.0.M17.jar 运行 WebGoat 工作得非常好。

但是,当我尝试添加我的代理时,我收到以下错误日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.12.RELEASE)

2018-06-06 22:36:08.528  INFO 3741 --- [           main] org.owasp.webgoat.StartWebGoat           : Starting StartWebGoat v8.0.0.M17 on MacBook-Pro.local with PID 3741 (/Users/andrewfan/Desktop/Lang Agent Dev Proj help info/webgoat-server-8.0.0.M17.jar started by andrewfan in /Users/andrewfan/Desktop/Lang Agent Dev Proj help info)
2018-06-06 22:36:08.531  INFO 3741 --- [           main] org.owasp.webgoat.StartWebGoat           : No active profile set, falling back to default profiles: default
2018-06-06 22:36:08.844  INFO 3741 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1376c05c: startup date [Wed Jun 06 22:36:08 EDT 2018]; root of context hierarchy
2018-06-06 22:36:11.354  INFO 3741 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$8e12590a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-06-06 22:36:11.442  WARN 3741 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'webgoat.user.directory' in value "${webgoat.user.directory}"
2018-06-06 22:36:11.455  INFO 3741 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-06-06 22:36:11.464 ERROR 3741 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'webgoat.user.directory' in value "${webgoat.user.directory}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1268) ~[spring-beans-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.16.RELEASE.jar!/:4.3.16.RELEASE]
    at 

...

我缩短了错误消息,因为跟踪只有几页长,但主要错误似乎是 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConfiguration': Injection of autowired依赖失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${webgoat.user.directory}”中的占位符“webgoat.user.directory”


我正在按如下方式运行我的代理: java -javaagent:/Users/path/to/jar/Spn-LangAgent-0.0.jar -jar webgoat-server-8.0.0.M17.jar --server.port=8080 --server.address=localhost

我的代理如下:

package com.spnlangagent.langagent;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.lang.reflect.Field;
import java.lang.instrument.Instrumentation;
import java.lang.instrument.UnmodifiableClassException;

import com.google.monitoring.runtime.instrumentation.AllocationRecorder;

public class LangAgent {

    public static void premain(String agentArgs, Instrumentation inst) throws Exception {
        System.out.println("LangAgent: premain now running");
        setupInstrumentation(agentArgs, inst);
        startRuntime(agentArgs);
    }

    private static void setupInstrumentation(String agentArgs, Instrumentation inst) throws Exception {
        System.out.println("setupInstrumentation: now running with agentArgs: " + agentArgs);
    }

    private static void startRuntime(String agentArgs) throws Exception {
        System.out.println("startRuntime: now running with agentArgs: " + agentArgs);
    }
}

除了一些打印语句外,代理的原始内容被注释掉了,但即便如此,WebGoat 在启动时还是崩溃了。

我用 WebGoat 尝试了另一个代理,它工作正常,所以我唯一能想到的是我的代理有问题,或者它的打包方式有问题。

我用的是Maven,我的MANIFEST.MF如下:

Manifest-Version: 1.0
Premain-Class: com.spnlangagent.langagent.LangAgent
Can-Redefine-Classes: true
Can-Retransform-Classes: true

运行mvn package后,.jar中打包的MANIFEST如下:

Manifest-Version: 1.0
Premain-Class: com.spnlangagent.langagent.LangAgent
Built-By: andrewfan
Can-Redefine-Classes: true
Can-Retransform-Classes: true
Created-By: Apache Maven 3.5.3
Build-Jdk: 1.8.0_172

在我的 pom.xml 中,我正在执行以下操作以获取 list :

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                </archive>
            </configuration>
        </plugin>

如果有人能为我指明正确的方向以找出 WebGoat 崩溃的原因,或者如果有人能提供更多关于为什么我目前所做的错误的见解,那将不胜感激。

谢谢。

注意:如果我的其余 pom.xml 是调试所必需的,我会很乐意提供;只是这个问题已经很长了。

最佳答案

Webgoat(以及大多数基于 Spring 的应用程序)依赖属性文件(通常为 propertiesyaml 格式)来执行占位符查找。

您失败的症状表明 Spring 未能查找占位符处理的属性。

鉴于占位符查找在您的代理 JAR 不存在的情况下运行良好,并且根据您在评论中提到的信息,问题是由

  • 您的代理 JAR 提供了 application.properties(它与 Webgoat 用于占位符的属性文件有名称冲突)
  • 代理 JAR 将成为类路径的一部分,甚至可能早于主 JAR 出现
  • 您的空application.properties“隐藏”了Webgoat 主JAR 中的那个。这意味着,当 Webgoat 启动时,Spring 为它的占位符处理选择了你的空 application.properties,因此失败了。

关于java - 尝试使用 Java 代理运行 WebGoat 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50732288/

相关文章:

java - 蛇游戏 : how to stop the game when the snake eat itself

java - JUnit 不会在 Eclipse 中的断点处停止(使用 JDK 1.6.0.20)

java - Apache FreeMarker 中的文本格式(首字母大写,其余字母小写)

Maven 代理与镜像

java - 无法将项目方面动态 Web 模块的版本更改为 2.5

spring-boot - 如何使用Spring Data ElasticSearch?

java - Spring boot,JVM参数太多还是刚好够用?

java - 带有 thymeleaf 国际化的 Spring 4 无法识别来自资源属性文件的消息

java - 当我在 Jenkins 上发布时,为什么我的测试看不到我的系统属性?

java - 如何参数化 Spring Controller 和 thymeleaf 中的链接值?