Skip to content

Handle multi-pattern match on one line

Johan requested to merge yoamarf/reaction:multi-pattern into main

Salut,

Tout d'abord, merci pour reaction :)

Je l'utilise comme actionneur sur des match de regex, pour recenser des connexions, et il fait très bien ce job.
J'ai eu besoin de matcher plusieurs regex sur une même ligne de log (par exemple une IP et un login), et j'ai ajouté cette possibilité.
Essentiellement, les pattern de type string ont été modifiés en tableau de string.

show et flush ont été mis à jour pour le prendre en compte, et à l'usage ça donne ça :

# ./reaction show
ssh:
  failedlogin:
    13.41.56.249 / postgres:
      actions:
        unban:
        - "2024-02-24 11:24:55"
    13.41.56.249 / root:
      actions:
        unban:
        - "2024-02-24 11:24:54"
        
# ./reaction show -p "13.41.56.249 / postgres"
ssh:
  failedlogin:
    13.41.56.249 / postgres:
      actions:
        unban:
        - "2024-02-24 11:24:55"

Avec une configuration dans ce style :

patterns:
  ip:
    # reaction regex syntax is defined here: https://github.com/google/re2/wiki/Syntax
    # simple version: regex: '(?:(?:[0-9]{1,3}\.){3}[0-9]{1,3})|(?:[0-9a-fA-F:]{2,90})'
    regex: '(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|(?:(?:[0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-
F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4})
{1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|
:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9]
)\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0
,1}[0-9]){0,1}[0-9]))'
    ignore:
      - 127.0.0.1
      - ::1

  login:
    regex: '.*'

streams:
  ssh:
    cmd: [ 'tail', '-F', '/var/log/auth.log' ]
    filters:
      failedlogin:
        regex:
          - 'Failed password for <login> from <ip>'
          - 'Connection (reset|closed) by (authenticating|invalid) user <login> <ip>'
          - 'Invalid user <login> from <ip> port [0-9]{1,5}'
        retry: 1
        retryperiod: 6h
        actions:
          ban:
            cmd: [ '/bin/sh', '-c', 'echo "ban: <ip>;<login>" >> /var/log/reaction.banned.txt' ]
          unban:
            cmd: [ '/bin/sh', '-c', 'echo "unban: <ip>;<login>" >> /var/log/reaction.banned.txt' ]

Qu'en pensez-vous? Est-ce que ça pourrait être inclus dans la branche principale?

Edited by Johan

Merge request reports