|
| 1 | +import unittest |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from lib.cuckoo.common.integrations.parse_office import Office |
| 7 | + |
| 8 | +data_dir = Path(__file__).parent / "data" / "office" |
| 9 | +rtf_path = data_dir / "rtf_exploit.doc" |
| 10 | + |
| 11 | + |
| 12 | +class TestParseOffice(unittest.TestCase): |
| 13 | + @patch("lib.cuckoo.common.integrations.parse_office.RtfObjParser") |
| 14 | + @patch("lib.cuckoo.common.integrations.parse_office.path_exists") |
| 15 | + @patch("lib.cuckoo.common.integrations.parse_office.hashlib.sha256") |
| 16 | + def test_parse_rtf(self, mock_sha256, mock_path_exists, MockRtfObjParser): |
| 17 | + # Setup |
| 18 | + mock_sha256.return_value.hexdigest.return_value = "dummy_sha256" |
| 19 | + mock_path_exists.return_value = False |
| 20 | + mock_rtfobj = MagicMock() |
| 21 | + mock_rtfobj.format_id = 1 |
| 22 | + mock_rtfobj.is_package = False |
| 23 | + mock_rtfobj.is_ole = False |
| 24 | + mock_rtfobj.rawdata = b"rawdata" |
| 25 | + mock_rtfobj.start = 0 |
| 26 | + MockRtfObjParser.return_value.objects = [mock_rtfobj] |
| 27 | + |
| 28 | + office = Office( |
| 29 | + file_path="dummy_path", |
| 30 | + task_id="dummy_task_id", |
| 31 | + sha256="dummy_sha256", |
| 32 | + options={}, |
| 33 | + ) |
| 34 | + |
| 35 | + # Execute |
| 36 | + result = office._parse_rtf(b"dummy_data") |
| 37 | + |
| 38 | + # Verify |
| 39 | + expected_result = { |
| 40 | + "1": [ |
| 41 | + { |
| 42 | + "class_name": "", |
| 43 | + "size": len(mock_rtfobj.rawdata), |
| 44 | + "filename": "object_00000000.raw", |
| 45 | + "type_embed": "", |
| 46 | + "CVE": "", |
| 47 | + "sha256": "dummy_sha256", |
| 48 | + "index": "00000000h", |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + self.assertEqual(result, expected_result) |
| 53 | + |
| 54 | + |
| 55 | + @pytest.mark.skipif(not data_dir.exists(), reason="Required data file is not present") |
| 56 | + @pytest.mark.skipif(not rtf_path.exists(), reason="Required data file is not present") |
| 57 | + def test_parse_real_rtf(self): |
| 58 | + office = Office( |
| 59 | + file_path=rtf_path, |
| 60 | + task_id="1", |
| 61 | + sha256="5b307600b1ceb84f29315c95e5b21776eb6154b79214528629e4fc2310cd50e3", |
| 62 | + options={}, |
| 63 | + ) |
| 64 | + result = office._parse_rtf(Path(rtf_path).read_bytes()) |
| 65 | + |
| 66 | + assert result == { |
| 67 | + "2": [ |
| 68 | + { |
| 69 | + "class_name": "Equation.3", |
| 70 | + "size": 3584, |
| 71 | + "filename": "object_0000272F.bin", |
| 72 | + "type_embed": "Embedded", |
| 73 | + "CVE": "Microsoft Equation 3.0 (Known Related to CVE-2017-11882 or CVE-2018-0802)", |
| 74 | + "sha256": "c00b73082638eda4af3d5318aba64ae32d23f703a02c7338d5e34230a7855e70", |
| 75 | + "index": "0000272Fh", |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + unittest.main() |
0 commit comments