rawclient_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package smbserver
  2. import (
  3. "encoding/binary"
  4. "io"
  5. "net"
  6. "testing"
  7. "time"
  8. "imuslab.com/arozos/mod/storage/smbserver/auth"
  9. "imuslab.com/arozos/mod/storage/smbserver/crypt"
  10. "imuslab.com/arozos/mod/storage/smbserver/wire"
  11. )
  12. /*
  13. rawClient speaks the protocol byte by byte, for what go-smb2 has no call
  14. for: compounds, share modes, locks, pending requests and cancels. It is
  15. only used where the behaviour under test is the server's answer to an
  16. exact sequence, and it never checks its own encoding against itself.
  17. */
  18. type rawClient struct {
  19. t *testing.T
  20. nc net.Conn
  21. mid uint64
  22. session uint64
  23. tree uint32
  24. signer *crypt.Signer
  25. preauth [64]byte
  26. }
  27. type rawReply struct {
  28. hdr wire.Header
  29. body []byte
  30. raw []byte
  31. }
  32. func (c *rawClient) writeFrame(msg []byte) {
  33. c.t.Helper()
  34. f := make([]byte, 4+len(msg))
  35. f[1], f[2], f[3] = byte(len(msg)>>16), byte(len(msg)>>8), byte(len(msg))
  36. copy(f[4:], msg)
  37. if _, err := c.nc.Write(f); err != nil {
  38. c.t.Fatalf("write: %v", err)
  39. }
  40. }
  41. func (c *rawClient) readFrame() []byte {
  42. c.t.Helper()
  43. c.nc.SetReadDeadline(time.Now().Add(5 * time.Second))
  44. head := make([]byte, 4)
  45. if _, err := io.ReadFull(c.nc, head); err != nil {
  46. c.t.Fatalf("read: %v", err)
  47. }
  48. f := make([]byte, int(head[1])<<16|int(head[2])<<8|int(head[3]))
  49. if _, err := io.ReadFull(c.nc, f); err != nil {
  50. c.t.Fatalf("read: %v", err)
  51. }
  52. return f
  53. }
  54. // parseReplies splits a frame into its compound parts.
  55. func parseReplies(t *testing.T, f []byte) []rawReply {
  56. t.Helper()
  57. out := []rawReply{}
  58. for {
  59. h, err := wire.ParseHeader(f)
  60. if err != nil {
  61. t.Fatalf("reply: %v", err)
  62. }
  63. end := len(f)
  64. if h.NextCommand != 0 {
  65. end = int(h.NextCommand)
  66. }
  67. out = append(out, rawReply{hdr: h, body: f[wire.HeaderSize:end], raw: f[:end]})
  68. if h.NextCommand == 0 {
  69. return out
  70. }
  71. f = f[end:]
  72. }
  73. }
  74. func (c *rawClient) message(cmd uint16, body []byte, flags uint32) []byte {
  75. h := wire.Header{Command: cmd, Credits: 8, CreditCharge: 1, MessageID: c.mid, SessionID: c.session, TreeID: c.tree, Flags: flags}
  76. c.mid++
  77. msg := make([]byte, wire.HeaderSize+len(body))
  78. h.Put(msg)
  79. copy(msg[wire.HeaderSize:], body)
  80. return msg
  81. }
  82. // call sends one signed request and returns its reply, skipping interim ones.
  83. func (c *rawClient) call(cmd uint16, body []byte) rawReply {
  84. c.t.Helper()
  85. msg := c.message(cmd, body, 0)
  86. if c.signer != nil {
  87. c.signer.Sign(msg)
  88. }
  89. c.writeFrame(msg)
  90. for {
  91. r := parseReplies(c.t, c.readFrame())[0]
  92. if r.hdr.Status != wire.StatusPending {
  93. c.verify(r)
  94. return r
  95. }
  96. }
  97. }
  98. func (c *rawClient) verify(r rawReply) {
  99. c.t.Helper()
  100. if c.signer == nil || r.hdr.Status == wire.StatusPending {
  101. return
  102. }
  103. if r.hdr.Flags&wire.FlagSigned == 0 {
  104. c.t.Fatalf("command %d: reply is not signed (status %x)", r.hdr.Command, r.hdr.Status)
  105. }
  106. if !c.signer.Verify(r.raw) {
  107. c.t.Fatalf("command %d: reply signature is wrong", r.hdr.Command)
  108. }
  109. }
  110. func negotiateBody(dialects ...uint16) []byte {
  111. w := wire.NewWriter(64)
  112. w.U16(36)
  113. w.U16(uint16(len(dialects)))
  114. w.U16(wire.SigningEnabled)
  115. w.U16(0)
  116. w.U32(0)
  117. w.Zero(16)
  118. ctxAt := w.Len()
  119. w.U32(0)
  120. w.U16(0)
  121. w.U16(0)
  122. for _, d := range dialects {
  123. w.U16(d)
  124. }
  125. for _, d := range dialects {
  126. if d == wire.Dialect311 {
  127. w.Align(8)
  128. for (wire.HeaderSize+w.Len())%8 != 0 {
  129. w.U8(0)
  130. }
  131. binary.LittleEndian.PutUint32(w.B[ctxAt:], uint32(wire.HeaderSize+w.Len()))
  132. binary.LittleEndian.PutUint16(w.B[ctxAt+4:], 2)
  133. w.U16(wire.CtxPreauthIntegrity)
  134. w.U16(38)
  135. w.U32(0)
  136. w.U16(1)
  137. w.U16(32)
  138. w.U16(wire.HashSHA512)
  139. w.Zero(32)
  140. w.Align(8)
  141. w.U16(wire.CtxSigning)
  142. w.U16(4)
  143. w.U32(0)
  144. w.U16(1)
  145. w.U16(wire.SigningAESGMAC)
  146. }
  147. }
  148. return w.B
  149. }
  150. func sessionSetupRequest(token []byte) []byte {
  151. w := wire.NewWriter(24 + len(token))
  152. w.U16(25)
  153. w.U8(0)
  154. w.U8(1)
  155. w.U32(0)
  156. w.U32(0)
  157. w.U16(wire.HeaderSize + 24)
  158. w.U16(uint16(len(token)))
  159. w.U64(0)
  160. w.Bytes(token)
  161. return w.B
  162. }
  163. // newRawClient logs in at SMB 3.1.1 and connects the share.
  164. func newRawClient(t *testing.T, h *harness) *rawClient {
  165. t.Helper()
  166. nc, err := net.Dial("tcp", h.addr)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. t.Cleanup(func() { nc.Close() })
  171. c := &rawClient{t: t, nc: nc}
  172. neg := c.message(wire.CmdNegotiate, negotiateBody(wire.Dialect210, wire.Dialect311), 0)
  173. c.writeFrame(neg)
  174. reply := c.readFrame()
  175. if r := parseReplies(t, reply)[0]; r.hdr.Status != 0 {
  176. t.Fatalf("negotiate: %x", r.hdr.Status)
  177. }
  178. c.preauth = crypt.PreauthHash(crypt.PreauthHash([64]byte{}, neg), reply)
  179. session, key := loginExchange(t, c)
  180. c.session = session
  181. signer, err := crypt.NewSigner(wire.SigningAESGMAC, key)
  182. if err != nil {
  183. t.Fatal(err)
  184. }
  185. c.signer = signer
  186. path := wire.EncodeUTF16(`\\host\data`)
  187. w := wire.NewWriter(8 + len(path))
  188. w.U16(9)
  189. w.U16(0)
  190. w.U16(wire.HeaderSize + 8)
  191. w.U16(uint16(len(path)))
  192. w.Bytes(path)
  193. r := c.call(wire.CmdTreeConnect, w.B)
  194. if r.hdr.Status != 0 {
  195. t.Fatalf("tree connect: %x", r.hdr.Status)
  196. }
  197. c.tree = r.hdr.TreeID
  198. return c
  199. }
  200. // loginExchange plays a full NTLMv2 login and returns the signing key, which
  201. // also proves the preauth hash of both sides agrees.
  202. func loginExchange(t *testing.T, c *rawClient) (uint64, []byte) {
  203. t.Helper()
  204. first := c.message(wire.CmdSessionSetup, sessionSetupRequest(auth.TestNegotiateMessage()), 0)
  205. c.writeFrame(first)
  206. raw := c.readFrame()
  207. r := parseReplies(t, raw)[0]
  208. if r.hdr.Status != wire.StatusMoreProcessingRequired {
  209. t.Fatalf("session setup: %x", r.hdr.Status)
  210. }
  211. session := r.hdr.SessionID
  212. c.preauth = crypt.PreauthHash(crypt.PreauthHash(c.preauth, first), raw)
  213. token := r.body[8:]
  214. authMsg, sessionKey := auth.TestAuthenticateMessage(testUserName, testPassword, token)
  215. c.session = session
  216. second := c.message(wire.CmdSessionSetup, sessionSetupRequest(authMsg), 0)
  217. c.writeFrame(second)
  218. c.preauth = crypt.PreauthHash(c.preauth, second)
  219. final := parseReplies(t, c.readFrame())[0]
  220. if final.hdr.Status != 0 {
  221. t.Fatalf("session setup: %x", final.hdr.Status)
  222. }
  223. key := crypt.SigningKey(wire.Dialect311, sessionKey, c.preauth)
  224. signer, _ := crypt.NewSigner(wire.SigningAESGMAC, key)
  225. if final.hdr.Flags&wire.FlagSigned == 0 || !signer.Verify(final.raw) {
  226. t.Fatal("the final SESSION_SETUP must be signed with the derived key")
  227. }
  228. return session, key
  229. }
  230. type createOpts struct {
  231. access, share, disposition, options uint32
  232. }
  233. func createBody(name string, o createOpts) []byte {
  234. encoded := wire.EncodeUTF16(name)
  235. w := wire.NewWriter(56 + len(encoded))
  236. w.U16(57)
  237. w.U8(0)
  238. w.U8(0)
  239. w.U32(2)
  240. w.Zero(16)
  241. w.U32(o.access)
  242. w.U32(0)
  243. w.U32(o.share)
  244. w.U32(o.disposition)
  245. w.U32(o.options)
  246. w.U16(wire.HeaderSize + 56)
  247. w.U16(uint16(len(encoded)))
  248. w.U32(0)
  249. w.U32(0)
  250. w.Bytes(encoded)
  251. if len(encoded) == 0 {
  252. w.U8(0)
  253. }
  254. return w.B
  255. }
  256. func (c *rawClient) create(name string, o createOpts) (wire.FileID, uint32) {
  257. c.t.Helper()
  258. r := c.call(wire.CmdCreate, createBody(name, o))
  259. var id wire.FileID
  260. if r.hdr.Status == 0 {
  261. copy(id[:], r.body[64:80])
  262. }
  263. return id, r.hdr.Status
  264. }
  265. func closeBody(id wire.FileID) []byte {
  266. w := wire.NewWriter(24)
  267. w.U16(24)
  268. w.U16(0)
  269. w.U32(0)
  270. w.Bytes(id[:])
  271. return w.B
  272. }
  273. func (c *rawClient) close(id wire.FileID) uint32 {
  274. c.t.Helper()
  275. return c.call(wire.CmdClose, closeBody(id)).hdr.Status
  276. }
  277. func writeBodyOf(id wire.FileID, offset uint64, data []byte) []byte {
  278. w := wire.NewWriter(48 + len(data))
  279. w.U16(49)
  280. w.U16(wire.HeaderSize + 48)
  281. w.U32(uint32(len(data)))
  282. w.U64(offset)
  283. w.Bytes(id[:])
  284. w.U32(0)
  285. w.U32(0)
  286. w.U16(0)
  287. w.U16(0)
  288. w.U32(0)
  289. w.Bytes(data)
  290. return w.B
  291. }
  292. func readBodyOf(id wire.FileID, offset uint64, length uint32) []byte {
  293. w := wire.NewWriter(49)
  294. w.U16(49)
  295. w.U8(0)
  296. w.U8(0)
  297. w.U32(length)
  298. w.U64(offset)
  299. w.Bytes(id[:])
  300. w.U32(0)
  301. w.U32(0)
  302. w.U32(0)
  303. w.U16(0)
  304. w.U16(0)
  305. w.U8(0)
  306. return w.B
  307. }
  308. func lockBody(id wire.FileID, offset, length uint64, flags uint32) []byte {
  309. w := wire.NewWriter(48)
  310. w.U16(48)
  311. w.U16(1)
  312. w.U32(0)
  313. w.Bytes(id[:])
  314. w.U64(offset)
  315. w.U64(length)
  316. w.U32(flags)
  317. w.U32(0)
  318. return w.B
  319. }
  320. func setInfoBody(id wire.FileID, class uint8, data []byte) []byte {
  321. w := wire.NewWriter(32 + len(data))
  322. w.U16(33)
  323. w.U8(wire.InfoFile)
  324. w.U8(class)
  325. w.U32(uint32(len(data)))
  326. w.U16(wire.HeaderSize + 32)
  327. w.U16(0)
  328. w.U32(0)
  329. w.Bytes(id[:])
  330. w.Bytes(data)
  331. return w.B
  332. }
  333. func queryDirBody(id wire.FileID, class uint8, flags uint8, pattern string) []byte {
  334. encoded := wire.EncodeUTF16(pattern)
  335. w := wire.NewWriter(32 + len(encoded))
  336. w.U16(33)
  337. w.U8(class)
  338. w.U8(flags)
  339. w.U32(0)
  340. w.Bytes(id[:])
  341. w.U16(wire.HeaderSize + 32)
  342. w.U16(uint16(len(encoded)))
  343. w.U32(65536)
  344. w.Bytes(encoded)
  345. return w.B
  346. }
  347. func notifyBody(id wire.FileID) []byte {
  348. w := wire.NewWriter(32)
  349. w.U16(32)
  350. w.U16(0)
  351. w.U32(4096)
  352. w.Bytes(id[:])
  353. w.U32(0x17)
  354. w.U32(0)
  355. return w.B
  356. }
  357. func queryInfoBody(id wire.FileID, infoType, class uint8, additional uint32) []byte {
  358. w := wire.NewWriter(40)
  359. w.U16(41)
  360. w.U8(infoType)
  361. w.U8(class)
  362. w.U32(65536)
  363. w.U16(0)
  364. w.U16(0)
  365. w.U32(0)
  366. w.U32(additional)
  367. w.U32(0)
  368. w.Bytes(id[:])
  369. w.U8(0)
  370. return w.B
  371. }
  372. func netDial(h *harness) (net.Conn, error) { return net.Dial("tcp", h.addr) }