Malicious PowerShell Keywords

Detects keywords from well-known PowerShell exploitation frameworks

Rule Content

- title: Malicious PowerShell Keywords
  id: f62176f3-8128-4faa-bf6c-83261322e5eb
  status: experimental
  description: Detects keywords from well-known PowerShell exploitation frameworks
  modified: 2019/01/22
  references:
  - https://adsecurity.org/?p=2921
  tags:
  - attack.execution
  - attack.t1086
  author: Sean Metcalf (source), Florian Roth (rule)
  logsource:
    product: windows
    service: powershell
    definition: It is recommended to use the new "Script Block Logging" of PowerShell
      v5 https://adsecurity.org/?p=2277
    category: null
  detection:
    keywords:
      Message:
      - '*AdjustTokenPrivileges*'
      - '*IMAGE_NT_OPTIONAL_HDR64_MAGIC*'
      - '*Microsoft.Win32.UnsafeNativeMethods*'
      - '*ReadProcessMemory.Invoke*'
      - '*SE_PRIVILEGE_ENABLED*'
      - '*LSA_UNICODE_STRING*'
      - '*MiniDumpWriteDump*'
      - '*PAGE_EXECUTE_READ*'
      - '*SECURITY_DELEGATION*'
      - '*TOKEN_ADJUST_PRIVILEGES*'
      - '*TOKEN_ALL_ACCESS*'
      - '*TOKEN_ASSIGN_PRIMARY*'
      - '*TOKEN_DUPLICATE*'
      - '*TOKEN_ELEVATION*'
      - '*TOKEN_IMPERSONATE*'
      - '*TOKEN_INFORMATION_CLASS*'
      - '*TOKEN_PRIVILEGES*'
      - '*TOKEN_QUERY*'
      - '*Metasploit*'
      - '*Mimikatz*'
    condition: keywords
  falsepositives:
  - Penetration tests
  level: high

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-endpoint-winevent-powershell-*', doc_type='doc')

Run Elasticsearch Query


In [ ]:
s = searchContext.query('query_string', query='Message.keyword:(*AdjustTokenPrivileges* OR *IMAGE_NT_OPTIONAL_HDR64_MAGIC* OR *Microsoft.Win32.UnsafeNativeMethods* OR *ReadProcessMemory.Invoke* OR *SE_PRIVILEGE_ENABLED* OR *LSA_UNICODE_STRING* OR *MiniDumpWriteDump* OR *PAGE_EXECUTE_READ* OR *SECURITY_DELEGATION* OR *TOKEN_ADJUST_PRIVILEGES* OR *TOKEN_ALL_ACCESS* OR *TOKEN_ASSIGN_PRIMARY* OR *TOKEN_DUPLICATE* OR *TOKEN_ELEVATION* OR *TOKEN_IMPERSONATE* OR *TOKEN_INFORMATION_CLASS* OR *TOKEN_PRIVILEGES* OR *TOKEN_QUERY* OR *Metasploit* OR *Mimikatz*)')
response = s.execute()
if response.success():
    df = pd.DataFrame((d.to_dict() for d in s.scan()))

Show Results


In [ ]:
df.head()