黑帽联盟

标题: shell防ftp暴力破解 [打印本页]

作者: 定位    时间: 2017-3-22 21:50
标题: shell防ftp暴力破解
朋友的服务器搭建了ftp,这几天一直在被暴力破解,找到我要我帮忙解决这个问题,登录到他服务器上一看,是用的Pure-ftp搭建的ftp服务器,它日志文件跟系统日志文件绑定在一起:/var/log/messages,知道了这些后,专门从网上找了个脚本给他,让脚本去帮他挡攻击.

脚本内容:
  1. #!/usr/bin/python
  2. #This script can deny anythings to all of ports
  3. import os,re

  4. #Find time today
  5. tm=os.popen('date').read()

  6. #Basic steup
  7. deny_port= '21'
  8. log_path = '/var/log/messages'
  9. ip_count= '30'
  10. aut_message = tm[4:10]+ '.*pure-ftpd.*failed.*'
  11. list_path='/shell/ip_list.txt'

  12. #The program run to decide
  13. class port:

  14.     def re_ip(self):
  15.         r_file=open(log_path,'r').read()
  16.         s_rule=re.compile(aut_message,re.I)
  17.         n_rule=s_rule.findall(r_file)
  18.         self.f_ip =re.findall('\d+\.\d+\.\d+\.\d+',''.join(n_rule))

  19.     def loop_list(self):
  20.         for ip in set(self.f_ip):
  21.             if not os.path.isfile(list_path):
  22.                 os.mknod(list_path)
  23.             self.ip_list=open(list_path,'rw+')
  24.             if re.search(ip,self.ip_list.read()) is None:
  25.                 if self.f_ip.count(ip) >= int(ip_count):
  26.                     self.ip_list.write(ip+'\n')
  27.                 self.ip_list.seek(0)
  28.             else:
  29.                 self.ip_list.seek(0)

  30.     def iptables(self):
  31.         for ip in self.ip_list.readlines():
  32.             iptables_list=os.popen('iptables --list --numeric').read()
  33.             if re.search(ip.strip('\n'),iptables_list) is None:
  34.                 if re.search('RH-Firewall-1-INPUT',iptables_list,re.I) is None:
  35.                     os.system("iptables -I INPUT 2 -m state --state NEW -s %s -m tcp -p tcp --dport %s -j DROP"%(ip.strip('\n'),deny_port))
  36.                 else:
  37.                     os.system("iptables -I RH-Firewall-1-INPUT 2 -m state --state NEW -s %s -m tcp -p tcp --dport %s -j DROP"%(ip.strip('\n'),deny_port))

  38. D=port()
  39. D.re_ip()
  40. D.loop_list()
  41. D.iptables()
复制代码
脚本实现的原理是:
通过程序读取->带登录信息验证的日志文件->调用iptables直接进行封杀

把脚本丢到系统的计划任务里实现自动抵御:
chmod +x /root/soft_shell/deny_port.py
vi /etc/crontab
*/3 * * * * root /root/soft_shell/deny_port.py

好了,做完这些重启下系统的crond服务,等脚本自己去封杀ip.






欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5