Detects Suspicious edit of .bash_profile and .bashrc on Linux systems

Detects change of user environment. Adversaries can insert code into these files to gain persistence each time a user logs in or opens a new shell.

Rule Content

- title: Detects Suspicious edit of .bash_profile and .bashrc on Linux systems
  id: e74e15cc-c4b6-4c80-b7eb-dfe49feb7fe9
  status: experimental
  description: Detects change of user environment. Adversaries can insert code into
    these files to gain persistence each time a user logs in or opens a new shell.
  references:
  - 'MITRE Attack technique T1156; .bash_profile and .bashrc. '
  date: 2019/05/12
  tags:
  - attack.s0003
  - attack.t1156
  - attack.persistence
  author: Peter Matkovski
  logsource:
    product: linux
    service: auditd
    category: null
  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

Querying Elasticsearch

Import Libraries


In [ ]:
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
import pandas as pd

Initialize Elasticsearch client


In [ ]:
es = Elasticsearch(['http://helk-elasticsearch:9200'])
searchContext = Search(using=es, index='logs-*', doc_type='doc')

Run Elasticsearch Query


In [ ]:
s = searchContext.query('query_string', query='(type:"PATH" AND name.keyword:(\/home\/*\/.bashrc OR \/home\/*\/.bash_profile OR \/home\/*\/.profile OR \/etc\/profile OR \/etc\/shells OR \/etc\/bashrc OR \/etc\/csh.cshrc OR \/etc\/csh.login))')
response = s.execute()
if response.success():
    df = pd.DataFrame((d.to_dict() for d in s.scan()))

Show Results


In [ ]:
df.head()