你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

SMB/CIFS协议解析(一)

2022/5/15 11:12:45

转载地址:http://blog.sina.com.cn/s/blog_705eb43a0100o5ah.html

一、SMB/CIFS协议的区别     

     在NetBIOS出现之后,Microsoft就使用NetBIOS实现了一个网络文件/打印服务系统,这个系统基于NetBIOS设定了一套文件共享协议,Microsoft称之为SMB(Server Message Block)协议。这个协议被Microsoft用于它们LanManager和WindowsNT服务器系统中,而Windows系统均包括这个协议的客户软件,因而这个协议在局域网系统中影响很大。
  随着Internet的流行,Microsoft希望将这个协议扩展到Internet上去,成为Internet上计算机之间相互共享数据的一种标准。因此它将原有的几乎没有多少技术文档的SMB协议进行整理,重新命名为CIFS(Common Internet FileSystem),并打算将它与NetBIOS相脱离,试图使它成为Internet上的一个标准协议。

      SMB(Server Message Block)协议在NT/2000中用来作文件共享,在NT中,SMB运行于NBT(NetBIOSover TCP/IP)上,使用137,139(UDP),139(TCP)端口。在2000中,SMB可以直接运行在tcp/ip上,而没有额外的NBT层,使用TCP445端口。因此在2000上应该比NT稍微变化多一些。可以在“网络连接/属性/TCPIP协议/属性/高级/WINS中设置启用或者禁用NBT(NetBIOS over TCP/IP)。当2000使用网络共享的时候,就面临着选择139或者445端口了。下面的情况确定会话使用的端口:

1、如果客户端启用了NBT,那么连接的时候将同时访问139和445端口,如果从445端口得到回应,那么客户端将发送RST到139端口,终止这个端口的连接,接着就从445端口进行SMB的会话了;如果没有从445端口而是从139得到回应,那么就从139端口进行会话;如果没有得到任何回应,那么SMB会话失败。

2、如果客户端禁用了NBT,他就将只从445端口进行连接。当然如果服务器(开共享端)没有445端口进行SMB会话的话,那么就会访问失败了,所以禁用445端口后,对访问NT机器的共享会失败。

3、如果服务器端启用NBT,那么就同时监听UDP137、138端口和TCP139,445。如果禁用NBT,那么就只监听445端口了。所以对于2000来说,共享问题就不仅仅是139端口,445端口同样能够完成。

二、SMB包头部分:

SMB/CIFS协议解析(一)

其中SMB Header的长度为32个byte,NETBIOS Header的长度为4个byte,TCPHeader为20个byte,SMB Command Header的长度不是固定的,不同的命令有不同的长度。

三、SMB Header

 

typedef unsigned char UCHAR;         // 8 unsigned bits

typedef unsigned short USHORT;       // 16 unsigned bits

typedef unsigned long ULONG;         // 32 unsigned bits

 

typedef struct {

   ULONG LowPart;

   LONG HighPart;

} LARGE_INTEGER;                     // 64 bits of data

 

typedef struct {

   UCHAR Protocol[4];               // Contains 0xFF,'SMB'

   UCHAR Command;                // Command code

   union {

       struct {

           UCHAR ErrorClass;        // Error class

           UCHAR Reserved;          // Reserved for future use

           USHORT Error;            // Error code

       } DosError;

       ULONG Status;                // 32-bit error code

    }Status;

   UCHAR Flags;                     // Flags

   USHORT Flags2;                   // More flags

   union {

       USHORT Pad[6];               // Ensure section is 12 bytes long

       struct {

           USHORT PidHigh;          // High part of PID

           ULONG  Unused;           // Not used

           ULONG  Unused2;

    }Extra;

   };

   USHORT Tid;                      // Tree identifier

   USHORT Pid;                      // Caller's process id

   USHORT Uid;                      //Unauthenticated user id

   USHORT Mid;                      // multiplex id

} SMB_HEADER;

 下图为SMB Header每个字段占用的字节图:

SMB/CIFS协议解析(一)

用wireshark抓包,SMB Header的截图:
SMB/CIFS协议解析(一)
SMB Command:SMB命令
NT Status:SMB命令的状态,0x00000000为成功
四、SMB Command
 
1、SMB_COM_NEGOTIATE 0x72            
        协商命令  
Must be the first message sent by client to theserver.  Includes a list of SMB dialects supportedby the client.  Server response indicates whichSMB dialect should be used.
  2、SMB_COM_SESSION_SETUP_ANDX 0x73
            建立会话,成功以后,用户正确登录。可以得到用户名和登录的主机名等信息
Transmits the user's name and credentials to the server forverification. Successful server response has Uid field set in SMBheader used for subsequent SMBs on behalf of this user.
  3、SMB_COM_TREE_CONNECT            0x75
              遍历共享文件夹的目录及文件
Transmits the name of the disk share the client wants toaccess.  Successful server response has Tid fieldset in SMB header used for subsequent SMBs referring to thisresource.
4、 SMB_COM_NT_CREATE_ANDX (0xa2)
            打开或者创建文件(夹),可以获取文件名及其目录,可以判断打开的是文件还是文件夹,获得读取文件的总长度。
5、SMB_COM_OPEN                      0x2d
          读取文件,与read命令很相似。获得文件内容。
    Transmits the name of thefile, relative to Tid, the client wants to open. Successful serverresponse includes a  file id (Fid) the clientshould supply for subsequent operations on this file.
6、SMB_COM_READ                  0x2e        
            读取文件,获得读取文件内容。
Client supplies Tid, Fid, file offset, and number of bytes toread.  Successful server response includes therequested  file data.
7、SMB_COM_WRITE                0x2f    
            写入文件,获得写入的文件内容
8、SMB_COM_CLOSE        (0x04) 
Client closes the file represented byTid    and Fid.  Server responds with success  code.
9、SMB_COM_TREE_DISCONNECT      (0x71)
Client disconnects from resource represented by Tid.