一、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
協商命令
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)
打開或者創建文件(夾),可以獲取文件名及其目錄,可以判斷打開的是文件還是文件夾,獲得讀取文件的總長度。
讀取文件,與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.