Python-strace命令追踪ssh操作

Python-strace命令追踪ssh操作

通过strace 命令追踪ssh的进程ID,记录操作的命令[实际上是内核里面记录的东西],进行操作日志的Py解析达到效果

追踪进程并写入ssh操作到文件中

Ps: 此时机器A已经ssh登录了机器B,取得它的ssh进程PID

机器A登录后的操作命令就记录在了ssh.log文件中了

登陆从A登陆B机器

[root@136 ~]# ssh 192.168.0.137

root@192.168.0.137's password:

Last login: Sun Apr 28 13:59:08 2019 from 192.168.0.136

[root@137 ~]#        

取得PID

[root@136 ~]# ps -ef | grep ssh                        

root       6861      1  0 4月19 ?       00:00:00 /usr/sbin/sshd -D

root      45477   6861  0 13:57 ?        00:00:00 sshd: root@pts/2

root      45478   6861  0 13:57 ?        00:00:00 sshd: root@pts/0

root      45479   6861  0 13:57 ?        00:00:00 sshd: root@pts/1

root      45579  45483  0 14:04 pts/1    00:00:00 ssh 192.168.0.137

root      45591  45485  0 14:07 pts/2    00:00:00 grep --color=auto ssh

[root@136 ~]#

strace追踪

[root@136 ~]# strace -f -p 45579 -t -o ssh.log
 
 strace: Process 45579 attached
[root@136 ~]#

 

远端执行命令并退出

 1 ls
 2 
 3 df -h
 4 
 5 ifconfig
 6 
 7 ls
 8 
 9 touch aa
10 
11 echo '123' >> aa
12 
13 vim aa

本地分析

基本取得远端执行的命令

[root@136 ~]#     python audit.py

['14:07:20', '\\r']

['14:07:22', 'ls\\rdf -h\\r']

['14:07:24', 'ifcon\\t\\r']

['14:07:25', 'ls\\r']

['14:07:30', 'vim\\10\\10\\10\\10touc h aa\\r']

['14:07:37', "\\10echo '[1<-]123[->1] >> aa\\t\\r"]

['14:07:39', 'vim aa\\t\\r']

['14:07:44', '\\33[2;2R\\33[>1;10;0c[down 1]o456\\33:wq\\r']

[root@136 ~]#      

脚本

  1 #_*_coding:utf-8_*_
  2 
  3 import re
  4 
  5 class AuditLogHandler(object):
  6 
  7     '''分析audit log日志'''
  8 
  9     def __init__(self, log_file):
 10 
 11         self.log_file_obj = self._get_file(log_file)
 12 
 13     def _get_file(self,log_file):
 14 
 15         return open(log_file)
 16 
 17     def parse(self):
 18 
 19         cmd_list = []
 20 
 21         cmd_str = ''
 22 
 23         catch_write5_flag = False #for tab complication
 24 
 25         for line in self.log_file_obj:
 26 
 27             #print(line.split())
 28 
 29             line = line.split()
 30 
 31             try:
 32 
 33                 pid,time_clock,io_call,char = line[0:4]
 34 
 35                 if io_call.startswith('read(4'):
 36 
 37                     if char == '"\\177",':#回退
 38 
 39                         char = '[1<-del]'
 40 
 41                     if char == '"\\33OB",': #vim中下箭头
 42 
 43                         char = '[down 1]'
 44 
 45                     if char == '"\\33OA",': #vim中下箭头
 46 
 47                         char = '[up 1]'
 48 
 49                     if char == '"\\33OC",': #vim中右移
 50 
 51                         char = '[->1]'
 52 
 53                     if char == '"\\33OD",': #vim中左移
 54 
 55                         char = '[1<-]'
 56 
 57                     if char == '"\33[2;2R",': #进入vim模式
 58 
 59                         continue
 60 
 61                     if char == '"\\33[>1;95;0c",':  # 进入vim模式
 62 
 63                         char = '[----enter vim mode-----]'
 64 
 65  
 66 
 67  
 68 
 69                     if char == '"\\33[A",': #命令行向上箭头
 70 
 71                         char = '[up 1]'
 72 
 73                         catch_write5_flag = True #取到向上按键拿到的历史命令
 74 
 75                     if char == '"\\33[B",':  # 命令行向上箭头
 76 
 77                         char = '[down 1]'
 78 
 79                         catch_write5_flag = True  # 取到向下按键拿到的历史命令
 80 
 81                     if char == '"\\33[C",':  # 命令行向右移动1位
 82 
 83                         char = '[->1]'
 84 
 85                     if char == '"\\33[D",':  # 命令行向左移动1位
 86 
 87                         char = '[1<-]'
 88 
 89  
 90 
 91                     cmd_str += char.strip('"",')
 92 
 93                     if char == '"\\t",':
 94 
 95                         catch_write5_flag = True
 96 
 97                         continue
 98 
 99                     if char == '"\\r",':
100 
101                         cmd_list.append([time_clock,cmd_str])
102 
103                         cmd_str = ''  # 重置
104 
105                     if char == '"':#space
106 
107                         cmd_str += ' '
108 
109  
110 
111                 if catch_write5_flag:  # to catch tab completion
112 
113                     if io_call.startswith('write(5'):
114 
115                         if io_call == '"\7",':  # 空键,不是空格,是回退不了就是这个键
116 
117                             pass
118 
119                         else:
120 
121                             cmd_str += char.strip('"",')
122 
123                         catch_write5_flag = False
124 
125             except ValueError as e:
126 
127                 print("\033[031;1mSession log record err,please contact your IT admin,\033[0m",e)
128 
129  
130 
131     #    print(cmd_list)
132 
133         for cmd in cmd_list:
134 
135             print(cmd)
136 
137         # return cmd_list
138 
139  
140 
141 if __name__ == "__main__":
142 
143     parser = AuditLogHandler('ssh.log')
144 
145     parser.parse()
posted on 2019-04-28 14:16  光阴8023  阅读(1356)  评论(0编辑  收藏  举报