跳转至

T1546-004-linux-.bash_profile和.bashrc

来自ATT&CK的描述

~/.bash_profile和.bashrc是在新shell打开时或用户登录时在用户的上下文中执行,以便正确设置其环境。为登录shell执行,并为交互式非登录shell执行。这意味着当用户(通过用户名和密码)登录到控制台(本地或通过SSH等远程登录)时,会在将初始命令提示符返回给用户之前执行。每次打开新的shell时都会执行。这允许用户在需要执行某些命令时进行更细粒度的控制。

这些文件是由本地用户编写的,用于配置自己的环境; 但是,每次用户登录或打开新的shell时,攻击者可以在这些文件中插入代码以获得持久性。

测试案例

echo“#{command_to_add}”>>〜/ .bashrc

echo“#{command_to_add}”>>〜/ .bash_profile

检测日志

linux audit日志 (值得注意的是:Ubuntu默认情况下没有audit,需要下载安装并配置相关策略)

bash历史记录

测试复现

暂无

测试留痕

linux_audit日志

type=SYSCALL msg=audit(1565526007.269:5543): arch=c000003e syscall=59 success=yes exit=0 a0=5637bba2a518 a1=5637bba32968 a2=5637bba30130 a3=fffffffffffff000 items=2 ppid=7724 pid=7725 auid=1000 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=3 comm="echo" exe="/usr/bin/echo" key="auditcmd" type=EXECVE msg=audit(1565526007.269:5543): argc=2 a0="echo" a1="/home/icbc/1.sh" type=CWD msg=audit(1565526007.269:5543): cwd="/home/icbc" ...... type=SYSCALL msg=audit(1565526021.625:5547): arch=c000003e syscall=257 success=yes exit=3 a0=ffffff9c a1=5610372c7240 a2=441 a3=1b6 items=2 ppid=4356 pid=7726 auid=1000 uid=1000 gid=1000 euid=1000 suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts0 ses=3 comm="bash" exe="/usr/bin/bash" key="bashrc_changes"

bash历史记录

icbc@icbc:~$ history

100 sudo echo "/home/icbc/1.sh">> ~/.bashrc 101 sudo echo "/home/icbc/1.sh">> ~/.bash_profile

检测规则/思路

基于audit检测

index = linux sourcetype = linux_audit bashrc_changes

值得注意的是你要手动配置自己的检测规则,如下:

-w /home/user/.bashrc -p wa -k bashrc_changes

-w /home/user/.bash_profile -p wa -k bashrc_changes

注意:我们需要为每个用户的bashrc和bash_profile文件添加审核规则。在audit规则中不接受正则表达式,所以我们无法创建这样的监视规则:/home/*/.bash_history

基于bash历史记录

index=linux sourcetype=bash_history bash_command="nano .bashrc" OR bash_command="vi .bashrc" OR echo .bashrc | table host,user_name,bash_command

index=linux sourcetype=bash_history bash_command="nano .bashrc_profile" OR bash_command="vi .bashrc_profile" OR echo .bashrc_profile | table host,user_name,bash_command

sigma检测规则

title: 编辑修改.bash_profile和.bashrc
status: experimental
description: 检测用户环境的变化。攻击者可以在这些文件中插入代码,以便在每次用户登录或打开新shell时获得持久性。
references:
    - 'MITRE Attack technique T1156; .bash_profile and .bashrc. '
date: 2019/05/12
tags:
    - attack.s0003
    - attack.t1156
    - attack.persistence
    - attack.t1546.004
author: Peter Matkovski
translator: 12306Bro
logsource:
    product: linux
    service: auditd #auditd 日志
detection:
    selection:
        type: 'PATH' #路径
        name:
            - '/home/*/.bashrc'
            - '/home/*/.bash_profile'
            - '/home/*/.profile'
            - '/etc/profile'
            - '/etc/shells'
            - '/etc/bashrc'
            - '/etc/csh.cshrc'
            - '/etc/csh.login'
    condition: selection
falsepositives:
    - Admin or User activity
level: medium

参考推荐

MITRE-ATT&CK-T1546-004

https://attack.mitre.org/techniques/T1546/004/