审计系统---堡垒机项目之strace追踪ssh

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

修改ssh源码添加访问标志位

源码下载:【本文示例:openssh-7.4p1.tar.gz】

https://cloudflare.cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/

导入工程到PyCharm

ssh.c

image

image

image

在ubuntu上安装openssl dev组件

1
2
3
sudo apt-get install zlib1g
sudo apt-get install zlib1g-dev
sudo apt-get install libssl-dev

上传软件包到Ubuntu并解压: 

1
2
3
4
5
6
unzip openssh-7.4p1.zip
  cd src/openssh-7.4p1/
chmod 750 ./mkinstalldirs
chmod 7500 ./configure     
  sudo ./configure --prefix=/usr/local/openssh7/
sudo make && sudo make install    【make clean 可以清除上次的编译结果】

image

image

登录成功

1
omc@omc-virtual-machine:~/CityHunter$ python3 user_enterpoint.py

image

 

audit.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#_*_coding:utf-8_*_
import re
class AuditLogHandler(object):
    '''分析audit log日志'''
    def __init__(self, log_file):
        self.log_file_obj = self._get_file(log_file)
    def _get_file(self,log_file):
        return open(log_file)
    def parse(self):
        cmd_list = []
        cmd_str = ''
        catch_write5_flag = False #for tab complication
        for line in self.log_file_obj:
            #print(line.split())
            line = line.split()
            try:
                pid,time_clock,io_call,char = line[0:4]
                if io_call.startswith('write(9'):
                    if char == '"\\177",':#回退
                        char = '[1<-del]'
                    if char == '"\\33OB",': #vim中下箭头
                        char = '[down 1]'
                    if char == '"\\33OA",': #vim中下箭头
                        char = '[up 1]'
                    if char == '"\\33OC",': #vim中右移
                        char = '[->1]'
                    if char == '"\\33OD",': #vim中左移
                        char = '[1<-]'
                    if char == '"\33[2;2R",': #进入vim模式
                        continue
                    if char == '"\\33[>1;95;0c",':  # 进入vim模式
                        char = '[----enter vim mode-----]'
 
 
                    if char == '"\\33[A",': #命令行向上箭头
                        char = '[up 1]'
                        catch_write5_flag = True #取到向上按键拿到的历史命令
                    if char == '"\\33[B",':  # 命令行向上箭头
                        char = '[down 1]'
                        catch_write5_flag = True  # 取到向下按键拿到的历史命令
                    if char == '"\\33[C",':  # 命令行向右移动1位
                        char = '[->1]'
                    if char == '"\\33[D",':  # 命令行向左移动1位
                        char = '[1<-]'
 
                    cmd_str += char.strip('"",')
                    if char == '"\\t",':
                        catch_write5_flag = True
                        continue
                    if char == '"\\r",':
                        cmd_list.append([time_clock,cmd_str])
                        cmd_str = ''  # 重置
                    if char == '"':#space
                        cmd_str += ' '
 
                if catch_write5_flag:  # to catch tab completion
                    if io_call.startswith('write(5'):
                        if io_call == '"\7",':  # 空键,不是空格,是回退不了就是这个键
                            pass
                        else:
                            cmd_str += char.strip('"",')
                        catch_write5_flag = False
            except ValueError as e:
                print("\033[031;1mSession log record err,please contact your IT admin,\033[0m",e)
 
        #print(cmd_list)
        for cmd in cmd_list:
            print(cmd)
        # return cmd_list
 
if __name__ == "__main__":
    parser = AuditLogHandler('ssh.log')
    parser.parse()

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

Ps: 此时机器A已经ssh登录了机器B,这里的2087就是它的ssh进程PID

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

1
omc@omc-virtual-machine:~$ sudo strace -f -p 2087 -ttt -o /home/omc/ssh.log

image

追踪到的ssh操作文件的解析:

1
2
3
4
omc@omc-virtual-machine:~/CityHunter/backend$ cd /home/omc/CityHunter/backend
omc@omc-virtual-machine:~/CityHunter/backend$ vim audit.py
omc@omc-virtual-machine:~/CityHunter/backend$ cp /home/omc/ssh.log ./  [复制文件到当前目录]
omc@omc-virtual-machine:~/CityHunter/backend$ python3 audit.py

image

posted @   小a玖拾柒  阅读(1439)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示