Skip to content

Support for PCRE regular expressions#6480

Merged
vikman90 merged 27 commits intomasterfrom
dev-205-regex-pcre2
Nov 6, 2020
Merged

Support for PCRE regular expressions#6480
vikman90 merged 27 commits intomasterfrom
dev-205-regex-pcre2

Conversation

@jnasselle
Copy link
Member

@jnasselle jnasselle commented Nov 3, 2020

Related issue
205

Description

Hello team!

This PR add support for Perl Compatible Regular Expressions(PCRE) and allow to select osregex, osmatch or pcre2 using new type attribute for several options/fields

Decoders:

Option Supported types Default type
program_name osregex,osmatch,pcre2 osmatch
prematch osregex,pcre2 osregex
regex osregex,pcre2 osregex

Rules:

Option Supported types Default type
regex osregex,osmatch,pcre2 osregex
field osregex,osmatch,pcre2 osregex
match osregex,osmatch,pcre2 osmatch
action osregex,osmatch,pcre2 string
extra_data osregex,osmatch,pcre2 osmatch
hostname osregex,osmatch,pcre2 osmatch
id osregex,osmatch,pcre2 osmatch
location osregex,osmatch,pcre2 osmatch
match osregex,osmatch,pcre2 osmatch
program_name osregex,osmatch,pcre2 osmatch
protocol osregex,osmatch,pcre2 osmatch
user osregex,osmatch,pcre2 osmatch
url osregex,osmatch,pcre2 osmatch
srcport osregex,osmatch,pcre2 osmatch
dstport osregex,osmatch,pcre2 osmatch
status osregex,osmatch,pcre2 osmatch
system_name osregex,osmatch,pcre2 osmatch
extra_data osregex,osmatch,pcre2 osmatch
srcgeoip osregex,osmatch,pcre2 osmatch
dstgeoip osregex,osmatch,pcre2 osmatch

libpcre2 specs

  • PCRE2 10.34 used
  • Included and linked in Wazuh Manager and Wazuh Agent
  • 8 bit and UTF-8 support
  • Static library compilation only
  • JIT compiler set to auto, in order to test arch support. Code don't use this feature yet
  • Compilation time increases 10% compared with base branch of this PR.

ossec-analysisd getconfig endpoint example

  • Rules
[
  {
    "sigid": 100001,
    "level": 5,
    "regex": {
      "pattern": "^Hello World.$",
      "negate": false,
      "type": "pcre2"
    }
  },
 {
    "sigid": 100002,
    "level": 7,
    "dstport": {
      "pattern": "^Hello from Wazuh.$",
      "negate": true,
      "type": "osregex"
    }
  }
]
  • Decoders
[{
	"id": 519,
	"name": "test_pcre2_prematch",
	"children": [{
		"id": 519,
		"name": "test_pcre2_prematch",
		"parent": "test_pcre2_prematch",
		"order": [
			"id",
			"otro"
		],
		"use_own_name": "false",
		"accumulate": "no",
		"prematch": {
			"pattern": "MAGICWORD",
			"type": "osregex"
		},
		"regex": {
			"pattern": " testing after_parent id:(\\S+) (\\w+)",
			"type": "pcre2"
		},
		"type": "syslog"
	}],
	"use_own_name": "false",
	"accumulate": "no",
	"prematch": {
		"pattern": "test_pcre(\\d)_prematch",
		"type": "osregex"
	},
	"type": "syslog"
}, {
	"id": 517,
	"name": "test_lcre2_program_name",
	"children": [],
	"use_own_name": "false",
	"accumulate": "no",
	"program_name": {
		"pattern": "^pcre2_house(cat((?i)s|)|)$",
		"type": "pcre2"
	},
	"type": "syslog"
}]

Examples

Decoders

<decoder name="test_pcre2">
  <program_name type="pcre2">^(?i)test_pcre2</program_name>
  <regex type="pcre2">(?i)\S+ (\S+) (\w+) (\S+)</regex>
  <order>url,action,querystring</order>
</decoder>

Rules

<group name="qa,test">
 <rule id="100001" level="3">
    <decoded_as>test_pcre2</decoded_as>
    <match>test_regex</match>
    <regex type="pcre2">(?i)regex_example</regex>
    <description>Testing PCRE2 regex</description>
  </rule>
  <rule id="100002" level="3">
    <decoded_as>test_pcre2</decoded_as>
    <match>test_field</match>
    <field name="querystring" type="pcre2">(?i)format=json</field>
    <description>Testing PCRE2 dynamic field</description>
  </rule>
</group>

NOTE: (?i) in PCRE syntax means to be case insensitive.

Logs/Alerts example

Lower case log

ossec-testrule: Type one log per line.

Dec 19 17:20:08 ubuntu test_pcre2[12345]:test_regex regex_example https://localhost GET format=xml


**Phase 1: Completed pre-decoding.
       full event: 'Dec 19 17:20:08 ubuntu test_pcre2[12345]:test_regex regex_example https://localhost GET format=xml'
       timestamp: 'Dec 19 17:20:08'
       hostname: 'ubuntu'
       program_name: 'test_pcre2'
       log: 'test_regex regex_example https://localhost GET format=xml'

**Phase 2: Completed decoding.
       decoder: 'test_pcre2'
       url: 'https://localhost'
       action: 'GET'
       querystring: 'format=xml'

**Phase 3: Completed filtering (rules).
       Rule id: '100001'
       Level: '3'
       Description: 'Testing PCRE2 regex'
**Alert to be generated.


Dec 19 17:20:08 ubuntu test_pcre2[12345]:test_regex regex_example https://localhost GET format=json


**Phase 1: Completed pre-decoding.
       full event: 'Dec 19 17:20:08 ubuntu test_pcre2[12345]:test_regex regex_example https://localhost GET format=json'
       timestamp: 'Dec 19 17:20:08'
       hostname: 'ubuntu'
       program_name: 'test_pcre2'
       log: 'test_regex regex_example https://localhost GET format=json'

**Phase 2: Completed decoding.
       decoder: 'test_pcre2'
       url: 'https://localhost'
       action: 'GET'
       querystring: 'format=json'

**Phase 3: Completed filtering (rules).
       Rule id: '100001'
       Level: '3'
       Description: 'Testing PCRE2 regex'
**Alert to be generated.

Upper case log

ossec-testrule: Type one log per line.

DEC 19 17:20:08 UBUNTU TEST_PCRE2[12345]:TEST_REGEX REGEX_EXAMPLE HTTPS://LOCALHOST GET FORMAT=XML


**Phase 1: Completed pre-decoding.
       full event: 'DEC 19 17:20:08 UBUNTU TEST_PCRE2[12345]:TEST_REGEX REGEX_EXAMPLE HTTPS://LOCALHOST GET FORMAT=XML'
       timestamp: 'DEC 19 17:20:08'
       hostname: 'UBUNTU'
       program_name: 'TEST_PCRE2'
       log: 'TEST_REGEX REGEX_EXAMPLE HTTPS://LOCALHOST GET FORMAT=XML'

**Phase 2: Completed decoding.
       decoder: 'test_pcre2'
       url: 'HTTPS://LOCALHOST'
       action: 'GET'
       querystring: 'FORMAT=XML'

**Phase 3: Completed filtering (rules).
       Rule id: '100001'
       Level: '3'
       Description: 'Testing PCRE2 regex'
**Alert to be generated.


DEC 19 17:20:08 UBUNTU TEST_PCRE2[12345]:TEST_REGEX REGEX_EXAMPLE HTTPS://LOCALHOST GET FORMAT=JSON


**Phase 1: Completed pre-decoding.
       full event: 'DEC 19 17:20:08 UBUNTU TEST_PCRE2[12345]:TEST_REGEX REGEX_EXAMPLE HTTPS://LOCALHOST GET FORMAT=JSON'
       timestamp: 'DEC 19 17:20:08'
       hostname: 'UBUNTU'
       program_name: 'TEST_PCRE2'
       log: 'TEST_REGEX REGEX_EXAMPLE HTTPS://LOCALHOST GET FORMAT=JSON'

**Phase 2: Completed decoding.
       decoder: 'test_pcre2'
       url: 'HTTPS://LOCALHOST'
       action: 'GET'
       querystring: 'FORMAT=JSON'

**Phase 3: Completed filtering (rules).
       Rule id: '100001'
       Level: '3'
       Description: 'Testing PCRE2 regex'
**Alert to be generated.

@jnasselle jnasselle marked this pull request as ready for review November 3, 2020 14:14
juliancnn and others added 3 commits November 4, 2020 17:43
[X] extra_data
[X] hostname
[X] location
[X] program_name
[x] protocol
[X] user
[X] url
[X] srcport
[X] dstport
[x] status
[X] system_name
[x] data
[x] srcgeoip
[x] dstgeoip
@Lopuiz Lopuiz removed the request for review from juliancnn November 5, 2020 16:41
@Lopuiz Lopuiz requested review from vikman90 and removed request for JcabreraC and Lopuiz November 5, 2020 16:41
@Lopuiz Lopuiz force-pushed the dev-205-regex-pcre2 branch from ea2d012 to d0b44cc Compare November 5, 2020 16:59
@jnasselle jnasselle added module/analysis Issues related to the Analysis daemon feed module/building Compilation of the core product regex Issue related to regular expressions labels Nov 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feed module/analysis Issues related to the Analysis daemon module/building Compilation of the core product regex Issue related to regular expressions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants