如何保证Linux服务器的安全

  很少见有人马上为一台新安装的服务器做安全措施,然而我们生活所在的这个社会使得这件事情是必要的。不过为什么仍旧这么多人把它拖在最后?我已经做了相同的事情,它常常可以归结为想要马上进入有趣的东西。希望这篇文章将向大家展示,确保服务器安全没有你想得那样难。在攻击开始后,俯瞰你的“堡垒”会相当享受的。

  这篇文章为Ubuntu 12.04.2 LTS而写,你也可以在任何其他Linux分发版上做相同的事情。

  我从哪儿开始?

  如果服务器已经有了一个公有IP,你会希望立即锁定 root 访问。事实上,你得锁定整个ssh访问,并确保只有你可以访问。增加一个新用户,把它加入admin组(在/etc/sudoers预配置以拥有sudo访问权限)。

$ sudo addgroup admin
Adding group 'admin' (GID 1001)
Done.

$ sudo adduser spenserj
Adding 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 successfully
Changing the user information for spenserj
Enter 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

  你也将希望在你电脑上创建一个私有key,并且在服务器上禁用讨厌的密码验证。

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

  /etc/ssh/sshd_config

PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
AllowUsers spenserj

  重新加载SSH,使用修改生效,之后尝试在一个新会话中登陆来确保所有事情正常工作。如果你不能登陆,你将仍然拥有你的原始会话来做修改。

$ sudo service ssh restart
ssh stop/waiting
ssh start/running,process 1599

  更新服务器

  既然你是访问服务器的唯一用户,你就不用担心黑客鬼鬼祟祟进入,再次正常呼吸。当有一些针对你服务器的更新时,正是修补的机会,所以动手吧,就现在。

$ sudo apt-get update
...
Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en_CA
Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/main Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/multiverse Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/restricted Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/universe Translation-en
Fetched 3,285 kB in 5s (573 kB/s)
Reading package lists... Done

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages have been kept back:
  linux-headers-generic-lts-quantal linux-image-generic-lts-quantal
The 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) ...
...

  安装防火墙

  安装现在正最流行的防火墙软件?好,行动吧。那就配置一个防火墙。之后你总是可以增加另一个异常,几分钟额外的工作并不会折腾死你。Iptables在Ubuntu里预装了,所以去设置一些规则吧。

$ 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 ACCEPT

COMMIT

  通过 iptables-apply 命令为规则集生效。如果你丢失连接,修补你的规则,在继续之前再试一下

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

  创建文件 /etc/network/if-pre-up.d/iptables,然后写入下面内容。当你启动服务器的时候,将自动载入你的iptables规则。

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

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

  现在给它执行权限,执行文件,以确保它正常载入

$ sudo chmod +x /etc/network/if-pre-up.d/iptables
$ sudo /etc/network/if-pre-up.d/iptables

  用 Fail2ban 处理潜在黑客

  当谈到安全的时,Fail2ban 是我最喜欢的工具之一,它将监控你的日志文件,并且可以临时禁止那些正在滥用你资源,或者正在强制肆虐你的SSH连接,或者正在dos攻击你web服务器的用户。

  Install Fail2ban

$ sudo apt-get install fail2ban
[sudo] password for sjones:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  gamin libgamin0 python-central python-gamin python-support whois
Suggested packages:
  mailx
The following NEW packages will be installed:
  fail2ban gamin libgamin0 python-central python-gamin python-support whois
0 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 安装一个默认配置(/etc/fail2ban/jail.conf),但我们希望在 /etc/fail2ban/jail.local 写配置,所以把它拷贝到那儿。

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

  配置

  把 ignoreip 行修改为你的ip,并且可以设置禁止恶意用户的时间量(默认是10分钟)。你也将希望设置一个destemail,这里我通常输入我自已的email地址,再在后面加上 ,fail2ban@blocklist.deBlockList.de 是一个跟踪并且自动报告黑客IP的系统。

  /etc/fail2ban/jail.local

[DEFAULT]

# ignoreip can be an IP address,a CIDR mask or a DNS host
ignoreip = 127.0.0.1/8
bantime  = 600
maxretry = 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 now
backend = auto

#
# Destination email address used solely for the interpolations in
# jail.{conf,local} configuration files.
destemail = root@localhost,fail2ban@blocklist.de

  这有一些其他的你想检查的配置,尽管缺省配置已经相当不错了,所以,快速浏览这些,直到你读到Actions章节。

  Actions

  Actions 允许你对恶意行为作出反应,然而当我们想要它禁止和发邮件的时候,默认是禁用了 iptables。值得感谢的是,有一个预配置文件 action_wml,它恰恰是做这个的。

  /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 section
action = %(action_mwl)s

  Jails 监控

  为了让Fail2ban工作,需要了解要监控哪些东西。这些已在Jails部分的配置文件,并且这有一些预载入而未启用的例子。既然到目前为止,你仅仅在服务器上启用了SSH访问,那我们就只启用SSH和SSH-DDos 监控,然而你还是会想给安装在这台服务器上的公共访问服务增加新的监控。

  /etc/fail2ban/jail.local

[ssh]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 6
[ssh-ddos]
enabled  = true
port     = ssh
filter   = sshd-ddos
logpath  = /var/log/auth.log
maxretry = 6

  应用改变

  既然我们已经配置了Fail2ban,你将希望重新载入它,并且确保向iptables增加了合适的规则。

$ sudo service fail2ban restart
 * Restarting authentication failure monitor fail2ban
   ...done.

$ sudo iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
fail2ban-ssh-ddos  tcp  --  anywhere             anywhere             multiport dports ssh
fail2ban-ssh  tcp  --  anywhere             anywhere             multiport dports ssh
...
Chain fail2ban-ssh (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

Chain fail2ban-ssh-ddos (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

  在任何时间,你都可以使用sudo iptables -L 来列出你的规则,随后列出所有当前禁止的 IP。此时,Fail2ban正在处理两个恶意的用户。

  Banned IPs

DROP       all  --  204.50.33.22         anywhere
DROP       all  --  195.128.126.114      anywhere

  保持最新更新

  你可能现在拥有一个已经锁定并且准备投入使用的服务器,然而这并不是你安全之旅的终点。保持最新更新(并且总是首先在非产品环境下测试),总是关闭你不需要的端口,定期检查你的日志,并且由内而外了解你的服务器。

  HackerNews 上的讨论

  我的这篇文章,在 HackerNews 上有一些很好的评论,如果你对不同观点和更好的安全性感兴趣的话,我建议你去看看。这篇文章目的是作为服务器安全的新手指南,在这篇文章结束的时候,并不意味着你的服务器是无懈可击的。用本文来快速锁定一个新服务器,在它之上为你特有的情况建立其他措施。你可能希望查询 IPV6 安全,改变你的SSH端口(通过隐藏达到安全目的),安全内核(SELinux和GRSecurity),跟踪系统改变,并且如果你的服务器曾经不安全或已经在线相当长时间了的话,全面检查一番。一台服务器有好几百个入口点,并且每一个你安装的应用都带来了额外的潜在漏洞,但是通过合适的工具,你可以免去困扰,直接去睡大觉了。

  原文链接: Spenser Jones   翻译: 伯乐在线 - 伯乐在线读者

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


每个HTTP请求和响应都会带有相应的头部信息。默认情况下,在发送XHR请求的同时,还会发送下列头部信息: Accept:浏览器能够处理的内容类型 Accept-Charset:浏览器能够显示的字符集
&quot;Markdown自动生成目录&quot; &quot;使用npm语法生成&quot; &quot;1、安装npm&quot; &quot;2、安装doctoc插件&quot; &quot;
当我们从客户端向服务器发送请求时&#160;服务器向我们返回状态码&#160;状态码就是告诉我们服务器响应的状态&#160;通过它,我们就可以知道当前请求是成功了还是出现了什么问题&#160;状态码是
原理 介绍 哈希表(Hash table,也叫散列表), 是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映
一 共享秘钥 1.1 概念 共享秘钥和我们生活中同一把锁的钥匙概念类似,对同一把锁来说,加锁时使用什么钥匙,解锁也必须使用同样的钥匙。 1.2 共享秘钥在HTTP传输中的缺点 以共享密钥方式加密时必须
正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单的说,我是一个用户,我访问不了某网站,但是我能访问一个代理服务器这个代理服务器呢,他能访问那个我不能访问的网站于是我先连上代
如果你是网站的开发者或维护者,就不得不重视盗链的问题了。如果你刚刚开发完一个没有防盗链的带有文件下载功能的网站,挂上internet,然后上传几个时下非常热门的软件或电影并在网站内公布下载地址,让MS
select,poll,epoll区别总结 select,poll,epoll都是I/O多路复用。I/O多路复用就是通过一种机制,可以监测多个描述符,一旦某个描述就绪(一般是读或者写),能够通知程序进
PS: https就是http和TCP之间有一层SSL层,这一层的实际作用是防止钓鱼和加密。防止钓鱼通过网站的证书,网站必须有CA证书,证书类似于一个解密的签名。另外是加密,加密需要一个密钥交换算法,
一、什么是http协议 HTTP是一个应用层协议,无状态的,端口号为80。主要的版本有1.0/1.1/2.0. HTTP/1.* 一次请求-响应,建立一个连接,用完关闭; HTTP/1.1 串行化单线
host文件的工作原理及应用 Hosts文件是一个用于存储计算机网络中节点信息的文件,它可以将主机名映射到相应的IP地址,实现DNS的功能,它可以由计算机的用户进行控制。 一、Hosts文件基本介绍
HTTP 2.0是在SPDY(An experimental protocol for a faster web, The Chromium Projects)基础上形成的下一代互联网通信协议。HTT
虚拟地址和物理地址 第一层理解 1、每个进程都有自己独立的4g内存空间,每个进程的内存空间都具有类似的结构。 2、一个新进程建立的时候,将会建立自己的内存空间,此进程的数据,代码等数据从磁盘拷贝到自己
0x00 前言 发现自己学习python已经有半个月了,也开发了自己的一些渗透的小脚本,但觉得还是不够,我个人觉得工具和脚本还有框架是个本质上的区别。脚本的话,不会考虑到其他的一些因素,例如报错和交互
0x00 前言 由于昨天520,今天又是521,我被朋友圈和qq空间给刷屏了,都在秀对象。一气之下决定把我上次写的nc拿出来使用类进行重构,多实例化几个对象,这下子我也有对象了。 0x01 一些小插曲
upload labs通关 0x00 前言 这段时间一直在忙,没时间来更新文章,这里就写篇upload labs的通关手册吧,现在包括网上也有很多upload通关手册,但是在这里还是想自己去写一篇,来
0x00 前言 介于这段时间比较忙,所以博客的更新也比较慢。本来想前几天就发这个mssql数据库的,但是因为mssql的结构比较复杂,利用方式也比较多,所以又去深入研究了一下mssql的数据库结构和各
0x00 了解数据库 数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。 数据库是以一定方式储存在一起、能与多个用户共享、
0x00 前言 现在access的站,比较少,有的话也是小型网站在用,因为access的性能比较差,多人访问都能卡死,所以很多网站都很少会采用access的数据库搭建。但是该学的我们还是得学。 0x0
记一次某企业实战 0x00 前言 近段时间来也没怎么更新过博客,在这里就来水篇文章吧。 前段时间一直在做项目,也来分享并且记录一下自己的一些成果,和一些小思路。 0x01 信息收集 渗透的第一步肯定是