|
| 1 | +import io |
| 2 | +import pytest |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from test import test_volatility |
| 5 | + |
| 6 | +HAS_PYARROW = False |
| 7 | +try: |
| 8 | + import pyarrow as pa |
| 9 | + import pyarrow.parquet as pq |
| 10 | + import pyarrow.compute as pc |
| 11 | + HAS_PYARROW = True |
| 12 | +except ImportError: |
| 13 | + # The user doesn't have pyarrow installed, but HAS_PYARROW will be false so just continue |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.skipif(not HAS_PYARROW, reason="pyarrow not installed") |
| 18 | +class TestArrowRendererBase(ABC): |
| 19 | + """Base class for testing Arrow-based renderers. |
| 20 | +
|
| 21 | + Re-implements Windows and Linux plugin tests using PyArrow operations |
| 22 | + instead of text-based assertions. |
| 23 | + """ |
| 24 | + |
| 25 | + renderer_format = None # Override in subclasses |
| 26 | + |
| 27 | + @abstractmethod |
| 28 | + def _get_table_from_output(self, output_bytes) -> "pa.Table": |
| 29 | + """Parse output bytes into Arrow table. Override in subclasses.""" |
| 30 | + |
| 31 | + def test_windows_generic_pslist(self, volatility, python, image): |
| 32 | + rc, out, _err = test_volatility.runvol_plugin( |
| 33 | + "windows.pslist.PsList", |
| 34 | + image, |
| 35 | + volatility, |
| 36 | + python, |
| 37 | + globalargs=("-r", self.renderer_format), |
| 38 | + ) |
| 39 | + assert rc == 0 |
| 40 | + |
| 41 | + table = self._get_table_from_output(out) |
| 42 | + assert table.num_rows > 10 |
| 43 | + |
| 44 | + assert table.filter(pc.match_substring(pc.utf8_lower(table.column('ImageFileName')), "system")).num_rows > 0 |
| 45 | + assert table.filter(pc.match_substring(pc.utf8_lower(table.column('ImageFileName')), "csrss.exe")).num_rows > 0 |
| 46 | + assert table.filter(pc.match_substring(pc.utf8_lower(table.column('ImageFileName')), "svchost.exe")).num_rows > 0 |
| 47 | + assert table.filter(pc.greater(table.column('PID'), 0)).num_rows == table.num_rows |
| 48 | + |
| 49 | + def test_linux_generic_pslist(self, volatility, python, image): |
| 50 | + rc, out, _err = test_volatility.runvol_plugin( |
| 51 | + "linux.pslist.PsList", |
| 52 | + image, |
| 53 | + volatility, |
| 54 | + python, |
| 55 | + globalargs=("-r", self.renderer_format), |
| 56 | + ) |
| 57 | + assert rc == 0 |
| 58 | + |
| 59 | + table = self._get_table_from_output(out) |
| 60 | + assert table.num_rows > 10 |
| 61 | + |
| 62 | + init_rows = table.filter(pc.match_substring(pc.utf8_lower(table.column('COMM')), "init")) |
| 63 | + systemd_rows = table.filter(pc.match_substring(pc.utf8_lower(table.column('COMM')), "systemd")) |
| 64 | + assert (init_rows.num_rows > 0) or (systemd_rows.num_rows > 0) |
| 65 | + |
| 66 | + assert table.filter(pc.match_substring(pc.utf8_lower(table.column('COMM')), "watchdog")).num_rows > 0 |
| 67 | + assert table.filter(pc.greater(table.column('PID'), 0)).num_rows == table.num_rows |
| 68 | + |
| 69 | + def test_windows_generic_handles(self, volatility, python, image): |
| 70 | + rc, out, _err = test_volatility.runvol_plugin( |
| 71 | + "windows.handles.Handles", |
| 72 | + image, |
| 73 | + volatility, |
| 74 | + python, |
| 75 | + globalargs=("-r", self.renderer_format), |
| 76 | + pluginargs=("--pid", "4"), |
| 77 | + ) |
| 78 | + assert rc == 0 |
| 79 | + |
| 80 | + table = self._get_table_from_output(out) |
| 81 | + assert table.num_rows > 500 |
| 82 | + assert table.filter(pc.match_substring(pc.utf8_lower(table.column('Name')), "machine\\system")).num_rows > 0 |
| 83 | + |
| 84 | + def test_linux_generic_lsof(self, volatility, python, image): |
| 85 | + rc, out, _err = test_volatility.runvol_plugin( |
| 86 | + "linux.lsof.Lsof", |
| 87 | + image, |
| 88 | + volatility, |
| 89 | + python, |
| 90 | + globalargs=("-r", self.renderer_format), |
| 91 | + ) |
| 92 | + assert rc == 0 |
| 93 | + |
| 94 | + table = self._get_table_from_output(out) |
| 95 | + assert table.num_rows > 35 |
| 96 | + |
| 97 | +class TestParquetRenderer(TestArrowRendererBase): |
| 98 | + renderer_format = "parquet" |
| 99 | + |
| 100 | + def _get_table_from_output(self, output_bytes): |
| 101 | + return pq.read_table(io.BytesIO(output_bytes)) |
| 102 | + |
| 103 | + |
| 104 | +class TestArrowRenderer(TestArrowRendererBase): |
| 105 | + renderer_format = "arrow" |
| 106 | + |
| 107 | + def _get_table_from_output(self, output_bytes): |
| 108 | + return pa.ipc.open_stream(io.BytesIO(output_bytes)).read_all() |
| 109 | + |
0 commit comments