该工具的后续开发将继续在 corretto/hotpatch-for-apache-log4j2 进行。
感谢大家的分享、评论、试用以及对本项目的贡献!
这是一个简单工具的POC,用于向正在运行的JVM进程注入一个Java agent。该agent会修补所有已加载的 org.apache.logging.log4j.core.lookup.JndiLookup 实例中的 lookup() 方法,使其无条件返回字符串 "Patched JndiLookup::lookup()"。这应该在无需重启Java进程的情况下修复 Log4j 中的 CVE-2021-44228 远程代码执行漏洞。
目前仅在 JDK 8 和 11 上测试过!
免责声明: 提供的代码希望有用,但不提供任何担保!
JDK 8
javac -XDignore.symbol.file=true -cp <java-home>/lib/tools.jar Log4jPatch.java
JDK 11
javac --add-exports java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED Log4jPatch.java
JDK 8
java -cp .:<java-home>/lib/tools.jar Log4jPatch <java-pid>
JDK 11
java Log4jPatch <java-pid>
如果遇到类似如下错误:
Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: The VM does not support the attach mechanism
at jdk.attach/sun.tools.attach.HotSpotAttachProvider.testAttachable(HotSpotAttachProvider.java:153)
at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:56)
at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
at Log4jPatch.loadInstrumentationAgent(Log4jPatch.java:115)
at Log4jPatch.main(Log4jPatch.java:139)
这意味着你的JVM由于以 -XX:+DisableAttachMechanism 参数运行而拒绝任何帮助。