Refactor conversion code from objects.utility to a conversion module in renderers.

This commit is contained in:
Mike Auty
2018-12-12 23:39:10 +00:00
parent 1a5c9d3af8
commit 8acbe8bba3
7 changed files with 66 additions and 62 deletions
+1 -44
View File
@@ -1,7 +1,6 @@
import datetime
import typing
from volatility.framework import interfaces, objects, renderers, constants
from volatility.framework import interfaces, objects, constants
def array_to_string(array: objects.Array,
@@ -44,45 +43,3 @@ def array_of_pointers(array: interfaces.objects.ObjectInterface,
return array.cast("array", count = count, subtype = subtype_pointer)
def wintime_to_datetime(wintime: int) -> typing.Union[
interfaces.renderers.BaseAbsentValue, datetime.datetime]:
unix_time = wintime // 10000000
if unix_time == 0:
return renderers.NotApplicableValue()
unix_time = unix_time - 11644473600
try:
return datetime.datetime.utcfromtimestamp(unix_time)
except ValueError:
return renderers.UnparsableValue()
def unixtime_to_datetime(unixtime: int) -> typing.Union[interfaces.renderers.BaseAbsentValue, datetime.datetime]:
ret = renderers.UnparsableValue() # type: typing.Union[interfaces.renderers.BaseAbsentValue, datetime.datetime]
if unixtime > 0:
try:
ret = datetime.datetime.utcfromtimestamp(unixtime)
except ValueError:
pass
return ret
def round(addr: int, align: int, up: bool = False) -> int:
"""Round an address up or down based on an alignment.
Args:
addr: the address
align: the alignment value
up: Whether to round up or not
Returns:
The aligned address
"""
if addr % align == 0:
return addr
else:
if up:
return (addr + (align - (addr % align)))
return (addr - (addr % align))
@@ -0,0 +1,48 @@
import datetime
import typing
from volatility.framework import interfaces, renderers
def wintime_to_datetime(wintime: int) -> typing.Union[
interfaces.renderers.BaseAbsentValue, datetime.datetime]:
unix_time = wintime // 10000000
if unix_time == 0:
return renderers.NotApplicableValue()
unix_time = unix_time - 11644473600
try:
return datetime.datetime.utcfromtimestamp(unix_time)
except ValueError:
return renderers.UnparsableValue()
def unixtime_to_datetime(unixtime: int) -> typing.Union[interfaces.renderers.BaseAbsentValue, datetime.datetime]:
ret = renderers.UnparsableValue() # type: typing.Union[interfaces.renderers.BaseAbsentValue, datetime.datetime]
if unixtime > 0:
try:
ret = datetime.datetime.utcfromtimestamp(unixtime)
except ValueError:
pass
return ret
def round(addr: int, align: int, up: bool = False) -> int:
"""Round an address up or down based on an alignment.
Args:
addr: the address
align: the alignment value
up: Whether to round up or not
Returns:
The aligned address
"""
if addr % align == 0:
return addr
else:
if up:
return (addr + (align - (addr % align)))
return (addr - (addr % align))
@@ -1,6 +1,7 @@
from volatility.framework import exceptions
from volatility.framework import objects
from volatility.framework.objects import utility
from volatility.framework.renderers import conversion
class hist_entry(objects.Struct):
@@ -41,7 +42,7 @@ class hist_entry(objects.Struct):
def get_time_object(self):
nsecs = self.get_time_as_integer()
# Build a timestamp object from the integer
return utility.unixtime_to_datetime(nsecs)
return conversion.unixtime_to_datetime(nsecs)
def get_command(self):
return utility.array_to_string(self.line.dereference())
@@ -3,9 +3,9 @@ import functools
import logging
import typing
import volatility.framework.objects.utility
from volatility.framework import constants, exceptions, interfaces, objects, renderers, symbols
from volatility.framework.layers import intel
from volatility.framework.renderers import conversion
from volatility.framework.symbols import generic
from volatility.framework.symbols.windows.extensions.registry import RegKeyFlags
@@ -45,7 +45,7 @@ class _POOL_HEADER(objects.Struct):
else:
alignment = pool_header_size
type_size = self._context.symbol_space.get_type(symbol_table_name + constants.BANG + type_name).size
rounded_size = objects.utility.round(type_size, alignment, up = True)
rounded_size = conversion.round(type_size, alignment, up = True)
mem_object = self._context.object(symbol_table_name + constants.BANG + type_name,
layer_name = self.vol.layer_name,
@@ -69,7 +69,7 @@ class _KSYSTEM_TIME(objects.Struct):
def get_time(self):
wintime = (self.High1Time << 32) | self.LowPart
return objects.utility.wintime_to_datetime(wintime)
return conversion.wintime_to_datetime(wintime)
class _MMVAD_SHORT(objects.Struct):
@@ -564,10 +564,10 @@ class _EPROCESS(generic.GenericIntelProcess, ExecutiveObject):
return renderers.UnreadableValue()
def get_create_time(self):
return objects.utility.wintime_to_datetime(self.CreateTime.QuadPart)
return conversion.wintime_to_datetime(self.CreateTime.QuadPart)
def get_exit_time(self):
return objects.utility.wintime_to_datetime(self.ExitTime.QuadPart)
return conversion.wintime_to_datetime(self.ExitTime.QuadPart)
def get_wow_64_process(self):
if self.has_member("Wow64Process"):
@@ -3,7 +3,7 @@ import typing
from volatility.framework import constants
from volatility.framework import objects, interfaces
from volatility.framework.objects import utility
from volatility.framework.renderers import conversion
class _IMAGE_DOS_HEADER(objects.Struct):
@@ -127,7 +127,7 @@ class _IMAGE_DOS_HEADER(objects.Struct):
sect_sizes.append(sect.VirtualAddress - prevsect.VirtualAddress)
prevsect = sect
if prevsect is not None:
sect_sizes.append(utility.round(prevsect.Misc.VirtualSize, section_alignment, up = True))
sect_sizes.append(conversion.round(prevsect.Misc.VirtualSize, section_alignment, up = True))
counter = 0
start_addr = nt_header.FileHeader.SizeOfOptionalHeader + \