CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/meterpreter/packet_parser.rb
Views: 11784
1
# -*- coding: binary -*-
2
3
module Rex
4
module Post
5
module Meterpreter
6
7
###
8
#
9
# This class is responsible for reading in and decrypting meterpreter
10
# packets that arrive on a socket
11
#
12
###
13
class PacketParser
14
15
#
16
# Initializes the packet parser context.
17
#
18
def initialize
19
reset
20
end
21
22
#
23
# Resets the parser state so that a new packet can begin being parsed.
24
#
25
def reset
26
self.packet = Packet.new(0)
27
end
28
29
#
30
# Reads data from the socket and parses as much of the packet as possible.
31
#
32
def recv(sock)
33
raw = nil
34
if self.packet.raw_bytes_required > 0
35
while (raw = sock.read(self.packet.raw_bytes_required))
36
self.packet.add_raw(raw)
37
break if self.packet.raw_bytes_required == 0
38
end
39
end
40
41
if self.packet.raw_bytes_required > 0
42
if raw == nil
43
raise EOFError
44
else
45
return nil
46
end
47
end
48
49
packet = self.packet
50
reset
51
packet
52
end
53
54
protected
55
attr_accessor :cipher, :packet # :nodoc:
56
57
end
58
59
60
end; end; end
61
62
63