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.



975
976
977
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 975

def ref_id
  @ref_id
end

Instance Method Details

#assign(val) ⇒ Object



1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1086

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



1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1128

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



1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1059

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
    BinData.trace_message do |tracer|
      tracer.trace_obj("#{debug_name}.ref_id", @ref_id.to_s)
    end
    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



1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1035

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



989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 989

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



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1103

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



977
978
979
980
981
982
983
984
985
986
987
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 977

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



1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1172

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



1164
1165
1166
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1164

def instantiate_referent
  @ref_id = INITIAL_REF_ID
end

#is_alias?Boolean

Returns:

  • (Boolean)


1099
1100
1101
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1099

def is_alias?
  has_parameter?(:ref_to)
end

#is_null_ptr?Boolean

Returns:

  • (Boolean)


1168
1169
1170
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1168

def is_null_ptr?
  @ref_id == 0
end

#referent_bytes_align(offset) ⇒ Object



1013
1014
1015
1016
1017
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1013

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



1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1003

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



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/ruby_smb/dcerpc/ndr.rb', line 1019

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