2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-04-10 00:39:39 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class is responsible for reading in and decrypting meterpreter
|
|
|
|
# packets that arrive on a socket
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class PacketParser
|
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
2017-06-20 09:18:37 +00:00
|
|
|
# Initializes the packet parser context.
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
2017-06-20 09:18:37 +00:00
|
|
|
def initialize
|
2013-08-30 21:28:33 +00:00
|
|
|
reset
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Resets the parser state so that a new packet can begin being parsed.
|
|
|
|
#
|
|
|
|
def reset
|
2017-06-20 09:18:37 +00:00
|
|
|
self.packet = Packet.new(0)
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2017-10-27 09:29:25 +00:00
|
|
|
# Reads data from the socket and parses as much of the packet as possible.
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
def recv(sock)
|
2017-10-27 09:29:25 +00:00
|
|
|
raw = nil
|
|
|
|
if self.packet.raw_bytes_required > 0
|
2017-10-27 09:15:08 +00:00
|
|
|
while (raw = sock.read(self.packet.raw_bytes_required))
|
2017-06-20 09:18:37 +00:00
|
|
|
self.packet.add_raw(raw)
|
2017-10-27 09:15:08 +00:00
|
|
|
break if self.packet.raw_bytes_required == 0
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-27 09:15:08 +00:00
|
|
|
if self.packet.raw_bytes_required > 0
|
2017-10-27 09:29:25 +00:00
|
|
|
if raw == nil
|
|
|
|
raise EOFError
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2017-06-20 09:18:37 +00:00
|
|
|
|
2017-10-27 09:15:08 +00:00
|
|
|
packet = self.packet
|
|
|
|
reset
|
|
|
|
packet
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
protected
|
2017-06-20 09:18:37 +00:00
|
|
|
attr_accessor :cipher, :packet # :nodoc:
|
2005-04-10 00:39:39 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-12-08 18:32:26 +00:00
|
|
|
end; end; end
|
|
|
|
|