Skip to content

Commit

Permalink
start drafting a datetime object for deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
malwarefrank committed Oct 19, 2024
1 parent 1c4007b commit fb6d2a6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/dnfile/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import enum
import struct as _struct
import logging
import datetime
import functools as _functools
import itertools as _itertools
from typing import TYPE_CHECKING, Any, Dict, List, Type, Tuple, Union, Generic, TypeVar, Optional, Sequence
Expand Down Expand Up @@ -980,3 +981,32 @@ def set_data(self, data: bytes):
@abc.abstractmethod
def parse(self):
raise NotImplementedError()


class DateTimeStruct(Structure):
Ticks: int
Kind: enums.DateTimeKind


class DateTime(object):
def __init__(self, rva: int, raw_bytes: bytes):
self.struct: Optional[DateTimeStruct] = None
self.raw: bytes = raw_bytes
self.value: Optional[datetime.datetime] = None

def parse(self):
if not self.raw:
# TODO: warn/error
return
# Should be 64 bites
if len(self.raw) != 8:
# TODO: warn/error
return
x = _struct.unpack("<q", self.raw)[0]
self.struct = DateTimeStruct()
self.struct.Ticks = x & 0x3FFFFFFFFFFFFFFF
self.struct.Kind = x >> 62
# https://stackoverflow.com/questions/3169517/python-c-sharp-binary-datetime-encoding
secs = self.struct.Ticks / 10.0 ** 7
delta = datetime.timedelta(seconds=secs)
self.value = datetime.datetime(1, 1, 1) + delta
14 changes: 14 additions & 0 deletions src/dnfile/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -861,3 +861,17 @@ class AssemblyHashAlgorithm(_enum.IntEnum):
SHA256 = 0x800c
SHA384 = 0x800d
SHA512 = 0x800e


class DateTimeKind(_enum.IntEnum):
"""
Per Microsoft documenation, provide additional context to DateTime instances.
REFERENCE:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/DateTime.cs
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/DateTimeKind.cs
"""
Unspecified = 0
Utc = 1
Local = 2
LocalAmbiguousDst = 3

0 comments on commit fb6d2a6

Please sign in to comment.