msg_file_read.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package smb
  2. import "io"
  3. func init() {
  4. commandRequestMap[CommandRead] = func() DataI {
  5. return &ReadRequest{}
  6. }
  7. }
  8. //CommandRead
  9. type ReadRequest struct {
  10. Header
  11. StructureSize uint16
  12. Padding uint8
  13. Flags uint8
  14. Length uint32
  15. Offset uint64
  16. FileId GUID
  17. MinimumCount uint32
  18. Channel uint32
  19. RemainingBytes uint32
  20. ReadChannelInfoOffset uint16 `smb:"offset:ReadChannelInfo"`
  21. ReadChannelInfoLength uint16 `smb:"len:ReadChannelInfo"`
  22. ReadChannelInfo []byte
  23. }
  24. type ReadResponse struct {
  25. Header
  26. StructureSize uint16
  27. DataOffset uint8 `smb:"offset:Data"`
  28. Reserved uint8
  29. DataLength uint32 `smb:"len:Data"`
  30. DataRemaining uint32
  31. Reserved2 uint32
  32. Data []byte
  33. }
  34. func (data *ReadRequest) ServerAction(ctx *DataCtx) (interface{}, error) {
  35. data.Header.Flags = SMB2_FLAGS_RESPONSE
  36. fileid := ctx.FileID(data.FileId)
  37. webfile, ok := ctx.session.openedFiles[fileid]
  38. if !ok {
  39. return ERR(data.Header, STATUS_FILE_CLOSED)
  40. }
  41. if fileid.IsSvrSvc(ctx.session) {
  42. return DcerpcRead(ctx, data)
  43. }
  44. buffer := make([]byte, data.Length)
  45. _, err := webfile.Seek(int64(data.Offset), 0)
  46. if err != nil {
  47. return ERR(data.Header, STATUS_UNSUCCESSFUL)
  48. }
  49. n, err := webfile.Read(buffer)
  50. if n < int(data.MinimumCount) {
  51. return ERR(data.Header, STATUS_END_OF_FILE)
  52. }
  53. if err != nil && err != io.EOF {
  54. return ERR(data.Header, STATUS_UNSUCCESSFUL)
  55. }
  56. resp := ReadResponse{
  57. Header: data.Header,
  58. StructureSize: 17,
  59. Data: buffer[:n],
  60. DataOffset: 80,
  61. DataLength: uint32(n),
  62. }
  63. return &resp, nil
  64. }