Skip to content

Commit

Permalink
String-Similiarity-Fix (#30364)
Browse files Browse the repository at this point in the history
* fix script

* RN

* fix

* fixing

* RN

* fix to array

* testing

* Fix

* fix

* Fix

* test

* test

* RN

* fix

* fix

* fix unittest

* fix

* fix

* fix

* Bump pack from version CommonScripts to 1.12.39.

* Review Fixes

* ReleaseNotes fix

---------

Co-authored-by: Content Bot <[email protected]>
  • Loading branch information
ArikDay and Content Bot authored Nov 5, 2023
1 parent 52439ce commit 4063969
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
5 changes: 5 additions & 0 deletions Packs/CommonScripts/ReleaseNotes/1_12_39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#### Scripts

##### StringSimilarity
The script now supports comparing every string between two arrays of strings.
9 changes: 5 additions & 4 deletions Packs/CommonScripts/Scripts/StringSimilarity/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This automation calculates the similarity ratio between text and a list of strings and outputs a decimal value between 0.0 and 1.0 (1.0 if the sequences are identical, and 0.0 if they don't have anything in common).
This automation calculates the similarity ratio between every string in 2 different arrays and outputs a decimal value between 0.0 and 1.0 (1.0 if the sequences are identical, and 0.0 if they don't have anything in common).

## Script Data

Expand All @@ -14,16 +14,17 @@ This automation calculates the similarity ratio between text and a list of strin
---
This script is used in the following playbooks and scripts.

RDP Bitmap Cache - Detect and Hunt
* RDP Bitmap Cache - Detect and Hunt
* Compare Process Execution Arguments To LOLBAS Patterns

## Inputs

---

| **Argument Name** | **Description** |
| --- | --- |
| string_A | First input string. |
| string_B | Second input string. |
| string_A | First array of strings to compare. |
| string_B | Second array of strings to compare. |
| similarity_threshold | The similarity threshold to show results for, a value between 0 &lt; x &gt;1. |

## Outputs
Expand Down
33 changes: 17 additions & 16 deletions Packs/CommonScripts/Scripts/StringSimilarity/StringSimilarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import difflib


def stringSimilarity(first_string: str, second_string: str, similarity_threshold: float):
def stringSimilarity(first_array: str, second_array: str, similarity_threshold: float):
"""
Calculate the similarity score between two strings using the SequenceMatcher.
Expand All @@ -26,26 +26,27 @@ def stringSimilarity(first_string: str, second_string: str, similarity_threshold
with a message indicating that no similarity score is calculated.
"""

similarity_ratio = difflib.SequenceMatcher(None, first_string, second_string).ratio()
if similarity_ratio >= float(similarity_threshold):
results = {
"StringA": first_string,
"StringB": second_string,
"SimilarityScore": similarity_ratio
}

return CommandResults("StringSimilarity", ["StringA", "StringB"], results)
return None
results = []
for string_a in first_array:
for string_b in second_array:
similarity_ratio = difflib.SequenceMatcher(None, string_a, string_b).ratio()
if similarity_ratio >= float(similarity_threshold):
results.append({
"StringA": string_a,
"StringB": string_b,
"SimilarityScore": similarity_ratio
})
if not results:
return None
return CommandResults("StringSimilarity", ["StringA", "StringB"], results)


def main():
similarity_threshold = demisto.getArg('similarity_threshold')
first_string = demisto.getArg('string_A')
second_string = demisto.getArg('string_B')

first_array = argToList(demisto.getArg('string_A'))
second_array = argToList(demisto.getArg('string_B'))
try:
results = stringSimilarity(first_string, second_string, similarity_threshold)

results = stringSimilarity(first_array, second_array, similarity_threshold)
return_results(results)
except Exception as e:
return_error(f'Failed to check string similarity. Problem: {str(e)}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ name: StringSimilarity
script: ''
type: python
tags: []
comment: This automation calculates the similarity ratio between text and a list of strings and outputs a decimal value between 0.0 and 1.0 (1.0 if the sequences are identical, and 0.0 if they don't have anything in common).
comment: This automation calculates the similarity ratio between every string in 2 different arrays and outputs a decimal value between 0.0 and 1.0 (1.0 if the sequences are identical, and 0.0 if they don't have anything in common).
enabled: true
args:
- name: string_A
required: true
description: First input string.
description: First array of strings to compare.
isArray: true
- name: string_B
required: true
description: Second input string.
description: Second array of strings to compare.
isArray: true
- name: similarity_threshold
description: The similarity threshold to show results for, a value between 0 < x >1.
defaultValue: "0.1"
outputs:
- contextPath: StringSimilarity.SimilarityScore
description: Similarity score - a value between 1.0 if the sequences are identical, and 0.0 if they have nothing in common.
scripttarget: 0
dockerimage: demisto/python3:3.10.12.68714
dockerimage: demisto/python3:3.10.13.78960
subtype: python3
runonce: false
runas: DBotWeakRole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@


@pytest.mark.parametrize("first_string, second_string, similarity_threshold, expected_result", [
("hello", "hello", 0.9, {
(["hello"], ["hello"], 0.9, [{
"StringA": "hello",
"StringB": "hello",
"SimilarityScore": 1.0,
}),
("deeply", "deeply", 0.6, {
}]),
(["deeply"], ["deeply"], 0.6, [{
"StringA": "deeply",
"StringB": "deeply",
"SimilarityScore": 1.0,
}),
("testing", "test", 0.7, {
}]),
(["testing"], ["test"], 0.7, [{
"StringA": "testing",
"StringB": "test",
"SimilarityScore": 0.7272727272727273,
}),
}]),
("a", "b", 0.1, {
None})
])
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_main_with_no_similarity_match(mocker):
similarity_threshold = 0.1
first_string = "hello"
second_string = "world"
expected_results = {'StringA': 'hello', 'StringB': 'world', 'SimilarityScore': 0.2}
expected_results = [{'StringA': 'hello', 'StringB': 'world', 'SimilarityScore': 0.2}]
# Mock demisto.getArg function to return the input arguments
with patch.object(demisto, 'getArg') as mocked_getArg:
mocked_getArg.side_effect = lambda arg_name: {
Expand Down
2 changes: 1 addition & 1 deletion Packs/CommonScripts/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Common Scripts",
"description": "Frequently used scripts pack.",
"support": "xsoar",
"currentVersion": "1.12.38",
"currentVersion": "1.12.39",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit 4063969

Please sign in to comment.