File: tsk_data_stream.py

package info (click to toggle)
dfvfs 20240505-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 475,508 kB
  • sloc: python: 36,533; vhdl: 1,922; sh: 448; xml: 52; makefile: 16
file content (321 lines) | stat: -rw-r--r-- 11,512 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the data stream implementation using pytsk3."""

import unittest

import pytsk3

from dfvfs.lib import definitions
from dfvfs.path import factory as path_spec_factory
from dfvfs.resolver import context
from dfvfs.vfs import tsk_data_stream
from dfvfs.vfs import tsk_file_system

from tests import test_lib as shared_test_lib


class TSKDataStreamTestExt2(shared_test_lib.BaseTestCase):
  """Tests the SleuthKit (TSK) data stream on ext2."""

  _INODE_ANOTHER_FILE = 15

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    self._resolver_context = context.Context()
    test_path = self._GetTestFilePath(['ext2.raw'])
    self._SkipIfPathNotExists(test_path)

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    self._raw_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_RAW, parent=test_os_path_spec)
    self._tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, location='/',
        parent=self._raw_path_spec)

    self._file_system = tsk_file_system.TSKFileSystem(
        self._resolver_context, self._tsk_path_spec)
    self._file_system.Open()

  def tearDown(self):
    """Cleans up the needed objects used throughout the test."""
    self._resolver_context.Empty()

  def testName(self):
    """Test the name property."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_EXT, inode=self._INODE_ANOTHER_FILE,
        location='/a_directory/another_file', parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)
    self.assertEqual(test_data_stream.name, '')

  def testGetExtents(self):
    """Test the GetExtents function."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_EXT, inode=self._INODE_ANOTHER_FILE,
        location='/a_directory/another_file', parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)

    extents = test_data_stream.GetExtents()
    self.assertEqual(len(extents), 1)

    self.assertEqual(extents[0].extent_type, definitions.EXTENT_TYPE_DATA)
    self.assertEqual(extents[0].offset, 527360)
    self.assertEqual(extents[0].size, 1024)

  def testIsDefault(self):
    """Test the IsDefault function."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_EXT, inode=self._INODE_ANOTHER_FILE,
        location='/a_directory/another_file', parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)

    result = test_data_stream.IsDefault()
    self.assertTrue(result)


class TSKDataStreamTestHFSPlus(shared_test_lib.BaseTestCase):
  """Tests the SleuthKit (TSK) data stream on HFS+."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    self._resolver_context = context.Context()
    test_path = self._GetTestFilePath(['hfsplus.raw'])
    self._SkipIfPathNotExists(test_path)

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    self._raw_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_RAW, parent=test_os_path_spec)
    self._tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, location='/',
        parent=self._raw_path_spec)

    self._file_system = tsk_file_system.TSKFileSystem(
        self._resolver_context, self._tsk_path_spec)
    self._file_system.Open()

  def tearDown(self):
    """Cleans up the needed objects used throughout the test."""
    self._resolver_context.Empty()

  def _GetResourceForkAttribute(self, tsk_file):
    """Retrieves an resource fork attribute.

    Args:
      tsk_file (pytsk3.File): TSK file.

    Returns:
      pytsk3.Attribute: TSK attribute or None.
    """
    for tsk_attribute in tsk_file:
      if getattr(tsk_attribute, 'info', None):
        attribute_type = getattr(tsk_attribute.info, 'type', None)
        if attribute_type == pytsk3.TSK_FS_ATTR_TYPE_HFS_RSRC:
          return tsk_attribute

    return None

  def testName(self):
    """Test the name property."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, inode=25,
        location='/a_directory/a_resourcefork', parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)
    self.assertEqual(test_data_stream.name, '')

    tsk_file = file_entry.GetTSKFile()
    self.assertIsNotNone(tsk_file)

    tsk_attribute = self._GetResourceForkAttribute(tsk_file)
    self.assertIsNotNone(tsk_attribute)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, tsk_attribute)
    self.assertEqual(test_data_stream.name, 'rsrc')

  def testGetExtents(self):
    """Test the GetExtents function."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, inode=25,
        location='/a_directory/a_resourcefork', parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)

    extents = test_data_stream.GetExtents()
    self.assertEqual(len(extents), 0)

    tsk_file = file_entry.GetTSKFile()
    self.assertIsNotNone(tsk_file)

    tsk_attribute = self._GetResourceForkAttribute(tsk_file)
    self.assertIsNotNone(tsk_attribute)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, tsk_attribute)

    extents = test_data_stream.GetExtents()
    self.assertEqual(len(extents), 1)

    self.assertEqual(extents[0].extent_type, definitions.EXTENT_TYPE_DATA)
    self.assertEqual(extents[0].offset, 1142784)
    self.assertEqual(extents[0].size, 4096)

  def testIsDefault(self):
    """Test the IsDefault function."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, inode=25,
        location='/a_directory/a_resourcefork', parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)

    result = test_data_stream.IsDefault()
    self.assertTrue(result)

    tsk_file = file_entry.GetTSKFile()
    self.assertIsNotNone(tsk_file)

    tsk_attribute = self._GetResourceForkAttribute(tsk_file)
    self.assertIsNotNone(tsk_attribute)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, tsk_attribute)

    result = test_data_stream.IsDefault()
    self.assertFalse(result)


class TSKDataStreamTestNTFS(shared_test_lib.BaseTestCase):
  """Tests the SleuthKit (TSK) data stream on NTFS."""

  def setUp(self):
    """Sets up the needed objects used throughout the test."""
    self._resolver_context = context.Context()
    test_path = self._GetTestFilePath(['ntfs.raw'])
    self._SkipIfPathNotExists(test_path)

    test_os_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_OS, location=test_path)
    self._raw_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_RAW, parent=test_os_path_spec)
    self._tsk_path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, location='/',
        parent=self._raw_path_spec)

    self._file_system = tsk_file_system.TSKFileSystem(
        self._resolver_context, self._tsk_path_spec)
    self._file_system.Open()

  def tearDown(self):
    """Cleans up the needed objects used throughout the test."""
    self._resolver_context.Empty()

  def _GetAlternateDataStreamAttribute(self, tsk_file):
    """Retrieves an ADS attribute.

    Args:
      tsk_file (pytsk3.File): TSK file.

    Returns:
      pytsk3.Attribute: TSK attribute or None.
    """
    for tsk_attribute in tsk_file:
      if getattr(tsk_attribute, 'info', None):
        attribute_name = getattr(tsk_attribute.info, 'name', None)
        attribute_type = getattr(tsk_attribute.info, 'type', None)
        if (attribute_type == pytsk3.TSK_FS_ATTR_TYPE_NTFS_DATA and
            attribute_name):
          return tsk_attribute

    return None

  def testName(self):
    """Test the name property."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, inode=10, location='/$UpCase',
        parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)
    self.assertEqual(test_data_stream.name, '')

    tsk_file = file_entry.GetTSKFile()
    self.assertIsNotNone(tsk_file)

    tsk_attribute = self._GetAlternateDataStreamAttribute(tsk_file)
    self.assertIsNotNone(tsk_attribute)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, tsk_attribute)
    self.assertEqual(test_data_stream.name, '$Info')

  def testGetExtents(self):
    """Test the GetExtents function."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, inode=10, location='/$UpCase',
        parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)

    extents = test_data_stream.GetExtents()
    self.assertEqual(len(extents), 1)

    self.assertEqual(extents[0].extent_type, definitions.EXTENT_TYPE_DATA)
    self.assertEqual(extents[0].offset, 823296)
    self.assertEqual(extents[0].size, 131072)

    tsk_file = file_entry.GetTSKFile()
    self.assertIsNotNone(tsk_file)

    tsk_attribute = self._GetAlternateDataStreamAttribute(tsk_file)
    self.assertIsNotNone(tsk_attribute)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, tsk_attribute)

    extents = test_data_stream.GetExtents()
    # No extents are returned for a resident $DATA attribute.
    self.assertEqual(len(extents), 0)

  def testIsDefault(self):
    """Test the IsDefault function."""
    path_spec = path_spec_factory.Factory.NewPathSpec(
        definitions.TYPE_INDICATOR_TSK, inode=10, location='/$UpCase',
        parent=self._raw_path_spec)
    file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
    self.assertIsNotNone(file_entry)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, None)

    result = test_data_stream.IsDefault()
    self.assertTrue(result)

    tsk_file = file_entry.GetTSKFile()
    self.assertIsNotNone(tsk_file)

    tsk_attribute = self._GetAlternateDataStreamAttribute(tsk_file)
    self.assertIsNotNone(tsk_attribute)

    test_data_stream = tsk_data_stream.TSKDataStream(file_entry, tsk_attribute)

    result = test_data_stream.IsDefault()
    self.assertFalse(result)


if __name__ == '__main__':
  unittest.main()