Linux 系統安全講座

Linux 系統安全講座

---------------
大綱

* 註:
授課內容主要以<<Linux 網路安全百寶箱>>為主﹐
再配以網頁資料為副。
---------------

網路安全層次
網路
服務
系統

系統安全
物理保安
地理防禦
門禁制度
社交工程 P2-13
機房保護
機櫃
主機
電源
鍵盤
熒幕
開機保護
鑰匙
電源按鈕
BIOS
啟動設備:
硬碟
軟碟
光碟
OS Loader
LILO P8-2
GRUB info grub (參考範例一)

開機程式 (run level & rc) P7-1
系統登錄
Login
PAM P1-20
ls /usr/share/doc/pam-0.75/
Limit more /etc/security/limits.conf
Nologin touch /etc/nologin
Consol vs remote more /etc/securetty
L istfile (參考範例二)
帳號管理
帳號名稱和 UID P1-5
群組 P2-2~P2-7
帳號密碼 P2-12
Crack / John
shadow P1-11
chage info change
usermod info usermod
gpasswd info gpasswd
權限管理
ugo & rwx P3-8
檔案 vs 目錄 x ?
木馬與病毒 root or non-root?
SUID & SGID & Sticky Bit P4-2
檔案 vs 目錄
偵查技巧 P4-4
su vs sudo
su 缺失 passwd? privilege?
sudoers 設計 info sudoers
檔案屬性 P4-8
appand only
read only
檔案系統設計
file type P3-2
inode & block P3-6
http://www.study-area.org/linux/system/linux_fs.htm#fstab
mount point
quota http://www.study-area.org/linux/system/linux_fs.htm#fquota
read only /usr/bin /usr/sbin...
no dev /home
no suid / no sgid
no exec
安全下載
信任網站 rpmfind, sourceforge...
GPG 簽章 *.sign
MD5 checksum info md5sum
軟體測試
執行碼
生成檔
連線/生成封包
源碼追蹤
入侵偵測
異常行為
多餘連線
記錄檔殘缺
檔案替換
檔案權限改變
隱藏檔
擁有者變更
suid / sgid
設備檔
工具:
chkrootkit
http://linux.tnc.edu.tw/techdoc/check-rootkit-by-u-self.htm
tripwire
http://www.study-area.org/tips/tripwire.htm
tiger
ftp://coast.cs.purdue.edu/pub/tools/unix/TAMU
日誌保護
延增屬性 chattr +a
存取權限 (root only)
size 控制 logrotate
http://www.study-area.org/linux/system/linux_conf.htm#log
真實性 (防止修改) @central / printer
記錄分析
工具:
logcheck
logwatch
資料備份
原始備份
系統備份 vs 資料備份
完整備份 vs 差異備份
http://www.study-area.org/linux/system/linux_conf.htm#backup
備份媒體 & 保存
備份抽樣 tar, cpio/apio
災後復原
風險評估
容錯等級
復原程度
容錯方案/設備:
連線:
redundent connection
load balance
cluster
內容:
RAIDS
SAN & NAS
Mirror & rsync
修補 P14-3
版本選擇
最安全版本﹖ ftp://linux.sinica.edu.tw/
更新 mirror / APT / rhn
測試

------------
網路安全
安全資訊
網站
http://www.cert.org.tw
http://www.vtcif.telstra.com.au/info/security.html
http://www.redhat.com/apps/support/errata/
wu-ftp
zlib
UW imap
openssh
ssnldap
.....
http://www.securityfocus.com/
http://safe.ip-market.com/
討論群組/新聞組
news:comp.security.announce
Mail List / 技術論壇
listserv@securityfocus.com
digest@sams.org
雜誌
服務程式漏洞與修補
服務程式數量 P12-3
集中 vs 分散
版本更新 wu-ftp, bind, sendmail
測試
chroot 技巧
使用時機
可行軟體
http://www.study-area.org/tips/dns_chr.htm
困難點
tcpwrapper 之應用
Compile time vs run time
Supper daemon vs tcp_wrapper P15-2
Inetd vs xinetd P11-2
hosts.allow vs hosts.deny P15-9
services * 附錄一
防火牆和 NAT
防火牆種類
proxy vs filtering P16-1 / P17-1
防火牆工作原理
http://www.study-area.org/network/network_fw.htm
基礎知識
http://www.study-area.org/linux/servers/linux_nat.htm
防火牆規則設計
ACL vs state list
NAT 之應用場合
NAT 種類
SNAT vs DNAT
Statice vs Dynamic
http://www.study-area.org/tips/NAT-HOWTO/NAT-HOWTO-chn-3.html
網路架構之設計與部署
信任網路 vs 非信任網路
DMZ 應用
物理區隔
Protocol Switching
常見攻擊方法
DoS
Ping of death P13-2
Syn Flood P13-6
其他 P13-8
因應 P13-9
自我攻擊測試
netstat
http://www.study-area.org/linux/servers/linux_net.htm#network
nmap P25-8
portsentry / snort
-------------
資訊加密
明文 vs 密文
熒幕顯示 vs 封包內容
http://www.study-area.org/network/network_enscp.htm
竊聽技巧與防範
竊聽前提 packet?
竊聽工具
tcpdump
sniffit
攔截點 routing ?
Hub vs Swtich CSMA/CD?
加密手段和原理
加密原理
原始加密方法
http://www.study-area.org/network/network_enscp.htm
演算法
技術專利
加密技術
對稱密鑰 vs 非對稱密鑰
http://www.study-area.org/network/network_enscp.htm
美國之出口限制
電子簽章
紙張 vs 電子
可確認性/非抵賴性
電子交易之現況與將來
安全連連線
ssl & ssh
破解成本
http://www.study-area.org/tips/security.htm
(http://www.nchu.edu.tw/trnc/90-2/firewall.ppt)
ssh 工作原理
http://www.study-area.org/tips/security.htm
vpn 之建置
××× 原理
應用場合
××× 方案:
ssh
vpnd
http://www.study-area.org/tips/vpn.htm
ipsec P21-3
網路設計 vs 實作技術 subnet/routing?

實作參考
安全的伺服器環境(1)
http://safe.ip-market.com/article.php?sid=5
確保Linux安全的十招
http://safe.ip-market.com/article.php?sid=26
Linux安全設置手冊(轉貼)
http://phorum.study-area.org/vie ... light=time-out%3D00

---------------

* 範例一: GRUB 密碼保護

1) 輸入 grub 進入其中產生 MD5 密碼:
grub> md5crypt
Password: **********
Encrypted: $1$U$JK7xFegdxWH6VuppCUSIb.
** 完成後請用滑鼠左鍵將密碼選取,並輸入 quit 退出 grub 。

2) 修改 /etc/grub.conf 進行密碼保護設定:
password --md5 $1$U$JK7xFegdxWH6VuppCUSIb.
** 請用滑鼠中鍵將密碼貼上。
title Red Hat Linux 7.3 (2.4.18-3)
lock
root (hd0,1)
kernel /vmlinuz-2.4.18-3 ro root=/dev/hda12
initrd /initrd-2.4.18-3.img

3) 在開機的時候輸入 p 和密碼。


* 範例二: PAM 之 listfile (ssh)

1) 參考 ftp 之現成設定:
grep listfile /etc/pam.d/ftp

2) 模仿之並在 ssh 中設定:
vi /etc/pam.d/sshd
在前面加入:
auth required /lib/security/pam_listfile.so item=user \
sense=deny file=/etc/sshusers onerr=succeed


---------------
附錄一: 系統服務建議
finger 強烈建議關閉
ftp 如果不需要﹐儘量關閉。尤其 anonymous 更應關閉。
gopher 關閉
imap 如果不需要﹐儘量關閉。
pop2 關閉
pop3 如果不需要﹐儘量關閉。
talk 關閉
ntalk 關閉
telnet 如果不需要﹐儘量關閉。請以 ssh 取代之。
uucp 關閉

samba 只對內部開放
nfs/nis 只對內部開放
r-command 儘量關閉﹐以 ssh 取代。
x-protocol 只對內部開放



* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~

-----------------------
Linux 系統安全實作
-----------------------

目標:
透過實作加強 Linux 系統的安全防禦能力。
並以此舉一反三﹐提高管理員的安全意識和技巧。

作業平臺環境:
OS: RedHat7.3
安裝選項: 伺服器模式

範例帳號資料:
sysop: 系統管理員
u1: 普通帳號
u2: 受限制帳號

範例網路規劃:
分組模擬:
WAN: 10.0.1.x
LAN: 192.168.1.x ~ 192.168.254.x
閘道主機環境:
外部界面: eth0
外部IP: 10.0.1.#
GATEWAY: 10.0.1.254
內部界面: eth1
內部 IP: 192.168.#.254
主機名稱: gw.g#.test.edu
內部主機環境:
界面: eth0
IP: 192.168.1.#.2
GATEWAY: 192.168.#.254
主機名稱: server.g#.test.edu


主要項目:
* clock
* Firewall
* tcpwrapper
* PAM
* service
* system
* ×××
* NAT + T_PROXY
* Intrusion detection
* Package Upgrade


-----------------------
實作步驟:

註: # 開頭的句子基本上為一行一個命令﹔若在 vi 命令中則為註解符號。
^ 為 Ctrl 鍵( ^D 就是同時按下 Ctrl 和 D 鍵)


一﹑修正時間

# vi /etc/cron.daily/clock
echo
#rdate -s time.nist.gov
ntpdate time.stdtime.gov.tw
clock -w
echo time sync to $(date)
echo
# chmod +x /etc/cron.daily/clock
# sh /etc/cron.daily/clock

二﹑防火牆

1) 關閉 ipchains:
# service ipchains stop
# rmmod ipchains
# chkconfig --del ipchains
# chkconfig --add iptables
# chkconfig iptables on
# mv /etc/sysconfig/ipchains /etc/sysconfig/ipchains.bak
(** 以上若干命令若執行失敗可置之不理)
2) 載入模組 iptalbes:
# modprobe ip_tables
# mkdir /etc/fw
# vi /etc/fw/load_ipt_mod # 增加如下句子
#!/bin/bash
echo "Loading iptables' modules"
for MOD in iptable_filter ip_conntrack ip_conntrack_ftp; do
modprobe $MOD
done
# chmod +x /etc/fw/load_ipt_mod
# /etc/fw/load_ipt_mod
# vi /etc/rc.d/init.d/iptables # 找到如下句子:
touch /var/lock/subsys/iptables
在其後增加如下數行:
LIM=/etc/fw/load_ipt_mod
if [ -x $LIM ]; then
echo "Loading iptables modules:" && \
$LIM && success || failure
echo
fi
3) 設定規則:
# service iptables stop
# for PORT in 20 21 22 25 53 80 110 113 143 220 443 465 993 995; do
> iptables -A INPUT -p tcp --dport $PORT -j ACCEPT
> done
# iptables -A INPUT -p udp --dport 53 -j ACCEPT
# for TYPE in 0 3 3/4 4 11 12 14 16 18; do
> iptables -A INPUT -p icmp --icmp-type $TYPE -j ACCEPT
> done
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A INPUT -i lo -j ACCEPT
# iptables -A INPUT -j DROP
4) 保存設定規則:
# mv /etc/sysconfig/iptables /etc/sysconfig/iptables.bak
(以上命令若執行失敗可置之不理)
# iptables-save > /etc/sysconfig/iptables

三﹑TCPWRAPPER

1) 備份:
# cp /etc/hosts.allow /etc/hosts.allow.bak
# cp /etc/hosts.deny /etc/hosts.deny.bak
2) 關閉所有:
# echo "ALL: ALL" >> /etc/hosts.deny
3) 視個別開放:
# vi /etc/hosts.allow
ALL: 127.0.0.1, localhost
sshd: ALL
in.ftpd: ALL
sendmail: ALL
ipop3d: 192.168.
* 註: 這樣的設計並不對外提供 pop 服務。
如果 pop 主機不是和內部網路直接相連接﹐
同時內部網路透過 NAT 連線至 pop 主機的話﹐
請將 192.168. 改為 NAT 主機的外部 IP 。


四﹑PAM

1) 設定 ssh: (可參考前文之範例二)

# vi /etc/pam.d/sshd (** 在檔案前面加入如下: )
auth required /lib/security/pam_listfile.so item=user \
sense=allow file=/etc/sshusers onerr=fail
# echo "root" >> /etc/sshusers
# echo "sysop" >> /etc/sshusers
2) 限制 su 群組:
# vi /etc/pam.d/su (** 取消如下註解: )
auth required /lib/security/pam_wheel.so use_uid

五﹑SERVICE

* FTP
1) 取消 anonymous FTP
# rpm -e anonftp
# cp /etc/ftpusers /etc/ftpusers.bak
# echo "ftp" >> /etc/ftpusers
2) 取消部份 user 之 guest 限制:
# vi /etc/ftpaccess (** 在 # realuser user1,user2 句子下面增加如下: )
realuser sysop u1
** sysop 與 u1 僅作範例,請改用您的真實帳號。
* POP / IMAP
1) 安裝 imap (請先 mount 好 rh7.3 第二片 CDROM )
# rpm -ivh /mnt/cdrom/RedHat/RPMS/imap-*
2) 開啟 pop3s
# vi /etc/xinetd.d/pop3s
disable = no
** 如下 ipop3 則視需要而定﹐除非不能用 ssl ﹐否則不建議開啟:
# vi /etc/xinetd.d/ipop3
disable = no
3) 重新啟動 xinetd:
# service xinetd restart
4) 設定 ssl 認證:
# cd /usr/share/ssl/certs
# make ipop3d.pem
# /usr/bin/openssl req -newkey rsa:1024 -keyout /root/1.pem -nodes -x509 -days 365 -out /root/2.pem
Using configuration from /usr/share/ssl/openssl.cnf
Generating a 1024 bit RSA private key
.....++++++
....++++++
writing new private key to '/root/1.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:TW # 輸入國家代碼
State or Province Name (full name) [Berkshire]:Taiwan # 輸入省名
Locality Name (eg, city) [Newbury]:TaiChung # 輸入市名
Organization Name (eg, company) [My Company Ltd]:Test Edu # 輸入工作單位
Organizational Unit Name (eg, section) []:G1 # 輸入部門
Common Name (eg, your name or your server's hostname) []:server.g1.test.edu # 主機名
Email Address []:sysop@server.g1.test.edu # 郵件信箱
# cat /root/1.pem > ipop3d.pem
# echo "" >> ipop3d.pem
# cat /root/2.pem >> ipop3d.pem
5) 設定 Client (OutLook Express 為例):
工具->帳號->郵件->內容->進階->下面的pop3
[ V ] 此服務器要求安全連結(ssl)
** 注意: “伺服器”名稱一欄不能使用 IP ﹐必須為 DNS 能解釋的名稱。

* SMTP (AUTH)
1) 設定 sendmail:
# chkconfig sendmail on
# cd /etc/mail
# vi sendmail.mc (** 取消兩行註解 dnl ,另再註解末行: )
TRUST_AUTH_MECH(`DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
......
dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')
# cp -a /etc/sendmail.cf /etc/sendmail.cf.orig
# m4 sendmail.mc > /etc/sendmail.cf
# echo '192.168 RELAY' >> /etc/mail/access
# makemap hash /etc/mail/access.db < /etc/mail/access
** 注意: 請每天使用 sysop 帳號來收 root 的信件,否則不需要設定如下兩行。
# echo 'root: sysop' >> /etc/aliases
# newaliases
2) 重新啟動 sendmail
# service sendmail stop
# service sendmail stop
# service sendmail start

3) 設定 client 端( AUTH SMTP)
工具->帳號->郵件->內容->伺服器
[ V ] 我的伺服器需要查驗身份(v)
** 注意: 可惜目前還不支援 SSL 的連線﹐為安全其見﹐別透過 ineternet 使用密碼。
** 或參考如下實作完成:
http://www.study-area.org/tips/ssl_email.htm

* HTTP
1) 設定 server
預設即可﹐需要特別留意 CGI 的權限設定。
在網頁設計上﹐遇到密碼驗證的提示網頁﹐儘量使用 https 。

* 其它服務
1) 關閉不需要的服務:
# for SRV in autofs canna isdn lpd netfs nfs nfslock portmap rhnsd sgi_fam; do
> chkconfig $SRV off
> done
** 注意: 上述服務儘量不對外提供。


六﹑系統

* BIOS
1) 設定 BIOS 系統和設定密碼
2) 將開機設備設定為硬碟優先
3) 開機時輸入密碼

* BOOT LOADER
1) 設定 grub (參考另文之範例一):
# grub
grub> md5crypt
Password: **********
Encrypted: $1$U$JK7xFegdxWH6VuppCUSIb.
** 完成後請用滑鼠左鍵將密碼選取,並輸入 quit 退出 grub 。
# vi /etc/grub.conf
password --md5 $1$U$JK7xFegdxWH6VuppCUSIb.
** 請用滑鼠中鍵將密碼貼上。
title Red Hat Linux 7.3 (2.4.18-3)
lock
root (hd0,1)
kernel /vmlinuz-2.4.18-3 ro root=/dev/hda1
initrd /initrd-2.4.18-3.img
(**注意:請勿修改 title, root, kernel, initrd 等句子,只需在 title 下加入 lock 設定。)

2) 在開機的時候輸入 p 和密碼。

* SYSTEM ACCOUNT
1) 刪除不必要的帳號/群組:
# for FILE in /etc/group /etc/passwd /etc/shadow; do
> cp -a $FILE ${FILE}.orig
> done
# for UG in shutdown halt news uucp operator games gopher rpc rpcuser; do
> userdel $UG
> groupdel $UG
> done
** 注意: 帳號名稱以實際的機器用途為準。當前範例主要以“非 LAN server” 為準。
2) 增加密碼長度限制:
# vi /etc/login.defs
PASS_MIN_LEN 8
3) 設定自動登離時間:
# vi /etc/profile
export TMOUT=1800
4) 取消普通帳號之 console 程式:
# mv /etc/security/console.apps /etc/security/console.apps.orig
5) 設定預設 RunLevel ﹐並取消 Ctrl+Alt+Del 鍵之 reboot 功能:
# vi /etc/inittab
id:3:initdefault:
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now
(註:為上行注解,請在退出 vi 之前完成)
重新讀入 inittab 設定:
# /sbin/init q
6) 修改登錄提示
# vi /etc/issue
WARNING: This system is open to authorized users only.
Please do NOT break in and all activity is logged.
# cat /etc/issue > /etc/issue.net
7) 設定 sudoers (本例增加 sysop ):
# visudo
** 取消如下註解:
%wheel ALL=(ALL) ALL
# gpasswd -a sysop wheel
8) 修改 logroate 的設定(檔案屬性):
# vi /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {
prerotate
chattr -a /var/log/messages
chattr -a /var/log/secure
chattr -a /var/log/maillog
chattr -a /var/log/spooler
chattr -a /var/log/boot.log
chattr -a /var/log/cron
endscript
sharedscripts
postrotate
chattr +a /var/log/messages
chattr +a /var/log/secure
chattr +a /var/log/maillog
chattr +a /var/log/spooler
chattr +a /var/log/boot.log
chattr +a /var/log/cron
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
9) 修改 fstab :
** 這裡僅作範例,請依實際情況調整。若沒此設定要求,請略過。
# vi /etc/fstab
LABEL=/home /home ext3 defaults,nodev,noexec,nosuid 1 2
LABEL=/tmp /tmp ext3 defaults,noexec,nosuid 1 2
# cd
# umount /home
# mount -a



七﹑×××

(模擬機器將轉移到‘閘道主機’中進行)

--------------
測試環境(假設):

remote 端(g1):
real: 201.0.1.1
vpn: 192.168.1.254
local 端 (g2):
real: 202.0.2.2
vpn: 192.168.2.254

--------------
設定步驟:

1) 在 201.0.1.1 上設定 SSH
# mkdir /etc/skel/.ssh
# useradd -m vpn1
2) 在 202.0.2.2 上執行:
# ssh-keygen -t rsa
# 按三下 Enter 不設定密碼
3) 轉回 201.0.1.1 上面執行:
# cd ~vpn1/.ssh
# scp 202.0.2.2:/root/.ssh/id_rsa.pub ./
# cat id_rsa.pub >> authorized_keys
# chown -R vpn1.vpn1 ~vpn1/.ssh
# chmod 711 ~vpn1/.ssh
# chmod 644 ~vpn1/.ssh/authorized_keys
4) 再到 202.0.2.2 上面測試 ssh 的 RSA 連線﹐確定無需密碼:
# ssh -l vpn1 201.0.1.1

* 設定 remote 端

1) 在 201.0.1.1 上執行 visudo ﹐增加如下數行:
User_Alias ×××USER=root,vpn1
Cmnd_Alias ×××=/usr/sbin/pppd,/sbin/route
×××USER ALL=(ALL) NOPASSWD: ×××
2) 在 /usr/local/sbin 裡面建立一個可執行的 script ﹐取名為 vpn-ppp :
#!/bin/bash
exec sudo /usr/sbin/pppd
** 注意: 請記得執行: chmod +x /usr/local/sbin/vpn-ppp
3) 修改 vpn1 的 shell :
usermod -s /usr/local/sbin/vpn-ppp vpn1

* 設定 local 端

1) 下載 pty-redir-0.1.tgz 到 202.0.2.2 機器﹐並將檔案解至 /usr/local 內:
# wget http://www.study-area.org/linux/src/pty-redir-0.1.tgz
# tar zxvf pty-redir-0.1.tgz
# mv pty-redir-0.1 /usr/local
# cd /usr/local/pty-redir-0.1
# make
# cp pty-redir /usr/local/sbin
2)複製 vpnd 至 init.d 目錄:
# cp vpnd /etc/rc.d/init.d

3) 修改其中的內容:
LOCALIP=192.168.2.254
LOCALNET=192.168.2.0/24
REMOTEIP=192.168.1.254
REMOTENET=192.168.1.0/24
SSHSERVER=201.0.1.1
$REDIR $SSH -q -t -l $SSHACC $SSHSERVER &gt; $DEVNAME
ps -ax | grep "ssh -q -t -l $SSHACC " | grep -v grep \
# ** 註: 最後兩行只是將其中的 -1 拿掉而已﹐請務必注意。

4) 進行連線:
# /etc/rc.d/init.d/vpnd start
如果需要開機就啟動 vpnd 的話﹐請執行如下命令﹐否則略:
chkconfig --add vpnd
chkconfig vpnd on
斷線則輸入:
# /etc/rc.d/init.d/vpnd stop

5) 設路由:
當連線建立好之後﹐在 201.0.1.1 上面執行:
# route add -net 192.168.2.0/24 dev ppp0
在 202.0.2.2 上面執行:
# route add -net 192.168.1.0/24 dev ppp0



八﹑ NAT + Transparent Proxy

** 模擬機器將轉移到‘閘道主機’中進行﹐這裡以 group1 為例子。
** 且注意:gateway 上並不提供任何 service ,單純作 firewall & nat 。

閘道主機環境(假設):
對外界面: eth0
對外 IP: 201.0.1.1
GATEWAY: 201.0.1.254
對內界面: eth1
對內 IP: 192.168.1.254
內部服務主機 IP (假設) : 192.168.1.1

* T_PROXY
1) 修改 /etc/squid/squid.conf ﹐增加如下兩行(可以先搜尋同組設定組態):
acl g1 src 192.168.1.0/255.255.255.0
http_access allow g1
2) 修改如下句子(第一行需要新增):
httpd_accel_host virtual
httpd_accel_port 0
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
3) 重新啟動 squid :
# service squid restart
# chkconfig squid on


* NAT

1) 與前面的主機放火牆略有不同﹐可下載如下 script 進行設定:
# mkdir /etc/fw
# chmod u+x,go-x /etc/fw
# cd /etc/fw
# wget http://www.study-area.org/linux/src/ipt_nat.tgz
# tar zxvf ip_nat.tgz
2) 修改設定參數:
EXT_IF=eth0
INT_IF=eth1
DMZ_IF=eth1 (** 若您沒設 DMZ ,請用內部屆面 )
DMZ_SERVER="192.168.1.1"
3) 執行 script:
# /etc/fw/ipt_nat
4) 保存設定:
# vi /etc/rc.d/rc.loal
if [ -x /etc/fw/ipt_nat ]; then
/etc/fw/ipt_nat
fi

九﹑入侵偵測

* SUID/SGID:
1) 建立原始列表:
# find / \( -perm +u+s -o -perm +g+s \) -exec ls -l {} \; >/root/suid.list
# chattr +i /root/suid.list
2) 修改 crontab 進行檢查:
# vi /etc/cron.daily/chk_suid.sh
#!/bin/bash
ORIG_LIST=/root/suid.list
NEW_LIST=/root/suid.new
echo
find / \( -perm +u+s -o -perm +g+s \) -exec ls -l {} \; > $NEW_LIST
if diff $ORIG_LIST $NEW_LIST
then
echo "Looks fine: no new suid files found."
else
echo "WARNING: new suid files found!"
fi
echo
echo
# chmod +x /etc/cron.daily/chk_suid.sh
# /etc/cron.daily/chk_suid.sh
* 提示: 該結果每天會 email 到 root 的信箱。
** 注意: 如果確定新增的 suid 檔案是安全的﹐請更新 suid.list:
# chattr -i /root/suid.list
# find / \( -perm +u+s -o -perm +g+s \) -exec ls -l {} \; >/root/suid.list
# chattr +i /root/suid.list


* TRIPWIRE (可參考: http://www.study-area.org/tips/tripwire.htm):

1) 取得 tripwire 的安裝套件(rh7.2 第二片光碟)﹐並進行安裝:
# rpm -ivh /mnt/cdrom/RedHat/RPMS/tripwire-2.3.0-58.i386.rpm
* 提示: 如果沒有 CDROM ﹐可以從網路抓:
# wget ftp://linux.sinica.edu.tw/pub1/r ... e-2.3.1-10.i386.rpm
2) 進行初始化設定
# /etc/tripwire/twinstall.sh
# 輸入多次密碼 ( site & local)

3) 進行一些針對性修改
# cd /etc/tripwire
# /usr/sbin/tripwire --init
# /usr/sbin/tripwire --check | grep Filename > twnotfound.txt
# vi twfilter.sh
(** 或抓: # wget http://www.study-area.org/linux/src/twfilter.sh)
#!/bin/bash
orig_file=/etc/tripwire/twpol.txt
not_file=twnotfound.txt
tmp_file=tmp.txt
new_file=new.txt
cat $orig_file > $tmp_file
for i in $(cat $not_file | cut -d ":" -f 2); do
grep -v $i $tmp_file > $new_file
cat $new_file > $tmp_file
done
mv $orig_file $orig_file.bak
cat $new_file > $orig_file
rm -f $new_file
rm -f $tmp_file
#-- end of script --#
4) 執行 script :
# chmod +x twfilter.sh
# sh twfilter.sh

5) 重新建立資料庫:
# /usr/sbin/twadmin -m P /etc/tripwire/twpol.txt
# /usr/sbin/tripwire -m i

6) 執行如下命令檢查:
# /usr/sbin/tripwire --check
** 提示: 該結果每天會 email 到 root 的信箱。
7) 如果要對資料庫進行過更新﹐讀入最新 report 檔案
# /usr/sbin/tripwire -m u -r \
# /var/lib/tripwire/report/your.machine.name-2002xxxx-xxxxx.twr
## 退出 vi 模式 並輸入密碼

* CHKROOTKIT:
1) 下載 chkrootkit:
# cd /root
# wget http://www.study-area.org/linux/src/chkrootkit.tar.gz

2) 安裝 chkrootkit:
# tar zxvf chkrootkit.tar.gz
# cd chkrootkit-0.35
# make sense
3) 設定 chkrootkit:
# vi /etc/cron.daily/chkrootkit
#!/bin/bash
echo
echo "checking rootkit..."
/root/chkrootkit-0.35/chkrootkit
echo
echo
# chmod +x /etc/cron.daily/chkrootkit
# /etc/cron.daily/chkrootkit
* 提示: 該結果每天會 email 到 root 的信箱。

十﹑套件更新

1) 架設 mirror:
(說明一下: 之所以使用 mirror ﹐是針對單一 LAN 環境中﹐
同時有多台主機需要 Upgrade 的情況下使用。
雖然第一次 mirror 比較吃頻寬﹐但長期而言﹐
每次 upgrade 事實上只有一次對外頻寬的需求而已﹐
其它的機器集中到 local server 上抓取就是了。
)
# mkdir /usr/local/mirror
# cd /usr/local/mirror
# wget http://www.study-area.org/linux/src/mirror-2.9.tar.gz
# tar zxvf mirror-2.9.tar.gz
# vi packages/redhat73
package=redhat73
comment=Updates for Redhat Linux 7.3
site=linux.sinica.edu.tw
remote_dir=/redhat/updates/7.3
local_dir=/var/www/html/apt/redhat/updates/7.3
# vi /etc/cron.daily/mirror
#!/bin/bash
echo
perl /usr/local/mirror/mirror.pl /usr/local/mirror/packages/redhat73
echo
echo
# chmod +x /etc/cron.daily/mirror
# /etc/cron.daily/mirror
** 注意: 在進行 mirror 之前﹐請儘可能禮貌性知會對方的管理員。
2) 架設 APT SERVER:
# cd /usr/local/src
# wget ftp://cle.linux.org.tw/pub/CLE/i386/RedHat/RPMS/apt*.rpm
# rpm -ivh apt-*.rpm
# cd /var/www/html/apt
# ln -s redhat/updates/7.3/en/os/SRPMS SRPMS.73
# cd redhat/
# ln -s updates/7.3/en/os/i386 RPMS.73
# mkdir base
# sh /usr/bin/genbasedir /var/www/html/apt
# sh /usr/bin/genbasedir /var/www/html/apt/redhat
** 注意: 測試成功後﹐將上兩行命令增加至 /etc/cron.daily/mirror 中。
也就是接在那兩行 perl 句子後面就可以了。
這樣才能順利更新 APT server 裡面的 package list 。

3) 安裝 APT CLIENT:
# cd /usr/local/src
# wget ftp://cle.linux.org.tw/pub/CLE/i386/RedHat/RPMS/apt*.rpm
# rpm -ivh apt*.rpm
# mv /etc/apt/sources.list /etc/apt/sources.list.bak
# vi /etc/apt/sources.list
rpm http://ADDRESS.OF.YOUR-APT-SERVER/apt redhat 73
rpm-src http://ADDRESS.OF.YOUR-APT-SERVER/apt redhat 73

** 註:若您並沒建置自己的 APT server ,將 /etc/apt/source.list 換成如下則可:
rpm ftp://linux.nctu.edu.tw/dists/redhat/apt i386 redhat-7.3-updates
rpm-src ftp://linux.nctu.edu.tw/dists/redhat/apt i386 redhat-7.3-updates
rpm ftp://linux.nctu.edu.tw/dists/redhat/apt i386 redhat-7.3
rpm-src ftp://linux.nctu.edu.tw/dists/redhat/apt i386 redhat-7.3

4) 執行 APT:
# apt-get update
# atp-get check
如果要更新全部套件:
# apt-get upgrade
如果是單獨安裝某套件(例如 my-package):
# apt-get install my-pakage
如果是取得某套件之 source rpm (例如 my-package):
# apt-get source my-pakage
如果想每天自動更新全部套件:
# vi /etc/cron.daily/apt-get
#!/bin/bash
apt_file=/root/apt_file
echo
/usr/bin/apt-get update
/usr/bin/apt-get check
/usr/bin/apt-get upgrade -y
echo "Checking new rpm..."
find / -name "*.rpmnew" -o -name "*.rpmsave" > $apt_file.new
diff $apt_file $apt_file.new || {
echo "APT-WARNING: new rpm files found!"
mv $apt_file.new $apt_file
}
echo
echo

# chmod +x /etc/cron.daily/apt-get
** 提示: 以上介紹的 apt-get 命令﹐在有問題的時候﹐不妨加上 -f 參數來修正。

5) 關於 CLE
如果網路環境中只有極少數的系統需要更新﹐並且都是 CLE 版本的話﹐
不妨參考如下兩篇文章:
http://cle.linux.org.tw/~candyz/index.htm
ftp://cle.linux.org.tw/pub/CLE/APT-HOWTO_CLE.txt
(最後一篇文章﹐在不需要架設 server 的情況下﹐只需參考 client 端設定就可以了。)