Linux 安全快速入门指南 | ||
---|---|---|
上一页 |
让我们快速、非技术性地了解一些网络概念,以及它们可能如何影响我们的安全。我们不需要了解太多关于网络的知识,但对事物如何运作有一个大致的了解肯定有助于我们处理防火墙和其他相关问题。
您可能已经注意到,Linux 是一个非常面向网络的操作系统。很多操作都是通过连接到各种类型的 “服务器” 来完成的——X 服务器、字体服务器、打印服务器等等。
我们可以从 netstat 输出的这一行摘录中看到两台计算机之间的典型连接
tcp 30 0 169.254.179.139:1359 18.29.1.67:21 CLOSE_WAIT |
总结一下,使用上面的例子,我们有客户端(我)连接到服务器(rpmfind.net),连接由两端的相应端口定义和控制。数据通过数据包传输和控制。服务器正在使用 “特权” 端口(即端口号低于 1024 的端口),该端口保持打开状态,监听连接。“非特权” 端口由我的客户端应用程序在我这一端使用,它是临时的,仅在连接期间打开,并且仅响应另一端服务器的端口。通常来说,这种类型的端口不易受到攻击或入侵。服务器的端口是易受攻击的,因为它保持打开状态。FTP 服务器的管理员需要采取适当的预防措施来确保其服务器的安全。其他互联网连接,例如与 Web 服务器或邮件服务器的连接,其工作原理与上述示例类似,尽管服务器端口不同。SMTP 邮件服务器使用端口 25,而 Web 服务器通常使用端口 80。有关其他常用端口和服务,请参阅 端口部分。
关于端口的另一点:只有在该端口上有东西监听时,端口才是可访问的。如果没有任何服务或守护程序在那里监听,准备处理传入的连接请求,则没有人可以强制打开端口。关闭的端口是完全安全的端口。
关于客户端和服务器之间区别的最后一点。上面的示例在LISTENER部分的 netstat 示例中没有 telnet 或 ftp 服务器。换句话说,没有这样的服务器在本地运行。您不需要运行 telnet 或 ftp 服务器守护程序才能连接到别人的 telnet 或 ftp 服务器。这些仅用于为那些将要连接到您的人提供这些服务。在大多数情况下,您并不真的想这样做。这绝不会影响使用 telnet 和 ftp 客户端软件的能力。
快速浏览一些常见的和常用的端口,以及常用的关联服务名称和风险系数。所有端口都存在一些风险。只是有些端口历史上比其他端口有更多的漏洞利用。这就是下面评估它们的方式,不一定被解释为任何给定服务是否安全。
1-19,各种协议,其中许多协议已经过时,并且现代系统可能都不需要。如果您不知道这些协议中的任何一个是什么,那么您肯定不需要它们。echo 服务(端口 7)不应与常用的 ping 程序混淆。关闭所有这些端口。 |
20 - FTP-DATA。“主动” FTP 连接使用两个端口:21 是控制端口,20 是数据通过的端口。被动 FTP 根本不使用端口 20。低风险,但请参见下文。 |
21 - FTP 服务器端口,也称为文件传输协议。一种用于在系统之间传输文件的根深蒂固的协议。非常高风险,可能是头号破解目标。 |
22 - SSH(安全外壳),或有时是 PCAnywhere。低到中等风险(是的,即使是针对所谓的 “安全” 服务也存在漏洞利用)。 |
23 - Telnet 服务器。仅供局域网使用。在非安全环境中使用 ssh 代替。中等风险。 |
25 - SMTP,简单邮件传输协议,或邮件服务器端口,用于发送外发邮件,以及在不同地方之间传输邮件。中等风险。这在漏洞利用方面有着糟糕的历史,但最近有所改善。 |
37 - 时间服务。这是内置的 inetd 时间服务。低风险。仅供局域网使用。 |
53 - DNS,或域名服务器端口。名称服务器监听此端口,并回答将主机名解析为 IP 地址的查询。高风险。 |
67 (UDP) - BOOTP 或 DHCP 服务器端口。低风险。如果在您的局域网上使用 DHCP,则不需要将其暴露于互联网。 |
68 (UDP) - BOOTP 或 DHCP 客户端端口。低风险。 |
69 - tftp 或简单文件传输协议。极其不安全。仅限局域网,如果真的、真的需要。 |
79 - Finger,用于提供有关系统和登录用户的信息。作为破解目标风险较低,但泄露的信息太多,不应运行。 |
80 - WWW 或 HTTP 标准 Web 服务器端口。互联网上最常用的服务。低风险。 |
98 - Linuxconf Web 访问管理端口。仅限局域网,如果真的需要。 |
110 - POP3,也称为邮局协议,邮件服务器端口。POP 邮件是用户从远程系统检索的邮件。低风险。 |
111 - sunrpc(Sun 远程过程调用)或 portmapper 端口。由 NFS(网络文件系统)、NIS(网络信息服务)和各种相关服务使用。听起来很危险,而且风险很高。仅限局域网使用。最喜欢的破解目标。 |
113 - identd 或 auth 服务器端口。由某些旧式服务(如 SMTP 和 IRC)使用,有时是必需的,以验证连接。在大多数情况下可能不需要。低风险,但可能会向攻击者提供太多关于您系统的信息。 |
119 -- nntp 或新闻服务器端口。低风险。 |
123 - 网络时间协议,用于与需要高精度的时间服务器同步。低风险,但大多数用户可能不需要。rdate 提供了一种更简单、更安全的方式来更新系统时钟。然后 inetd 内置的时间服务是同步局域网系统的另一种选择。 |
137-139 - NetBios (SMB) 服务。主要是 Windows 的东西。在 Linux 上风险较低,但仅限局域网使用。137 是一个非常常见的端口尝试。来自 Redmond 的一个相当令人讨厌的协议,会产生很多 “噪音”,其中大部分是无害的。 |
143 - IMAP,临时邮件访问协议。另一种邮件检索协议。低到中等风险。 |
161 - SNMP,简单网络管理协议。更常用于路由器和交换机中,以监控统计信息和重要指标。我们大多数人不需要,风险也较低。 |
177 - XDMCP,X 显示管理控制协议,用于远程连接到 X 服务器。低风险,但建议仅限局域网。 |
443 - HTTPS,一种相当广泛使用的安全 HTTP (WWW) 协议。低风险。 |
465 - SMTP over SSL,安全邮件服务器协议。低风险。 |
512 (TCP) - exec 是它在 netstat 中显示的方式。实际上,正确的名称是 rexec,用于远程执行。听起来很危险,而且确实如此。高风险,如果可以的话,仅限局域网。 |
512 (UDP) - biff,邮件通知协议。低风险,仅限局域网。 |
513 - login,实际上是 rlogin,也称为远程登录。与我们每次登录时使用的标准 /bin/login 无关。听起来很危险,而且确实如此。高风险,如果真的需要,仅限局域网。 |
514 (TCP) - shell 是昵称,也是 netstat 显示它的方式。实际上,rsh 是 “远程 Shell” 的应用程序。像所有 “r” 命令一样,这是对更友好、更温和时代的追溯。非常不安全,因此风险很高,并且仅限局域网使用,如果可以的话。 |
514 (UDP) - syslog 守护程序端口,仅用于远程日志记录目的。普通用户不需要这个。可能风险较低,但如果真的需要,绝对仅限局域网。 |
515 - lp 或打印服务器端口。高风险,当然仅限局域网。世界另一端的人不想将您的打印机用于其预期目的! |
587 - MSA 或 “提交”,邮件提交代理协议。大多数 MTA(邮件服务器)支持的新邮件处理协议。低风险。 |
631 - CUPS(打印守护程序)Web 管理端口。仅限局域网,低风险。 |
635 - mountd,NFS 的一部分。仅限局域网使用。 |
901 - SWAT,Samba Web 管理工具端口。仅限局域网。 |
993 - IMAP over SSL,安全 IMAP 邮件服务。非常低风险。 |
995 - POP over SSL,安全 POP 邮件服务。非常低风险。 |
1024 - 这是第一个 “非特权” 端口,由内核动态分配给请求它的任何应用程序。这几乎可以是任何东西。对于此端口之上的端口也是如此。 |
1080 - Socks 代理服务器。最喜欢的破解目标。 |
1243 - SubSeven 木马。仅 Windows 问题。 |
1433 - MS SQL 服务器端口。有时是目标。在 Linux 上不适用。 |
2049 - nfsd,网络文件服务守护程序端口。高风险,建议仅限局域网使用。 |
3128 - Squid 代理服务器端口。低风险,但对于大多数人来说应该仅限局域网。 |
3306 - MySQL 服务器端口。低风险,但对于大多数人来说应该仅限局域网。 |
5432 - PostgreSQL 服务器端口。仅限局域网,风险相对较低。 |
5631 (TCP), 5632 (UDP) - PCAnywhere 端口。仅限 Windows。PCAnywhere 可能非常 “嘈杂”,并广播广泛的地址范围。 |
6000 - X11 TCP 端口,用于远程连接。低到中等风险,但同样,这应该仅限局域网。实际上,这可以包括端口 6000-6009,因为 X 可以支持多个显示器,并且每个显示器都有自己的端口。ssh 的 X11Forwarding 将开始使用 6010 端口。 |
6346 - gnutella。 |
6667 - ircd,互联网中继聊天守护程序。 |
6699 - napster。 |
7100-7101 - 一些字体服务器使用这些端口。低风险,但仅限局域网。 |
8000 和 8080 - 常用 Web 缓存和代理服务器端口。仅限局域网。 |
10000 - webmin,一个基于 Web 的系统管理实用程序。目前风险较低。 |
27374 - SubSeven,一个常见的被探测的仅限 Windows 的木马。还有 1243。 |
31337 - Back Orifice,另一个常见的被探测的仅限 Windows 的木马。 |
更多服务和相应的端口号可以在 /etc/services 中找到。此外,“官方” 列表是 http://www.iana.org/assignments/port-numbers。
Robert Graham 对探测这些端口和其他端口可能意味着什么的精彩分析:http://www.linuxsecurity.com/resource_files/firewalls/firewall-seen.html。一个非常好的参考。
另一点,这些是标准端口指定。没有任何法律规定任何服务必须在特定端口上运行。通常它们是这样做的,但当然它们并不总是必须这样做。
只是提醒您,当您在防火墙日志中看到这些类型的端口时,不必过于紧张。如果您已按照上述步骤 1-3 操作,并验证了您的防火墙是否正常工作,则您是相当安全的。很多流量也可能是 “流弹” ——互联网背景噪音、配置错误的客户端或路由器、嘈杂的 Windows 内容等。
$ netstat -tua Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:printer *:* LISTEN tcp 0 0 bigcat:8000 *:* LISTEN tcp 0 0 *:time *:* LISTEN tcp 0 0 *:x11 *:* LISTEN tcp 0 0 *:http *:* LISTEN tcp 0 0 bigcat:domain *:* LISTEN tcp 0 0 bigcat:domain *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 *:631 *:* LISTEN tcp 0 0 *:smtp *:* LISTEN tcp 0 1 dsl-78-199-139.s:1174 64.152.100.93:nntp SYN_SENT tcp 0 1 dsl-78-199-139.s:1175 64.152.100.93:nntp SYN_SENT tcp 0 1 dsl-78-199-139.s:1173 64.152.100.93:nntp SYN_SENT tcp 0 0 dsl-78-199-139.s:1172 207.153.203.114:http ESTABLISHED tcp 1 0 dsl-78-199-139.s:1199 www.xodiax.com:http CLOSE_WAIT tcp 0 0 dsl-78-199-139.sd:http 63.236.92.144:34197 TIME_WAIT tcp 400 0 bigcat:1152 bigcat:8000 CLOSE_WAIT tcp 6648 0 bigcat:1162 bigcat:8000 CLOSE_WAIT tcp 553 0 bigcat:1164 bigcat:8000 CLOSE_WAIT udp 0 0 *:32768 *:* udp 0 0 bigcat:domain *:* udp 0 0 bigcat:domain *:* udp 0 0 *:631 *:* |
现在,使用 “-n” 标志进行相同的操作,以禁止转换为 “名称”,以便我们实际看到端口号
$ netstat -taun Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:515 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:37 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN tcp 0 0 192.168.1.1:53 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:631 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN tcp 0 1 169.254.179.139:1174 64.152.100.93:119 SYN_SENT tcp 0 1 169.254.179.139:1175 64.152.100.93:119 SYN_SENT tcp 0 1 169.254.179.139:1173 64.152.100.93:119 SYN_SENT tcp 0 0 169.254.179.139:1172 207.153.203.114:80 ESTABLISHED tcp 1 0 169.254.179.139:1199 216.26.129.136:80 CLOSE_WAIT tcp 0 0 169.254.179.139:80 63.236.92.144:34197 TIME_WAIT tcp 400 0 127.0.0.1:1152 127.0.0.1:8000 CLOSE_WAIT tcp 6648 0 127.0.0.1:1162 127.0.0.1:8000 CLOSE_WAIT tcp 553 0 127.0.0.1:1164 127.0.0.1:8000 CLOSE_WAIT udp 0 0 0.0.0.0:32768 0.0.0.0:* udp 0 0 192.168.1.1:53 0.0.0.0:* udp 0 0 127.0.0.1:53 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* |
tcp 0 0 0.0.0.0:515 0.0.0.0:* LISTEN |
“本地地址” 是0.0.0.0,这意味着可用的 “所有” 接口。本地端口是 515,或标准打印服务器端口,通常由 lpd 守护程序拥有。您可以在文件/etc/services.
tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN |
tcp 0 0 0.0.0.0:37 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN |
以上前两个绝对不是您希望任何人都能连接的服务类型。这些服务应该被防火墙隔离,以便拒绝所有外部连接。同样,我们无法从该输出中判断是否已部署任何防火墙,更不用说其有效实施程度了。
tcp 0 0 192.168.1.1:53 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN |
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:631 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN |
下一个分组是已建立的连接。对于我们的目的而言,最后一列指示的连接状态并不那么重要。手册页中对此有很好的解释。
tcp 0 1 169.254.179.139:1174 64.152.100.93:119 SYN_SENT tcp 0 1 169.254.179.139:1175 64.152.100.93:119 SYN_SENT tcp 0 1 169.254.179.139:1173 64.152.100.93:119 SYN_SENT tcp 0 0 169.254.179.139:1172 207.153.203.114:80 ESTABLISHED tcp 1 0 169.254.179.139:1199 216.26.129.136:80 CLOSE_WAIT tcp 0 0 169.254.179.139:80 63.236.92.144:34197 TIME_WAIT tcp 400 0 127.0.0.1:1152 127.0.0.1:8000 CLOSE_WAIT tcp 6648 0 127.0.0.1:1162 127.0.0.1:8000 CLOSE_WAIT tcp 553 0 127.0.0.1:1164 127.0.0.1:8000 CLOSE_WAIT |
由于我们给 netstat 提供了-t和-u选项,因此我们同时获得了 TCP 和 UDP 监听服务器。最后几行是 UDP 的
udp 0 0 0.0.0.0:32768 0.0.0.0:* udp 0 0 192.168.1.1:53 0.0.0.0:* udp 0 0 127.0.0.1:53 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* |
从以上章节中,我们已经了解了很多关于 bigcat 网络的信息。但是,假设我们看到一些我们不认识的东西,并想知道是什么启动了该特定服务?或者我们想停止特定的服务器,但从上面的输出中看不出来?
的-p选项应在最后一列中为我们提供进程的 PID 和启动该进程的程序名称。让我们再次查看 TCP 服务器(前三列为间距而裁剪)。我们将必须以 root 身份运行此命令才能获得所有可用信息
# netstat -tap Active Internet connections (servers and established) Local Address Foreign Address State PID/Program name *:printer *:* LISTEN 988/inetd bigcat:8000 *:* LISTEN 1064/junkbuster *:time *:* LISTEN 988/inetd *:x11 *:* LISTEN 1462/X *:http *:* LISTEN 1078/httpd bigcat:domain *:* LISTEN 956/named bigcat:domain *:* LISTEN 956/named *:ssh *:* LISTEN 972/sshd *:631 *:* LISTEN 1315/cupsd *:smtp *:* LISTEN 1051/master |
$ /bin/ps ax |grep 1051 |grep -v grep 1051 ? S 0:24 /usr/libexec/postfix/master |
我们在这里通过将 ps 与 grep 结合使用来走捷径。看起来该文件属于 postfix,它确实是一个可与 sendmail 媲美的邮件服务器软件包。
$ /bin/ps -axf 956 ? S 0:00 named -u named 957 ? S 0:00 \_ named -u named 958 ? S 0:46 \_ named -u named 959 ? S 0:47 \_ named -u named 960 ? S 0:00 \_ named -u named 961 ? S 0:11 \_ named -u named 1051 ? S 0:30 /usr/libexec/postfix/master 1703 ? S 0:00 \_ tlsmgr -l -t fifo -u -c 1704 ? S 0:00 \_ qmgr -l -t fifo -u -c 1955 ? S 0:00 \_ pickup -l -t fifo -c 1863 ? S 0:00 \_ trivial-rewrite -n rewrite -t unix -u -c 2043 ? S 0:00 \_ cleanup -t unix -u -c 2049 ? S 0:00 \_ local -t unix 2062 ? S 0:00 \_ smtpd -n smtp -t inet -u -c |
简写)运行可能有助于确定哪些进程是父进程或子进程或另一个进程。一个编辑过的例子
$ locate /master /etc/postfix/master.cf /var/spool/postfix/pid/master.pid /usr/libexec/postfix/master /usr/share/vim/syntax/master.vim /usr/share/vim/vim60z/syntax/master.vim /usr/share/doc/postfix-20010202/html/master.8.html /usr/share/doc/postfix-20010202/master.cf /usr/share/man/man8/master.8.gz |
如果所有这些都没有提供任何线索,我们也可以尝试 locate
$ find / -name master /usr/libexec/postfix/master |
find 可能是最灵活的文件查找实用程序,但不像 locate 那样使用数据库,因此速度慢得多
# lsof -i :631 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME cupsd 1315 root 0u IPv4 3734 TCP *:631 (LISTEN) |
如果安装了 lsof,它是另一个有用的命令,用于查找谁拥有进程或端口
# fuser -v -n tcp 631 USER PID ACCESS COMMAND 631/tcp root 1315 f.... cupsd |
这再次告诉我们 cupsd 打印守护程序是端口 631 的所有者。只是一种不同的获取方式。另一种获取方式是使用 fuser,应该已安装
# grep rpc.statd /etc/init.d/* /etc/init.d/nfslock: [ -x /sbin/rpc.statd ] || exit 0 /etc/init.d/nfslock: daemon rpc.statd /etc/init.d/nfslock: killproc rpc.statd /etc/init.d/nfslock: status rpc.statd /etc/init.d/nfslock: /sbin/pidof rpc.statd >/dev/null 2>&1; STATD="$?" |
我们实际上并不需要所有这些信息,但至少我们现在清楚地看到了是哪个脚本启动了它。也要记住,并非所有服务都以这种方式启动。有些可能通过 inetd 或 xinetd 启动。
的/proc文件系统也保存了我们想知道的关于正在运行的进程的所有信息。我们可以查询它来了解关于每个进程的更多信息。您需要知道启动进程的命令的完整路径吗?
# ls -l /proc/1315/exe lrwxrwxrwx 1 root root 0 July 4 19:41 /proc/1315/exe -> /usr/sbin/cupsd |
最后,我们在 UDP 监听服务中还有一两个悬而未决的问题。记住我们有一个看起来很奇怪的端口号 32768,它也没有关联的服务名称
# netstat -aup Active Internet connections (servers and established) Local Address Foreign Address State PID/Program name *:32768 *:* 956/named bigcat:domain *:* 956/named bigcat:domain *:* 956/named *:631 *:* 1315/cupsd |
![]() | 如果您怀疑自己已被入侵,请不要信任 netstat 或 ps 的输出。它们和其他系统组件很可能已被篡改,以至于输出不可靠。 |
在本节中,我们将快速了解一些常见的威胁和技术,并尝试将它们置于一定的视角。
"worm" (蠕虫)是一种自我复制的漏洞利用程序。它感染一个系统,然后尝试通过相同的漏洞传播自身。各种 "worms" (蠕虫)不断地在整个互联网地址空间中穿梭,并在传播自身。
欺骗 IP 地址有多容易?使用正确的工具,非常容易。这有多大的威胁?对于我们大多数人来说,威胁不大,并且作为一种威胁被过度炒作。
这确实有可能用于 "DoS" (拒绝服务)攻击(见下文),在这种攻击中,了解有关目标系统的某些信息并不重要。也可能用于一些一般的恶作剧。
在这种情况下,攻击者会仔细检查系统是否存在漏洞。并且可能会进行许多不同类型的尝试,直到他找到一个可以钻进去的裂缝。或者放弃。这更难防御。可以说,攻击者是武装且危险的,并且正在跟踪他的猎物。
下面列出了一些供进一步阅读的参考文献。未列出的是您的发行版的站点、安全页面或 ftp 下载站点。您将不得不自己找到这些。然后您应该将它们加入书签!
安全 HOWTO: https://tldp.cn/HOWTO/Security-HOWTO.html |
防火墙 HOWTO: https://tldp.cn/HOWTO/Firewall-HOWTO.html |
Ipchains HOWTO: https://tldp.cn/HOWTO/IPCHAINS-HOWTO.html |
用户身份验证: https://tldp.cn/HOWTO/User-Authentication-HOWTO/index.html,包括关于 PAM 的精彩讨论。 |
VPN (虚拟专用网络): https://tldp.cn/HOWTO/VPN-HOWTO.html 和 https://tldp.cn/HOWTO/VPN-Masquerade-HOWTO.html |
远程 X Apps Mini HOWTO, http://www.tldp.org/HOWTO/mini/Remote-X-Apps.html,包括关于运行 X Windows 的安全影响的精彩讨论。 |
Linux 网络管理员指南: https://tldp.cn/LDP/nag2/index.html,包括网络和 TCP/IP 以及防火墙的良好概述。 |
Linux 管理员安全指南: http://www.seifried.org/lasg/,包括许多感兴趣的明显主题,包括防火墙、密码和身份验证、PAM 等。 |
保护 Red Hat: https://tldp.cn/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/index.html |
用于创建自定义 ipchains 和 iptables 防火墙脚本的工具
Firestarter: http://firestarter.sourceforge.net |
两个相关项目: http://seawall.sourceforge.net/ 用于 ipchains, 和 http://shorewall.sourceforge.net/ 用于 iptables。 |
来自 netfilter 开发人员的 Netfilter 和 iptables 文档(也提供多种其他语言版本)
http://www.linuxsecurity.com/resource_files/firewalls/firewall-seen.html |
http://www.sans.org/newlook/resources/IDFAQ/oddports.htm |
http://www.iana.org/assignments/port-numbers, 官方分配。 |
通用安全站点。这些站点都包含文档、警报、新闻通讯、邮件列表和其他资源区域。
Linux Security.com: http://www.linuxsecurity.com, 载入大量良好信息,并且特定于 Linux。许多好的文档: http://www.linuxsecurity.com/docs/ |
CERT, http://www.cert.org |
SANS 研究所: http://www.sans.org/ |
Coroner's Toolkit (TCT): http://www.fish.com/security/, 关于处理入侵后问题(以及首先防止它们)的讨论和工具。 |
Junkbuster: http://www.junkbuster.com, 一个 Web 代理和 cookie 管理器。 |
PGP: http://www.gnupg.org/ |
Linux Security.com: http://www.linuxsecurity.com/docs/ |
Linux Newbie: http://www.linuxnewbie.org/nhf/intel/security/index.html |
comp.os.linux.security FAQ: http://www.linuxsecurity.com/docs/colsfaq.html |
Internet 防火墙 FAQ: http://www.interhack.net/pubs/fwfaq/ |
站点安全手册 RFC: http://www.ietf.org/rfc/rfc2196.txt |
http://www.bastille-linux.org, 仅适用于 Mandrake 和 Red Hat。 |
SAINT: http://www.wwdsi.com/saint/, 系统安全分析。 |
SSL: http://www.openssl.org/ |
SSH: http://www.openssh.org/ |
扫描您自己: http://www.hackerwhacker.com |
PAM: https://linuxkernel.org.cn/pub/linux/libs/pam/index.html |
检测木马化的 Linux 内核模块: http://members.prestige.net/tmiller12/papers/lkm.htm |
Rootkit 检查器: http://www.chkrootkit.org |
端口扫描工具 nmap 的主页: http://www.insecure.org |
Nessus, 不仅仅是一个端口扫描器: http://www.nessus.org |
Tripwire, 入侵检测: http://www.tripwire.org |
Snort, sniffer 和更多: http://www.snort.org |
http://www.mynetwatchman.com 和 http://dshield.org 是 "Distributed Intrusion Detection Systems" (分布式入侵检测系统)。他们从订阅的 "agents" (代理)收集日志数据,并整理数据以查找和报告恶意活动。如果您想反击,请查看这些。 |
文件有很多类型,但我在这里要扩展一下,将它们分为两个非常广泛的类别
二进制文件旨在由机器读取,文本文件可以轻松编辑,并且通常由人读取。但是文本文件可以(并且经常)由机器读取。例如配置文件和脚本。
在 *nix 中有许多不同的文本编辑器可用。一些编辑器在每个系统上都可以找到。那就是 '/bin/ed' 和 '/bin/vi'。由于许可问题,'vi' 几乎总是克隆版本,例如 'vim'。'vi' 和 'ed' 的问题在于它们非常不友好。另一个常见的编辑器 'emacs' 并不总是默认安装。它具有更多的功能和能力,而且也不容易学习。
至于“用户友好”的编辑器,'mcedit' 和 'pico' 是入门的好选择。对于 *nix 新手来说,这些通常要容易得多。
首先要学习的是如何退出编辑会话,如何将更改保存到文件,以及如何避免打断不应被打断(换行)的长行。
'vi' 编辑器
'vi' 是 Unix 世界中最常见的文本编辑器之一,几乎在任何 *nix 系统上都可以找到。实际上,由于许可问题,Linux 系统上的 '/bin/vi' 始终是“克隆”版本,例如 'elvis'、'nvi' 或 'vim'(还有其他版本)。这些克隆版本可以像原始 'vi' 一样工作,但通常具有使其使用起来稍微不那么困难的附加功能。
那么,如果它如此糟糕,为什么要学习它呢?两个原因。首先,如前所述,几乎可以保证它已安装,而其他(更用户友好)的编辑器可能未默认安装。其次,许多“命令”在其他应用程序中也有效(例如,也用于查看手册页的分页器 'less')。在大多数安装中,在 'less' 中意外按下 'v' 键会启动 'vi'。
'vi' 有两种模式。第一种是“命令模式”,按键被解释为命令。另一种模式是“插入”模式,其中几乎所有按键都被解释为要插入的文本。
==> 'vi' 的紧急退出 1. 按 <esc> 键最多三次,直到计算机发出哔哔声或屏幕闪烁。 2. 按键 :q! <Enter>
也就是说:冒号,字母 Q,然后是感叹号,后跟 Enter 键。
'vi' 命令如下。所有这些都在“命令”模式下
a 在光标之后进入插入模式。
A 在当前行末尾进入插入模式。
i 在光标之前进入插入模式。
o 打开当前行下方的新一行进入插入模式。
O 打开当前行上方的新一行进入插入模式。
h 将光标向左移动一个字符。
l 将光标向右移动一个字符。
j 将光标向下移动一行。
k 将光标向上移动一行。
/mumble 将光标向前移动到文本中下一个出现的 'mumble' 处。
文本中
?mumble 将光标向后移动到文本中下一个出现的 'mumble' 处。
文本中
n 重复上次搜索(? 或 /,不带 'mumble',将搜索
将执行相同的操作)
u 撤消上次更改
^B 向上滚动一个窗口。
^F 向下滚动一个窗口。
^U 向上滚动半个窗口。
^D 向下滚动半个窗口。
:w 写入文件。
:wq 写入文件并退出。
:q 退出。
:q! 不保存退出。
<esc> 离开插入模式。
注意:四个“箭头”键几乎总是在“命令”或“插入”模式下工作。
'ed' 编辑器。
'ed' 编辑器是一个行编辑器。除了几乎可以保证它存在于任何 *nix 计算机上之外,它没有其他社会意义上的优点,尽管某些应用程序可能需要它。自 1975 年以来,已经有很多东西被提出来替代这个“东西”。
==> 'ed' 的紧急退出
1. 在单独的一行中键入句点,然后按 <Enter>。这将使您进入命令模式,或者如果您处于命令模式,则打印一行文本。 2. 键入 q 并按 <Enter>。如果文件没有更改,此操作将退出 ed。如果您随后看到 '?',则表示文件已更改,并且 'ed' 询问您是否要保存更改。再次按 q 和 <Enter> 以确认您要退出。
'pico' 编辑器。
'pico' 是华盛顿大学(美国州)的 Pine 邮件/新闻软件包的一部分。它是一个非常友好的编辑器,但有一个小缺点。当行超过(通常)74 个字符时,它会静默插入换行符并换行。虽然这在创建邮件、新闻文章和文本注释时很好,但在编辑系统文件时通常是致命的。解决此问题的方法很简单。使用 -w 选项调用程序,如下所示
pico -w file_2_edit
Pico 非常用户友好,不需要进一步的说明。它_应该_很明显(查看屏幕底部以获取命令)。有一个广泛的帮助功能。Pico 几乎在所有发行版中都可用,尽管它_可能_不是默认安装的。
==> 'pico' 的紧急退出
按住 <Ctrl> 键,然后按字母 x。如果文件没有进行任何更改,这将退出 pico。如果进行了更改,它将询问您是否要保存更改。按 n 将退出。
'mcedit' 编辑器。
'mcedit' 是 Midnight Commander shell 程序的一部分,这是一个用于类 Unix 系统的全功能可视化 shell。可以从命令行直接访问它 ( mcedit file_2_edit ),也可以作为 'mc' 的一部分访问(使用箭头键突出显示要编辑的文件,然后按 F4 键)。
mcedit 可能是最直观的编辑器,并带有广泛的帮助。“命令”通过 F* 键访问。Midnight Commander 几乎在所有发行版中都可用,尽管它_可能_不是默认安装的。
==> 'mcedit' 的紧急退出
按下 F10 键。如果文件没有做任何更改,这将退出 mcedit。如果文件已经更改,它会询问您是否要取消此操作。按下 n 键将会退出。
首先,有几句警告。如果使用 portsentry,请将其关闭。它会丢弃到扫描来源的路由。您可能还需要关闭任何日志记录,或者至少要意识到,如果进行多次扫描,您可能会获得大量的日志。
# nmap localhost Starting nmap V. 2.53 by fyodor@insecure.org ( www.insecure.org/nmap/ ) Interesting ports on bigcat (127.0.0.1): (The 1507 ports scanned but not shown below are in state: closed) Port State Service 22/tcp open ssh 25/tcp open smtp 37/tcp open time 53/tcp open domain 80/tcp open http 3000/tcp open ppp Nmap run completed -- 1 IP address (1 host up) scanned in 2 seconds |
让我们做一个更密集的扫描。让我们检查所有端口 -- TCP 和 UDP。
# nmap -sT -sU -p 1-65535 localhost Starting nmap V. 2.53 by fyodor@insecure.org ( www.insecure.org/nmap/ ) Interesting ports on bigcat (127.0.0.1): (The 131050 ports scanned but not shown below are in state: closed) Port State Service 22/tcp open ssh 25/tcp open smtp 37/tcp open time 53/tcp open domain 53/udp open domain 80/tcp open http 3000/tcp open ppp 8000/tcp open unknown 32768/udp open unknown Nmap run completed -- 1 IP address (1 host up) scanned in 385 seconds |
# nmap bigcat Starting nmap V. 2.53 by fyodor@insecure.org ( www.insecure.org/nmap/ ) Interesting ports on bigcat (192.168.1.1): (The 1520 ports scanned but not shown below are in state: closed) Port State Service 22/tcp open ssh 3000/tcp open ppp Nmap run completed -- 1 IP address (1 host up) scanned in 1 second |
我承认在这里篡改了 iptables 规则以说明一个问题。在此扫描中只有两个可见端口。其他一切都是"关闭"的。 nmap 如此说。再一次
# nmap bigcat Starting nmap V. 2.53 by fyodor@insecure.org ( www.insecure.org/nmap/ ) Note: Host seems down. If it is really up, but blocking our ping probes, try -P0 Nmap run completed -- 1 IP address (0 hosts up) scanned in 30 seconds |
# nmap -P0 bigcat Starting nmap V. 2.53 by fyodor@insecure.org ( www.insecure.org/nmap/ ) All 1523 scanned ports on bigcat (192.168.1.1) are: filtered Nmap run completed -- 1 IP address (1 host up) scanned in 1643 seconds |
"sysctl" 选项是内核参数,可以通过以下方式配置/proc文件系统。这些可以在运行时动态调整。通常,如果设置为 "0",这些选项将关闭,如果设置为 "1",则打开。
#!/bin/sh # # Configure kernel sysctl run-time options. # ################################################################### # Anti-spoofing blocks for i in /proc/sys/net/ipv4/conf/*/rp_filter; do echo 1 > $i done # Ensure source routing is OFF for i in /proc/sys/net/ipv4/conf/*/accept_source_route; do echo 0 > $i done # Ensure TCP SYN cookies protection is enabled [ -e /proc/sys/net/ipv4/tcp_syncookies ] &&\ echo 1 > /proc/sys/net/ipv4/tcp_syncookies # Ensure ICMP redirects are disabled for i in /proc/sys/net/ipv4/conf/*/accept_redirects; do echo 0 > $i done # Ensure oddball addresses are logged [ -e /proc/sys/net/ipv4/conf/all/log_martians ] &&\ echo 1 > /proc/sys/net/ipv4/conf/all/log_martians [ -e /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts ] &&\ echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts [ -e /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses ] &&\ echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses ## Optional from here on down, depending on your situation. ############ # Ensure ip-forwarding is enabled if # we want to do forwarding or masquerading. [ -e /proc/sys/net/ipv4/ip_forward ] &&\ echo 1 > /proc/sys/net/ipv4/ip_forward # On if your IP is dynamic (or you don't know). [ -e /proc/sys/net/ipv4/ip_dynaddr ] &&\ echo 1 > /proc/sys/net/ipv4/ip_dynaddr # eof |
#!/bin/sh # # ipchains.sh # # An example of a simple ipchains configuration. This script # can enable 'masquerading' and will open user definable ports. # ################################################################### # Begin variable declarations and user configuration options ###### # # Set the location of ipchains (default). IPCHAINS=/sbin/ipchains # Local Interfaces # # This is the WAN interface, that is our link to the outside world. # For pppd and pppoe users. # WAN_IFACE="ppp0" WAN_IFACE="eth0" # # Local Area Network (LAN) interface. #LAN_IFACE="eth0" LAN_IFACE="eth1" # Our private LAN address(es), for masquerading. LAN_NET="192.168.1.0/24" # For static IP, set it here! #WAN_IP="1.2.3.4" # Set a list of public server port numbers here...not too many! # These will be open to the world, so use caution. The example is # sshd, and HTTP (www). Any services included here should be the # latest version available from your vendor. Comment out to disable # all PUBLIC services. #PUBLIC_PORTS="22 80 443" PUBLIC_PORTS="22" # If we want to do port forwarding, this is the host # that will be forwarded to. #FORWARD_HOST="192.168.1.3" # A list of ports that are to be forwarded. #FORWARD_PORTS="25 80" # If you get your public IP address via DHCP, set this. DHCP_SERVER=66.21.184.66 # If you need identd for a mail server, set this. MAIL_SERVER= # A list of unwelcome hosts or nets. These will be denied access # to everything, even our 'PUBLIC' services. Provide your own list. #BLACKLIST="11.22.33.44 55.66.77.88" # A list of "trusted" hosts and/or nets. These will have access to # ALL protocols, and ALL open ports. Be selective here. #TRUSTED="1.2.3.4/8 5.6.7.8" ## end user configuration options ################################# ################################################################### # The high ports used mostly for connections we initiate and return # traffic. LOCAL_PORTS=`cat /proc/sys/net/ipv4/ip_local_port_range |cut -f1`:\ `cat /proc/sys/net/ipv4/ip_local_port_range |cut -f2` # Any and all addresses from anywhere. ANYWHERE="0/0" # Start building chains and rules ################################# # # Let's start clean and flush all chains to an empty state. $IPCHAINS -F # Set the default policies of the built-in chains. If no match for any # of the rules below, these will be the defaults that ipchains uses. $IPCHAINS -P forward DENY $IPCHAINS -P output ACCEPT $IPCHAINS -P input DENY # Accept localhost/loopback traffic. $IPCHAINS -A input -i lo -j ACCEPT # Get our dynamic IP now from the Inet interface. WAN_IP will be our # IP address we are protecting from the outside world. Put this # here, so default policy gets set, even if interface is not up # yet. [ -z "$WAN_IP" ] &&\ WAN_IP=`ifconfig $WAN_IFACE |grep inet |cut -d : -f 2 |cut -d \ -f 1` # Bail out with error message if no IP available! Default policy is # already set, so all is not lost here. [ -z "$WAN_IP" ] && echo "$WAN_IFACE not configured, aborting." && exit 1 WAN_MASK=`ifconfig $WAN_IFACE | grep Mask | cut -d : -f 4` WAN_NET="$WAN_IP/$WAN_MASK" ## Reserved IPs: # # We should never see these private addresses coming in from outside # to our external interface. $IPCHAINS -A input -l -i $WAN_IFACE -s 10.0.0.0/8 -j DENY $IPCHAINS -A input -l -i $WAN_IFACE -s 172.16.0.0/12 -j DENY $IPCHAINS -A input -l -i $WAN_IFACE -s 192.168.0.0/16 -j DENY $IPCHAINS -A input -l -i $WAN_IFACE -s 127.0.0.0/8 -j DENY $IPCHAINS -A input -l -i $WAN_IFACE -s 169.254.0.0/16 -j DENY $IPCHAINS -A input -l -i $WAN_IFACE -s 224.0.0.0/4 -j DENY $IPCHAINS -A input -l -i $WAN_IFACE -s 240.0.0.0/5 -j DENY # Bogus routing $IPCHAINS -A input -l -s 255.255.255.255 -d $ANYWHERE -j DENY ## LAN access and masquerading # # Allow connections from our own LAN's private IP addresses via the LAN # interface and set up forwarding for masqueraders if we have a LAN_NET # defined above. if [ -n "$LAN_NET" ]; then echo 1 > /proc/sys/net/ipv4/ip_forward $IPCHAINS -A input -i $LAN_IFACE -j ACCEPT $IPCHAINS -A forward -s $LAN_NET -d $LAN_NET -j ACCEPT $IPCHAINS -A forward -s $LAN_NET -d ! $LAN_NET -j MASQ fi ## Blacklist hosts/nets # # Get the blacklisted hosts/nets out of the way, before we start opening # up any services. These will have no access to us at all, and will be # logged. for i in $BLACKLIST; do $IPCHAINS -A input -l -s $i -j DENY done ## Trusted hosts/nets # # This is our trusted host list. These have access to everything. for i in $TRUSTED; do $IPCHAINS -A input -s $i -j ACCEPT done # Port Forwarding # # Which ports get forwarded to which host. This is one to one # port mapping (ie 80 -> 80) in this case. # NOTE: ipmasqadm is a separate package from ipchains and needs # to be installed also. Check first! [ -n "$FORWARD_HOST" ] && ipmasqadm portfw -f &&\ for i in $FORWARD_PORTS; do ipmasqadm portfw -a -P tcp -L $WAN_IP $i -R $FORWARD_HOST $i done ## Open, but Restricted Access ports/services # # Allow DHCP server (their port 67) to client (to our port 68) UDP traffic # from outside source. [ -n "$DHCP_SERVER" ] &&\ $IPCHAINS -A input -p udp -s $DHCP_SERVER 67 -d $ANYWHERE 68 -j ACCEPT # Allow 'identd' (to our TCP port 113) from mail server only. [ -n "$MAIL_SERVER" ] &&\ $IPCHAINS -A input -p tcp -s $MAIL_SERVER -d $WAN_IP 113 -j ACCEPT # Open up PUBLIC server ports here (available to the world): for i in $PUBLIC_PORTS; do $IPCHAINS -A input -p tcp -s $ANYWHERE -d $WAN_IP $i -j ACCEPT done # So I can check my home POP3 mailbox from work. Also, so I can ssh # in to home system. Only allow connections from my workplace's # various IPs. Everything else is blocked. $IPCHAINS -A input -p tcp -s 255.10.9.8/29 -d $WAN_IP 110 -j ACCEPT # Uncomment to allow ftp data back (active ftp). Not required for 'passive' # ftp connections. #$IPCHAINS -A input -p tcp -s $ANYWHERE 20 -d $WAN_IP $LOCAL_PORTS -y -j ACCEPT # Accept non-SYN TCP, and UDP connections to LOCAL_PORTS. These are # the high, unprivileged ports (1024 to 4999 by default). This will # allow return connection traffic for connections that we initiate # to outside sources. TCP connections are opened with 'SYN' packets. # We have already opened those services that need to accept SYNs # for, so other SYNs are excluded here for everything else. $IPCHAINS -A input -p tcp -s $ANYWHERE -d $WAN_IP $LOCAL_PORTS ! -y -j ACCEPT # We can't be so selective with UDP since that protocol does not know # about SYNs. $IPCHAINS -A input -p udp -s $ANYWHERE -d $WAN_IP $LOCAL_PORTS -j ACCEPT # Allow access to the masquerading ports conditionally. Masquerading # uses it's own port range -- on 2.2 kernels ONLY! 2.4 kernels, do not # use these ports, so comment out! [ -n "$LAN_NET" ] &&\ $IPCHAINS -A input -p tcp -s $ANYWHERE -d $WAN_IP 61000: ! -y -j ACCEPT &&\ $IPCHAINS -A input -p udp -s $ANYWHERE -d $WAN_IP 61000: -j ACCEPT ## ICMP (ping) # # ICMP rules, allow the bare essential types of ICMP only. Ping # request is blocked, ie we won't respond to someone else's pings, # but can still ping out. $IPCHAINS -A input -p icmp --icmp-type echo-reply \ -s $ANYWHERE -i $WAN_IFACE -j ACCEPT $IPCHAINS -A input -p icmp --icmp-type destination-unreachable \ -s $ANYWHERE -i $WAN_IFACE -j ACCEPT $IPCHAINS -A input -p icmp --icmp-type time-exceeded \ -s $ANYWHERE -i $WAN_IFACE -j ACCEPT ####################################################################### # Set the catchall, default rule to DENY, and log it all. All other # traffic not allowed by the rules above, winds up here, where it is # blocked and logged. This is the default policy for this chain # anyway, so we are just adding the logging ability here with '-l'. # Outgoing traffic is allowed as the default policy for the 'output' # chain. There are no restrictions on that. $IPCHAINS -A input -l -j DENY echo "Ipchains firewall is up `date`." ##-- eof ipchains.sh |
#!/bin/sh # # iptables.sh # # An example of a simple iptables configuration. This script # can enable 'masquerading' and will open user definable ports. # ################################################################### # Begin variable declarations and user configuration options ###### # # Set the location of iptables (default). IPTABLES=/sbin/iptables # Local Interfaces # This is the WAN interface that is our link to the outside world. # For pppd and pppoe users. # WAN_IFACE="ppp0" WAN_IFACE="eth0" # # Local Area Network (LAN) interface. #LAN_IFACE="eth0" LAN_IFACE="eth1" # Our private LAN address(es), for masquerading. LAN_NET="192.168.1.0/24" # For static IP, set it here! #WAN_IP="1.2.3.4" # Set a list of public server port numbers here...not too many! # These will be open to the world, so use caution. The example is # sshd, and HTTP (www). Any services included here should be the # latest version available from your vendor. Comment out to disable # all Public services. Do not put any ports to be forwarded here, # this only direct access. #PUBLIC_PORTS="22 80 443" PUBLIC_PORTS="22" # If we want to do port forwarding, this is the host # that will be forwarded to. #FORWARD_HOST="192.168.1.3" # A list of ports that are to be forwarded. #FORWARD_PORTS="25 80" # If you get your public IP address via DHCP, set this. DHCP_SERVER=66.21.184.66 # If you need identd for a mail server, set this. MAIL_SERVER= # A list of unwelcome hosts or nets. These will be denied access # to everything, even our 'Public' services. Provide your own list. #BLACKLIST="11.22.33.44 55.66.77.88" # A list of "trusted" hosts and/or nets. These will have access to # ALL protocols, and ALL open ports. Be selective here. #TRUSTED="1.2.3.4/8 5.6.7.8" ## end user configuration options ################################# ################################################################### # Any and all addresses from anywhere. ANYWHERE="0/0" # These modules may need to be loaded: modprobe ip_conntrack_ftp modprobe ip_nat_ftp # Start building chains and rules ################################# # # Let's start clean and flush all chains to an empty state. $IPTABLES -F $IPTABLES -X # Set the default policies of the built-in chains. If no match for any # of the rules below, these will be the defaults that IPTABLES uses. $IPTABLES -P FORWARD DROP $IPTABLES -P OUTPUT ACCEPT $IPTABLES -P INPUT DROP # Accept localhost/loopback traffic. $IPTABLES -A INPUT -i lo -j ACCEPT # Get our dynamic IP now from the Inet interface. WAN_IP will be the # address we are protecting from outside addresses. [ -z "$WAN_IP" ] &&\ WAN_IP=`ifconfig $WAN_IFACE |grep inet |cut -d : -f 2 |cut -d \ -f 1` # Bail out with error message if no IP available! Default policy is # already set, so all is not lost here. [ -z "$WAN_IP" ] && echo "$WAN_IFACE not configured, aborting." && exit 1 WAN_MASK=`ifconfig $WAN_IFACE |grep Mask |cut -d : -f 4` WAN_NET="$WAN_IP/$WAN_MASK" ## Reserved IPs: # # We should never see these private addresses coming in from outside # to our external interface. $IPTABLES -A INPUT -i $WAN_IFACE -s 10.0.0.0/8 -j DROP $IPTABLES -A INPUT -i $WAN_IFACE -s 172.16.0.0/12 -j DROP $IPTABLES -A INPUT -i $WAN_IFACE -s 192.168.0.0/16 -j DROP $IPTABLES -A INPUT -i $WAN_IFACE -s 127.0.0.0/8 -j DROP $IPTABLES -A INPUT -i $WAN_IFACE -s 169.254.0.0/16 -j DROP $IPTABLES -A INPUT -i $WAN_IFACE -s 224.0.0.0/4 -j DROP $IPTABLES -A INPUT -i $WAN_IFACE -s 240.0.0.0/5 -j DROP # Bogus routing $IPTABLES -A INPUT -s 255.255.255.255 -d $ANYWHERE -j DROP # Unclean $IPTABLES -A INPUT -i $WAN_IFACE -m unclean -m limit \ --limit 15/minute -j LOG --log-prefix "Unclean: " $IPTABLES -A INPUT -i $WAN_IFACE -m unclean -j DROP ## LAN access and masquerading # # Allow connections from our own LAN's private IP addresses via the LAN # interface and set up forwarding for masqueraders if we have a LAN_NET # defined above. if [ -n "$LAN_NET" ]; then echo 1 > /proc/sys/net/ipv4/ip_forward $IPTABLES -A INPUT -i $LAN_IFACE -j ACCEPT # $IPTABLES -A INPUT -i $LAN_IFACE -s $LAN_NET -d $LAN_NET -j ACCEPT $IPTABLES -t nat -A POSTROUTING -s $LAN_NET -o $WAN_IFACE -j MASQUERADE fi ## Blacklist # # Get the blacklisted hosts/nets out of the way, before we start opening # up any services. These will have no access to us at all, and will # be logged. for i in $BLACKLIST; do $IPTABLES -A INPUT -s $i -m limit --limit 5/minute \ -j LOG --log-prefix "Blacklisted: " $IPTABLES -A INPUT -s $i -j DROP done ## Trusted hosts/nets # # This is our trusted host list. These have access to everything. for i in $TRUSTED; do $IPTABLES -A INPUT -s $i -j ACCEPT done # Port Forwarding # # Which ports get forwarded to which host. This is one to one # port mapping (ie 80 -> 80) in this case. [ -n "$FORWARD_HOST" ] &&\ for i in $FORWARD_PORTS; do $IPTABLES -A FORWARD -p tcp -s $ANYWHERE -d $FORWARD_HOST \ --dport $i -j ACCEPT $IPTABLES -t nat -A PREROUTING -p tcp -d $WAN_IP --dport $i \ -j DNAT --to $FORWARD_HOST:$i done ## Open, but Restricted Access ports # # Allow DHCP server (their port 67) to client (to our port 68) UDP # traffic from outside source. [ -n "$DHCP_SERVER" ] &&\ $IPTABLES -A INPUT -p udp -s $DHCP_SERVER --sport 67 \ -d $ANYWHERE --dport 68 -j ACCEPT # Allow 'identd' (to our TCP port 113) from mail server only. [ -n "$MAIL_SERVER" ] &&\ $IPTABLES -A INPUT -p tcp -s $MAIL_SERVER -d $WAN_IP --dport 113 -j ACCEPT # Open up Public server ports here (available to the world): for i in $PUBLIC_PORTS; do $IPTABLES -A INPUT -p tcp -s $ANYWHERE -d $WAN_IP --dport $i -j ACCEPT done # So I can check my home POP3 mailbox from work. Also, so I can ssh # in to home system. Only allow connections from my workplace's # various IPs. Everything else is blocked. $IPTABLES -A INPUT -p tcp -s 255.10.9.8/29 -d $WAN_IP --dport 110 -j ACCEPT ## ICMP (ping) # # ICMP rules, allow the bare essential types of ICMP only. Ping # request is blocked, ie we won't respond to someone else's pings, # but can still ping out. $IPTABLES -A INPUT -p icmp --icmp-type echo-reply \ -s $ANYWHERE -d $WAN_IP -j ACCEPT $IPTABLES -A INPUT -p icmp --icmp-type destination-unreachable \ -s $ANYWHERE -d $WAN_IP -j ACCEPT $IPTABLES -A INPUT -p icmp --icmp-type time-exceeded \ -s $ANYWHERE -d $WAN_IP -j ACCEPT # Identd Reject # # Special rule to reject (with rst) any identd/auth/port 113 # connections. This will speed up some services that ask for this, # but don't require it. Be careful, some servers may require this # one (IRC for instance). #$IPTABLES -A INPUT -p tcp --dport 113 -j REJECT --reject-with tcp-reset ################################################################### # Build a custom chain here, and set the default to DROP. All # other traffic not allowed by the rules above, ultimately will # wind up here, where it is blocked and logged, unless it passes # our stateful rules for ESTABLISHED and RELATED connections. Let # connection tracking do most of the worrying! We add the logging # ability here with the '-j LOG' target. Outgoing traffic is # allowed as that is the default policy for the 'output' chain. # There are no restrictions placed on that in this script. # New chain... $IPTABLES -N DEFAULT # Use the 'state' module to allow only certain connections based # on their 'state'. $IPTABLES -A DEFAULT -m state --state ESTABLISHED,RELATED -j ACCEPT $IPTABLES -A DEFAULT -m state --state NEW -i ! $WAN_IFACE -j ACCEPT # Enable logging for anything that gets this far. $IPTABLES -A DEFAULT -j LOG -m limit --limit 30/minute --log-prefix "Dropping: " # Now drop it, if it has gotten here. $IPTABLES -A DEFAULT -j DROP # This is the 'bottom line' so to speak. Everything winds up # here, where we bounce it to our custom built 'DEFAULT' chain # that we defined just above. This is for both the FORWARD and # INPUT chains. $IPTABLES -A FORWARD -j DEFAULT $IPTABLES -A INPUT -j DEFAULT echo "Iptables firewall is up `date`." ##-- eof iptables.sh |
为了演示在极简主义情况下 iptables 可以配置得多么简洁,以下内容来自 Netfilter 团队的 Rusty's Really Quick Guide To Packet Filtering
"Most people just have a single PPP connection to the Internet, and don't want anyone coming back into their network, or the firewall:"
## Insert connection-tracking modules (not needed if built into kernel). insmod ip_conntrack insmod ip_conntrack_ftp ## Create chain which blocks new connections, except if coming from inside. iptables -N block iptables -A block -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A block -m state --state NEW -i ! ppp0 -j ACCEPT iptables -A block -j DROP ## Jump to that chain from INPUT and FORWARD chains. iptables -A INPUT -j block iptables -A FORWARD -j block |
这个简单的脚本将允许我们启动的所有出站连接,即任何 NEW 连接(因为 ACCEPT 的默认策略未更改)。然后,任何与这些连接 "ESTABLISHED" 和 "RELATED" 的连接也被允许。并且,任何不是来自我们的 WAN 侧接口的传入连接,ppp0,也被允许。这将是 lo 或可能是像 eth1 这样的 LAN 接口。因此,我们可以做任何我们想做的事情,但是不允许来自 Internet 的任何不需要的传入连接尝试。一个也没有。
此脚本还演示了自定义链的创建,在此处定义为 "block",它同时用于 INPUT 和 FORWARD 链。