zoukankan      html  css  js  c++  java
  • SMB/CIFS协议解析一概述

    一、SMB/CIFS协议的区别      

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

           SMB(Server Message Block)协议在NT/2000中用来作文件共享,在NT中,SMB运行于NBT(NetBIOS over TCP/IP)上,使用137,139(UDP),139(TCP)端口。 在2000中,SMB可以直接运行在tcp/ip上,而没有额外的NBT层,使用TCP 445端口。因此在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,那么就同时监听UDP 137、138端口和TCP139,445。如果禁用NBT,那么就只监听445端口了。 所以对于2000来说,共享问题就不仅仅是139端口,445端口同样能够完成。

    二、SMB包头部分:

    其中SMB Header的长度为32个byte,NETBIOS Header的长度为4个byte,TCP Header为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每个字段占用的字节图:

    用wireshark抓包,SMB Header的截图:

    SMB Command:SMB命令
    NT Status:SMB命令的状态,0x00000000为成功
    四、SMB Command
     

    1、SMB_COM_NEGOTIATE0x72)      
           协商命令  
    Must be the first message sent by client to the server.  Includes a list of SMB dialects supported by the client.  Server response indicates which SMB dialect should be used.
     2、SMB_COM_SESSION_SETUP_ANDX 0x73
           建立会话,成功以后,用户正确登录。可以得到用户名和登录的主机名等信息
    Transmits the user's name and credentials to the server for verification. Successful server response has Uid field set in SMB header 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 to access.  Successful server response has Tid field set in SMB header used for subsequent SMBs referring to this resource.
    4、SMB_COM_NT_CREATE_ANDX (0xa2)
           打开或者创建文件(夹),可以获取文件名及其目录,可以判断打开的是文件还是文件夹,获得读取文件的总长度。
     
    5、 SMB_COM_OPEN 
               0x2d
          读取文件,与read命令很相似。获得文件内容。
       Transmits the name of the file, relative to Tid, the client wants to open. Successful server response includes a  file id (Fid) the client should supply for subsequent operations on this file.
    6、SMB_COM_READ          0x2e)    
           读取文件,获得读取文件内容。
    Client supplies Tid, Fid, file offset, and number of bytes to read.  Successful server response includes the requested  file data.
    7、SMB_COM_WRITE          0x2f)  
           写入文件,获得写入的文件内容
    8、 SMB_COM_CLOSE     (0x04) 
    Client closes the file represented by Tid   and Fid.  Server responds with success  code.
    9、 SMB_COM_TREE_DISCONNECT    (0x71)
    Client disconnects from resource represented by Tid.
  • 相关阅读:
    protobuf 中的嵌套消息的使用
    Mysql++详解
    MYSQL++之Connect类型
    c/c++中宏定义##连接符 和#符的使用
    c指针
    linux学习历程
    linux sar 命令详解
    Linux下多线程查看工具(pstree、ps、pstack)
    知识杂项
    python 使用xrld
  • 原文地址:https://www.cnblogs.com/zhangzl/p/6121206.html
Copyright © 2011-2022 走看看