Red Hat Linux 安全快速入门 HOWTO | ||
---|---|---|
上一页 |
让我们快速地、以非技术性的方式了解一些网络概念,以及它们如何潜在地影响我们的安全。我们不需要了解太多的网络知识,但对事物如何运作有一个大致的了解,肯定会对我们处理防火墙和其他相关问题有所帮助。
您可能已经注意到,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 端口。请参见端口部分,了解其他常用的端口和服务。
关于端口的另一点:只有在该端口上有东西监听时,端口才是可访问的。如果没有服务或守护程序在那里监听,准备处理传入的连接请求,则没有人可以强制打开端口。关闭的端口是完全安全的端口。
关于客户端和服务器之间区别的最后一点。上面的示例在监听器部分中没有 telnet 或 ftp 服务器 在上面的 netstat 示例中。换句话说,没有这样的服务器在本地运行。您不需要运行 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,或“submission”,邮件提交代理协议。一种新的邮件处理协议,受大多数 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 |
使用--forest标志(-f简称)运行 ps 可以帮助确定哪些进程是父进程或子进程或另一个进程。一个编辑过的例子
$ /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
$ 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 |
find 可能是最灵活的文件查找实用程序,但不像 locate 那样使用数据库,因此速度要慢得多
$ find / -name master /usr/libexec/postfix/master |
如果安装了 lsof,则它是另一个有用的命令,用于查找谁拥有进程或端口
# lsof -i :631 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME cupsd 1315 root 0u IPv4 3734 TCP *:631 (LISTEN) |
这再次告诉我们 cupsd 打印守护进程是端口 631 的所有者。只是获得它的另一种方式。另一种获取方法是使用 fuser,应该已安装
# fuser -v -n tcp 631 USER PID ACCESS COMMAND 631/tcp root 1315 f.... cupsd |
# 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 的输出。很有可能它们和其他系统组件已被篡改,以至于输出不可靠。 |
在本节中,我们将快速了解一些常见的威胁和技术,并尝试将它们置于一定的角度来看待。
"蠕虫" 是一种自我复制的漏洞利用程序。它感染一个系统,然后尝试通过相同的漏洞来传播自己。各种 "蠕虫" 不断地穿梭于整个 Internet 地址空间中,并在传播过程中传播自己。
再次强调,关键在于他们遵循一个 "脚本",寻找容易攻击的目标。就像上面的蠕虫威胁一样,一个功能正常的防火墙和一些非常基本的预防措施,应该足以抵御任何威胁。到目前为止,你应该相对安全地避免这种滋扰。
欺骗IP地址有多容易?有了合适的工具,非常容易。这有多大的威胁?对我们大多数人来说,威胁不大,而且被过度炒作了。
这确实存在进行 "DoS" 攻击(见下文)的潜力,在这种攻击中,了解目标系统的任何信息并不重要。而且也可能被用于一些普通的恶作剧。
在这种情况下,攻击者会仔细检查系统是否存在弱点。并可能进行许多不同类型的尝试,直到找到一个可以钻进去的缝隙。或者放弃。这种攻击更难防御。可以说,攻击者是全副武装且危险的,并且正在跟踪他的猎物。
"DDoS",分布式拒绝服务,是指使用多个来源来最大化影响。同样,不太可能直接针对家庭用户。这些是 "奴隶",它们被黑客或脚本小子 "拥有",被唤醒并针对受害者。攻击中可能涉及许多计算机。
如果你是家庭用户,并且拥有动态IP地址,你可能会发现断开连接,然后重新连接以获取新IP地址是一种有效的摆脱困境的方法。也许吧。
下面列出了一些参考资料,供进一步阅读。未列出的是你的发行版的网站、安全页面或ftp下载站点。你必须自己找到这些。然后你应该将它们添加到书签!
Redhat watch/security 邮件列表: https://listman.redhat.com/mailman/listinfo/redhat-watch-list |
Red Hat 勘误和安全公告: http://redhat.com/errata/ |
Red Hat 更新 FTP 站点: ftp://updates.redhat.com/ |
安全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应用程序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/ |
验尸官工具包(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,嗅探器以及更多:http://www.snort.org |
http://www.mynetwatchman.com 和 http://dshield.org 是“分布式入侵检测系统”。它们从订阅的“代理”收集日志数据,并整理数据以查找和报告恶意活动。 如果你想反击,请查看这些。 |
二进制文件供机器读取,文本文件可以轻松编辑,通常供人读取。但是文本文件可以(并且经常)被机器读取。例如配置文件和脚本。
在*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”,也用于查看man手册页)。 在“less”中,意外按下“v”键会在大多数安装中启动“vi”。
“vi”有两种模式。 第一种是“命令模式”,按键被解释为命令。 另一种模式是“插入”模式,几乎所有的按键都被解释为要插入的文本。
==> 从“vi”紧急退出 1. 按<esc>键最多三次,直到计算机发出哔哔声或屏幕闪烁。 2. 按键 :q! <Enter>
即:冒号、字母Q,然后是感叹号,后跟回车键。
“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 |
使用以下方法可以获得相同的效果/etc/sysctl.conf代替
# # Add to existing sysctl.conf # # Anti-spoofing blocks net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.all.rp_filter = 1 # Ensure source routing is OFF net.ipv4.conf.default.accept_source_route = 0 net.ipv4.conf.all.accept_source_route = 0 # Ensure TCP SYN cookies protection is enabled net.ipv4.tcp_syncookies = 1 # Ensure ICMP redirects are disabled net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.accept_redirects = 0 # Ensure oddball addresses are logged net.ipv4.conf.default.log_martians = 1 net.ipv4.conf.all.log_martians = 1 net.ipv4.icmp_echo_ignore_broadcasts = 1 net.ipv4.icmp_ignore_bogus_error_responses = 1 ## Optional from here on down, depending on your situation. ############ # Ensure ip-forwarding is enabled if # we want to do forwarding or masquerading. net.ipv4.ip_forward = 1 # On if your IP is dynamic (or you don't know). net.ipv4.ip_dynaddr = 1 # end of example |
#!/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 |
“大多数人只有一个到 Internet 的 PPP 连接,并且不希望任何人返回到他们的网络或防火墙:”
## 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 或可能的 LAN 接口,如 eth1。 因此,我们可以做任何我们想做的事情,但不允许来自 Internet 的任何不需要的传入连接尝试。 没有任何。
此脚本还演示了自定义链的创建,在此定义为 “block(阻止)”,它既用于 INPUT 链,也用于 FORWARD 链。