o2o网站开发免费发布网站建设信息
o2o网站开发,免费发布网站建设信息,北京网站建站网,购物网站后台订单处理流程Python-strace命令追踪ssh操作 通过strace 命令追踪ssh的进程ID#xff0c;记录操作的命令[实际上是内核里面记录的东西]#xff0c;进行操作日志的Py解析达到效果 追踪进程并写入ssh操作到文件中 Ps: 此时机器A已经ssh登录了机器B#xff0c;取得它的ssh进程PID 机器A登录后…Python-strace命令追踪ssh操作 通过strace 命令追踪ssh的进程ID记录操作的命令[实际上是内核里面记录的东西]进行操作日志的Py解析达到效果 追踪进程并写入ssh操作到文件中 Ps: 此时机器A已经ssh登录了机器B取得它的ssh进程PID 机器A登录后的操作命令就记录在了ssh.log文件中了 登陆从A登陆B机器 [root136 ~]# ssh 192.168.0.137root192.168.0.137s password:Last login: Sun Apr 28 13:59:08 2019 from 192.168.0.136[root137 ~]# 取得PID [root136 ~]# ps -ef | grep ssh root 6861 1 0 4月19 ? 00:00:00 /usr/sbin/sshd -Droot 45477 6861 0 13:57 ? 00:00:00 sshd: rootpts/2root 45478 6861 0 13:57 ? 00:00:00 sshd: rootpts/0root 45479 6861 0 13:57 ? 00:00:00 sshd: rootpts/1root 45579 45483 0 14:04 pts/1 00:00:00 ssh 192.168.0.137root 45591 45485 0 14:07 pts/2 00:00:00 grep --colorauto ssh[root136 ~]#strace追踪 [root136 ~]# strace -f -p 45579 -t -o ssh.logstrace: Process 45579 attached[root136 ~]#远端执行命令并退出 1 ls2 3 df -h4 5 ifconfig6 7 ls8 9 touch aa
10
11 echo 123 aa
12
13 vim aa 本地分析 基本取得远端执行的命令[root136 ~]# 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][root136 ~]# 脚本 1 #_*_coding:utf-8_*_2 3 import re4 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 complication24 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 continue60 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 True96 97 continue98 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() 转载于:https://www.cnblogs.com/wangxu01/articles/10783712.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/92070.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!