MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / decode

Method decode

pager_lib/smb/smb2_structs.py:86–143  ·  view source on GitHub ↗

Decodes the SMB message in buf. All fields of the SMB2Message object will be reset to default values before decoding. On errors, do not assume that the fields will be reinstated back to what they are before this method is invoked. References ========

(self, buf)

Source from the content-addressed store, hash-verified

84 return headers_data + self.data
85
86 def decode(self, buf):
87 """
88 Decodes the SMB message in buf.
89 All fields of the SMB2Message object will be reset to default values before decoding.
90 On errors, do not assume that the fields will be reinstated back to what they are before
91 this method is invoked.
92
93 References
94 ==========
95 - [MS-SMB2]: 2.2.1
96
97 @param buf: data containing one complete SMB2 message
98 @type buf: string
99 @return: a positive integer indicating the number of bytes used in buf to decode this SMB message
100 @raise ProtocolError: raised when decoding fails
101 """
102 buf_len = len(buf)
103 if buf_len < 64: # All SMB2 headers must be at least 64 bytes. [MS-SMB2]: 2.2.1.1, 2.2.1.2
104 raise ProtocolError('Not enough data to decode SMB2 header', buf)
105
106 self.reset()
107
108 protocol, struct_size, self.credit_charge, self.status, \
109 self.command, self.credit_re, self.flags = struct.unpack(self.HEADER_STRUCT_FORMAT, buf[:self.HEADER_STRUCT_SIZE])
110
111 if protocol != b'\xFESMB':
112 raise ProtocolError('Invalid 4-byte SMB2 protocol field', buf)
113
114 if struct_size != self.HEADER_SIZE:
115 raise ProtocolError('Invalid SMB2 header structure size')
116
117 if self.isAsync:
118 if buf_len < self.HEADER_STRUCT_SIZE+self.ASYNC_HEADER_STRUCT_SIZE:
119 raise ProtocolError('Not enough data to decode SMB2 header', buf)
120
121 self.next_command_offset, self.mid, self.async_id, self.session_id, \
122 self.signature = struct.unpack(self.ASYNC_HEADER_STRUCT_FORMAT,
123 buf[self.HEADER_STRUCT_SIZE:self.HEADER_STRUCT_SIZE+self.ASYNC_HEADER_STRUCT_SIZE])
124 else:
125 if buf_len < self.HEADER_STRUCT_SIZE+self.SYNC_HEADER_STRUCT_SIZE:
126 raise ProtocolError('Not enough data to decode SMB2 header', buf)
127
128 self.next_command_offset, self.mid, self.pid, self.tid, self.session_id, \
129 self.signature = struct.unpack(self.SYNC_HEADER_STRUCT_FORMAT,
130 buf[self.HEADER_STRUCT_SIZE:self.HEADER_STRUCT_SIZE+self.SYNC_HEADER_STRUCT_SIZE])
131
132 if self.next_command_offset > 0:
133 self.raw_data = buf[:self.next_command_offset]
134 self.data = buf[self.HEADER_SIZE:self.next_command_offset]
135 else:
136 self.raw_data = buf
137 self.data = buf[self.HEADER_SIZE:]
138
139 self._decodeCommand()
140 if self.payload:
141 self.payload.decode(self)
142
143 return len(self.raw_data)

Callers

nothing calls this directly

Calls 4

resetMethod · 0.95
_decodeCommandMethod · 0.95
ProtocolErrorClass · 0.85
decodeMethod · 0.45

Tested by

no test coverage detected