jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
320 stars 74 forks source link

OverflowError when parsing timestamp header. #114

Closed Bargenish closed 3 years ago

Bargenish commented 3 years ago

In the DateTimeField class the _parse_value implemention is as follows:

  def _parse_value(self, value):
      if value is None:
          datetime_value = datetime.today()
      elif isinstance(value, types.LambdaType):
          datetime_value = value
      elif isinstance(value, bytes):
          format = self._get_struct_format(8)
          struct_string = "%s%s"\
                          % ("<" if self.little_endian else ">", format)
          int_value = struct.unpack(struct_string, value)[0]
          return self._parse_value(int_value)  # just parse the value again
      elif isinstance(value, integer_types):

          time_microseconds = (value - self.EPOCH_FILETIME) // 10
          datetime_value = datetime(1970, 1, 1) + \
              timedelta(microseconds=time_microseconds)
      elif isinstance(value, datetime):
          datetime_value = value
      else:
          raise TypeError("Cannot parse value for field %s of type %s to a "
                          "datetime" % (self.name, type(value).__name__))
      return datetime_value

When (for some reasons) the data being sent in the SMB2HeaderResponse contains an invalid epoch time, e.g - 94661952, the timedelta method fails on OverflowError which causes the entire connection to fail even though the creds and data are ok.

We implemented a monkeypatch to set the datetime_value to datetime.today() in case an OverflowError exception occurs.

jborean93 commented 3 years ago

Sorry the delay in looking at this, I've just merged https://github.com/jborean93/smbprotocol/pull/125 which should solve this issue. It sets the value to the maximum datetime if there is an overflow.

Bargenish commented 3 years ago

Awesome. thanks.