Outgoing SMB packet :param pkt: the packet to send :param Compound: if True, will be stack to be send with the next un-compounded packet :param ForceSign: if True, force to sign the packet. :param ForceEncrypt: if True, force to encr
(self, pkt, Compounded=False, ForceSign=False, ForceEncrypt=False)
| 4943 | return pkt |
| 4944 | |
| 4945 | def out_pkt(self, pkt, Compounded=False, ForceSign=False, ForceEncrypt=False): |
| 4946 | """ |
| 4947 | Outgoing SMB packet |
| 4948 | |
| 4949 | :param pkt: the packet to send |
| 4950 | :param Compound: if True, will be stack to be send with the next |
| 4951 | un-compounded packet |
| 4952 | :param ForceSign: if True, force to sign the packet. |
| 4953 | :param ForceEncrypt: if True, force to encrypt the packet. |
| 4954 | |
| 4955 | Handles: |
| 4956 | - handle compounded requests (if any): [MS-SMB2] 3.3.5.2.7 |
| 4957 | - handles signing and encryption (if required) |
| 4958 | """ |
| 4959 | # Note: impacket and wireshark get crazy on compounded+signature, but |
| 4960 | # windows+samba tells we're right :D |
| 4961 | if SMB2_Header in pkt: |
| 4962 | if self.CompoundQueue: |
| 4963 | # this is a subsequent compound: only keep the SMB2 |
| 4964 | pkt = pkt[SMB2_Header] |
| 4965 | if Compounded: |
| 4966 | # [MS-SMB2] 3.2.4.1.4 |
| 4967 | # "Compounded requests MUST be aligned on 8-byte boundaries; the |
| 4968 | # last request of the compounded requests does not need to be padded to |
| 4969 | # an 8-byte boundary." |
| 4970 | # [MS-SMB2] 3.1.4.1 |
| 4971 | # "If the message is part of a compounded chain, any |
| 4972 | # padding at the end of the message MUST be used in the hash |
| 4973 | # computation." |
| 4974 | length = len(pkt[SMB2_Header]) |
| 4975 | padlen = (-length) % 8 |
| 4976 | if padlen: |
| 4977 | pkt.add_payload(b"\x00" * padlen) |
| 4978 | pkt[SMB2_Header].NextCommand = length + padlen |
| 4979 | if ( |
| 4980 | self.Dialect |
| 4981 | and self.SigningKey |
| 4982 | and (ForceSign or self.SigningRequired and not ForceEncrypt) |
| 4983 | ): |
| 4984 | # [MS-SMB2] sect 3.2.4.1.1 - Signing |
| 4985 | smb = pkt[SMB2_Header] |
| 4986 | smb.Flags += "SMB2_FLAGS_SIGNED" |
| 4987 | smb.sign( |
| 4988 | self.Dialect, |
| 4989 | self.SigningKey, |
| 4990 | # SMB 3.1.1 parameters: |
| 4991 | SigningAlgorithmId=self.SigningAlgorithmId, |
| 4992 | IsClient=False, |
| 4993 | ) |
| 4994 | if Compounded: |
| 4995 | # There IS a next compound. Store in queue |
| 4996 | self.CompoundQueue.append(pkt) |
| 4997 | return [] |
| 4998 | else: |
| 4999 | # If there are any compounded responses in store, sum them |
| 5000 | if self.CompoundQueue: |
| 5001 | pkt = functools.reduce(lambda x, y: x / y, self.CompoundQueue) / pkt |
| 5002 | self.CompoundQueue.clear() |