免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1150 | 回复: 0

自动封堵暴力破解ssh账号的入侵者 [复制链接]

论坛徽章:
0
发表于 2011-12-21 08:43 |显示全部楼层
无限发现他的ssh服务器有人企图暴力破解账号,我也自查了一下,发现我也一样受到了此类攻击,ssh中的相关log如下:

  1. May 10 23:20:21 localhost sshd[9075]: Invalid user x from x.x.x.x
明显x.x.x.x在进行账号猜测。随即写了个ruby脚本,监控sshd的此类日志,并对连续输错用户名十次以上的IP用iptables进行封堵。

  1. #!/usr/bin/ruby
  2. #

  3. class Daemon
  4.   def Daemon.start
  5.     exit!(0) if fork
  6.     Process::setsid
  7.     exit!(0) if fork
  8.     Dir::chdir("/")
  9.     File::umask(0)
  10.     STDIN.reopen("/dev/null")
  11.     STDOUT.reopen("/dev/null", "w")
  12.     STDERR.reopen("/dev/null", "w")
  13.     yield if block_given?
  14.   end
  15. end

  16. def block_ip(ip)
  17.   cmd = "iptables -A block_ip -s #{ip} -j DROP"
  18.   system(cmd)
  19. end

  20. def block_invalid(filename)
  21.   block_limit = 10

  22.   log_file = File.new(filename)
  23.   ips = Hash.new
  24.   blocked_ips = Hash.new
  25.   log_file.each do |line|
  26.     field = line.split
  27.     if field[5] == "Invalid"
  28.       ip = field[field.length - 1]
  29.       if ips.key?(ip)
  30.         ips[ip] += 1
  31.       else
  32.         ips[ip] = 1
  33.       end
  34.       if ips[ip] > block_limit and not blocked_ips.key?(ip)
  35.         blocked_ips[ip] = 1
  36.         block_ip(ip)
  37.       end
  38.     end
  39.   end
  40. end

  41. if system("iptables -nvL block_ip &>/dev/null")
  42.   system("iptables -F block_ip")
  43. else
  44.   system("iptables -N block_ip")
  45.   system("iptables -I INPUT -j block_ip")
  46. end

  47. Daemon.start do
  48.   block_invalid("/var/lib/myips.fifo")
  49. end
本质上,这就是一个简单的LIPS--基于日志的入侵防护系统。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP