Skip to content

Commit 355136a

Browse files
committed
Merge branch 'add-fields-to-file-encryption-telemetry' into develop
2 parents 8ad8223 + 444a18d commit 355136a

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

monkey/infection_monkey/ransomware/ransomware_payload.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ def _encrypt_files(self, file_list: List[Path]) -> List[Tuple[Path, Optional[Exc
6464
LOG.debug(f"Encrypting {filepath}")
6565
self._encryptor.encrypt_file_in_place(filepath)
6666
self._add_extension(filepath)
67-
self._send_telemetry(filepath, "")
67+
self._send_telemetry(filepath, True, "")
6868
except Exception as ex:
6969
LOG.warning(f"Error encrypting {filepath}: {ex}")
70-
self._send_telemetry(filepath, str(ex))
70+
self._send_telemetry(filepath, False, str(ex))
7171

7272
return results
7373

7474
def _add_extension(self, filepath: Path):
7575
new_filepath = filepath.with_suffix(f"{filepath.suffix}{self._new_file_extension}")
7676
filepath.rename(new_filepath)
7777

78-
def _send_telemetry(self, filepath: Path, error: str):
79-
encryption_attempt = FileEncryptionTelem((str(filepath), str(error)))
78+
def _send_telemetry(self, filepath: Path, success: bool, error: str):
79+
encryption_attempt = FileEncryptionTelem(str(filepath), success, error)
8080
self._telemetry_messenger.send_telemetry(encryption_attempt)
8181

8282
def _leave_readme(self):

monkey/infection_monkey/telemetry/file_encryption_telem.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Tuple
1+
from pathlib import Path
22

33
from common.common_consts.telem_categories import TelemCategoryEnum
44
from infection_monkey.telemetry.base_telem import BaseTelem
@@ -7,17 +7,16 @@
77

88

99
class FileEncryptionTelem(BatchableTelemMixin, IBatchableTelem, BaseTelem):
10-
def __init__(self, entry: Tuple[str, str]):
10+
def __init__(self, filepath: Path, success: bool, error: str):
1111
"""
1212
File Encryption telemetry constructor
13-
:param attempts: List of tuples with each tuple containing the path
14-
of a file it tried encrypting and its result.
15-
If ransomware fails completely - list of one tuple
16-
containing the directory path and error string.
13+
:param filepath: The path to the file that monkey attempted to encrypt
14+
:param success: True if encryption was successful, false otherwise
15+
:param error: An error message describing the failure. Empty unless success == False
1716
"""
1817
super().__init__()
1918

20-
self._telemetry_entries.append(entry)
19+
self._telemetry_entries.append({"path": filepath, "success": success, "error": error})
2120

2221
telem_category = TelemCategoryEnum.FILE_ENCRYPTION
2322

monkey/tests/unit_tests/infection_monkey/ransomware/test_ransomware_payload.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from pathlib import Path, PurePath
2+
from pathlib import Path, PurePosixPath
33

44
import pytest
55
from tests.unit_tests.infection_monkey.ransomware.ransomware_target_files import (
@@ -138,24 +138,27 @@ def test_telemetry_success(ransomware_payload, telemetry_messenger_spy):
138138
telem_1 = telemetry_messenger_spy.telemetries[0]
139139
telem_2 = telemetry_messenger_spy.telemetries[1]
140140

141-
assert ALL_ZEROS_PDF in telem_1.get_data()["files"][0][0]
142-
assert telem_1.get_data()["files"][0][1] == ""
143-
assert TEST_KEYBOARD_TXT in telem_2.get_data()["files"][0][0]
144-
assert telem_2.get_data()["files"][0][1] == ""
141+
assert ALL_ZEROS_PDF in telem_1.get_data()["files"][0]["path"]
142+
assert telem_1.get_data()["files"][0]["success"]
143+
assert telem_1.get_data()["files"][0]["error"] == ""
144+
assert TEST_KEYBOARD_TXT in telem_2.get_data()["files"][0]["path"]
145+
assert telem_2.get_data()["files"][0]["success"]
146+
assert telem_2.get_data()["files"][0]["error"] == ""
145147

146148

147149
def test_telemetry_failure(monkeypatch, ransomware_payload, telemetry_messenger_spy):
148150
monkeypatch.setattr(
149151
ransomware_payload_module,
150152
"select_production_safe_target_files",
151-
lambda a, b: [PurePath("/file/not/exist")],
153+
lambda a, b: [PurePosixPath("/file/not/exist")],
152154
),
153155

154156
ransomware_payload.run_payload()
155157
telem_1 = telemetry_messenger_spy.telemetries[0]
156158

157-
assert "/file/not/exist" in telem_1.get_data()["files"][0][0]
158-
assert "No such file or directory" in telem_1.get_data()["files"][0][1]
159+
assert "/file/not/exist" in telem_1.get_data()["files"][0]["path"]
160+
assert not telem_1.get_data()["files"][0]["success"]
161+
assert "No such file or directory" in telem_1.get_data()["files"][0]["error"]
159162

160163

161164
def test_readme_false(ransomware_payload_config, ransomware_target, telemetry_messenger_spy):

monkey/tests/unit_tests/infection_monkey/telemetry/test_file_encryption_telem.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
from infection_monkey.telemetry.file_encryption_telem import FileEncryptionTelem
44

5-
ENCRYPTION_ATTEMPTS = [("<file1>", "<encryption attempt result>"), ("<file2>", "")]
5+
ENCRYPTION_ATTEMPTS = [
6+
{"path": "<file1>", "success": False, "error": "<encryption attempt result>"},
7+
{"path": "<file2>", "success": True, "error": ""},
8+
]
69

710

811
def test_file_encryption_telem_send(spy_send_telemetry):
9-
file_encryption_telem_1 = FileEncryptionTelem(ENCRYPTION_ATTEMPTS[0])
10-
file_encryption_telem_2 = FileEncryptionTelem(ENCRYPTION_ATTEMPTS[1])
12+
file_encryption_telem_1 = FileEncryptionTelem(
13+
ENCRYPTION_ATTEMPTS[0]["path"],
14+
ENCRYPTION_ATTEMPTS[0]["success"],
15+
ENCRYPTION_ATTEMPTS[0]["error"],
16+
)
17+
file_encryption_telem_2 = FileEncryptionTelem(
18+
ENCRYPTION_ATTEMPTS[1]["path"],
19+
ENCRYPTION_ATTEMPTS[1]["success"],
20+
ENCRYPTION_ATTEMPTS[1]["error"],
21+
)
1122

1223
file_encryption_telem_1.add_telemetry_to_batch(file_encryption_telem_2)
1324

0 commit comments

Comments
 (0)