這是本文件的舊版!
目錄表
Linux Server Hacks 駭客一百招 - 讀書筆記
1 移除非必要服務
grep -v "^#" /etc/inetd.conf
非必要:portmap, rpc.mountd, rpc.nfsd, smbd, nmbd, automounter, named, lpd, inetd, telnet, rlogin, rexec, ftp, finger, comsat, echo, identd
4 利用 init 建立常駐服務
zz:12345:respawn:/usr/local/sbin/my_daemon
| 字串 | 說明 |
|---|---|
| zz | 程式代號,必須唯一 |
| 12345 | 程式應該在哪些 runlevel 執行 |
| respawn | 關鍵字(?) |
| path | 程式路徑 |
讓 init 重新讀取設定檔
kill -HUP 1
在 init 執行的程式會用 root 執行,可利用 sudo 讓執行者不是 root。
5 n>&m 交換 stdout 與 stderr
9 加速編譯
make -j2 bzImage:
11 找出非必要的 setuid/setgid 程式
find / -perm +6000 -type f -exec ls -ld {} \; > setuid.txt &
刪除 setuid 權限
chmod a-s /usr/bin/[filename]
find -perm
-perm mode
File's permission bits are exactly mode (octal or symbolic). Since an exact match is required, if you want to use this form for symbolic modes, you may have to specify a rather complex mode string. For example '-perm g=w' will only match files which have mode 0020 (that is, ones for which group write permission is the only permission set). It is more likely that you will want to use the '/' or '-' forms, for example '-perm -g=w', which matches any file with group write permission. See the EXAMPLES section for some illustrative examples.
12 sudo
user machine = (effective_user) command root ALL=(ALL) ALL peter ns.oreillynet.com=(bind) /usr/sbin/rndc, /usr/sbin/named %www-data ALL=(www) ALL
| 欄位 | 說明 |
|---|---|
| user | 被授權人帳號,若前面有 % 符號代表群組 |
| machine | 該帳號所屬主機名稱,不一定要是本機使用者(?) |
| effective_user | 可以執行 command 的有效帳號 |
| command | 可以執行的命令 |
sudo Alias
User_Alias ADMINS=a,b,c Runas_Alias DAEMONS=bind,www,ircd Host_Alias WEBSERVERS=www.oreilly.com Cmnd_Alias APACHE=/usr/local/apache/bin/apachectl ADMINS WEBSERVERS=(DAEMONS) APACHE # 使用各種 alias
flag
不用輸入密碼
test ALL=(ALL) NOPASSWD: ALL
14 域名查詢
利用 whois.twnic.net 找出在字典中結尾為 sars 的單字,尚未被註冊的域名,五秒鐘查一個。
cat /usr/share/dict/words | grep 'sars$' | sed 's/$/.tw/' | while read i; do (whois -h whois.twnic.net $i | grep -qi '^No Found') && echo $i; sleep 5; done
15 du
du -cks * | sort -rn
16 proc
目前啟動的核心
/proc/version
系統記憶體大小
ls -lh /proc/kcore
計算 /proc 下有多少個「數字目錄」,與 ps 取得的比較,若第二個數值大於第一個,表 ps 可能被竄改。
ls -d /proc/* | grep [0-9] | wc -l; ps ax | wc -l
參考
19 清理門戶
鎖定使用者
passwd -l [username]
更改預設 shell
chsh -s /bin/false [username]
透過 RSA 或 DSA key 驗證身份,成為跳板,將自己機器的 port 8000 forward 到網路上的 HTTP port。
ssh -f -N -L8000:private.intranet.server.com:80 old.server.com
移除 ~userhome/.ssh/authorized_keys*。移除 sudo 權限。刪除 cron、at 排程工作。檢查 public_html。確認 .forward。
刪除該使用者仍在執行的程式
ps auxw | grep -i ^username
找尋該使用者擁有的檔案是否出現在其他地方
find / -user username > ~root/user_files.report
37 透過 ssh 與 tar 壓縮傳檔案
將 test_ssh 壓縮後傳到另一台主機上存成 .tgz 檔案
tar zcvf - test_ssh | ssh hostname.tw " cat > test_cc.sars.tgz"
將本機的 apache 設定檔複製到遠端機器上,並先在遠端機器備份舊設定。-p 參數
tar zcf - /usr/local/apache/ | ssh desthost.tw "cd /usr/local; mv apache apache.bak; tar zpxvf -"
直接遠端還原壓縮檔
ssh fromhost.tw "cat really-big-archive.tgz" | tar zpvxf -
38 rsync
連上 fromhost.tw,將裡面的 frompath 檔案複製到本機的 destpath。–delete 參數表若來源的檔案被刪除,目的地的檔案會跟著刪除。
rsync -ave ssh --delete fromhost.tw:/[frompath] [destpath] rsync -ave ssh fromhost.tw:/tmp/ /tmp/rsync_test/
若用 crontab 跑,可不使用 -v 參數輸出訊息。
rsync -ae ssh master.machine.com:/usr/local/apache/htdocs/ /usr/local/apache/htdocs/
43 ISO、燒錄
mkisofs:-r 表將 Unix 檔案系統的特有資訊存在光碟上,又保持 ISO9660 的相容性。
mkisofs -r /home/test/ > /tmp/home.iso
將光碟片製作成 .iso 檔,bs 需要實際測試,找出理想值。
dd if=/dev/cdrom of=image.iso bs=10k
掛載 iso 檔
mount -o loop,ro -t iso9660 ./image.iso /mnt/cdrom
45 iptables
列出所有規則
iptables -L
清空所有過濾規則
iptables -F
允許
iptables -A INPUT -t filter -s 1.2.3.0/24 -j ACCEPT
阻隔
iptables -A INPUT -t filter -s 5.6.7.8 -j DROP
可使用的 port - 22
iptables -A INPUT -t filter -p tcp --dport 22 -j ACCEPT
剩下的都阻隔掉
iptables -A INPUT -t filter -p tcp --syn -j DROP
NAT(以 eth0 為連外介面)
echo "1" > /proc/sys/net/ipv4/ip_forward iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Port forwarding(Destination NAT)
iptables -t nat -A PREROUTING ! -i eth1 -p tcp --destination-port 3389 -j DNAT --to 192.168.1.5:3389 # eth1 為對內網卡
