# This file is part of Scapy # See http://www.secdev.org/projects/scapy for more information # Copyright (C) Philippe Biondi # This program is published under a GPLv2 license """ SMB (Server Message Block), also known as CIFS - version 2 """ from scapy.config import conf from scapy.packet import Packet, bind_layers from scapy.fields import StrFixedLenField, LEIntField, LEShortEnumField, \ ShortEnumField, XLEIntField, LEShortField, FlagsField, LELongField, \ XLELongField, XNBytesField, FieldLenField, IntField, FieldListField, \ XStrLenField, ShortField, IntEnumField, StrFieldUtf16, XLEShortField, \ UUIDField, XLongField, PacketListField, PadField # EnumField SMB_DIALECTS = { 0x0202: 'SMB 2.0.2', 0x0210: 'SMB 2.1', 0x0300: 'SMB 3.0', 0x0302: 'SMB 3.0.2', 0x0311: 'SMB 3.1.1', } # EnumField SMB2_NEGOCIATE_CONTEXT_TYPES = { 0x0001: 'SMB2_PREAUTH_INTEGRITY_CAPABILITIES', 0x0002: 'SMB2_ENCRYPTION_CAPABILITIES', 0x0003: 'SMB2_COMPRESSION_CAPABILITIES', 0x0005: 'SMB2_NETNAME_NEGOCIATE_CONTEXT_ID', } # FlagField SMB2_CAPABILITIES = { 30: "CapabilitiesEncryption", 29: "CapabilitiesDirectoryLeasing", 28: "CapabilitiesPersistentHandles", 27: "CapabilitiesMultiChannel", 26: "CapabilitiesLargeMTU", 25: "CapabilitiesLeasing", 24: "CapabilitiesDFS", } # EnumField SMB2_COMPRESSION_ALGORITHMS = { 0x0000: "None", 0x0001: "LZNT1", 0x0002: "LZ77", 0x0003: "LZ77 + Huffman", 0x0004: "Pattern_V1", } class SMB2_Header(Packet): name = "SMB2 Header" fields_desc = [ StrFixedLenField("Start", b"\xfeSMB", 4), LEShortField("HeaderLength", 0), LEShortField("CreditCharge", 0), LEShortField("ChannelSequence", 0), LEShortField("Unused", 0), ShortEnumField("Command", 0, {0x0000: "SMB2_COM_NEGOCIATE"}), LEShortField("CreditsRequested", 0), # XLEIntField("Flags", 0), FlagsField("Flags", 0, 32, { 24: "SMB2_FLAGS_SERVER_TO_REDIR", }), XLEIntField("ChainOffset", 0), LELongField("MessageID", 0), XLEIntField("ProcessID", 0), XLEIntField("TreeID", 0), XLELongField("SessionID", 0), XNBytesField("Signature", 0, 16), ] class SMB2_Compression_Transform_Header(Packet): name = "SMB2 Compression Transform Header" fields_desc = [ StrFixedLenField("Start", b"\xfcSMB", 4), LEIntField("OriginalCompressedSegmentSize", 0x0), LEShortEnumField( "CompressionAlgorithm", 0, SMB2_COMPRESSION_ALGORITHMS ), ShortEnumField("Flags", 0x0, { 0x0000: "SMB2_COMPRESSION_FLAG_NONE", 0x0001: "SMB2_COMPRESSION_FLAG_CHAINED", }), XLEIntField("Offset/Length", 0), ] class SMB2_Negociate_Context(Packet): name = "SMB2 Negociate Context" fields_desc = [ LEShortEnumField("ContextType", 0x0, SMB2_NEGOCIATE_CONTEXT_TYPES), FieldLenField("DataLength", 0x0, fmt="> 24) & 1 == 0 ) bind_layers( SMB2_Header, SMB2_Negociate_Protocol_Response_Header, Command=0x0000, Flags=lambda f: (f >> 24) & 1 == 1 ) bind_layers( SMB2_Negociate_Context, SMB2_Preauth_Integrity_Capabilities, ContextType=0x0001 ) bind_layers( SMB2_Negociate_Context, SMB2_Encryption_Capabilities, ContextType=0x0002 ) bind_layers( SMB2_Negociate_Context, SMB2_Compression_Capabilities, ContextType=0x0003 ) bind_layers( SMB2_Negociate_Context, SMB2_Netname_Negociate_Context_ID, ContextType=0x0005 )