Class: Rex::Post::Meterpreter::GroupTlv

Inherits:
Tlv
  • Object
show all
Defined in:
lib/rex/post/meterpreter/packet.rb

Overview

Group TLVs contain zero or more TLVs

Direct Known Subclasses

Packet

Constant Summary

Constants inherited from Tlv

Tlv::HEADER_SIZE

Instance Attribute Summary collapse

Attributes inherited from Tlv

#compress, #type, #value

Instance Method Summary collapse

Methods inherited from Tlv

#htonq, #inspect, #meta_type?, #ntohq, #type?, #value?

Constructor Details

#initialize(type) ⇒ GroupTlv

Initializes the group TLV container to the supplied type and creates an empty TLV array.


918
919
920
921
922
# File 'lib/rex/post/meterpreter/packet.rb', line 918

def initialize(type)
  super(type)

  self.tlvs = []
end

Instance Attribute Details

#tlvsObject

Returns the value of attribute tlvs


906
907
908
# File 'lib/rex/post/meterpreter/packet.rb', line 906

def tlvs
  @tlvs
end

Instance Method Details

#add_tlv(type, value = nil, replace = false, compress = false) ⇒ Object

Adds a TLV of a given type and value.


986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/rex/post/meterpreter/packet.rb', line 986

def add_tlv(type, value = nil, replace = false, compress=false)

  # If we should replace any TLVs with the same type...remove them first
  if replace
    each(type) { |tlv|
      if (tlv.type == type)
        self.tlvs.delete(tlv)
      end
    }
  end

  if (type & TLV_META_TYPE_GROUP == TLV_META_TYPE_GROUP)
    tlv = GroupTlv.new(type)
  else
    tlv = Tlv.new(type, value, compress)
  end

  self.tlvs << tlv

  tlv
end

#add_tlvs(tlvs) ⇒ Object

Adds zero or more TLVs to the packet.


1011
1012
1013
1014
1015
1016
1017
# File 'lib/rex/post/meterpreter/packet.rb', line 1011

def add_tlvs(tlvs)
  if tlvs
    tlvs.each { |tlv|
      add_tlv(tlv['type'], tlv['value'])
    }
  end
end

#each(type = TLV_TYPE_ANY, &block) ⇒ Object

Enumerates TLVs of the supplied type.


933
934
935
# File 'lib/rex/post/meterpreter/packet.rb', line 933

def each(type = TLV_TYPE_ANY, &block)
  get_tlvs(type).each(&block)
end

#each_tlv(type = TLV_TYPE_ANY, &block) ⇒ Object

Synonym for each.


940
941
942
# File 'lib/rex/post/meterpreter/packet.rb', line 940

def each_tlv(type = TLV_TYPE_ANY, &block)
  each(type, &block)
end

#each_tlv_with_index(type = TLV_TYPE_ANY, &block) ⇒ Object

Synonym for each_with_index.


954
955
956
# File 'lib/rex/post/meterpreter/packet.rb', line 954

def each_tlv_with_index(type = TLV_TYPE_ANY, &block)
  each_with_index(type, block)
end

#each_with_index(type = TLV_TYPE_ANY, &block) ⇒ Object

Enumerates TLVs of a supplied type with indexes.


947
948
949
# File 'lib/rex/post/meterpreter/packet.rb', line 947

def each_with_index(type = TLV_TYPE_ANY, &block)
  get_tlvs(type).each_with_index(&block)
end

#from_r(raw) ⇒ Object

Converts the TLV group container from raw to all of the individual TLVs.


1087
1088
1089
1090
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
# File 'lib/rex/post/meterpreter/packet.rb', line 1087

def from_r(raw)
  offset = HEADER_SIZE

  # Reset the TLVs array
  self.tlvs = []
  self.type = raw.unpack("NN")[1]

  # Enumerate all of the TLVs
  while offset < raw.length-1

    tlv = nil

    # Get the length and type
    length, type = raw[offset..offset+HEADER_SIZE].unpack("NN")

    if (type & TLV_META_TYPE_GROUP == TLV_META_TYPE_GROUP)
      tlv = GroupTlv.new(type)
    else
      tlv = Tlv.new(type)
    end

    tlv.from_r(raw[offset..offset+length])

    # Insert it into the list of TLVs
    tlvs << tlv

    # Move up
    offset += length
  end
end

#get_tlv(type, index = 0) ⇒ Object

Gets the first TLV of a given type.


1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
# File 'lib/rex/post/meterpreter/packet.rb', line 1022

def get_tlv(type, index = 0)
  type_tlvs = get_tlvs(type)

  if type_tlvs.length > index
    type_tlvs[index]
  else
    nil
  end

end

#get_tlv_value(type, index = 0) ⇒ Object

Returns the value of a TLV if it exists, otherwise nil.


1036
1037
1038
1039
1040
# File 'lib/rex/post/meterpreter/packet.rb', line 1036

def get_tlv_value(type, index = 0)
  tlv = get_tlv(type, index)

  (tlv != nil) ? tlv.value : nil
end

#get_tlv_values(type) ⇒ Object

Returns an array of values for all tlvs of type type.


1045
1046
1047
# File 'lib/rex/post/meterpreter/packet.rb', line 1045

def get_tlv_values(type)
  get_tlvs(type).collect { |a| a.value }
end

#get_tlvs(type) ⇒ Object

Returns an array of TLVs for the given type.


961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# File 'lib/rex/post/meterpreter/packet.rb', line 961

def get_tlvs(type)
  if type == TLV_TYPE_ANY
    self.tlvs
  else
    type_tlvs = []

    self.tlvs.each() { |tlv|
      if (tlv.type?(type))
        type_tlvs << tlv
      end
    }

    type_tlvs
  end
end

#has_tlv?(type) ⇒ Boolean

Checks to see if the container has a TLV of a given type.

Returns:

  • (Boolean)

1052
1053
1054
# File 'lib/rex/post/meterpreter/packet.rb', line 1052

def has_tlv?(type)
  get_tlv(type) != nil
end

#resetObject

Zeros out the array of TLVs.


1059
1060
1061
# File 'lib/rex/post/meterpreter/packet.rb', line 1059

def reset
  self.tlvs = []
end

#to_rObject

Converts all of the TLVs in the TLV array to raw and prefixes them with a container TLV of this instance's TLV type.


1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'lib/rex/post/meterpreter/packet.rb', line 1073

def to_r
  raw = ''

  self.each() { |tlv|
    raw << tlv.to_r
  }

  [raw.length + HEADER_SIZE, self.type].pack("NN") + raw
end