MapPattern
This Script is part of the Filters And Transformers Pack.#
Supported versions
Supported Cortex XSOAR versions: 6.5.0 and later.
This transformer will take in a value and transform it based on multiple condition expressions (wildcard, regex, etc) defined in a JSON dictionary structure. The key:value pair of the JSON dictionary should be:
"condition expression": "desired outcome"
For example:
The transformer will return the value matched to a pattern following to the priority. When unmatched or the input value is structured (dict or list), it will simply return the input value.
Script Data#
| Name | Description |
|---|---|
| Script Type | python3 |
| Tags | transformer, string |
Inputs#
| Argument Name | Description |
|---|---|
| value | The value to modify. |
| mappings | A JSON dictionary or list of it that contains key:value pairs that represent the "Condition":"Outcome". |
| algorithm | The default algorithm for pattern match. Available algorithm: literal, wildcard, regex, regmatch and dt. |
| caseless | Set to true for caseless comparison, false otherwise. |
| priority | The option to choose which value matched to return. Available options: first_match (default) and last_match. |
| context | The context: Input . (single dot) on `From previous tasks` to enable to extract the context data. |
| flags | The comma separated flags for pattern matching in regex. dotall (s), multiline (m), ignorecase (i) and unicode (u) are supported. This will apply to all the algorithms. |
| compare_fields | Set to true if you want pattern matching for each field, otherwise false. |
| wildcards | The list of the special patterns which match to any values regardless of algorithm. |
| default_value | The value to return when all the patterns are not satisfied. |
Outputs#
There are no outputs for this script.
Syntax for mappings#
Pattern Matching#
When you choose the dt as the algorithm, the value generated by a DT is handled as unmatched when it is considered as false in boolean condition in python, otherwise it is handles as matched.
In python, null, boolean False, integer 0, empty string, empty list and empty dict are considered as false.
Examples#
Transform a severity name to the corresponding number.
algorithm: regmatch
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| High | 3 |
| Informational | 1 |
| Info | 1 |
| Abc | Abc |
Normalize a human readable phrase to a cannonical name.
algorithm: wildcard
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| 1 - Low | low |
| Medium | medium |
| high (3) | high |
| infomation | unknown |
Remove all the heading "Re:" or "Fw:" from an email subject.
algorithm: regex
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| Re: Re: Fw: Hello! | Hello! |
| Hello! | Hello! |
Extract the user name field from an text in an Active Directory user account format.
algorithm: regex
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| username@domain | username |
| domain\username | username |
| username | username |
| 012abc$ | <unknown> |
Extract the user name field from an quoted text in an Active Directory user account format.
algorithm: regex
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| "username@domain" | username |
| username@domain | username |
| "domain\username" | username |
| domain\username | username |
| "username" | username |
| username | username |
| 012abc$ | <unknown> |
Extract first name and last name from an email address in firstname.lastname@domain, but the format is lastname.firstname@domain in some particular domains.
algorithm: regex
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| john.doe@example1.com | john doe |
| doe.john@example2.com | john doe |
| username@example1.com | username |
Normalize a date/time text to YYYY-MM-DD HH:mm:ss TZ.
algorithm: regex
caseless: true
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| 2021-01-02T01:23:45.010Z | 2021-01-02 01:23:45 GMT |
| 2021-01-02T01:23:45Z | 2021-01-02 01:23:45 GMT |
| Tue, 3 Jun 2008 11:05:30 GMT | 2008-06-03 11:05:30 GMT |
Normalize a date/time text to YYYY-MM-DD HH:mm:ss TZ.
algorithm: regex
caseless: true
priority: first_match
context: . [From previous tasks]
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| 2021-01-02T01:23:45.010Z | 2021-01-02 01:23:45 GMT |
| 2021-01-02T01:23:45Z | 2021-01-02 01:23:45 GMT |
| Tue, 3 Jun 2008 11:05:30 GMT | 2008-06-03 11:05:30 GMT |
Pattern matching for different nodes
algorithm: wildcard
caseless: true
priority: first_match
context:
flags:
compare_fields: true
wildcards:
mappings#
| Input | Output |
|---|---|
| {"IP": "127.0.0.1"} | "localhost" |
| {"Host": "localhost"} | "localhost" |
| {"Host": "paloaltonetworks.local"} | "localhost" |
| {"IP": "192.168.1.1"} | "other" |
Make a text with the value field corresponding to the score field.
algorithm: regex
caseless: true
priority: first_match
context: . [From previous tasks]
flags:
compare_fields: true
wildcards: *
mappings#
| Input | Output |
|---|---|
| {"score": 1, "value": "192.168.1.1"} | "low - 192.168.1.1" |
| {"score": 4, "value": "192.168.1.1"} | "unknown - 192.168.1.1" |
Make a text with the value field corresponding to the score field.
algorithm: dt
caseless:
priority: first_match
context: . [From previous tasks]
flags:
compare_fields: true
wildcards: *
mappings#
| Input | Output |
|---|---|
| {"score": 10, "value": "192.168.1.1"} | "low - 192.168.1.1" |
| {"score": 40, "value": "192.168.1.1"} | "medium - 192.168.1.1" |
| {"score": 70, "value": "192.168.1.1"} | "high - 192.168.1.1" |
| {"score": "x", "value": "192.168.1.1"} | "unknown - 192.168.1.1" |
Make a phrase based on the values of score and type.
algorithm: dt
caseless:
priority: first_match
context: . [From previous tasks]
flags:
compare_fields: true
wildcards: *
mappings#
| Input | Output |
|---|---|
| {"score": 70, "value": "192.168.1.1", "type": "IP"} | "malicious IP" |
| {"score": 10, "value": "paloaltonetworks.com", "type": "domain"} | "low" |
| {"score": "x", "value": "192.168.1.1"} | "unknown - 192.168.1.1" |
Check if the date is a leap day.
algorithm: regex
caseless:
priority: first_match
context:
flags:
compare_fields:
wildcards:
mappings#
| Input | Output |
|---|---|
| Jun 6, 2021 | false |
| Feb 29, 2000 | true |
| Feb 29, 2004 | true |
| Feb 29, 2001 | Feb 29, 2001 |
| Jun 32, 2021 | Jun 32, 2021 |