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
|
|
|
|
|
|
|
|
#
|
|
|
|
# Reads data from the wire and parse as much of the packet as possible.
|
|
|
|
#
|
|
|
|
def recv(sock)
|
2017-06-20 09:18:37 +00:00
|
|
|
bytes_left = self.packet.raw_bytes_required
|
2013-08-30 21:28:33 +00:00
|
|
|
|
2017-06-20 09:18:37 +00:00
|
|
|
if bytes_left > 0
|
|
|
|
raw = sock.read(bytes_left)
|
|
|
|
if raw
|
|
|
|
self.packet.add_raw(raw)
|
2013-08-30 21:28:33 +00:00
|
|
|
else
|
|
|
|
raise EOFError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-20 09:18:37 +00:00
|
|
|
if self.packet.raw_bytes_required == 0
|
|
|
|
packet = self.packet
|
2013-08-30 21:28:33 +00:00
|
|
|
reset
|
2017-04-16 20:52:22 +00:00
|
|
|
return packet
|
2013-08-30 21:28:33 +00:00
|
|
|
end
|
2017-06-20 09:18:37 +00:00
|
|
|
|
|
|
|
nil
|
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
|
|
|
|
|