Thursday, 19 April 2007
SMB/CIFS
SMB协议已经被发展成为可以运行于DOS操作系统,因此字节顺序将会和网络顺序相反。
SMB可以运行在TCP/IP,NetBEUI,DECnet和IPX/SPX协议之上。如果SMB执行于TCP/IP,DECnet或则是NetBEUI之上,那么NETBIOS名字必须被使用。
NETBIOS名字用来在网络上鉴别一台计算机。
SMB域名是用来鉴别一个SMB服务器上的一组资源的(用户,打印机,文件……)。
让我们假设一个这样的环境:一个客户端希望访问一台服务器上的特定资源。
1 - 开始于客户端向服务器请求一个NETBIOS会话。客户端发送它的已编码的NETBIOS名字到SMB服务器(它们在139端口监听连接请求)。服务器接收到NETBIOS名字后回复一个NETBIOS会话数据报给有效的会话连接。客户端在建立了连接之后才能进入访问。
2 - 客户端发送一个SMB negprot请求数据报(negprot是磋商协议“negotiate protocol”的简写)。客户端列出了它所支持的所有SMB协议版本。
3 - 通过磋商之后,客户端进程向服务器发起一个用户或共享的认证。(下一章中我们将会看到共享认证和用户认证之间的不同)。
这个过程是通过发送SesssetupX(SesssetupX是会话建立和X“Session setup and X”的简称)请求数据报实现的。客户端发送一对登录名/密码或一个简单密码到服务器,然后服务器通过发送一个SesssetupX应答数据报来允许或拒绝本次连接。
4 - 好了,在客户端完成了磋商和认证之后,它会发送一个TconX数据报并列出它想访问的特定网络资源的名称,之后服务器会发送一个TconX应答数据报以表示此次连接是否接受或拒绝。
SMB的安全等级
第一种是“共享”级的安全模式。这种安全模式需要一个访问网络上共享资源的密码。用户通过这个正确的密码来访问网络资源(IPC,磁盘,打印机)。他可以是网络上任何一个知道共享资源服务器名字的用户。
第二种是“用户”级的安全模式。这是在第一种模式上的增强版。它坚持使用一对登录名/密码来访问共享资源。所以如果一个用户想访问这种类型的共享资源,就必须同时提供登录名和密码。这种
密码
在SMB协议中,如果你想进行一次在服务器上的请求认证,你的密码可以以原码或加密后的形式发送到服务器端。如果服务器支持加密属性,客户端必须发送一个应答信号。在negprot应答数据报中,服务器会给客户端发送一个密钥。然后,客户端将密码加密并通过SesssetupX请求数据报发送到服务器端。服务器将会核查密码的有效性,并由此允许或拒绝客户端的访问。
你必须知道一个SMB密码(未加密)的最大长度是14位。密钥的长度一般为8位,加密过后的口令长度为24位。在ANSI密码中,密码中的所有位都转换成大写的形式然后再加密。
密码是以DEC编码方式进行加密的。
几种SMB数据报的描述
SMB数据报的常规特征
通常情况下,SMB运行于TCP/IP协议组之上。那么我们就假设SMB运行在TCP层之上。在TCP层上面,你常常会发现NETBIOS(NBT)头部。在NBT上面,有SMB基础报文头部。在SMB基础报文头部之上,就是另一种依赖于特定请求命令的头部。
----------------------
│ TCP Header │
----------------------
│ NETBIOS Header │
----------------------
│ SMB Base Header │----------------------
│ SMB Command Header │----------------------
│ DATA │----------------------
“SMB Base header”包含了几种信息,像接收缓冲区的长度,允许的最大连接数目……它也包含了一个鉴别请求命令的数字。
“SMB Command header”包含了所有的请求命令的参数(像磋商协议版本命令……)。
“DATA”容纳了请求命令的数据。
我们把“SMB packet”看成:NETBIOS Header + SMB Base Header + SMB Command Header+ DATA。
注意:我将使用这个定义:
typedef unsigned char UCHAR; // 8 unsigned bitstypedef unsigned short USHORT; // 16 unsigned bitstypedef unsigned long ULONG; // 32 unsigned bits
而STRING被定义为以空字符结束的ASCII字符串。
NETBIOS与SMB
NETBIOS(NETwork Basic Input and Output System)在微软的网络系统中被广泛使用。它是一个软件接口和命名系统。每台主机都有一个长度为15个字符的NETBIOS名字,且第十六个字符用来标志主机的类型(域名服务器,工作站……)。
第十六个字符的选择:
0x00 基础电脑,工作站。0x20 资源共享服务器。
这还有一些其他的值可选,不过我们对这两个最感兴趣。第一个(0x00)鉴定一台工作站,第二个(0x20)鉴定服务器。
在一个SMB数据报中,NETBIOS头部对应NETBIOS会话头部。
定义如下:
UCHAR Type; // Type of the packetUCHAR Flags; // FlagsUSHORT Length; // Count of data bytes (netbios header not included)
“Flags”域的值总是被置为0。
“Type”域有几种可能的选择:
0x81 对应一个NETBIOS会话请求。这个代码在客户端发送它的NETBIOS名字到服务器是使用。
0x82 对应一个NETBIOS会话应答。这个代码在服务器向客户端批准NETBIOS会话时使用。
0x00 对应一个会话消息。这个代码总是在SMB会话中被使用。
“Length”域包含了数据字节的长度(NETBIOS头部没有被包含在内)。数据包含在NETBIOS头部以上的所有部分(它可能是 SMB Base Header + SMB Command Header + DATA 或 NETBIOS名字)。
NETBIOS名字与编码。
NETBIOS编码名字的长度为32字节。
NETBIOS名字总是以大写的形式存在的。
编码一个NETBIOS名字非常的简单。例如我的计算机的NETBIOS名字是“BILL”,它是一个工作站,所以它的第十六个字符为“0x00”。
首先,如果一个NETBIOS名字比15字节短,就会在右边补填上空格。
“BILL “
十六进制为: 0x42 0x49 0x4c 0x4c 0x20 0x20 ......0x00
每个字节都分裂为4位一组:
0x4 0x2 0x4 0x9 0x4 0xc 0x4 0xc 0x2 0x0 .......
而且每个4位都要添加ASCII码‘A’的值(0x41)。
0x4 + 0x41 = 0x45 -> ASCII value = E0x2 + 0x41 = 0x43 -> ASCII value = C……
最后NETBIOS名字被编码为32字节长。
注意:
SMB可以直接运行于TCP之上而无须NBT(在Windows 2k和XP上它们使用455端口)。此时,NETBIOS名字没有被限制在15字符以内
STDIN
first is that, I can write script to using STDIN instand of type in by hand. for example, there is a command: smb -U username password servicename. it can lead us into the windows share dirve on Linux service platform. however, when we get into this sharedrive, we need to type command into this program, which means we are in this program. but how can I use script to execute command in sequence? using STDIN. there are two ways, first is like this to write in script:
- smb -U zliybsj2 $1 //10.2.1.21/Central_Application_Center <<>
- ls
- exit
- EOF
first, script will using $1 as possward to login Central_Application_Center, then input ls, run it and exit, run again and finish to goto the next command. here EOF is a labell.
another way to write is creating another file, such as com1, we write what we want to do after we get into Central_Application_Center. so, as follow:
- smb -U zliybsj2 $1 //10.2.1.21/Central_Application_Center <>
in com1:
- ls
- exit
There is another way to test when a script or program is runing well by using returning number. as follow:
- succ=0
- smb -U zliybsj2 $1 //10.2.1.21/Central_Application_Center <>
- if [ succ -ed $? ]; then
- echo running_well
- else
- echo some_mistakes_happened
- fi
here, $? stands for the retunring number of last program. so if we can complete the left part, such as however to create a pasward, the do this loop to guess, this is the most stupid way to decrypt, if for us time is not a problem. just a joking.
awk
- deal with text file which is thought of as being made up of records and fields. we can write programing by awk to do anything we want.
- we can:execute UNIX command from within a script; process the output from UNIX commands; work with multiple input streams; define functions.
- we can combined with sed and shell scripting. compared: shell is very easy and quick to write, but lacks functionality. sed, awk, and shell are designed to be integrated.
invoke:
- awk [options] 'script' var=value file(s)
- awk [options] -f scriptfile var=value file(s) : we can have more than one -f scriptfile.
- a literal: awk -f scriptFile count=5
- a shell variable: awk -f scriptFile count=$count
- A command substitution: awk -f scriptFile count=`who wc-l`
set filed seperator:
- using -F
- one line can be sperated into different filed and can be refered by variables $1, $2 etc. $0 means the whole record.
awk script consist of patterns and procedure:
- a pattern can be : /regular expression/, eg /^...:/ or relational expression, eg $1=="gdm". ~ means match, !~ means don't match, eg awk -F: '$1 ~ /.dm.*/ {print $0}' /etc/passwd. BEGIN specifies procedure to take place before the first input line is processed, END as well, eg awk ‘BEGIN {print “Version 1.0”}’ dataFile. pattern missing, the procedure applies all lines
- procedure is arround by {} and seperated by newlines or semi-colons. can be: commands, functions and veriable assignment. procedure missing, printed all lines matched by pattern.
commands used with procedures: 5 types
- Assignments of variables or arrays
- Commands that print
- Built-in functions
- Control-flow commands
- User-defined functions (in nawk only)
awk Built-in Variables:
- FILENAME - current filename
- FS - Field separator
- NF - Number of fields in current record
- NR - Number of current record
- RS - Record separator
- $0 - Entire input record
- $n - nth field in current record
awk operators: $_++_ --_ +_-_!_*_/_%_<_<=_>=_>_!=_==_~_!~_in_$$__?:_=_+=_-+_*=_/=_%=
two ways to usingarrays in awk:
- in an assignment statement: myArray[i]++
- split() function : n=splict(input,words," ") : splict this input by " "(whitespace) into words.
functions in awk: works in the same way as java: calling a function, no sapce between function name and left bracket.