亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

如何保證Linux服務(wù)器的安全

瀏覽:135日期:2024-07-27 08:17:49

很少見(jiàn)有人馬上為一臺(tái)新安裝的服務(wù)器做安全措施,然而我們生活所在的這個(gè)社會(huì)使得這件事情是必要的。不過(guò)為什么仍舊這么多人把它拖在最后?我已經(jīng)做了相同的事情,它常常可以歸結(jié)為想要馬上進(jìn)入有趣的東西。希望這篇文章將向大家展示,確保服務(wù)器安全沒(méi)有你想得那樣難。在攻擊開(kāi)始后,俯瞰你的“堡壘”會(huì)相當(dāng)享受的。

如何保證Linux服務(wù)器的安全

這篇文章為Ubuntu 12.04.2 LTS而寫,你也可以在任何其他Linux分發(fā)版上做相同的事情。

我從哪兒開(kāi)始?

如果服務(wù)器已經(jīng)有了一個(gè)公有IP,你會(huì)希望立即鎖定 root 訪問(wèn)。事實(shí)上,你得鎖定整個(gè)ssh訪問(wèn),并確保只有你可以訪問(wèn)。增加一個(gè)新用戶,把它加入admin組(在/etc/sudoers預(yù)配置以擁有sudo訪問(wèn)權(quán)限)。

$ sudo addgroup adminAdding group ’admin’ (GID 1001)Done.$ sudo adduser spenserjAdding user `spenserj’ ...Adding new group `spenserj’ (1002) ...Adding new user `spenserj’ (1001) with group `spenserj’ ...Creating home directory `/home/spenserj’ ...Copying files from `/etc/skel’ ...Enter new UNIX password:Retype new UNIX password:passwd: password updated successfullyChanging the user information for spenserjEnter the new value, or press ENTER for the default Full Name []: Spenser Jones Room Number []: Work Phone []: Home Phone []: Other []:Is the information correct? [Y/n] y$ sudo usermod -a -G admin spenserj

你也將希望在你電腦上創(chuàng)建一個(gè)私有key,并且在服務(wù)器上禁用討厭的密碼驗(yàn)證。

$ mkdir ~/.ssh$ echo 'ssh-rsa [your public key]' > ~/.ssh/authorized_keys

/etc/ssh/sshd_config

PermitRootLogin noPermitEmptyPasswords noPasswordAuthentication noAllowUsers spenserj

重新加載SSH,使用修改生效,之后嘗試在一個(gè)新會(huì)話中登陸來(lái)確保所有事情正常工作。如果你不能登陸,你將仍然擁有你的原始會(huì)話來(lái)做修改。

$ sudo service ssh restartssh stop/waitingssh start/running, process 1599更新服務(wù)器

既然你是訪問(wèn)服務(wù)器的唯一用戶,你就不用擔(dān)心黑客鬼鬼祟祟進(jìn)入,再次正常呼吸。當(dāng)有一些針對(duì)你服務(wù)器的更新時(shí),正是修補(bǔ)的機(jī)會(huì),所以動(dòng)手吧,就現(xiàn)在。

$ sudo apt-get update...Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en_CAHit http://ca.archive.ubuntu.com precise-updates/universe Translation-enHit http://ca.archive.ubuntu.com precise-backports/main Translation-enHit http://ca.archive.ubuntu.com precise-backports/multiverse Translation-enHit http://ca.archive.ubuntu.com precise-backports/restricted Translation-enHit http://ca.archive.ubuntu.com precise-backports/universe Translation-enFetched 3,285 kB in 5s (573 kB/s)Reading package lists... Done$ sudo apt-get upgradeReading package lists... DoneBuilding dependency treeReading state information... DoneThe following packages have been kept back: linux-headers-generic-lts-quantal linux-image-generic-lts-quantalThe following packages will be upgraded: accountsservice apport apt apt-transport-https apt-utils aptitude bash ...73 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.Need to get 61.0 MB of archives.After this operation, 151 kB of additional disk space will be used.Do you want to continue [Y/n]? Y...Setting up libisc83 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up libdns81 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up libisccc80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up libisccfg82 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up libbind9-80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up liblwres80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up bind9-host (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up dnsutils (1:9.8.1.dfsg.P1-4ubuntu0.6) ...Setting up iptables (1.4.12-1ubuntu5) ......安裝防火墻

安裝現(xiàn)在正最流行的防火墻軟件?好,行動(dòng)吧。那就配置一個(gè)防火墻。之后你總是可以增加另一個(gè)異常,幾分鐘額外的工作并不會(huì)折騰死你。Iptables在Ubuntu里預(yù)裝了,所以去設(shè)置一些規(guī)則吧。

$ sudo mkdir /etc/iptables

/etc/iptables/rules

*filter:INPUT DROP [0:0]:FORWARD DROP [0:0]:OUTPUT DROP [0:0]# Accept any related or established connections-I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT-I OUTPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT# Allow all traffic on the loopback interface-A INPUT -i lo -j ACCEPT-A OUTPUT -o lo -j ACCEPT# Allow outbound DHCP request - Some hosts (Linode) automatically assign the primary IP#-A OUTPUT -p udp --dport 67:68 --sport 67:68 -j ACCEPT# Outbound DNS lookups-A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT# Outbound PING requests-A OUTPUT -p icmp -j ACCEPT# Outbound Network Time Protocol (NTP) request-A OUTPUT -p udp --dport 123 --sport 123 -j ACCEPT# SSH-A INPUT -i eth0 -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT# Outbound HTTP-A OUTPUT -o eth0 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT-A OUTPUT -o eth0 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPTCOMMIT

通過(guò) iptables-apply 命令為規(guī)則集生效。如果你丟失連接,修補(bǔ)你的規(guī)則,在繼續(xù)之前再試一下

$ sudo iptables-apply /etc/iptables/rulesApplying new ruleset... done.Can you establish NEW connections to the machine? (y/N) y... then my job is done. See you next time.

創(chuàng)建文件 /etc/network/if-pre-up.d/iptables,然后寫入下面內(nèi)容。當(dāng)你啟動(dòng)服務(wù)器的時(shí)候,將自動(dòng)載入你的iptables規(guī)則。

/etc/network/if-pre-up.d/iptables

#!/bin/shiptables-restore < /etc/iptables/rules

現(xiàn)在給它執(zhí)行權(quán)限,執(zhí)行文件,以確保它正常載入

$ sudo chmod +x /etc/network/if-pre-up.d/iptables$ sudo /etc/network/if-pre-up.d/iptables用 Fail2ban 處理潛在黑客

當(dāng)談到安全的時(shí),F(xiàn)ail2ban 是我最喜歡的工具之一,它將監(jiān)控你的日志文件,并且可以臨時(shí)禁止那些正在濫用你資源,或者正在強(qiáng)制肆虐你的SSH連接,或者正在dos攻擊你web服務(wù)器的用戶。

Install Fail2ban

$ sudo apt-get install fail2ban[sudo] password for sjones:Reading package lists... DoneBuilding dependency treeReading state information... DoneThe following extra packages will be installed: gamin libgamin0 python-central python-gamin python-support whoisSuggested packages: mailxThe following NEW packages will be installed: fail2ban gamin libgamin0 python-central python-gamin python-support whois0 upgraded, 7 newly installed, 0 to remove and 2 not upgraded.Need to get 254 kB of archives.After this operation, 1,381 kB of additional disk space will be used.Do you want to continue [Y/n]? y...

雖然 Fail2ban 安裝一個(gè)默認(rèn)配置(/etc/fail2ban/jail.conf),但我們希望在 /etc/fail2ban/jail.local 寫配置,所以把它拷貝到那兒。

sudo cp /etc/fail2ban/jail.{conf,local}配置

把 ignoreip 行修改為你的ip,并且可以設(shè)置禁止惡意用戶的時(shí)間量(默認(rèn)是10分鐘)。你也將希望設(shè)置一個(gè)destemail,這里我通常輸入我自已的email地址,再在后面加上 ,fail2ban@blocklist.de 。BlockList.de 是一個(gè)跟蹤并且自動(dòng)報(bào)告黑客IP的系統(tǒng)。

/etc/fail2ban/jail.local

[DEFAULT]# 'ignoreip' can be an IP address, a CIDR mask or a DNS hostignoreip = 127.0.0.1/8bantime = 600maxretry = 3# 'backend' specifies the backend used to get files modification. Available# options are 'gamin', 'polling' and 'auto'.# yoh: For some reason Debian shipped python-gamin didn’t work as expected# This issue left ToDo, so polling is default backend for nowbackend = auto## Destination email address used solely for the interpolations in# jail.{conf,local} configuration files.destemail = root@localhost,fail2ban@blocklist.de

這有一些其他的你想檢查的配置,盡管缺省配置已經(jīng)相當(dāng)不錯(cuò)了,所以,快速瀏覽這些,直到你讀到Actions章節(jié)。

Actions

Actions 允許你對(duì)惡意行為作出反應(yīng),然而當(dāng)我們想要它禁止和發(fā)郵件的時(shí)候,默認(rèn)是禁用了 iptables。值得感謝的是,有一個(gè)預(yù)配置文件 action_wml,它恰恰是做這個(gè)的。

/etc/fail2ban/jail.local

# Choose default action. To change, just override value of ’action’ with the# interpolation to the chosen action shortcut (e.g. action_mw, action_mwl, etc) in jail.local# globally (section [DEFAULT]) or per specific sectionaction = %(action_mwl)sJails 監(jiān)控

為了讓Fail2ban工作,需要了解要監(jiān)控哪些東西。這些已在Jails部分的配置文件,并且這有一些預(yù)載入而未啟用的例子。既然到目前為止,你僅僅在服務(wù)器上啟用了SSH訪問(wèn),那我們就只啟用SSH和SSH-DDos 監(jiān)控,然而你還是會(huì)想給安裝在這臺(tái)服務(wù)器上的公共訪問(wèn)服務(wù)增加新的監(jiān)控。

/etc/fail2ban/jail.local

[ssh]enabled = trueport = sshfilter = sshdlogpath = /var/log/auth.logmaxretry = 6[ssh-ddos]enabled = trueport = sshfilter = sshd-ddoslogpath = /var/log/auth.logmaxretry = 6應(yīng)用改變

既然我們已經(jīng)配置了Fail2ban,你將希望重新載入它,并且確保向iptables增加了合適的規(guī)則。

$ sudo service fail2ban restart * Restarting authentication failure monitor fail2ban ...done.$ sudo iptables -LChain INPUT (policy DROP)target prot opt source destinationfail2ban-ssh-ddos tcp -- anywhere anywhere multiport dports sshfail2ban-ssh tcp -- anywhere anywhere multiport dports ssh...Chain fail2ban-ssh (1 references)target prot opt source destinationRETURN all -- anywhere anywhereChain fail2ban-ssh-ddos (1 references)target prot opt source destinationRETURN all -- anywhere anywhere

在任何時(shí)間,你都可以使用sudo iptables -L 來(lái)列出你的規(guī)則,隨后列出所有當(dāng)前禁止的 IP。此時(shí),F(xiàn)ail2ban正在處理兩個(gè)惡意的用戶。

Banned IPs

DROP all -- 204.50.33.22 anywhereDROP all -- 195.128.126.114 anywhere保持最新更新

你可能現(xiàn)在擁有一個(gè)已經(jīng)鎖定并且準(zhǔn)備投入使用的服務(wù)器,然而這并不是你安全之旅的終點(diǎn)。保持最新更新(并且總是首先在非產(chǎn)品環(huán)境下測(cè)試),總是關(guān)閉你不需要的端口,定期檢查你的日志,并且由內(nèi)而外了解你的服務(wù)器。

HackerNews 上的討論

我的這篇文章,在 HackerNews 上有一些很好的評(píng)論,如果你對(duì)不同觀點(diǎn)和更好的安全性感興趣的話,我建議你去看看。這篇文章目的是作為服務(wù)器安全的新手指南,在這篇文章結(jié)束的時(shí)候,并不意味著你的服務(wù)器是無(wú)懈可擊的。用本文來(lái)快速鎖定一個(gè)新服務(wù)器,在它之上為你特有的情況建立其他措施。你可能希望查詢 IPV6 安全,改變你的SSH端口(通過(guò)隱藏達(dá)到安全目的),安全內(nèi)核(SELinux和GRSecurity),跟蹤系統(tǒng)改變,并且如果你的服務(wù)器曾經(jīng)不安全或已經(jīng)在線相當(dāng)長(zhǎng)時(shí)間了的話,全面檢查一番。一臺(tái)服務(wù)器有好幾百個(gè)入口點(diǎn),并且每一個(gè)你安裝的應(yīng)用都帶來(lái)了額外的潛在漏洞,但是通過(guò)合適的工具,你可以免去困擾,直接去睡大覺(jué)了。

原文鏈接: Spenser Jones 翻譯: 伯樂(lè)在線 - 伯樂(lè)在線讀者

標(biāo)簽: Linux系統(tǒng)
相關(guān)文章:
主站蜘蛛池模板: 成 人 免费 网站 | 欧美成人精品手机在线观看 | 67194午夜| 色青五月天| 国产人成免费视频 | 久久精品视频99 | 国产精品久久久一区二区三区 | 粉嫩高中生的第一次在线观看 | 国产成人高清亚洲一区91 | 国产一级久久免费特黄 | 一级全免费视频播放 | 91色久 | 精品一区二区三区免费视频 | 高清毛片aaaaaaaaa片 | 久久香蕉国产线看观看精品yw | 日韩三级在线免费观看 | 国产成人高清一区二区私人 | 久久99久久精品久久久久久 | 国产精品yjizz视频网一二区 | 一级毛片国产 | 久久国产精品久久国产片 | 亚洲国产精品一区二区首页 | a毛片免费看 | 国产欧美精品综合一区 | 精品 日韩 国产 欧美在线观看 | 波多野结衣在线观看一区二区 | 亚洲综合无码一区二区 | 欧美人一级淫片a免费播放 欧美人与z0z0xxxx | 免费特黄一区二区三区视频一 | 一道本久久 | 日韩欧美一区二区中文字幕 | 日韩一级片网址 | 韩国毛片视频 | 免费的三级毛片 | 日韩三级小视频 | 在线亚洲精品国产波多野结衣 | 久草在线最新 | 国产成人亚洲综合91精品555 | 久久亚洲国产成人影院 | 欧美二级在线观看免费 | 在线观看毛片视频 |