黑帽联盟

 找回密码
 会员注册
查看: 1199|回复: 0
打印 上一主题 下一主题

[脚本语言] shell防ftp暴力破解

[复制链接]

852

主题

38

听众

3178

积分

管理员

Rank: 9Rank: 9Rank: 9

  • TA的每日心情

    昨天 21:43
  • 签到天数: 1539 天

    [LV.Master]伴坛终老

    朋友的服务器搭建了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.

    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    勿忘初心,方得始终!
    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部