Documentation ¶
Overview ¶
Package smb2 implements the SMB2/3 client in [MS-SMB2].
https://msdn.microsoft.com/en-us/library/cc246482.aspx
This package doesn't support CAP_UNIX extension. Symlink is supported by FSCTL_SET_REPARSE_POINT and FSCTL_GET_REPARSE_POINT. The symlink-following algorithm is explained in 2.2.2.2.1 and 2.2.2.2.1.1.
https://msdn.microsoft.com/en-us/library/cc246542.aspx
Supported features and protocol versions are declared in feature.go.
Example ¶
package main import ( "fmt" "io" "net" "github.com/5amu/smb" ) func main() { conn, err := net.Dial("tcp", "localhost:445") if err != nil { panic(err) } defer conn.Close() d := &smb.Dialer{ Initiator: &smb.NTLMInitiator{ User: "Guest", Password: "", Domain: "MicrosoftAccount", }, } c, err := d.Dial(conn) if err != nil { panic(err) } defer func() { _ = c.Logoff() }() fs, err := c.Mount(`\\localhost\share`) if err != nil { panic(err) } defer func() { _ = fs.Umount() }() f, err := fs.Create("hello.txt") if err != nil { panic(err) } defer func() { _ = fs.Remove("hello.txt") _ = f.Close() }() _, err = f.Write([]byte("Hello world!")) if err != nil { panic(err) } _, err = f.Seek(0, io.SeekStart) if err != nil { panic(err) } bs, err := io.ReadAll(f) if err != nil { panic(err) } fmt.Println(string(bs)) // Hello world! }
Output:
Index ¶
- Constants
- Variables
- func IsPathSeparator(c uint8) bool
- func Match(pattern, name string) (matched bool, err error)
- type BinaryMarshallableV1
- type Client
- type ContextError
- type Dialer
- type File
- func (f *File) Chmod(mode os.FileMode) error
- func (f *File) Close() error
- func (f *File) Name() string
- func (f *File) Read(b []byte) (n int, err error)
- func (f *File) ReadAt(b []byte, off int64) (n int, err error)
- func (f *File) ReadFrom(r io.Reader) (n int64, err error)
- func (f *File) Readdir(n int) (fi []os.FileInfo, err error)
- func (f *File) Readdirnames(n int) (names []string, err error)
- func (f *File) Seek(offset int64, whence int) (ret int64, err error)
- func (f *File) Stat() (os.FileInfo, error)
- func (f *File) Statfs() (FileFsInfo, error)
- func (f *File) Sync() (err error)
- func (f *File) Truncate(size int64) error
- func (f *File) Write(b []byte) (n int, err error)
- func (f *File) WriteAt(b []byte, off int64) (n int, err error)
- func (f *File) WriteString(s string) (n int, err error)
- func (f *File) WriteTo(w io.Writer) (n int64, err error)
- type FileFsInfo
- type FileStat
- type HeaderV1
- type Initiator
- type InternalError
- type InvalidResponseError
- type MetadataV1
- type NTLMInitiator
- type NTLMSSPInfoMap
- type NTLMSSPInitiator
- type NegotiateReqV1
- type Negotiator
- type RemoteFile
- type RemoteFileStat
- type RemoteFileSystem
- type ResponseError
- type SMBFingerprint
- type Session
- type Share
- func (fs *Share) Chmod(name string, mode os.FileMode) error
- func (fs *Share) Chtimes(name string, atime time.Time, mtime time.Time) error
- func (fs *Share) Create(name string) (*File, error)
- func (s *Share) DirFS(dirname string) fs.FS
- func (fs *Share) Glob(pattern string) (matches []string, err error)
- func (fs *Share) Lstat(name string) (os.FileInfo, error)
- func (fs *Share) Mkdir(name string, perm os.FileMode) error
- func (fs *Share) MkdirAll(path string, perm os.FileMode) error
- func (fs *Share) Open(name string) (*File, error)
- func (fs *Share) OpenFile(name string, flag int, perm os.FileMode) (*File, error)
- func (fs *Share) ReadDir(dirname string) ([]os.FileInfo, error)
- func (fs *Share) ReadFile(filename string) ([]byte, error)
- func (fs *Share) Readlink(name string) (string, error)
- func (fs *Share) Remove(name string) error
- func (fs *Share) RemoveAll(path string) error
- func (fs *Share) Rename(oldpath, newpath string) error
- func (fs *Share) Stat(name string) (os.FileInfo, error)
- func (fs *Share) Statfs(name string) (FileFsInfo, error)
- func (fs *Share) Symlink(target, linkpath string) error
- func (fs *Share) Truncate(name string, size int64) error
- func (fs *Share) Umount() error
- func (fs *Share) WithContext(ctx context.Context) *Share
- func (fs *Share) WriteFile(filename string, data []byte, perm os.FileMode) error
- type TagMapV1
- type TransportError
- type V1Client
Examples ¶
Constants ¶
const DialectSmb_1_0 = "\x02NT LM 0.12\x00"
const MaxReadSizeLimit = 0x100000 // deprecated constant
const PathSeparator = '\\'
const ProtocolSmb = "\xFFSMB"
SMBv1 is supported as far as DETECTION goes. In 2024 I'm not willing to fully support it... I guess that help would be appreciated wut not actively wanted. Thank you for your understanding.
Variables ¶
var ErrBadPattern = errors.New("syntax error in pattern")
ErrBadPattern indicates a pattern was malformed.
var NORMALIZE_PATH = true // normalize path arguments automatically
Functions ¶
func IsPathSeparator ¶
func Match ¶
Match reports whether name matches the shell file name pattern. The pattern syntax is:
pattern: { term } term: '*' matches any sequence of non-Separator characters '?' matches any single non-Separator character '[' [ '^' ] { character-range } ']' character class (must be non-empty) c matches character c (c != '*', '?', '[') character-range: c matches character c (c != '-', ']') lo '-' hi matches character c for lo <= c <= hi
Match requires pattern to match all of name, not just a substring. The only possible returned error is ErrBadPattern, when pattern is malformed.
Types ¶
type BinaryMarshallableV1 ¶
type BinaryMarshallableV1 interface { MarshalBinary(*MetadataV1) ([]byte, error) UnmarshalBinary([]byte, *MetadataV1) error }
type ContextError ¶
type ContextError struct {
Err error
}
ContextError wraps a context error to support os.IsTimeout function.
func (*ContextError) Error ¶
func (err *ContextError) Error() string
func (*ContextError) Timeout ¶
func (err *ContextError) Timeout() bool
type Dialer ¶
type Dialer struct { MaxCreditBalance uint16 // if it's zero, clientMaxCreditBalance is used. (See feature.go for more details) Negotiator Negotiator Initiator Initiator }
Dialer contains options for func (*Dialer) Dial.
func (*Dialer) Dial ¶
Dial performs negotiation and authentication. It returns a session. It doesn't support NetBIOS transport. This implementation doesn't support multi-session on the same TCP connection. If you want to use another session, you need to prepare another TCP connection at first.
func (*Dialer) DialContext ¶
DialContext performs negotiation and authentication using the provided context. Note that returned session doesn't inherit context. If you want to use the same context, call Session.WithContext manually. This implementation doesn't support multi-session on the same TCP connection. If you want to use another session, you need to prepare another TCP connection at first.
type File ¶
type File struct {
// contains filtered or unexported fields
}
func (*File) ReadFrom ¶
ReadFrom implements io.ReadFrom. If r is *File on the same *Share as f, it invokes server-side copy.
func (*File) Statfs ¶
func (f *File) Statfs() (FileFsInfo, error)
type FileFsInfo ¶
type FileStat ¶
type InternalError ¶
type InternalError struct {
Message string
}
InternalError represents internal error.
func (*InternalError) Error ¶
func (err *InternalError) Error() string
type InvalidResponseError ¶
type InvalidResponseError struct {
Message string
}
InvalidResponseError represents a data sent by the server is corrupted or unexpected.
func (*InvalidResponseError) Error ¶
func (err *InvalidResponseError) Error() string
type MetadataV1 ¶
type NTLMInitiator ¶
type NTLMInitiator struct { User string Password string Hash []byte Domain string Workstation string TargetSPN string // contains filtered or unexported fields }
NTLMInitiator implements session-setup through NTLMv2. It doesn't support NTLMv1. You can use Hash instead of Password.
func (*NTLMInitiator) InfoMap ¶
func (i *NTLMInitiator) InfoMap() *ntlm.InfoMap
type NTLMSSPInfoMap ¶
type NTLMSSPInitiator ¶
type NTLMSSPInitiator struct { User string Password string Hash []byte Domain string Workstation string TargetSPN string // contains filtered or unexported fields }
func (*NTLMSSPInitiator) GetInfoMap ¶
func (i *NTLMSSPInitiator) GetInfoMap() *NTLMSSPInfoMap
type NegotiateReqV1 ¶
type Negotiator ¶
type Negotiator struct { RequireMessageSigning bool // enforce signing? ClientGuid [16]byte // if it's zero, generated by crypto/rand. SpecifiedDialect uint16 // if it's zero, clientDialects is used. (See feature.go for more details) }
Negotiator contains options for func (*Dialer) Dial.
type RemoteFile ¶
type RemoteFile = File // deprecated type name
type RemoteFileStat ¶
type RemoteFileStat = FileStat // deprecated type name
type RemoteFileSystem ¶
type RemoteFileSystem = Share // deprecated type name
type ResponseError ¶
type ResponseError struct { Code uint32 // NTSTATUS // contains filtered or unexported fields }
ResponseError represents a error with a nt status code sent by the server. The NTSTATUS is defined in [MS-ERREF]. https://msdn.microsoft.com/en-au/library/cc704588.aspx
func (*ResponseError) Error ¶
func (err *ResponseError) Error() string
type SMBFingerprint ¶
type SMBFingerprint struct { // V1Support if supports SMBv1 V1Support bool // Security Mode of the connection SigningRequired bool // Reported Vesion of OS OSVersion string // NETBIOS NetBIOSComputerName string NetBIOSDomainName string // DNS DNSComputerName string DNSDomainName string ForestName string }
func Fingerprint ¶
func Fingerprint(host string, port int) (*SMBFingerprint, error)
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a SMB session.
func (*Session) ListSharenames ¶
type Share ¶
type Share struct {
// contains filtered or unexported fields
}
Share represents a SMB tree connection with VFS interface.
func (*Share) RemoveAll ¶
RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).
func (*Share) Symlink ¶
Symlink mimics os.Symlink. This API should work on latest Windows and latest MacOS. However it may not work on Linux because Samba doesn't support reparse point well. Also there is a restriction on target pathname. Generally, a pathname begins with leading backslash (e.g `\dir\name`) can be interpreted as two ways. On windows, it is evaluated as a relative path, on other systems, it is evaluated as an absolute path. This implementation always assumes that format is absolute path. So, if you know the target server is Windows, you should avoid that format. If you want to use an absolute target path on windows, you can use // `C:\dir\name` format instead.
type TransportError ¶
type TransportError struct {
Err error
}
TransportError represents a error come from net.Conn layer.
func (*TransportError) Error ¶
func (err *TransportError) Error() string