Python SQL Exceptions

Generic rule for SQL exceptions in Python according to PEP 249

Rule Content

- title: Python SQL Exceptions
  id: 19aefed0-ffd4-47dc-a7fc-f8b1425e84f9
  description: Generic rule for SQL exceptions in Python according to PEP 249
  author: Thomas Patzke
  references:
  - https://www.python.org/dev/peps/pep-0249/#exceptions
  logsource:
    category: application
    product: python
    service: null
  detection:
    exceptions:
    - DataError
    - IntegrityError
    - ProgrammingError
    - OperationalError
    condition: exceptions
  falsepositives:
  - Application bugs
  - Penetration testing
  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='\*.keyword:(*DataError* OR *IntegrityError* OR *ProgrammingError* OR *OperationalError*)')
response = s.execute()
if response.success():
    df = pd.DataFrame((d.to_dict() for d in s.scan()))

Show Results


In [ ]:
df.head()