记一次树莓派(Raspbetty)系统ssh被爆破攻击日志分析
- 前面的文章有过提及,这个博客的载体是一块树莓派,就像下面图片的这样

今天在家无聊,所以想看下系统的登录日志,真应的那一句话,不看不知道,一看吓一跳
,我TMD居然被爆破攻击了,想起来了前几天需要有个东西测试下,远程到服务器上开放了一个ssh端口没有关闭,才隔两三天居然被爆破
攻击了,不过我当时应该是没有慌乱,因为之前在ssh服务上有过加固,爆破成功可能性不大,所以就开启了树莓派分析日志的过程,在此留一个记录。
- 第一步分析日志查看系统是否被成功登录
- 第二步查找IP归属地及攻击次数
树莓派里面有三个文件需要我们进行分析:(下面三个文件都在/var/log/
目录下)
auth. log 可以看到登陆的情况
btmp 中可以查看到登陆失败的记录(可通过lastb命令进行检查)
lastlog 中可以查看最近登陆的记录(可通过last命令进行检查)
看下系统最近登录失败的记录:
grep "Failed password" /var/log/auth.log | wc -l

看下系统最近登录成功的记录:
grep "password" /var/log/auth.log| grep -v Failed | grep -v Invalid

简单看了下,问题不大,因为都是自己的IP地址,最近也就自己登录了进来,大家应该发现了,这个应该是最近的登录记录,说明对方并没有成功登录过,我们还需要继续分析
我们查询下恶意攻击的IP及他们攻击的次数
cat /var/log/auth.log | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2" = "$1;}'

可以看到,这个攻击数量是比较大的,特别是60.191.11.242这个IP地址,居然爆破了14657次,实在是服~
先看下所有的auth 文件
ls /var/log/auth.log* -lht

可以看到系统目前产生了 5 个日志文件了。刚刚看的 auth.log 文件只是最近的日志,我们将日志全部放在一个目录里面进行分析
awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(ip in ips){print ip, ips[ip]}}' auth.* | wc -l

看情况还好,只有36个IP地址扫描过我这一台服务器,不算多也不算少,按照攻击我们排一个序看下
awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(ip in ips){print ip, ips[ip]}}' auth.* | sort -k2 -rn | head

可以通过上图发现,攻击排名第一的总共攻击了16万多次,牛批~
我们可以将上述IP保存到文件,以便后续查证
awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(ip in ips){print ip, ips[ip]}}' auth.* | sort -k2 -rn > ip.log
查看下IP地址他们的具体分布情况,只查文件top10的
head -10 ip.log | awk '{print $1" ";system("curl //freeapi.ipip.net/"$1);print("\n")}'

查看攻击所使用的用户名及尝试次数
awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(user in users){print user, users[user]}}' auth.* | sort -k2 -rn |wc -l
#查询总爆破次数
awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(user in users){print user, users[user]}}' auth.* | sort -k2 -rn | head
#查询爆破使用用户名及次数

可以看得出来,root绝对不是什么安全密码,我这里被爆破38682次,不过我这里有安全策略,你密码错两次就锁定了,锁定后输入正确的密码也无法登录成功,所以更别说其他系统不存在的用户名了,所以此次攻击者并没有爆破成功,不过还是建议将不必要的端口封闭,不要留下安全隐患~
将查询的保存到文件
awk '{if($6=="Failed"&&$7=="password"){if($9=="invalid"){ips[$13]++;users[$11]++}else{users[$9]++;ips[$11]++}}}END{for(user in users){print user, users[user]}}' auth.* | sort -k2 -rn > username.log
其他细节;
查看日志位置
一般来说 ubuntu 在 /var/log/auth.log
centos 在 /var/log/secure
这两者格式不一样。
具体在什么位置是在 rsyslog 的配置文件里:/etc/rsyslog.conf
或者 /etc/rsyslog.d
(当然你的系统必须是用 rsyslog 的才行,如果不是用这个就不在这里,使用命令 ps aux | grep "rsyslog" | grep -v "grep"
看系统里有没有 rsyslog 进程可以判断系统是否使用 rsyslog)
ubuntu 18.04
auth,authpriv.* /var/log/auth.log
具体位置是 /etc/rsyslog.d/50-default.conf
CentOS 7.6
# The authpriv file has restricted access.
authpriv.* /var/log/secure
具体位置是 /etc/rsyslog.conf
命令:
查看成功登录
grep "Accepted password for" /var/log/secure
查看每个用户名失败的次数
grep "Failed password" /var/log/secure | awk '{if (NF==16){c[$11]++}else{c[$9]++}}END{for(u in c)print u,c[u]}' | sort -k 2 -nr | head
查看每个 IP 地址失败的次数
grep "Failed password" /var/log/secure | awk '{if (NF==16){c[$13]++}else{c[$11]++}}END{for(u in c)print u,c[u]}' | sort -k 1 -n | head

































