SMB/CIFS協議解析(一)

tiny丶發表於2015-01-23

一、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.

相關文章