Skip to main content

Overview

The smb module provides a complete implementation of the SMB (Server Message Block) protocol version 1. This module handles low-level SMB packet construction, authentication, file operations, and named pipe communication.

Key Classes

SMB

Main class for SMB protocol implementation.
str
required
The NetBIOS name of the remote host. Use '*SMBSERVER' for auto-detection.
str
required
IP address or hostname of the remote server
str
Local NetBIOS name. Defaults to local hostname if not specified.
int
default:"139"
Port number for NetBIOS session service
int
default:"60"
Connection timeout in seconds

Methods

login()

Authenticate to the SMB server using NTLM.
str
required
Username for authentication
str
required
Password for authentication (not used if hashes provided)
str
default:"''"
Domain name for authentication
str
default:"''"
LM hash for pass-the-hash authentication
str
default:"''"
NT hash for pass-the-hash authentication
None
Raises SessionError if authentication fails

connect_tree()

Connect to a shared resource on the server.
str
required
UNC path to the share (e.g., '\\\\server\\share')
int
Tree ID (TID) used for subsequent file operations

list_path()

List files and directories in a share.
str
required
Name of the share to list
str
required
Path relative to the share root (e.g., '*' for all files)
str
Password for password-protected shares
list[SharedFile]
List of SharedFile objects containing file information

open()

Open or create a file on the share.
int
required
Tree ID from connect_tree()
str
required
Path to the file relative to share root
int
required
Access mask specifying desired operations (e.g., FILE_READ_DATA, FILE_WRITE_DATA)
int
required
Share mode flags (e.g., FILE_SHARE_READ, FILE_SHARE_WRITE)
int
required
Creation options (e.g., FILE_NON_DIRECTORY_FILE)
int
required
Creation disposition (e.g., FILE_OPEN, FILE_CREATE, FILE_OVERWRITE_IF)
int
required
File attributes (e.g., ATTR_NORMAL, ATTR_READONLY)
int
File ID (FID) used for read/write operations

read_andx()

Read data from an open file.
int
required
Tree ID
int
required
File ID from open()
int
default:"0"
Byte offset to start reading from
int
Maximum bytes to read. Defaults to server’s max buffer size.
bytes
Data read from the file

write_andx()

Write data to an open file.
int
required
Tree ID
int
required
File ID from open()
bytes
required
Data to write to the file
int
default:"0"
Byte offset to start writing at
int
Number of bytes written

close()

Close an open file.
int
required
Tree ID
int
required
File ID to close

logoff()

Log off from the SMB server.

Constants

File Attributes

Access Masks

Share Access Modes

Creation Disposition

Supporting Classes

SharedFile

Represents file information returned by list_path().
str
Returns the full filename
str
Returns the 8.3 short filename
int
Returns file size in bytes
bool
Returns True if item is a directory
bool
Returns True if file is read-only
bool
Returns True if file is hidden
int
Returns creation time as Unix timestamp
int
Returns modification time as Unix timestamp
int
Returns access time as Unix timestamp

SessionError

Exception raised when SMB operations fail.
int
Returns the SMB error code
int
Returns the error class

Usage Examples

Basic File Operations

Writing Files

Pass-the-Hash Authentication

Error Handling

Helper Functions

Time Conversion

See Also

  • SMBConnection - High-level SMB client wrapper
  • SMB3 - SMB2/SMB3 protocol implementation
  • NTLM - NTLM authentication helpers