Module: RubySMB::Dcerpc::Ndr::PointerPlugin

Defined in:
lib/ruby_smb/dcerpc/ndr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ref_idObject

Returns the value of attribute ref_id.



941
942
943
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 941

def ref_id
  @ref_id
end

Instance Method Details

#assign(val) ⇒ Object



1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1049

def assign(val)
  if val == :null
    @ref_id = 0
  elsif is_alias?
    ref_field = fetch_alias_referent
    raise ArgumentError, "Referent of alias pointer does not exist: #{get_parameter(:ref_to)}" unless ref_field
    ref_field.assign(val)
  else
    instantiate_referent if is_null_ptr?
    super
  end
end

#do_num_bytes(struct_offset = 0, is_deferred: false) ⇒ Object



1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1091

def do_num_bytes(struct_offset = 0, is_deferred: false)
  sum = 0
  if is_deferred
    if is_a?(NdrStruct) && self.class.superclass.has_conformant_array
      # align :max_count since it will be placed in front of the structure.
      # The structure itself will be properly aligned later.
      align = (4 - (struct_offset % 4)) % 4
      sum += align
    else
      sum += referent_bytes_align(struct_offset)
    end
  else
    # add ref_id size
    sum += 4

    parent_obj = nil
    if parent&.is_a?(ConstructedTypePlugin)
      parent_obj = parent.get_top_level_constructed_type
    end
    if parent_obj && @ref_id != 0 && !@standalone_ptr
      parent_obj.defer_ptr(self)
      # only return ref_id size, the actual referent size will be added later
      return sum
    end
  end
  unless (is_null_ptr? && !eval_parameter(:initial_value)) || is_alias?
    if is_a?(ArrayPlugin)
      sum += super(struct_offset + sum)
    else
      sum += super()
    end
  end

  sum
end

#do_read(io, is_deferred: false) ⇒ Object



1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1025

def do_read(io, is_deferred: false)
  if is_deferred
    if is_a?(NdrStruct) && self.class.superclass.has_conformant_array
      # align :max_count since it will be placed in front of the structure.
      # The structure itself will be properly aligned later.
      align = (4 - (io.offset % 4)) % 4
      io.seekbytes(align)
    else
      io.seekbytes(referent_bytes_align(io.offset))
    end
  else
    @ref_id = io.readbytes(4).unpack('L<').first
    parent_obj = nil
    if parent&.is_a?(ConstructedTypePlugin)
      parent_obj = parent.get_top_level_constructed_type
    end
    if parent_obj && @ref_id != 0
      parent_obj.defer_ptr(self)
      return
    end
  end
  super(io) unless is_null_ptr? || is_alias?
end

#do_write(io, is_deferred: false) ⇒ Object



1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1001

def do_write(io, is_deferred: false)
  if is_deferred
    if is_a?(NdrStruct) && self.class.superclass.has_conformant_array
      # align :max_count since it will be placed in front of the structure.
      # The structure itself will be properly aligned later.
      align = (4 - (io.offset % 4)) % 4
      io.writebytes("\x00" * align)
    else
      io.writebytes("\x00" * referent_bytes_align(io.offset))
    end
  else
    write_ref_id(io)
    parent_obj = nil
    if parent&.is_a?(ConstructedTypePlugin)
      parent_obj = parent.get_top_level_constructed_type
    end
    if parent_obj && @ref_id != 0 && !@standalone_ptr
      parent_obj.defer_ptr(self)
      return
    end
  end
  super(io) unless (is_null_ptr? && !eval_parameter(:initial_value)) || is_alias?
end

#extend_top_level_classObject



955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 955

def extend_top_level_class
  current = self
  loop do
    current.extend(TopLevelPlugin) unless current.is_a?(TopLevelPlugin)
    if current.parent.nil?
      current.set_top_level_ptr unless current.is_top_level_ptr
      break
    else
      current.unset_top_level_ptr if current.is_top_level_ptr
      current = current.parent
    end
  end
end

#fetch_alias_referent(current: parent, ref: get_parameter(:ref_to), name: nil) ⇒ Object



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1066

def fetch_alias_referent(current: parent, ref: get_parameter(:ref_to), name: nil)
  return if current.nil?
  if current.get_parameter(:ref_to) == ref
    raise ArgumentError.new(
      "Pointer alias refering to #{ref} cannot be found. This referent "\
      "should appears before the alias in the stream"
    )
  end
  return current if name == ref
  res = nil
  case current
  when ArrayPlugin
    current.each do |element|
      res = fetch_alias_referent(current: element, ref: ref, name: name)
      break if res
    end
  when BinData::Record, BinData::Struct
    current.each_pair do |name, field|
      res = fetch_alias_referent(current: field, ref: ref, name: name)
      break if res
    end
  end
  return res
end

#initialize_instanceObject



943
944
945
946
947
948
949
950
951
952
953
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 943

def initialize_instance
  if @ref_id.nil?
    if eval_parameter(:initial_value)
      instantiate_referent
    else
      @ref_id = 0
    end
  end
  extend_top_level_class
  super
end

#insert(index, *objs) ⇒ Object



1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1135

def insert(index, *objs)
  obj = super
  # If we just pushed a new element and it was a null pointer (ref_id==0),
  # we will initialize the ref_id to make sure it is not considered a null
  # pointer anymore
  if is_null_ptr? && is_a?(BinData::Array) && !empty?
    instantiate_referent
  end
  obj
end

#instantiate_referentObject



1127
1128
1129
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1127

def instantiate_referent
  @ref_id = INITIAL_REF_ID
end

#is_alias?Boolean

Returns:

  • (Boolean)


1062
1063
1064
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1062

def is_alias?
  has_parameter?(:ref_to)
end

#is_null_ptr?Boolean

Returns:

  • (Boolean)


1131
1132
1133
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1131

def is_null_ptr?
  @ref_id == 0
end

#referent_bytes_align(offset) ⇒ Object



979
980
981
982
983
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 979

def referent_bytes_align(offset)
  align = self.class.superclass.default_parameters[:byte_align]
  align = eval_parameter(:referent_byte_align) unless align
  (align - (offset % align)) % align
end

#snapshotObject



969
970
971
972
973
974
975
976
977
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 969

def snapshot
  if is_alias?
    fetch_alias_referent
  elsif is_null_ptr? && !eval_parameter(:initial_value)
    :null
  else
    super
  end
end

#write_ref_id(io) ⇒ Object



985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 985

def write_ref_id(io)
  if is_alias?
    ref_field = fetch_alias_referent
    if ref_field
      if ref_field.class != self.class
        raise ArgumentError, "Pointer points to a different referent type: #{ref_field.class} (set to #{obj.class})"
      end
      @ref_id = ref_field.ref_id
    end
  elsif @ref_id != 0 || (is_null_ptr? && eval_parameter(:initial_value))
    @ref_id = INITIAL_REF_ID + self.class.pos
    self.class.increment_pos unless @standalone_ptr
  end
  io.writebytes([@ref_id].pack('L<'))
end