Skip to content

Commit 0a2a93f

Browse files
authored
add rtf file test (#2503)
1 parent ba61597 commit 0a2a93f

File tree

5 files changed

+89
-25
lines changed

5 files changed

+89
-25
lines changed

installer/cape2.sh

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,7 @@ ARCH="$(dpkg --print-architecture)"
7171

7272
function issues() {
7373
cat << EOI
74-
Problems with PyOpenSSL?
75-
sudo rm -rf /usr/local/lib/python3.8/dist-packages/OpenSSL/
76-
sudo rm -rf /home/${USER}/.local/lib/python3.8/site-packages/OpenSSL/
77-
sudo apt-get install -y --reinstall python-openssl
78-
79-
Problem with PIP?
80-
sudo python -m pip3 uninstall pip3 && sudo apt-get install -y --reinstall python3-pip
81-
82-
Problem with pillow:
83-
* ValueError: jpeg is required unless explicitly disabled using --disable-jpeg, aborting
84-
* ValueError: zlib is required unless explicitly disabled using --disable-zlib, aborting
85-
Solution:
86-
# https://askubuntu.com/a/1094768
87-
# you may need to adjust version of libjpeg-turbo8
88-
sudo apt-get install -y zlib1g-dev libjpeg-turbo8-dev libjpeg-turbo8=1.5.2-0ubuntu5
74+
No known problems yet
8975
EOI
9076
}
9177

lib/cuckoo/common/integrations/parse_office.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def _parse_rtf(self, data: bytes) -> Dict[str, list]:
184184
rtfp.parse()
185185
save_dir = os.path.join(CUCKOO_ROOT, "storage", "analyses", self.task_id, "rtf_objects")
186186
if rtfp.objects and not path_exists(save_dir):
187-
path_mkdir(save_dir)
187+
path_mkdir(save_dir, exist_ok=True)
188188
for rtfobj in rtfp.objects:
189189
results.setdefault(str(rtfobj.format_id), [])
190190
temp_dict = {"class_name": "", "size": "", "filename": "", "type_embed": "", "CVE": "", "sha256": "", "index": ""}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ select = [
151151
"F", # pyflakes
152152
"E", # pycodestyle errors
153153
"W", # pycodestyle warnings
154-
"I", # isort
154+
# "I", # isort
155155
# "N", # pep8-naming
156156
"G", # flake8-logging-format
157157
]

tests/test_parse_office.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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()

tests/test_peepdf.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
expected_result = {
1111
"Info": {
12-
"Creator": "Scribus 1.3.3.12",
13-
"Producer": "Scribus PDF Library 1.3.3.12",
14-
"Author": ""
12+
"Creator": "Scribus 1.3.3.12",
13+
"Producer": "Scribus PDF Library 1.3.3.12",
14+
"Author": ""
1515
},
1616
"Dates": [],
1717
"Keywords": {},
@@ -31,13 +31,9 @@
3131
@pytest.mark.skipif(not data_dir.exists(), reason="Required data file is not present")
3232
class TestPeepdf:
3333
"""Class to test peepdf_parse."""
34-
@pytest.mark.skipif(
35-
not pdf_path.exists(),
36-
reason="Required data file is not present",
37-
)
34+
@pytest.mark.skipif(not pdf_path.exists(), reason="Required data file is not present")
3835
def test_peepdf_parse_valid_pdf(self):
3936
"""Test parsing a valid PDF sample."""
4037
result = peepdf_parse(str(pdf_path), pdfresult)
4138
del result["JSStreams"][0]["Data"]
42-
4339
assert result == expected_result

0 commit comments

Comments
 (0)