日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python-strace命令追踪ssh操作

發布時間:2023/11/29 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.137root@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 -Droot 45477 6861 0 13:57 ? 00:00:00 sshd: root@pts/2root 45478 6861 0 13:57 ? 00:00:00 sshd: root@pts/0root 45479 6861 0 13:57 ? 00:00:00 sshd: root@pts/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 --color=auto ssh[root@136 ~]#

strace追蹤

[root@136 ~]# strace -f -p 45579 -t -o ssh.logstrace: 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()

轉載于:https://www.cnblogs.com/wangxu01/articles/10783712.html

總結

以上是生活随笔為你收集整理的Python-strace命令追踪ssh操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。