Linux應急響應與分析
硬碟克隆
dd if=/dev/zero of=/dev/sda bs=4k 擦除硬碟
dd if=/dev/sda of=/root/image 克隆硬碟到映像
dd if=/root/image of=/dev/hdb 將備份檔案到指定硬碟
惡意檢測軟體
chkrootkit 檢測系統後門、rootkit、檔案完整性
chkrootkit -n| grep 'INFECTED'
rkhunter 與chkrootkit類似
rkhunter --checkall
webshell查殺:
測試樣本庫:https://github.com/tennc/webshell
將web目錄打包到windows下用D盾查殺 查殺數:1162
loki工具(跨平臺)https://github.com/Neo23x0/Loki/ 載入yara規則掃描webshell檔案與惡意軟體 查殺數:973
loki -p 掃描目錄 -s 掃描大小 -l 日誌檔案
河馬查殺(跨平臺) 查殺數:831
hm scan /opt/webshell/
深信服webshell查殺(跨平臺) 查殺數:1082
最終結果D盾》深信服webshell查殺》Loki》河馬
病毒檢測:
cleamav linux開源病毒查殺工具
系統檢查工具:
pkg audit freebsd下使用pkg命令檢測系統存在問題的軟體
debsecan | gre p "high urgency" | grep "remotely exploitable"
檢測系統各個設定與軟體 lynis audit system
aide 高階入侵檢測系統
以下是在freebsd系統中測試的,但是大體過程差不多大體就是先設定配置檔案,初始資料庫,然後在進行檢測。
cp /usr/ports/security/aide/files/aide.conf.freebsd /etc/aide.conf
touch /var/db/aide/databases/aide.db
chmod 755 /var/db/aide/databases/aide.db
aide --config=/etc/aide.conf --init 初始化aide資料
cp /var/db/aide/databases/aide.db.new
/var/db/aide/databases/aide.db
aide --config=/etc/aide.conf --check 對比上次初始化資料結果是否有過改動
系統資訊檢視
cat /etc/issue #Debian/Centos 檢視系統版本
freebsd-version #BSD下檢視系統版本
cat /proc/version | grep Linux 檢視系統核心
cat /etc/lsb-release 檢視 #debian系發行版本
cat /etc/redhat-release 檢視 #rhel系發行版本
cat /proc/modules 檢視系統核心模組
cat /proc/uptime 檢視系統執行時間
tune2fs -l /dev/sda1|grep created 檢視系統安裝時間
cat /etc/fstab 檢視自動掛載檔案
echo $? //檢視上一條命令是否執行成功 結果返回0為真
service --status-all | grep "is running" #Centos下檢視正在執行的服務
service --status-all | grep + #Debian系列下檢視正在執行的服務
資訊收集指令碼:
ofollow,noindex" target="_blank">http://www.securitysift.com/download/linuxprivchecker.py
只需在終端鍵入python linuxprivchecke.py就可以使用該指令碼,該指令碼將列舉檔案和目錄的許可權和內容。這個指令碼和LinEnum工作原理大致相同並且在相關係統網路和使用者獲得的資訊更詳細。
watch 監控命令執行的結果 實時變動
watch tail -n /var/log/1.log 實時監控日誌
watch netstat -antop 實時監控網路連線
使用者檢視:
grep :0 /etc/passwd:檢視是否有特權使用者(root許可權使用者)
ls -l /etc/passwd:檢視passwd最後修改時間
cat /etc/passwd|ag -v 'nologin$|false$' 檢視可以登陸的使用者
cat /etc/passwd | awk -F: 'length($2)==0 {print $1}':檢視是否存在空口令使用者
cat /etc/sudoers 檢視sudo配置檔案
使用者名稱 登陸來源 可切換的身份 可執行的命令 加了NOPASSWD: 後切換root不需要密碼
程序檢視:
ps aux/ps -ef //檢視程序詳細資訊
lsof -p pid //檢視程序開啟的埠和檔案
用ls 檢視/proc目錄下的程序id
lsof -u root -c mysql 檢視root使用者mysql程序開啟的情況(包括檔案還有網路連線)
lsof -i:8443 檢視與某埠相關的程式與網路連線
top 檢視系統程序
按1後 在輸入shitf+p 以cpu佔用排序,可以快速檢視不正常的程式,例如挖礦
按shift+m 以記憶體佔用排序
檔案管理:
ls -alh //檢視當前目錄下的詳細資訊 h代表以kb ,mb為單位
yes|cp a /test/a 強制複製 沒有提示
df -h 檢視系統分割槽大小
du -h 遞迴統計當前目錄下的檔案大小
find / -uid 0 -print:查詢特權使用者檔案
find / -size +10000k -print:查詢大於10000k的檔案
10M/10G
find / -name "..." -prin:查詢使用者名稱為...的檔案
find / -name core -exec ls -l {} \;:查詢core檔案,並列出詳細資訊
find /etc /bin /sbin /dev /root/ /home /tmp -atime/mtime/ctime -1 分別代表了檔案讀取日期/檔案更改日期/檔案許可權更改日期 後面數字代表n * 24 1就是24小時
+7 7天外 -7 天內
find ./ -mtime 0 -name "*.jsp" 查詢最近修改過的指令碼檔案
find . -type f -mtime -60 -delete 查詢結果並將其刪除
md5sum -b filename:檢視檔案的md5值
rpm -qf /bin/ls:檢查檔案的完整性(還有其它/bin目錄下的檔案) #Centos
stat 檢視檔案的atime/mtime/ctime 分別對應著訪問時間/修改時間/狀態時間
!!不要直接用vim檢視檔案內容,這樣會更改檔案的mtime值
批量重新命名
rename 's/ /_/g' *
批量刪除括號裡面沒有的內容
rm !(*.foo|*.bar|*.baz)
批量壓縮(從文字列表中壓縮):
find /var/log -mtime -3 > list.txt
tar -czvf list.tar.gz -T list.txt
rar格式的解壓縮例項:
unrar e filesname.rar
7z檔案解壓縮例項
yum install p7zip/apt-get install p7zip
7z x filename.7z
zip檔案解壓縮
gzip -v file 壓縮 gzip -d file 解壓
unzip 檔名
xz 壓縮與解壓
apt-get install xz-utils
壓縮時間長 空間更小 linux自帶
xz -z 要壓縮的檔案
xz -d 要解壓的檔案
xz -k -d 要解壓的檔案 保留壓縮檔案
tar.xz 壓縮與解壓
tar cvf xss.tar xss/
xz -z xxx.tar
xz -d xss.tar.xz
tar xvf xss.tar
tar.bz2 解壓
tar -jxvf xxx.tar.bz2
zcat 1.tar.gz 檢視壓縮檔案中的文字
用grep二進位制搜尋的方式檢視壓縮包裡面的文字 -a引數二進位制模式匹配
zgrep 壓縮檔案中搜索文字
檔案上傳到系統中:
先別的電腦上開啟web埠python -m SimpleHTTPServer
在使用wget http://1.1.1.1/1.exe 或 curl http://1.1.1.1/1.exe -o 1.exe
linux rz與sz命令
apt-get install lrzsz,輸入rz就可以選擇檔案上傳,需要ssh客戶端軟體支援不然會報錯。
scp&&sftp last與w不會存在登陸記錄,但是auth.log裡面會有pam認證資訊
scp -P 2222 /root/1.txt [email protected]:/root/1.txt 通過scp上傳到對方電腦上
scp -P 2222 [email protected]:/root/1.ico /root/1.txt 對方電腦下載到本地
#!/usr/bin/perl
use LWP::Simple;
getstore("http://domain/file", "file");
perl test.pl
通過nsf掛載進行下載
sudo mount -t nfs 1.1.1.1:/opt/test /root/tools
通過base64編碼上傳
base64 /bin/nc > nc.txt
base64 -d -i nc.txt > ./nc
通過xxd或者od上傳
xxd -p test.pcap>1.txt
xxd -r -p 1.txt test.pcap
檢查網路:
ip link | grep PROMISC:檢查混雜模式,正常網絡卡不應該存在promisc,如果存在可能有sniffer
sockstat -46 #FreeBSD下檢視網路監聽
lsof -i 檢視網路連線與埠監聽
netstat -antup:檢視網路連線
netstat --tulpen 監聽埠(tcp與udp的監聽)
arp -a:檢視arp記錄是否正常
/sbin/route -nee 檢視路由表
檢視外網ip
計劃任務:
crontab -u root -l:檢視root使用者的計劃任務
檢視使用者cron檔案 /var/spool/cron/crontabs
ls -l /etc/cron.*:檢視系統cron檔案, /etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly這幾個檔案的內容在crontab -l 看不到
使用者資訊檢視
如果安裝了zsh也可以看看~/.zshrc
cat ~/.bash_history
cat ~/.nano_history
cat ~/.mysql_history
cat ~/.php_history
cat ~/.bashrc
cat ~/.profile
cat ~/.ssh/authorized_keys
cat /etc/profile
cat ~/.bash_profile
cat ~/.bashrc
cat ~/.bash_logout
env
set
軟體相關
dpkg -l #Debian系檢視軟體包列表 iU 表示軟體包未安裝成功/ii 表示安裝成功/rc 表示軟體包已經被解除安裝,但配置檔案仍在
rpm -qa #RHEL系 檢視軟體包
whereis vim //檢視vim相關的路徑,定位軟體目錄。
登陸資訊檢視:
w/who 檢視當前系統登入情況
last:顯示系統的登入以及重啟情況
Last -x 針對關機重啟
Lastb 登陸錯誤資訊,可以統計爆破資訊。
Lastlog 檢視所有使用者的上一次登陸資訊
cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' //檢測ssh是否遇到爆破
netstat -anotp|grep ESTAB*|awk '{print $5}'|awk -F ':' '{print $1}'|sort|uniq
ag -a accepted /var/log/auth.*|ag -o '\d+\.\d+\.\d+\.\d+'|sort|uniq 根據auto.log檢視ssh登陸ip
簡單分析
file 1.rar 通過file命令判斷檔案型別
strings bin|less 檢視二進位制檔案中的字串
strings -n 8 提取字串
strace bin 記錄一個命令執行時系統呼叫,trace限定條件為只檢視網路、程序、檔案動作
strace -T -f -tt -e trace=process,network,file curl ipip.net
strace -T -f -tt -e trace=process,network,file -p 23825 根據PID從當前程序讀取
分析openssh後門
https://github.com/jivoi/openssh-backdoor-kit/
作用:後門密碼,記錄所有ssh連線密碼(入站與出站),使用後門密碼連線不會記錄last與lastlog
strace -T -f -tt -e trace=process,network,file -p 20123 -o sshd.out
然後在另一個視窗中登陸ssh,記錄ssh登陸時檔案呼叫。
strings sshd.out | grep open|grep -v '/dev'|grep 'WR',可以看到ssh登入時除了在utmp/wtmp/lastlog寫入還在tmp下一個檔案寫入(O_WRONLY 只寫開啟,O_RDWR 可讀可寫開啟)
gcore 記憶體轉儲,將當前程序轉儲為映像檔案,需要安裝gdb。
gcore 21919 將PID為21919程序轉儲到當前目錄
strings -n 8 core.21919|ag 'http:' 檢視網路行為
文章參考:
https://blog.g0tmi1k.com/2011/08/basic-linux-privilege-escalation/
《Unix/Linux網路日誌分析與流量監控》