[MS-SMB2] sect 3.1.4.3 - Encrypting the Message
(self, dialect, EncryptionKey, CipherId)
| 1999 | raise Exception("ERROR: SMB signature is invalid !") |
| 2000 | |
| 2001 | def encrypt(self, dialect, EncryptionKey, CipherId): |
| 2002 | """ |
| 2003 | [MS-SMB2] sect 3.1.4.3 - Encrypting the Message |
| 2004 | """ |
| 2005 | if dialect < 0x0300: |
| 2006 | raise Exception("Encryption is not supported on this SMB dialect !") |
| 2007 | elif dialect < 0x0311 and CipherId != "AES-128-CCM": |
| 2008 | raise Exception("CipherId is not supported on this SMB dialect !") |
| 2009 | |
| 2010 | data = bytes(self) |
| 2011 | smbt = SMB2_Transform_Header( |
| 2012 | OriginalMessageSize=len(self), |
| 2013 | SessionId=self.SessionId, |
| 2014 | Flags=0x0001, |
| 2015 | ) |
| 2016 | if "GCM" in CipherId: |
| 2017 | from cryptography.hazmat.primitives.ciphers.aead import AESGCM |
| 2018 | |
| 2019 | nonce = os.urandom(12) |
| 2020 | cipher = AESGCM(EncryptionKey) |
| 2021 | elif "CCM" in CipherId: |
| 2022 | from cryptography.hazmat.primitives.ciphers.aead import AESCCM |
| 2023 | |
| 2024 | nonce = os.urandom(11) |
| 2025 | cipher = AESCCM(EncryptionKey) |
| 2026 | else: |
| 2027 | raise Exception("Unknown CipherId !") |
| 2028 | |
| 2029 | # Add nonce to header and build the auth data |
| 2030 | smbt.Nonce = nonce |
| 2031 | aad = bytes(smbt)[20:] |
| 2032 | |
| 2033 | # Perform the actual encryption |
| 2034 | data = cipher.encrypt(nonce, data, aad) |
| 2035 | |
| 2036 | # Put the auth tag in the Signature field |
| 2037 | smbt.Signature, data = data[-16:], data[:-16] |
| 2038 | |
| 2039 | return smbt / data |
| 2040 | |
| 2041 | |
| 2042 | class _SMB2_Payload(Packet): |
no test coverage detected