Overview
TheSMBConnection class provides a unified, high-level interface for SMB communication that automatically handles protocol negotiation between SMB1, SMB2, and SMB3. It abstracts away protocol-specific details and provides a consistent API regardless of the underlying SMB version.
Class Definition
SMBConnection
Main class for SMB client operations with automatic protocol negotiation.str
required
NetBIOS name of the remote host. Use
'*SMBSERVER' for automatic detection, or provide the actual hostname.str
required
IP address or hostname of the target server
str
Local NetBIOS name. If
None, uses the local hostname.int
default:"445"
SMB session port. Use
445 for direct TCP or 139 for NetBIOSint
default:"60"
Connection timeout in seconds
str | int
Preferred SMB dialect. Options:
SMB_DIALECT- SMB1 (NT LM 0.12)SMB2_DIALECT_002- SMB 2.0.2SMB2_DIALECT_21- SMB 2.1SMB2_DIALECT_30- SMB 3.0SMB2_DIALECT_311- SMB 3.1.1
None, negotiates the highest supported version.Authentication Methods
login()
Authenticate using NTLM.str
required
Username for authentication
str
required
User password (not used if hashes are provided)
str
default:"''"
Domain name for the account
str
default:"''"
LM hash for pass-the-hash authentication (hex string)
str
default:"''"
NT hash for pass-the-hash authentication (hex string)
bool
default:"True"
Allow fallback to NTLMv1 if NTLMv2 fails (SMB1 only)
SessionError
Raised if authentication fails
kerberosLogin()
Authenticate using Kerberos.str
required
Username for authentication
str
required
User password
str
required
Domain name (required for Kerberos)
str
default:"''"
LM hash for RC4-HMAC if AES not supported
str
default:"''"
NT hash for RC4-HMAC if AES not supported
str
default:"''"
AES key (aes256-cts-hmac-sha1-96 or aes128-cts-hmac-sha1-96)
str
Hostname or IP of the KDC. If
None, uses DNS to resolve the domain.dict
Pre-obtained Ticket Granting Ticket
dict
Pre-obtained Ticket Granting Service ticket
bool
default:"True"
Use credential cache for ticket lookup
File and Directory Operations
connectTree()
Connect to a network share.Share name (e.g.,
'ADMIN$', 'C$', 'IPC$')int
Tree ID for use in subsequent operations
listPath()
List files and directories in a share.Name of the share to list
str
required
Path pattern (e.g.,
'*' for all files, '*.txt' for text files)str
Password for password-protected shares
list[SharedFile]
List of
SharedFile objectscreateFile()
Create or open a file.int
required
Tree ID from
connectTree()str
required
Path to the file relative to share root
int
default:"GENERIC_ALL"
Access mask (e.g.,
FILE_READ_DATA, FILE_WRITE_DATA, GENERIC_ALL)Share access mode
int
default:"FILE_NON_DIRECTORY_FILE"
File creation options
int
default:"FILE_OVERWRITE_IF"
Action to take if file exists
int
default:"FILE_ATTRIBUTE_NORMAL"
File attributes to set
int
File ID (FID) for subsequent operations
openFile()
Open an existing file.int
required
Tree ID
str
required
Path to the file
int
default:"FILE_READ_DATA"
Access rights requested
Sharing mode
int
File ID for the opened file
readFile()
Read data from a file.int
required
Tree ID
int
required
File ID from
openFile() or createFile()int
default:"0"
Byte offset to start reading from
int
Number of bytes to read. If
None, reads maximum buffer size.bool
default:"True"
If
True, reads only once. If False, continues reading until bytesToRead is satisfied.bytes
Data read from the file
writeFile()
Write data to a file.int
required
Tree ID
int
required
File ID
bytes
required
Data to write
int
default:"0"
Byte offset to write at
int
Number of bytes written
closeFile()
Close an open file.int
required
Tree ID
int
required
File ID to close
deleteFile()
Delete a file from the share.Share name
str
required
Path to the file to delete
getFile()
Download a file using a callback.Share name
str
required
Remote file path
callable
required
Function to call with file data chunks (receives bytes)
Share access mode
putFile()
Upload a file using a callback.Share name
str
required
Remote file path
callable
required
Function to call to get file data (receives int size, returns bytes)
createDirectory()
Create a directory.Share name
str
required
Directory path to create
deleteDirectory()
Delete a directory.Share name
str
required
Directory path to delete
rename()
Rename a file or directory.Share name
str
required
Current path
str
required
New path
Information Retrieval
listShares()
List available shares on the server.list[dict]
List of share dictionaries with keys like
'shi1_netname', 'shi1_type', 'shi1_remark'getDialect()
Get the negotiated SMB dialect.str | int
The negotiated dialect (e.g.,
SMB2_DIALECT_311)getServerName()
Get the server’s NetBIOS name.str
Server NetBIOS name
getServerDomain()
Get the server’s domain.str
Server domain name
getServerOS()
Get the server’s operating system.str
Operating system string (e.g.,
"Windows 10 Build 19041")isGuestSession()
Check if logged in as guest.bool
True if guest session, False otherwiseNamed Pipe Operations
waitNamedPipe()
Wait for a named pipe to become available.int
required
Tree ID (usually for IPC$ share)
str
required
Name of the pipe (e.g.,
'\\PIPE\\srvsvc')int
default:"5"
Timeout in seconds
transactNamedPipe()
Perform a transaction on a named pipe.int
required
Tree ID
int
required
File ID of the opened pipe
bytes
required
Data to send
bool
default:"True"
Wait for response