Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scapy/layers/smb2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from scapy.automaton import select_objects
from scapy.config import conf, crypto_validator
from scapy.error import log_runtime
from scapy.error import Scapy_Exception, log_runtime
from scapy.packet import Packet, bind_layers, bind_top_down
from scapy.fields import (
ByteEnumField,
Expand Down Expand Up @@ -1096,7 +1096,7 @@ def verify(
)
if self.SecuritySignature != sig:
log_runtime.error("SMB signature is invalid !")
raise Exception("ERROR: SMB signature is invalid !")
raise Scapy_Exception("ERROR: SMB signature is invalid !")

def encrypt(self, dialect, EncryptionKey, CipherId):
"""
Expand Down
26 changes: 24 additions & 2 deletions scapy/layers/smbclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def receive_session_setup_response(self, pkt):
if pkt.SessionFlags.ENCRYPT_DATA or self.REQUIRE_ENCRYPTION:
self.session.EncryptData = True
self.session.SigningRequired = False
raise self.AUTHENTICATED(pkt.SecurityBlob)
raise self.AUTHENTICATED(pkt.SecurityBlob, pkt)
else:
if SMB2_Header in pkt:
# If required, compute sessions
Expand Down Expand Up @@ -637,7 +637,7 @@ def AUTH_FAILED(self):
self.smb_sock_ready.set()

@ATMT.state()
def AUTHENTICATED(self, ssp_blob=None):
def AUTHENTICATED(self, ssp_blob=None, pkt=None):
self.session.sspcontext, _, status = self.session.ssp.GSS_Init_sec_context(
self.session.sspcontext,
input_token=ssp_blob,
Expand All @@ -650,6 +650,28 @@ def AUTHENTICATED(self, ssp_blob=None):
# Authentication was successful
self.session.computeSMBSessionKeys(IsClient=True)

# [MS-SMB2] sect 3.2.5.3.1 / 3.2.5.1.3 - the final SESSION_SETUP response
# is signed with the new session keys, which protects its SessionFlags.
# "If Connection.Dialect is "3.1.1", and if SMB2_FLAGS_SIGNED is not set
# [...] the client MUST return an error"
if (
pkt is not None
and SMB2_Session_Setup_Response in pkt
and not self.session.IsGuest
and (self.session.Dialect >= 0x0311 or self.session.SigningRequired)
):
try:
pkt[SMB2_Header].verify(
self.session.Dialect,
self.session.SigningKey,
# SMB 3.1.1 parameters:
SigningAlgorithmId=self.session.SigningAlgorithmId,
IsClient=False,
)
except Scapy_Exception:
self.ErrorStatus = "Invalid SESSION_SETUP response signature"
raise self.AUTH_FAILED()

# DEV: add a condition on AUTHENTICATED with prio=0

@ATMT.condition(AUTHENTICATED, prio=1)
Expand Down
58 changes: 58 additions & 0 deletions test/scapy/layers/smb2.uts
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,61 @@ s.Dialect = 0x0311

pkt = SMB2_Header(bytes(SMB2_Header(Command="SMB2_NEGOTIATE")))
assert SMB2_Header in s.in_pkt(pkt)

+ SMB2 client SESSION_SETUP response signature

= Reject a final SESSION_SETUP response whose SessionFlags were modified
~ smb2

import socket
import threading
from scapy.layers.ntlm import MD4le, NTLMSSP
from scapy.layers.smb2 import DirectTCP, SMBStreamSocket, SMB2_Session_Setup_Response
from scapy.layers.smbclient import SMB_SOCKET
from scapy.layers.smbserver import SMB_Server, SMBShare
from scapy.layers.spnego import SPNEGOSSP

def relay(src, dst, tamper):
# Forward each DirectTCP frame, clearing ENCRYPT_DATA in the final response.
while True:
try:
hdr = src.recv(4, socket.MSG_WAITALL)
if len(hdr) < 4:
return
frame = hdr + src.recv(struct.unpack("!I", hdr)[0], socket.MSG_WAITALL)
except OSError:
return
pkt = DirectTCP(frame)
if tamper and SMB2_Session_Setup_Response in pkt and pkt.Status == 0:
pkt.SessionFlags = int(pkt.SessionFlags) & ~0x4
frame = bytes(pkt)
try:
dst.sendall(frame)
except OSError:
return

client, mitm_client = socket.socketpair()
mitm_server, server = socket.socketpair()
srv = SMB_Server(
sock=SMBStreamSocket(server, DirectTCP),
shares=[SMBShare("test", get_temp_dir())],
ssp=SPNEGOSSP([NTLMSSP(IDENTITIES={"User1": MD4le("Password1")})]),
REQUIRE_ENCRYPTION=True,
verb=False,
)
srv.runbg()
threading.Thread(target=relay, args=(mitm_client, mitm_server, False), daemon=True).start()
threading.Thread(target=relay, args=(mitm_server, mitm_client, True), daemon=True).start()
try:
try:
SMB_SOCKET.from_tcpsock(
client,
ssp=SPNEGOSSP([NTLMSSP(UPN="User1", PASSWORD="Password1")]),
)
assert False, "tampered SESSION_SETUP response was accepted"
except Scapy_Exception as ex:
assert "Invalid SESSION_SETUP response signature" in str(ex)
finally:
srv.forcestop(wait=False)
for s in [client, mitm_client, mitm_server, server]:
s.close()
Loading