class Rex::Proto::ADB::Message

A Message for the ADB protocol. For documentation see: android.googlesource.com/platform/system/core/+/master/adb/protocol.txt

Constants

MESSAGE_TYPES

Avoid a dependency on Rails's nice Class#subclasses

WORD_PACK
WORD_WIDTH

Attributes

arg0[RW]
arg1[RW]
command[RW]
data[RW]

Public Class Methods

new(arg0, arg1, data) click to toggle source
# File lib/rex/proto/adb/message.rb, line 22
def initialize(arg0, arg1, data)
  self.command = self.class::COMMAND if defined?(self.class::COMMAND)
  self.arg0 = arg0
  self.arg1 = arg1
  self.data = data + "\0"
end
read(socket) click to toggle source
# File lib/rex/proto/adb/message.rb, line 68
def self.read(socket)
  header = socket.recvfrom(6 * WORD_WIDTH)[0]
  command = header[0, WORD_WIDTH]
  arg0 = header[WORD_WIDTH, WORD_WIDTH].unpack(WORD_PACK)[0]
  arg1 = header[WORD_WIDTH*2, WORD_WIDTH].unpack(WORD_PACK)[0]
  payload_len = header[WORD_WIDTH*3, WORD_WIDTH].unpack(WORD_PACK)[0]
  payload = socket.recvfrom(payload_len)[0]

  klass = MESSAGE_TYPES.find { |klass| klass::COMMAND == command }
  if klass.nil?
    raise "Invalid adb command: #{command}"
  end

  message = klass.allocate
  message.command = command
  message.arg0 = arg0
  message.arg1 = arg1
  message.data = payload
  message
end

Public Instance Methods

command_word() click to toggle source
# File lib/rex/proto/adb/message.rb, line 39
def command_word
  command.unpack(WORD_PACK)[0]
end
data_check() click to toggle source
# File lib/rex/proto/adb/message.rb, line 29
def data_check
  # this check is implemented in adb/transport.cpp, in the send_packet method.
  # it is not crc32 as the docs make it appear, it is just a 32bit sum.
  data.bytes.inject(&:+) & 0xffffffff
end
magic() click to toggle source
# File lib/rex/proto/adb/message.rb, line 35
def magic
  command_word ^ 0xffffffff
end
send_recv(socket) click to toggle source
# File lib/rex/proto/adb/message.rb, line 43
def send_recv(socket)
  socket.print self.serialize
  Message.read socket
end
serialize() click to toggle source
# File lib/rex/proto/adb/message.rb, line 48
def serialize
  [
    command_word,
    arg0,
    arg1,
    data.bytes.length,
    data_check,
    magic
  ].pack(WORD_PACK+'*') + data
end
to_s() click to toggle source
# File lib/rex/proto/adb/message.rb, line 59
def to_s
  [
    "command=#{command}",
    "arg0=0x#{arg0.to_s(16)}",
    "arg1=0x#{arg1.to_s(16)}",
    "data=#{data}"
  ].join("\n")
end