Skip to content

Instantly share code, notes, and snippets.

@lovemyliwu
Last active October 22, 2022 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovemyliwu/48eeaf566978ec24843b743877df0021 to your computer and use it in GitHub Desktop.
Save lovemyliwu/48eeaf566978ec24843b743877df0021 to your computer and use it in GitHub Desktop.
python connect smb server through windows domain user
import sys
from optparse import OptionParser
from smb.SMBConnection import SMBConnection
def main(argv):
parser = OptionParser()
parser.add_option("-u", "--username",
help="windows username that will be used for authentication")
parser.add_option("-p", "--password",
help="windows password that will be used for authentication")
parser.add_option("-s", "--system-name",
help="smb server machine name")
parser.add_option("-d", "--domain",
help="windows domain name that will be used for authentication")
(options, args) = parser.parse_args()
system_name = options.system_name
try:
print('### Analyzing system: ' + system_name)
# parameterize an smb connection with a system
conn = SMBConnection(options.username,
options.password,
'enumerator',
options.system_name,
options.domain,
use_ntlm_v2=True,
sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,
is_direct_tcp=True)
# establish the actual connection
connected = conn.connect(system_name,445)
try:
Response = conn.listShares(timeout=30) # obtain a list of shares
print('Shares on: ' + system_name)
for i in range(len(Response)): # iterate through the list of shares
print(" Share[",i,"] =", Response[i].name)
try:
# list the files on each share (recursivity?)
Response2 = conn.listPath(Response[i].name,'/',timeout=30)
print(' Files on: ' + system_name + '/' + " Share[",i,"] =",
Response[i].name)
for i in range(len(Response2)):
print(" File[",i,"] =", Response2[i].filename)
except:
print('### can not access the resource')
except Exception as error:
print('### can not list shares')
except:
print('### can not access the system')
if __name__ == "__main__":
main(sys.argv[1:])
@lovemyliwu
Copy link
Author

use example:

python smb_enumerator.py -u abc -p abcpassword -d GROUP -s ftp_server

just like GROUP\abc domain user can direct browse in explorer:

\\ftp_server\

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment