這是本文件的舊版!
目錄表
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 為對內網卡
55 watch
-n1 每秒更新一次,-d 標出有變動的文字
watch -n1 -d 'ps aux | grep tar'
57 lspf
查詢哪一個行程佔用 /mnt/cdrom
lsof /mnt/cdrom
查詢某 PID 或行程名稱開啟的檔案
lsof -p 345 lsof -c syslogd
查詢已開啟的 socket
lsof -i
60 ngrep
取得所有 HTTP GET request
ngrep -q GET -d eth0
bpf filter(Berkeley Packet Filter)
ngrep -qi [email protected] port 25
61 nmap
辨識作業系統
nmap -O abc.tw
64 ntop
apt-get install ntop
開成 daemon,可透過網頁連 port 3000 提供統計資料
ntop -d
66 ssh-keygen
產生一對金鑰,途中可考慮要不要輸入 passphrase
ssh-keygen -t rsa
產生公鑰:~/.ssh/id_rsa.pub 和私鑰:~/.ssh/id_rsa,將公鑰放入想要遠端的主機
$ scp ~/.ssh/id_rsa.pub [username]@[server.name]:~/.ssh/ $ ssh [username]@[server.name] $ cat ~/.ssh/id_rsa >> ~/.ssh/authorized_keys
若私鑰遭竊,別人即可輕易登入。
遠端命令
一個簡單的 ssh-to
#!/bin/sh ssh `basename $0` $*
$ ln -s ssh-to a.sars.tw $ ln -s ssh-to b.sars.tw
執行指令即可直接列出遠端主機的狀態
$ ./a.sars.tw uptime
68 ssh-agent
$ eval `ssh-agent` $ ssh-add
/etc/ssh/ssh_config
ForwardAgent yes
70 X over ssh
透過 X11 forwardng 執行遠端的程式
修改 sshd_config
X11Forwarding yes
$ ssh -X host
71 ssh forward
ssh -f -N -L110:mailserver:110 -l user mailserver
72 環境設定檔同步
movein.sh:(tar 的 -h 表在遠端產生的是普通檔案,而非 link )
#!/bin/sh
if [ -z "$1" ]; then echo "Usage: `basename $0` hostname"
exit
fi
cd ~/.skel
tar zhcf - . | ssh $1 "tar zpvxf -"
在家目錄下整理需要複製的設定檔
$ mkdir .skel $ cd .skel $ ln -s ../.bashrc .bashrc $ ln -s /etc/vim/vimrc .vimrc $ mkdir .ssh $ cd .ssh $ ln -s ../../.ssh/id_rsa.pub authorized_keys2
75 色彩化日誌
rcg 使用 Term::ANSIColor 將符合條件的字串改成特定色彩
less -r 將 ESC sequence 解釋成彩色效果
正規式:
\d+\.\d+\.\d+\.\d+\. # IP 位址的表示法 ^(J|F|M|A|S|O|N|D)\w\w (\d|)\d # 日期字串 \b\d\d:\d\d:\d\d\b # 時間字串
