From 944b022f3398d0177cb4935a289e80c0d6512108 Mon Sep 17 00:00:00 2001 From: twangboy Date: Tue, 4 Feb 2020 14:38:53 -0700 Subject: [PATCH 1/3] Fix regex string for **Del and **DelVals The regex string that updates the Registry.pol file wasn't properly matching instances where the reg_key was prepended with **Del or **DelVals due to the utf-16-le encoding of the Registry.pol file. This adds the null byte characters to the regex portion that searches for those values. --- salt/modules/win_lgpo.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/salt/modules/win_lgpo.py b/salt/modules/win_lgpo.py index 1892fa497f88..c34911b39d64 100644 --- a/salt/modules/win_lgpo.py +++ b/salt/modules/win_lgpo.py @@ -6777,13 +6777,16 @@ def _regexSearchKeyValueCombo(policy_data, policy_regpath, policy_regkey): for a policy_regpath and policy_regkey combo ''' if policy_data: - specialValueRegex = salt.utils.stringutils.to_bytes(r'(\*\*Del\.|\*\*DelVals\.){0,1}') + regex_str = [r'(\*', r'\*', 'D', 'e', 'l', r'\.', r'|\*', r'\*', 'D', + 'e', 'l', 'V', 'a', 'l', 's', r'\.', '){0,1}'] + specialValueRegex = '\x00'.join(regex_str) + specialValueRegex = salt.utils.stringutils.to_bytes(specialValueRegex) _thisSearch = b''.join([salt.utils.stringutils.to_bytes(r'\['), - re.escape(policy_regpath), - b'\00;', - specialValueRegex, - re.escape(policy_regkey), - b'\00;']) + re.escape(policy_regpath), + b'\x00;\x00', + specialValueRegex, + re.escape(policy_regkey.lstrip(b'\x00')), + b'\x00;']) match = re.search(_thisSearch, policy_data, re.IGNORECASE) if match: # add 2 so we get the ']' and the \00 From 7c81874a5ba01d0c0d174414abd4075a9f16835b Mon Sep 17 00:00:00 2001 From: twangboy Date: Wed, 5 Feb 2020 10:15:17 -0700 Subject: [PATCH 2/3] Add some tests --- tests/unit/modules/test_win_lgpo.py | 82 +++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/unit/modules/test_win_lgpo.py b/tests/unit/modules/test_win_lgpo.py index 3373e88aad79..25c51036f1de 100644 --- a/tests/unit/modules/test_win_lgpo.py +++ b/tests/unit/modules/test_win_lgpo.py @@ -60,6 +60,88 @@ def test__getAdmlDisplayName(self): expected = '300000 or 5 minutes (recommended)' self.assertEqual(result, expected) + def test__regexSearchKeyValueCombo_enabled(self): + ''' + Make sure + ''' + policy_data = '[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00D' \ + '\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n\x00s' \ + '\x00e\x00n\x00t\x00\x00\x00;\x00\x01\x00\x00\x00;\x00' \ + '\x04\x00\x00\x00;\x00\x02\x00\x00\x00]\x00' + policy_regpath = '\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' + policy_regkey = '\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ + '\x00s\x00e\x00n\x00t\x00\x00' + test = win_lgpo._regexSearchKeyValueCombo( + policy_data=policy_data, + policy_regpath=policy_regpath, + policy_regkey=policy_regkey + ) + self.assertEqual(test, policy_data) + + def test__regexSearchKeyValueCombo_not_configured(self): + ''' + Make sure + ''' + policy_data = '' + policy_regpath = '\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' + policy_regkey = '\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ + '\x00s\x00e\x00n\x00t\x00\x00' + test = win_lgpo._regexSearchKeyValueCombo( + policy_data=policy_data, + policy_regpath=policy_regpath, + policy_regkey=policy_regkey + ) + self.assertIsNone(test) + + def test__regexSearchKeyValueCombo_disabled(self): + ''' + Make sure + ''' + policy_data = '[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00*' \ + '\x00*\x00d\x00e\x00l\x00.\x00D\x00e\x00f\x00a\x00u' \ + '\x00l\x00t\x00C\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' \ + '\x00;\x00\x01\x00\x00\x00;\x00\x04\x00\x00\x00;\x00 ' \ + '\x00\x00\x00]\x00' + policy_regpath = '\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' + policy_regkey = '\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ + '\x00s\x00e\x00n\x00t\x00\x00' + test = win_lgpo._regexSearchKeyValueCombo( + policy_data=policy_data, + policy_regpath=policy_regpath, + policy_regkey=policy_regkey + ) + self.assertEqual(test, policy_data) + def test__encode_string(self): ''' ``_encode_string`` should return a null terminated ``utf-16-le`` encoded From ca260ef5824c21db10dcb06497fe2227003459c1 Mon Sep 17 00:00:00 2001 From: twangboy Date: Thu, 5 Mar 2020 08:37:42 -0700 Subject: [PATCH 3/3] Use byte-strings in the test --- tests/unit/modules/test_win_lgpo.py | 98 ++++++++++++++--------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/tests/unit/modules/test_win_lgpo.py b/tests/unit/modules/test_win_lgpo.py index 25c51036f1de..aa30ec9e9094 100644 --- a/tests/unit/modules/test_win_lgpo.py +++ b/tests/unit/modules/test_win_lgpo.py @@ -64,25 +64,25 @@ def test__regexSearchKeyValueCombo_enabled(self): ''' Make sure ''' - policy_data = '[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ - '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ - '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ - '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ - '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ - '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ - '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00D' \ - '\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n\x00s' \ - '\x00e\x00n\x00t\x00\x00\x00;\x00\x01\x00\x00\x00;\x00' \ - '\x04\x00\x00\x00;\x00\x02\x00\x00\x00]\x00' - policy_regpath = '\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ - '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ - '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ - '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ - '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ - '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ - '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' - policy_regkey = '\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ - '\x00s\x00e\x00n\x00t\x00\x00' + policy_data = b'[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + b'\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + b'\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + b'\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + b'\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + b'\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + b'\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00D' \ + b'\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n\x00s' \ + b'\x00e\x00n\x00t\x00\x00\x00;\x00\x01\x00\x00\x00;\x00' \ + b'\x04\x00\x00\x00;\x00\x02\x00\x00\x00]\x00' + policy_regpath = b'\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + b'\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + b'\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + b'\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + b'\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + b'\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + b'\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' + policy_regkey = b'\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ + b'\x00s\x00e\x00n\x00t\x00\x00' test = win_lgpo._regexSearchKeyValueCombo( policy_data=policy_data, policy_regpath=policy_regpath, @@ -94,16 +94,16 @@ def test__regexSearchKeyValueCombo_not_configured(self): ''' Make sure ''' - policy_data = '' - policy_regpath = '\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ - '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ - '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ - '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ - '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ - '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ - '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' - policy_regkey = '\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ - '\x00s\x00e\x00n\x00t\x00\x00' + policy_data = b'' + policy_regpath = b'\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + b'\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + b'\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + b'\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + b'\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + b'\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + b'\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' + policy_regkey = b'\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ + b'\x00s\x00e\x00n\x00t\x00\x00' test = win_lgpo._regexSearchKeyValueCombo( policy_data=policy_data, policy_regpath=policy_regpath, @@ -115,26 +115,26 @@ def test__regexSearchKeyValueCombo_disabled(self): ''' Make sure ''' - policy_data = '[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ - '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ - '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ - '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ - '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ - '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ - '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00*' \ - '\x00*\x00d\x00e\x00l\x00.\x00D\x00e\x00f\x00a\x00u' \ - '\x00l\x00t\x00C\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' \ - '\x00;\x00\x01\x00\x00\x00;\x00\x04\x00\x00\x00;\x00 ' \ - '\x00\x00\x00]\x00' - policy_regpath = '\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ - '\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ - '\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ - '\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ - '\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ - '\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ - '\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' - policy_regkey = '\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ - '\x00s\x00e\x00n\x00t\x00\x00' + policy_data = b'[\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + b'\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + b'\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + b'\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + b'\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + b'\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + b'\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00\x00;\x00*' \ + b'\x00*\x00d\x00e\x00l\x00.\x00D\x00e\x00f\x00a\x00u' \ + b'\x00l\x00t\x00C\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' \ + b'\x00;\x00\x01\x00\x00\x00;\x00\x04\x00\x00\x00;\x00 ' \ + b'\x00\x00\x00]\x00' + policy_regpath = b'\x00s\x00o\x00f\x00t\x00w\x00a\x00r\x00e\x00\\\x00p' \ + b'\x00o\x00l\x00i\x00c\x00i\x00e\x00s\x00\\\x00m\x00i' \ + b'\x00c\x00r\x00o\x00s\x00o\x00f\x00t\x00\\\x00w\x00i' \ + b'\x00n\x00d\x00o\x00w\x00s\x00\\\x00w\x00i\x00n\x00d' \ + b'\x00o\x00w\x00s\x00 \x00e\x00r\x00r\x00o\x00r\x00 ' \ + b'\x00r\x00e\x00p\x00o\x00r\x00t\x00i\x00n\x00g\x00\\' \ + b'\x00c\x00o\x00n\x00s\x00e\x00n\x00t\x00\x00' + policy_regkey = b'\x00D\x00e\x00f\x00a\x00u\x00l\x00t\x00C\x00o\x00n' \ + b'\x00s\x00e\x00n\x00t\x00\x00' test = win_lgpo._regexSearchKeyValueCombo( policy_data=policy_data, policy_regpath=policy_regpath,