一、登录ssh失败次数统计

1)错误的打开方式

awk '/Failed password/ {print $(NF-3)}' secure |sort -n |uniq -c|sort -n |tail /var/log/secure

2)拷贝文件,再查看失败

cp /var/log/secure .

awk '/Failed password/ {print $(NF-3)}' secure |sort -n |uniq -c|sort -n |tail

3)直接查看失败

$ awk '/Failed password/ {print $(NF-3)}' /var/log/secure |sort -n |uniq -c|sort -n

4)查看最近失败的时间

less  /var/log/secure

按G

二、对于防破解问题的处理

1)禁止密码登录方式

vi  /etc/ssh/sshd_config

2)禁止失败的IP登录的方式

#
# hosts.deny This file contains access rules which are used to
# deny connections to network services that either use
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd:192.168.2.41:deny

/etc/hosts.deny

在/etc/hosts.deny文件下面

添加 sshd:192.168.2.41:deny

重启sshd

三、实现python自动化写入文件

1)获取到失败IP的文件

awk '/Failed password/ {print $(NF-3)}' /var/log/secure |sort -n |uniq -c|sort -n  > ip_fail.txt

2)查看原有的被限制IP的文件

3)执行python脚本文件

def ip_index():
#读取文件获取到已经有被限制的IP
ip_list = set()
with open('hosts.deny',mode='r',encoding='utf-8') as f_log:
for line in f_log:
line = line.split('\n')[].split(' ')[]
if len(line) != and not line[].startswith("#"):
line = line.split(":")
ip_list.add(line[])
return ip_list def write():
# 写入失败的IP到配置文件中
with open('ip_fail.txt',mode='r',encoding='utf-8') as f:
for line in f:
line = line.split('\n')[].split(' ')
if int(line[]) > :
print('登录失败次数大于2的IP',line[])
with open('hosts.deny',mode='a',encoding='utf-8') as f:
if line[] not in ip_list:
f.write('sshd:%s:deny\n'%line[]) if __name__ == '__main__':
ip_list = ip_index()
write()

ip_add=>hosts.deny

四、定时任务自动写入hosts.deny配置文件的脚本

1)该脚本以失败次数大于3的进行测试(执行环境python3)

import subprocess
command = "awk '/Failed password/ {print $(NF-3)}' /var/log/secure |sort -n |uniq -c|sort -n"
def result(command):
# 获取命令结果
obj=subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
return obj.stdout def ip_list(result):
# 根据命令结果获取到失败IP的字典
ip_set={}
for line in result:
line=str(line)
ip = line.split(' ')[-].split('\\n')[]
count = line.split(' ')[-]
# 失败次数大于3的
if int(count) > :
ip_set[count]=ip
return ip_set def ip_index():
#读取文件获取到已经有被限制的IP
out_ip = set()
with open('/etc/hosts.deny',mode='r',encoding='utf-8') as f_log:
for line in f_log:
line = line.split('\n')[].split(' ')[]
if len(line) != and not line[].startswith("#"):
line = line.split(":")
out_ip.add(line[])
return out_ip def write(out_ip,in_ip):
with open('/etc/hosts.deny',mode='a',encoding='utf-8') as f:
for ip in out_ip:
if out_ip[ip] not in in_ip:
f.write('sshd:%s:deny\n'%out_ip[ip]) if __name__ == '__main__':
in_ip = ip_index() # 获取已有被限制的IP
result = result(command) # 得到命令结果
out_ip=ip_list(result) # 根据命令结果获取IP列表
write(out_ip,in_ip)

2) centos6默认的python2.6执行环境

import subprocess
command = "awk '/Failed password/ {print $(NF-3)}' /var/log/secure |sort -n |uniq -c|sort -n"
def result(command):
obj=subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
return obj.stdout def ip_list(result):
ip_set={}
for line in result:
line=str(line)
ip = line.split(' ')[-].split('\\n')[]
count = line.split(' ')[-]
if int(count) > :
ip_set[count]=ip
return ip_set def ip_index():
out_ip = set()
with open('/etc/hosts.deny',mode='r') as f_log:
for line in f_log:
line = line.split('\n')[].split(' ')[]
if len(line) != and not line[].startswith("#"):
line = line.split(":")
out_ip.add(line[])
return out_ip def write(out_ip,in_ip):
with open('/etc/hosts.deny',mode='a') as f:
for ip in out_ip:
if out_ip[ip] not in in_ip:
f.write('sshd:%s:deny\n'%out_ip[ip]) if __name__ == '__main__':
in_ip = ip_index()
result = result(command)
out_ip=ip_list(result)
write(out_ip,in_ip)

3)修改bug。(以IP为key),前面是以次数为key。

import subprocess
command = "awk '/Failed password/ {print $(NF-3)}' /var/log/secure |sort -n |uniq -c|sort -n"
def result(command):
# 获取命令结果
obj=subprocess.Popen(command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
return obj.stdout def ip_list(result):
# 根据命令结果获取到失败IP的字典
ip_set={}
for line in result:
line=str(line)
ip = line.split(' ')[-].split('\\n')[]
count = line.split(' ')[-]
# 失败次数大于3的
if int(count) > :
ip_set[ip]=count
return ip_set def ip_index():
#读取文件获取到已经有被限制的IP
out_ip = set()
with open('/etc/hosts.deny',mode='r',encoding='utf-8') as f_log:
for line in f_log:
line = line.split('\n')[].split(' ')[]
if len(line) != and not line[].startswith("#"):
line = line.split(":")
out_ip.add(line[])
return out_ip def write(out_ip,in_ip):
with open('/etc/hosts.deny',mode='a',encoding='utf-8') as f:
for ip in out_ip:
print(ip)
if ip not in in_ip:
f.write('sshd:%s:deny\n'%ip) if __name__ == '__main__':
in_ip = ip_index() # 获取已有被限制的IP
result = result(command) # 得到命令结果
out_ip=ip_list(result) # 根据命令结果获取IP列表
write(out_ip,in_ip)

登录服务器失败 IP 统计和处理方法的更多相关文章

  1. linux ssh 登录服务器失败,密码明明没错【解决】

    本来这样登录的: $ ssh 123.123.123.123 //ssh + IP 然后输入密码就是登录不了,显示permision denied 后来使用如下方式登录,成功! $ ssh -v us ...

  2. MySQL无法登录服务器解决方法

    提示:#2000 无法登录 MySQL 服务器 今天用本机装了个phpMyAdmin,版本3.4.8,想用它来连一台内网服务器上的Mysql,于是乎修改phpMyAdmin配置文件config.inc ...

  3. Ubuntu下通过SSH远程登录服务器的方法

    1. 首先在服务器上安装ssh的服务器端.$ sudo aptitude install openssh-server 2. 启动ssh-server.$ /etc/init.d/ssh restar ...

  4. TFS 改服务器IP 域名 端口方法

    长春电信伴随着开始的严打,所有未备案的80,8080等常用web端口都被封,使得原用8080作为服务端口的tfs代码服务器无法使用,现提供方法如下: 1.关掉VS 2.去掉要改的解决方案的sln文件的 ...

  5. 登录Linux服务器显示IP和自定义备注

    默认搭建好的Linux服务器,使用Xshell登录的窗口如下所示: 可根据需要执行如上代码,再重新登录服务器,效果如下图所示: 代码片段:echo "export PS1='\u@\[\e[ ...

  6. 域名做CDN来通过隐藏服务器真实IP的方法来防止DDoS攻击(转)

    隐藏服务器真实IP是解决问题最好和最快的方法,但只针对小流量,大流量同样会扛不住. 服务器前端加CDN中转,比如阿里云.百度云加速.360网站卫士.加速乐.安全宝等,如果资金充裕的话,可以购买高防的盾 ...

  7. 隐藏服务器真实IP的方法来防止DDOS攻击

    2017-08-22 作者:小唐 点击: 10,500次 在无盘系统的环境下,服务器软件存在漏洞,就容易受到DDOS攻击,隐藏服务器真实IP是解决问题最好的方法,下面小编与大家分享一下隐藏服务器真实I ...

  8. 防止DDOS攻击有效方法:隐藏服务器真实IP

    如今,网站服务器的安全受到越来越多的重视,但是难免会遇到黑客使用DDoS攻击网站,为了网站的安全通常都会做好防御,其中防止DDoS攻击有效方法:隐藏服务器真实IP ,该技术能够有效地保护网站的安全. ...

  9. centos linux ip地址无法连接数据库,ssh登录服务器时必须使用22端口

    问题一:连接数据库时直接使用ip地址无法连接,必须使用ssh方式才能连接? 问题二:ssh登录服务器时必须使用22端口,在/etc/ssh/sshd_config中添加了10086端口,防火墙中已开启 ...

随机推荐

  1. 启动kafka时报scala相关错误:java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc()

    1.报错现象: 启动kafka的时候启动失败,并且会报告下面的错误: java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(Ljava/lang/ ...

  2. 在Docker中监控Java应用程序的5个方法

    译者注:Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.通常情况下,监控的主要目的在于:减少宕机 ...

  3. CSS之metra&title&base&target

    <!DOCTYPE html><html lang="en"><head> <style type="text/css" ...

  4. 《算法导论》——计数排序Counting Sort

    今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...

  5. kettle实现多表同步

    本样例实现源库的所有表到目标库的同步sqlserver=>mysql(目标表存在表结构则同步),总调度如下: 由于复制记录到结果保存了多个表名,存在多个值,在高级选择对每个输入行执行一次进行循环 ...

  6. js对象拷贝遇到的坑

    问题:通过拷贝赋值后,所有的对象的name居然都是C test(){ let person = [{'name':'danny'}] let names = ['A','B','C'] let tem ...

  7. java中封装类(一)

    java中封装类共九个,分别是Boolean,Byte,Short,Integer,Long,Float,Double,Character,Void 其中Void对于使用者并无多大意义,也不可以构造任 ...

  8. 一个nginx 回源限速的bug处理过程记录

    一个生产环境,nginx占用cpu很高. top - :: up day, :, users, load average: 13.26, 13.20, 13.20 Tasks: total, runn ...

  9. Blob分析--粘连颗粒检测 基于距离变换的分水岭区域分割 盆地与原连通域求交集

    文章转自微信公众号:机器视觉那些事 *******************************************************************公众号:机器视觉那些事儿*** ...

  10. 原生js实现Base64编码解码

    注:ie10+ var str = window.btoa("liusong"); console.log(str); var s = window.atob("bGl1 ...