免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4202 | 回复: 0
打印 上一主题 下一主题

彻底封杀sshd 的致命诱惑 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-13 20:14 |只看该作者 |倒序浏览
接到某客户电话,发现LinuxAS4执行系统指令 ls 失败,并出现奇怪提示。随即我在他授权下登陆机器检查,发现是被入侵。过程如下:
暴力破解成功通过ssh登陆安装 rootkit 安装攻击工具
普通的攻击过程,连日志也不清除,大摇大摆的来去,如入无人之地。存在弱口令的时候,尤其是Linux机器一上线瞬间被入侵!
本文在讨论通过通常的加固(参考公司的 Linux 加固文档)步骤后,是否还能进一步采取更严格措施的方法。
一、准备工作
1.1测试所需要的环境
安装了Redhat9的VMware5.0
Wb.exe 连接耗尽
DoSend.c synflood攻击
Superscan3.exe 全端口扫描
Nmap4.0.exe 特殊方法扫描(包括syn半连接、空扫描、FIN扫描等)
Windows自带工具 Ping.exe 测试网络连通性
hydra-5.3-src.tar.gz 暴力破解SSH口令
1.2常规方法加固操作系统
可参考公司的《Linux加固方案》,此时操作系统的网络服务只剩下 ssh ,本地加固了口令档案,权限、profile的设置等,这里为了如果为了排除远程溢出的风险,可先升级 sshd 到最新版本,我自己测试就省略了这一步。
1.3 口令策略的加固
编辑口令设置文件,vi /etc/login.defs,找到如下语句:
PASS_MAX_DAYS 99999 把数字改成30,表示一个月强制用户更改一次口令
PASS_MIN_DAYS 0 把数字改成7,表示至少一个星期才能改一次密码
PASS_MIN_LEN 5 更改数字8 ,表示口令长度最短为8
PASS_WARN_AGE 7 把数字改成3,表示密码更改前3天提醒用户
以上配置是对新增用户的定义,如果要修改现存的用户密码配置可以用 chage 命令,它的基本用法是:
chage [-m mindays] [-M maxdays] [-d lastday] [-I inactive]
[-E expiredate] [-W warndays] user
对已经存在的用户可以用如下语句执行:
chage -m 7 -M 60 -W 3 root
更严厉的设置在 /etc/default/useradd
INACTIVE=-1 把数字更改为15,则表示,当需要更改口令而又没有更改时,15日后该用户被禁用
EXPIRE= 缺省没有,这里对所有用户停用的时间设置
查看配置好的结果
[root@bobkey]# chage -l root
Minimum: 7
Maximum: 60
Warning: 3
Inactive: -1
Last Change: Dec 05, 2006
Password Expires: Feb 03, 2007
Password Inactive: Never
Account Expires: Never
设置完毕可以运行 chattr +i /etc/shadow 把文件锁定,避免删除或修改操作
二、测试 ssh 暴露在公网的风险
2.1扫描获取到的信息
结果:危险!可成功获取。
图一:superscan的端口扫描截图
图二:nmap的全连接扫描截图
2.2 被synflood之后的反应
结果:危险!系统被攻击,系统资源急剧下降。
图三:攻击之后的系统网络连接状态截图
图四:攻击之前的系统资源截图
图五:攻击之后的系统资源截图
2.3 被口令穷举之后的效果
结果:危险!可猜测系统帐号。
图六:利用ssh_scan工具在10分钟扫描一个B class截图
图七:系统弱口令被破解截图
三、采取的应对措施
通过以上的攻击测试发现,尽管做了通常步骤的加固,但风险依然存在,表现在可被扫描、DOS攻击、远程暴力猜解口令。当然也许还有未知的溢出风险。
怎么防范?在本次例子中,被加固的对象是抓包服务器,没有特殊的应用,所以对于加固后给生产带来的风险相对不大,如果是其他机器切不可盲目照抄,否则后果自负。
3.1改变工作模式
SSHD缺省是stand-alone工作模式,改变为有Xinetd调用可降低被DDOS的风险,。xinetd能同时监听多个指定的端口,在接受用户请求时,根据用户请求的端口不同,启动不同的网络服务进程来处理这些用户请求。
图八:Xinetd工作原理
当然,这是理想做法,但不能改变根本问题,我并没去操作它。要提醒的是对于频繁访问的服务如:www mail,不要用xinetd去启动,这样反而降低效率。
3.2 改变缺省端口
SSHD的默认端口就是 22 ,地球人都知道,通常黑客在没有准确目标的情况下要寻找 Linux机器的最好方法就是扫描所有开放了 22 端口的机器,然后放在一个列表里,一个个去探求它的漏洞。
比如 nmap4 的新功能 nmap -v -iR 10000 -P0 -p 22 可随机在10000个IP里去寻找开放了 22 端口的机器。当然也可以有目标的把日本或者其他国家的ip段添上再扫描。
通常是按照服务对应的常规端口去扫描,除非用全端口1-65535,但除非是针对性的对一台机器扫描,否则这样效率不高。
把默认端口改变成60022的步骤如下:
vi /etc/ssh/sshd_config
找到#Port 22,标识默认使用22端口,如果需要更改为8888则去掉前面的 # 注释符号,修改为:Port 60022
然后重启服务进程
/etc/init.d/sshd restart
或者
kill -HUP `cat /var/run/sshd.pid`
3.3 sshd_config其他安全选项
把 #PermitRootLogin yes 改为 PermitRootLogin no 可防止 root 远程登陆
把 #Protocol 1,2 改 Protocol 2 ,不用 SSH protocol 1 协议,只用 2
当然还有一些选项,视需要而定,比如:Banner的伪造、登陆失败后的的锁定时间、是否允许空口令帐号登陆、服务器密钥的位数等、允许登陆的用户和IP等。
3.4 TCP Wrappers 列出白名单
TCP Wrappers的防护思想和防火墙原则一样,除非允许,否则默认是禁止的,在 Redhat 所有发行版上默认是安装了这个程序的,可利用此程序来允许可连接的客户端机器。先在 /etc/hosts.deny中添加禁止来自任何地方对所有服务的访问,然后在 /etc/hosts.allow 中添加允许的机器ip或者域名
Vi /etc/hosts.deny, ALL:ALL
Vi /etc/hosts.allow
sshd:172.168.20.0/255.255.255.0, 192.168.20.163
3.4 内核参数调整
sysctl -w net.ipv4.conf.default.accept_source_route=0
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1
#sysctl -w net.ipv4.icmp_echo_ignore_all=1
sysctl -w net.ipv4.icmp_ignore_bogus_error_responses=1
sysctl -w net.ipv4.ip_conntrack_max=65535
#
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.tcp_syn_retries=1
sysctl -w net.ipv4.tcp_fin_timeout=5
sysctl -w net.ipv4.tcp_synack_retries=1
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.route.gc_timeout=100
sysctl -w net.ipv4.tcp_keepalive_time=500
sysctl -w net.ipv4.tcp_max_syn_backlog=10000
3.5 Iptables 最后的铠甲
如果前面的都已经设置完毕,到这里其实相对较安全了,我都觉得不是很有必要再用iptables来做黑白名单之类的操作了,就好像穿了一件铠甲,里面又弄件刀枪不入的宝衣。显得没这么必要,但谁知道呢?这年头假货多,万一被弄穿一层也还有一层,多弄层防护也不是坏事,但自己要记住设置的地方,免得和其他设置有冲突,适得其反。
#1.初始化防火墙的表和链条,并设置所有链条的默认策略为禁止任何包通过
iptables -F
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
#2.ICMP是扫描器探测机器存活的手段之一,把它关闭可防止,视需要而定
#iptables -A INPUT -p icmp -i eth0 -m state --state NEW,ESTABLISHED -j ACCEPT
#3.防止端口探测存活技术的规则
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
#4.对唯一开放的 60022 sshd端口要限制连接数,这里选项是每秒只允许 1个连接,防止被DOS
iptables -A INPUT -m limit --limit 1/second -p tcp --tcp-flags ALL SYN --dport 60022 -j ACCEPT
iptables -A INPUT -m limit --limit 1/second -p tcp --tcp-flags ALL FIN --dport 60022 -j ACCEPT
iptables -A INPUT -m limit --limit 1/second -p tcp --tcp-flags ALL RST --dport 60022 -j ACCEPT
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -p tcp --dport 60022 -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#5.到这里防火墙设置差不多结束了,下面有更严格的,把允许登陆的IP和其MAC绑定在一起,可替换第4段规则,但灵活性就差些,也可视情况而定
iptables –A INPUT –p tcp --dport 60022 –m limit --limit 1/second --limit-burst 20 –j ACCEPT
iptables –A FORWARD –s 192.168.1.23 –m mac –mac-source 00:e0:4c:3d:5e:4f –j ACCEPT
图九:3个request包对应1个reply 启用ICMP防护规则效果截图
四、自动化阻断密码暴力破解者IP
首先系统已经加固,验证记录在/etc/syslog.conf中设置了/var/log/secure中可查到。系统设置了不允许用root远程登陆,如果黑客还用root来猜解密码,势必留下 Failed password for invalid user root from 的字样, 或者是其他不存在的用户登陆痕迹。
图九:/var/log/secure中错误登陆截图
那么我们用awk和sed等工具组合后筛选出恶意ip,然后把它加入IPTABLES的阻断规则中,其实网上已经有很多人用这样的思路做出了一些程序,比如防止DDOS,防止扫描等,手段都是一样的。
IP怎么加呢?
首先,取得恶意ip
Blickip=`cat /var/log/secure |grep sshd|grep Failed |awk '{ print $11 }'|uniq|sort`
然后,把恶意ip增加的iptables的阻断规则里
for i in $blackip;do
iptables -A INPUT -s $i -j DROP
done
这样就可以了,但,有个问题是这样会重复增加一样的ip到规则里,造成防火墙臃肿,效率降低,特别黑客如果用多台主机暴力破解,或者用代理主机时。
对此我有两个想法:
1.比较两次的ip,只增加新增的IP到规则 ,这个编写上要麻烦点
2.让防火墙每5分钟重运行一次,刷掉所有现存的规则,重载所有block的ip,这样就不会重复
如果让程序进程实时监控日志变化然后再加规则?
While ture;
Do
Command
Sleep 300
done
这样有点浪费资源,而且万一程序宕掉了,那么就失效了,所以还是老老实实用crontab来做,即计划任务,让它每5分钟自动执行一次。
Crontab -l
*/5 * * * * root /etc/iptables.sh >; /dev/null 2>;&1
如果非要挑刺,黑客在5分钟内就破解了口令,然后迅速登陆系统,再把规则修改掉怎么办?系统口令如果在8位以上,并且复杂度高,那是不可能的,除非管理员非要弄个弱密码,123456之类,那就没办法了,我只能扛把火枪去机房站岗,谁来我就顺着网线崩了他。
还有两个问题需要考虑:
1.防火墙频繁重启有什么问题?
我测试过运行完整的程序时间如下,大概2秒多,当然如果日志文件逐步增大,那么运行时间会延长,时间主要话费在统计log的ip和用新增ip比较现存的黑名单地址上,这里其实也可以用其他方法来改进:
#time { ./iptns.sh ; }
real 0m2.963s
user 0m1.740s
sys 0m0.510s
2./var/log/secure 是会轮循到另一个数字递增的文件,比如: secure.1 secure.2,怎么自动跟着取得它的变量?
虽然可以用编程的方法来处理,先得到secure后最大的数字,然后用它来处理,这样又怕管理员擅自改动,用 lsof 方法更简单,哪个文件是被syslogd调用,那么当前记录的也就是它了:
secure_log=`lsof /var/log/secure* |grep syslogd|awk '{ print $9 }'`
3.
五、第三方工具
这里不着重推荐,因为我并没去测试它们,其实原理和以上介绍一样
5.1 BlockSSHD
官方链接:
https://sourceforge.net/project/show...roup_id=163753
下面是官方的简介
BlockSSHD protects computers from SSH brute force attacks by dynamically blocking IP addresses by adding iptables rules.
BlockSSHD 是用 perl 写的程序,目前是1.0版。安装只有两个步骤,把程序放入/usr/sbin/,并初始化在/etc/init.d中可自启动,通过日志增加恶意IP到iptables的规则中
5.2 DenyHOSTS
官方链接:
http://denyhosts.sourceforge.net/
下面是官方的简介
What is DenyHosts?
DenyHosts is a script intended to be run by Linux system administrators to help thwart SSH server attacks (also known as dictionary based attacks and brute force attacks).
If you've ever looked at your ssh log (/var/log/secure on Redhat, /var/log/auth.log on Mandrake, etc...) you may be alarmed to see how many hackers attempted to gain access to your server. Hopefully, none of them were successful (but then again, how would you know?). Wouldn't it be better to automatically prevent that attacker from continuing to gain entry into your system?
DenyHosts是用python写的程序,目前是2.5版,通过SSH验证日志筛选出恶意IP放到 /etc/hosts.deny中达到防范的目的
六、制作加固包的想法
这个想法其实上次的经验总结我就提到过,还没去制作。现在想想,我还懒得去改目标文件了,直接替换它不是更爽?当然这样也有弊端,除非你很了解系统的作用,才能量身定做,而且在已有的生产环境中万不可这样操作,否则把别人已有的配置替换了,罪过就大了。
对于这一次的20台同样安装的机器,是可以用程序批量加固,因为加固步骤是一样的,又是没有上线的机器,风险不大,那么可以先用一台做样本,测试无误后,把所有文件打包,上传到被加固的机器里,强行覆盖即可,同时把增加的新文件按照目录所在文字自动解压。写到这里我突然想起,为了保险起见可以把原有配置先备份一次,吔!
比如 hardenLinux.tar 是制作好的包,里面已经包含了目录的路径和文件,用如下命令执行即可:
[root@rh9bk test]# tar tvf hardenLinux.tar
drwxr-xr-x root/root 0 2006-12-04 12:16:54 etc/
-rw-r--r-- root/root 84 2006-12-04 12:18:03 etc/bobkey.sh
[root@rh9bk test]# tar xvf hardenLinux.tar -C /
etc/
etc/bobkey.sh
七、测试效果
7.1.自动安装测试
包括开启自启动、crond自动运行、修改若干配置文件等,测试成功。
7.2.程序运行后效果测试
包括NMAP各参数扫描、superscan全端口扫描、wb连接耗尽攻击、synflood攻击、ping 测试、hydra口令暴力猜解测试、正常连接测试、登陆系统后往外连接测试,测试成功。
nmap.exe -sS -O 192.168.0.100
扫描结果如下,看不到系统类型和端口:
Warning: OS detection will be MUCH less reliable because we did not find at lea
st 1 open and 1 closed TCP port
All 1672 scanned ports on 192.168.0.100 are: filtered
MAC Address: 00:0C:29:06:CE:31 (VMware)
Too many fingerprints match this host to give specific OS details
用其他参数再扫描,同样看不到有效信息
nmap.exe –sN -O
nmap.exe –sX -O
nmap.exe -sT -O
nmap.exe –sF –O
nmap.exe –sO –O
nmap.exe –sA –O
指定端口用arp扫描参数再次扫描,通过对端口开放后的TCP指纹校验,这次能发现开放端口和系统类型:
nmap.exe -v -P0 -O -p 60022 192.168.0.100
Starting Nmap 4.00 (
http://www.insecure.org/nmap
) at 2006-12-10 16:11 中国标准
时间
Initiating ARP Ping Scan against 192.168.0.100 [1 port] at 16:11
The ARP Ping Scan took 1.59s to scan 1 total hosts.
DNS resolution of 1 IPs took 13.04s. Mode: Async [#: 2, OK: 0, NX: 0, DR: 1, SF:
0, TR: 4, CN: 0]
Initiating SYN Stealth Scan against 192.168.0.100 [1 port] at 16:11
Discovered open port 60022/tcp on 192.168.0.100
The SYN Stealth Scan took 0.92s to scan 1 total ports.
Warning: OS detection will be MUCH less reliable because we did not find at lea
st 1 open and 1 closed TCP port
For OSScan assuming port 60022 is open, 43061 is closed, and neither are firewal
led
Insufficient responses for TCP sequencing (1), OS detection may be less accurate
Host 192.168.0.100 appears to be up ... good.
Interesting ports on 192.168.0.100:
PORT STATE SERVICE
60022/tcp open unknown
MAC Address: 00:0C:29:06:CE:31 (VMware)
Device type: firewall|broadband router|general purpose|media device
Running: Fortinet embedded, Level One embedded, Linux 2.4.X|2.6.X, Pace embedded
, Panasonic embedded
Too many fingerprints match this host to give specific OS details
TCP/IP fingerprint:
SInfo(V=4.00%P=i686-pc-windows-windows%D=12/10%Tm=457BC147%O=60022%C=-1%M=000C29
)
T1(Resp=Y%DF=Y%W=16A0%ACK=S++%Flags=AS%Ops=MNNTNW)
T2(Resp=N)
T3(Resp=N)
T4(Resp=N)
T5(Resp=N)
T6(Resp=N)
T7(Resp=N)
PU(Resp=N)
Nmap finished: 1 IP address (1 host up) scanned in 19.738 seconds
Raw packets sent: 23 (1878B) | Rcvd: 4 (206B)
全端口扫描用superscan3测试没发现,nmap.exe -p 1-65535 192.168.0.100,扫描时间24分钟,也没发现开放端口,why?因为被INPUT链条过滤了,如果缩小范围比如 60000-60050,那是可以的,但这样花费的代价更大,所以对于端口的隐藏再加上防火墙的对于扫描探测规则,是可以成功实现的。
ICMP测试
Ping 192.168.0.100 –t
可以ping通,但会最断
Wb连接耗尽测试,可以抵御大部分攻击,其实这里也是可以改进的,用netstat查看某IP发起连接数,一旦超过一定数目,比如10个,就判断为攻击,再把其IP防止防火墙黑名单。
25分钟攻击完512条连接数,被攻击的Linux用netstat查看有34个TIME_WAIT 和 FIN_WAIT2
7分钟攻击完512,被攻击的Linux用netstat查看有197个TIME_WAIT 和 FIN_WAIT2
八、程序附件和说明
8.1程序附件
[root@rh9bk nu]# tar tvf hardenAS4.tar
-rwx--x--x root/root 2812 2006-12-10 14:26:47 hardenAS4.sh
drwxr-xr-x root/root 0 2006-12-10 14:18:54 iptns/
-rwx--x--x root/root 4672 2006-12-10 13:14:01 iptns/iptns.sh
-rw-r--r-- root/root 10 2006-12-10 13:03:18 iptns/wip_list
-rw-r--r-- root/root 28 2006-12-10 06:55:05 iptns/bip_list
-rw-r--r-- root/root 308 2006-12-10 13:10:20 iptns/allbip
-rw-r--r-- root/root 44 2006-12-10 13:10:20 iptns/Failed_count_ip
8.2 hardenAS4.sh
#!/bin/sh
#author:bobkey
#mail:qinbo@nsfocus.com
#date:06.12.01
#for gd.chinamobile cmnet harden Linux AS4
#
if [ -e install.log ];
then
clear
echo -e "\n\n\t\t\talready hardened OS & installed program! QUITTING!!!\n\n" &&exit 0
else
clear
echo -e "\n\n\t\t\tbegin install...\n\n" && echo "`date` first install" > install.log
fi
#
sshdconf="/etc/ssh/sshd_config"
loginconf="/etc/login.defs"
crond="/var/spool/cron/root"
#del sys default accounts
userdel lp
groupdel lp
userdel sync
userdel shutdown
userdel halt
userdel news
userdel uucp
userdel operator
userdel games
userdel gopher
echo "step 1. delete sys default accounts .......................[ OK ]"
#
chattr +a /var/log/messages
chattr +i /var/log/messages.*
echo "step 2. change /var/log/messages* attribute value .........[ OK ]"
#
chmod +t /tmp
chmod 644 /var/log/wtmp
chmod 644 /var/run/utmp
chmod -R 700 /etc/rc.d/init.d/*
echo "step 3. change /tmp wtmp utmp init.d/* attribute value ....[ OK ]"
#
chmod 000 /etc/rc.d/init.d/netfs
chmod 000 /etc/rc.d/init.d/portmap
chmod 000 /etc/rc.d/init.d/sendmail
chmod 000 /etc/rc.d/init.d/ypbind
echo "step 4. disable netfs portmap sendmail ypbind .............[ OK ]"
#
cp -p /etc/profile /etc/profile.bk
chmod 000 /etc/profile.bk
grep "^TMOUT" /etc/profile || sed -i 's/HISTSIZE=1000/HISTSIZE=1000\nTMOUT=300/g' /etc/profile
grep "^umask" /etc/profile||echo "umask 077" >>/etc/profile
echo "step 5. set umask & TMOUT in /etc/porfile .................[ OK ]"
test -e /etc/login.defs.bk||cp -p $loginconf /etc/login.defs.bk && \
echo "step 6. backup /etc/login.defs to /etc/login.defs.bk ......[ OK ]"
sed -i 's/^PASS/#PASS/g' $loginconf && \
echo -e "#by nsfocus's bobkey change 06.12.10\nPASS_MAX_DAYS\t60\nPASS_MIN_DAYS\t7\nPASS_MIN_LEN\t8\nPASS_WARN_AGE\t3" >>$loginconf && \
echo "step 7. reconfig /etc/login.defs ..........................[ OK ]"
test -e /etc/ssh/sshd_config.bk||cp -p $sshdconf /etc/ssh/sshd_config.bk && \
echo "step 8. backup sshd_config to /etc/ssh/sshd_config.bk .....[ OK ]"
sed -i 's/^#Port 22/Port 60022/g' $sshdconf && \
/etc/init.d/sshd restart && \
echo "step 9. reconfig /etc/ssh/sshd_config .....................[ OK ]"
test -e iptns.tar >/dev/null&& tar xvf iptns.tar -C /etc && \
chown root:root /etc/iptns && \
chmod 600 /etc/iptns && \
chmod 700 /etc/iptns/* && \
echo "step 10. install iptns.sh to /etc/iptns/iptns.sh ..........[ OK ]"
grep iptns $crond >/dev/null||echo "*/10 * * * * /etc/iptns/iptns.sh" >>$crond && \
chmod 600 $crond && \
/etc/init.d/crond restart
echo "step 11. set crontab auto run .............................[ OK ] "
grep iptns /etc/rc.d/rc.local >/dev/null||echo "/etc/iptns/iptns.sh" >>/etc/rc.d/rc.local && \
echo "step 12. set program to system startup auto run ...........[ OK ]"
echo -e "\n\n\tALL DONE! TOTAL 12 STEPS\n\n"
8.3 iptns.sh
#!/bin/sh
#name:iptns.sh for chinamobile's cmnet LinuxAS4 machines
#author:bobkey
#mail:qinbo@nsfocus.com
#date:06.12.01
if [ "$1" = "-stop" ]
then
echo -e "\n\n\tStopping the firewall...\n\n"
/etc/rc.d/init.d/iptables restart
exit 0
elif [ "$1" = "-allow" ]
then
echo -e "\n\n\tallow all packets transfers...\n\n"
iptables -F
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -L -n
exit 0
else
echo -e "\tUsage:"
echo -e "\t1. ./`basename $0` -stop # stop the firewall,resume default."
echo -e "\t2. ./`basename $0` -allow # allow packet transfers on all chain. "
echo -e "\t3. ./`basename $0` # go run . "
echo -e "\n\tFiles description:"
echo -e "\t1. `basename $0` is main programe, it's me"
echo -e "\t2. wip_list is white list"
echo -e "\t3. bip_list is black list"
echo -e "\t4. Failed_count_ip is from secure log statistic"
echo -e "\t5. allbip is temp file,recode all block ip address"
fi
echo -e "\n\t\t\t the iptables running now... \033[3;31m by bobkey config \033[0m \n"
echo -e " \t\t\t\t \033[1;32m mail:
qinbo@nsfocus.com
\033[m \n"
#define programme conf file
runpath=`dirname $0`
wip="$runpath/wip_list"
bip="$runpath/bip_list"
secure_log=`lsof /var/log/secure* |grep syslogd|awk '{ print $9 }'`
allbip="$runpath/allbip"
Failed_count_ip="$runpath/Failed_count_ip"
test -s $wip || touch $wip
test -s $bip || touch $bip
#FUNCTION statistic Failed ip from secrue log
function failedip_statistic ()
{
#for REDHAT AS4 secure file format
#cat $secure_log |grep sshd|grep Failed |cut -d: -f7|awk '{ print $1 }' > $allbip
#cat $secure_log |grep sshd|grep Invalid|cut -d: -f7 >> $allbip
#for REDHAT 9 secure file format
cat $secure_log |grep sshd|grep Failed |awk '{ print $11 }' > $allbip
sort $allbip|uniq -c >$Failed_count_ip
#cat /dev/null > $bip
while read count ipaddr;do
if [ $count -ge 5 ];then
echo "add ipaddress $ipaddr to block rules!"
#iptables -A INPUT -s $ipaddr -j DROP
grep $ipaddr $bip >/dev/null||echo $ipaddr >> $bip
#echo $ipaddr >>$bip
fi
done  $f
done
sysctl -w net.ipv4.conf.default.accept_source_route=0
sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1
#sysctl -w net.ipv4.icmp_echo_ignore_all=1
sysctl -w net.ipv4.icmp_ignore_bogus_error_responses=1
sysctl -w net.ipv4.ip_conntrack_max=65535
#
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.tcp_syn_retries=1
sysctl -w net.ipv4.tcp_fin_timeout=5
sysctl -w net.ipv4.tcp_synack_retries=1
sysctl -w net.ipv4.tcp_syncookies=1
sysctl -w net.ipv4.route.gc_timeout=100
sysctl -w net.ipv4.tcp_keepalive_time=500
sysctl -w net.ipv4.tcp_max_syn_backlog=10000
}
#define fw default policy
iptables -F
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
# run funcitons
failedip_statistic
set_kernel
add_wip_to_grant
add_bip_to_block
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p icmp -m limit --limit 6/m --limit-burst 5 -j ACCEPT
iptables -A INPUT -p icmp -m limit --limit 6/m --limit-burst 5 -j ACCEPT
#iptables -A INPUT -p icmp -m length --length 48:256 -j ACCEPT
#kill all portscan detection
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
#limit the connect number
iptables -A INPUT -m limit --limit 1/second -p tcp --tcp-flags ALL SYN --dport 60022 -j ACCEPT
iptables -A INPUT -m limit --limit 1/second -p tcp --tcp-flags ALL FIN --dport 60022 -j ACCEPT
iptables -A INPUT -m limit --limit 1/second -p tcp --tcp-flags ALL RST --dport 60022 -j ACCEPT
iptables -A INPUT -i eth0 -m state --state RELATED,ESTABLISHED -p tcp --dport 60022 -j ACCEPT
date >>/tmp/ipt_run_state
8.4安装界面
图十:运行hardenAS4.sh加固和安装截图
图十一:运行Iptns.sh截图
8.5安装说明
hardenAS4.sh:
为初次安装运行,会自动安装加固(参考Linux加固方案步骤)和安装防火墙程序。删除系统默认帐号,禁用某些网络服务、添加安全选项到/etc/profile、修改某些文件和目录权限、添加crond任务(每10分钟运行一次防火墙程序)和开机运行语句,并且自动修改和备份配置文件,包括/etc/login.defs /etc/ssh/sshd_config。
为了避免出错,此程序只能运行一次,当然非要多运行几次也不会出太大问题,已经做了判断机制。
Iptns.tar程序包说明:
Iptns.sh :为程序主体
运行参数 –stop会恢复iptables和内核网络参数的默认状态
运行参数 –allow 会把所有防火墙链路开放
不加参数,则为启动程序运行
Wip_list:为管理主机的白名单IP地址或网段,可自行增加
Bip_list:为黑名单IP地址或网段,可自行增加
Failed_bip_count:为统计恶意ip和数量的文本,由程序运行产生,不用去修改
Allbip:为所有恶意IP的保存文本,由程序运行产生,不要去修改
安装步骤:
1. 运行sshd –p 10000,再通过sshd的10000端口登陆连接,避免更改端口造成连接中断。
2. 上传install.sh 和nsipt.tar到目标主机的某目录;
3. 执行 sh install.sh;
4. 根据需要增加白名单地址到 /etc/iptns/wip_list,格式为主机 1.1.1.1或网段1.1.1.0/24,如果不确定可以不增加;
5. 黑名单地址为猜错密码5次以上的ip,会自动添加到阻断规则里 ;

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/73129/showart_1130077.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP