最近工作中遇到一些疑似黑客入侵的案例:
客户在阿里云上购买了ECS云服务器作为应用服务器,应用运行中收到了阿里云来的异地IP登录警告短信,提示有可能登录密码已被黑客攻破
于是找到我们服务商帮忙分析处理
正常来说我们会先建议看是看该IP是否还在登录,如在就立刻kill掉其登陆会话
[root@iZ9408690vwZ ~]# who
root tty1 2016-06-28 00:08
root pts/0 2016-06-28 00:02 (223.73.58.208)
diyu pts/1 2016-06-28 00:16 (116.23.247.151)
root pts/2 2016-06-28 01:32 (223.73.58.208)
[root@iZ9408690vwZ ~]# ps
PID TTY TIME CMD
14245 pts/2 00:00:00 bash
14282 pts/2 00:00:00 ps
然后就让客户加固一下安全配置(换ssh端口,去除ssh弱算法支持,更换复杂度高一些的密码...)
最后就看异地登录者干了些什么,于是用了history命令打算查看该IP在相应的登录时间里操作了什么命令(当然也有可能已被黑客篡改)
[root@iZ9408690vwZ ~]# last
root pts/2 223.73.58.208 Tue Jun 28 01:32 still logged in
diyu pts/2 116.23.247.151 Tue Jun 28 00:41 - 01:08 (00:26)
diyu pts/2 116.23.247.151 Tue Jun 28 00:28 - 00:41 (00:12)
diyu pts/2 116.23.247.151 Tue Jun 28 00:27 - 00:28 (00:00)
diyu pts/2 116.23.247.151 Tue Jun 28 00:27 - 00:27 (00:00)
diyu pts/2 116.23.247.151 Tue Jun 28 00:18 - 00:27 (00:08)
[root@iZ9408690vwZ ~]# history
7 telnet 127.0.0.1 1234
8 ls
9 service iptables status
10 icmp-type
11 service iptables status
12 service iptables restart
13 service iptables status
14 iptables -t nat -F
15 service iptables status
16 service iptables save
可见客户没设置bash里历史记录的时间
开启bash的历史记录的时间记录方法为:
在用户配置文件中加入命令:
export HISTTIMEFORMAT="`whoami` : %F %T :"
配置了该环境变量后的history输出为:
[root@iZ9408690vwZ ~]# history
10 root : 2016-06-28 01:32:29 :icmp-type
11 root : 2016-06-28 01:32:29 :service iptables status
12 root : 2016-06-28 01:32:29 :service iptables restart
13 root : 2016-06-28 01:32:29 :service iptables status
14 root : 2016-06-28 01:32:29 :iptables -t nat -F
15 root : 2016-06-28 01:32:29 :service iptables status
16 root : 2016-06-28 01:32:29 :service iptables save
...
1006 root : 2016-06-28 01:32:29 :history
1007 root : 2016-06-28 01:32:29 :ps
1008 root : 2016-06-28 02:00:20 :export HISTTIMEFORMAT="`whoami` : %F %T :"
1009 root : 2016-06-28 02:00:23 :history
这样一来就有用户和执行时间显示了,但留意到执行设置该环境变量前的记录都为登录时间
这是因为之前的都在没设置该环境变量时的记录,在~/.bash_history里就没存相应的执行时间数据
#1467043141
less /var/log/messages
#1467043359
logout
#1467043373
export HISTTIMEFORMAT="`whoami` : %F %T :"
#1467043375
history
设置该环境变量后的历史记录前会多出一个时间戳的数据
以上只针对bash类型的shell, history是具体的shell里实现的
其他类型的shell: sh,tcsh,ksh