class SmbClient(object):
        def __init__(self,ip,username,password,sharename):
                self.ip = ip
                self.username = username
                self.password = password
                self.sharename = sharename
        def connect(self):
                self.server = SMBConnection(self.username,
                                self.password,client,netbios_name,use_ntlm_v2=True)
                self.server.connect(self.ip,139)
        def upload(self,file):
                data = open(file,'rb')
                file = '/' + file
                self.server.storeFile(self.sharename,file,data)
                print "file has been uploaded"
        def download(self,file):
                fileobj = open(file,'wb')
                self.server.retrieveFile(self.sharename,fileobj)
                print "file has been downloaded in current dir"

        def delete(self,file):
                'remove file from remote share'
                file = '/' + file
                self.server.deleteFiles(self.sharename,file)

        def list(self):
                ' list files of remote share '
                filelist = self.server.listPath(self.sharename,'/')
                for f in filelist:
                        print f.filename
class smb_connector:
	def __init__(self, ip, shared_directory, username, password):
		self.connection = SMBConnection(username, password, "this_machine", "remote_machine", use_ntlm_v2=True, is_direct_tcp=True)
		self.connection.connect(ip, 445)
		self.shared_directory = shared_directory

	def __enter__(self):
		return self

	def __exit__(self, type, value, traceback):
		self.close()

	def close(self):
		self.connection.close()

	def file_contents(self, path):
		with tempfile.NamedTemporaryFile() as file_obj:
			self.connection.retrieveFile(self.shared_directory, path, file_obj)
			file_obj.seek(0)
			content = file_obj.read().decode('utf-8', 'ignore').translate({ord('\u0000'): None})
		return content

	def all_files_recursively(self, full_path, file_filter, directory_filter, relative_path=''):
		whats_here = self.connection.listPath(self.shared_directory, full_path)
		for file in whats_here:
			file_path = os.path.join(full_path, file.filename)
			file_relative_path = os.path.join(relative_path, file.filename)
			if file.isDirectory:
				if directory_filter(file.filename) and '.' not in file.filename:
					yield from self.all_files_recursively(file_path, file_filter, directory_filter, file_relative_path)
			elif file_filter(file.filename):
				yield os.path.normpath(file_relative_path)

	def write_file(self, path, contents):
		with tempfile.NamedTemporaryFile() as file_obj:
			bytes = file_obj.write(contents.encode('utf-8'))
			file_obj.seek(0)
			bytes = self.connection.storeFile(self.shared_directory, path, file_obj)
Example #3
0
 def connect_folder(self, ip, directory):
     try:
         conn = SMBConnection('guest', '', '', '', use_ntlm_v2=True)
         assert conn.connect(ip, 445)
         conn.listPath(directory, '/')
         self.open_folder[ip].append(directory)
         print("{2}[{4}]{3} {0}/{5}{1} ==> {2}Target seems to can access. ".
               format(ip, W, G, C, self.service, directory))
     except:
         print("{2}[{4}]{3} {0}/{5}{1} ==> {2}Authenthication required. ".
               format(ip, W, R, C, self.service, directory))
Example #4
0
def sendSMBConnection(activeResponderIp, drive, directory, domain):
    global bait_accounts
    try:
        credidx = randint(0, len(bait_accounts)-1)
        logging.info("Seeding Responder {} with {}:{}".format(activeResponderIp, bait_accounts[credidx][0], bait_accounts[credidx][1]))
        conn = SMBConnection(bait_accounts[credidx][0], bait_accounts[credidx][1], 'connector', activeResponderIp, domain=domain, use_ntlm_v2=True, is_direct_tcp=True)
        conn.connect(activeResponderIp, 445)
        conn.listPath(drive, directory)
        conn.close()
    except NotReadyError as smbex:
        pass
    except Exception as ex:
        logging.error("Failed sending SMB creds: {}".format(ex))
Example #5
0
def smb(host, port=445):
    try:
        conn = SMBConnection("", "", "", "", use_ntlm_v2=True)
        if conn.connect(host, port, timeout=timeout):
            color_print.green(f"[*] smb service detected:{host}:{port}")
            sharelist = conn.listShares()
            for i in sharelist:
                try:
                    conn.listPath(i.name, "/")
                    color_print.red(f"[+] smb unauthorised directory:{host}:{port}/{i.name}")
                except:
                    color_print.green(f"[*] smb directory:{host}:{port}/{i.name}")

        conn.close()
    except:
        pass
Example #6
0
 def display(self, a):
     try:
         self.dir = yp.get(yp.curselection())  #获取共享目录
         #print(yp.get(yp.curselection()))
     except:
         pass
     try:  #设置路径变量
         if self.display_path != '':
             if ml.get(ml.curselection()) != '..':
                 self.display_path = self.display_path + '/' + ml.get(
                     ml.curselection())
             elif ml.get(ml.curselection()) == '..':
                 self.display_path = self.display_path + '/' + ml.get(
                     ml.curselection())
         else:
             self.display_path = ml.get(ml.curselection())
         #print(ml.get(ml.curselection()))
     except:
         pass
     conn = SMBConnection(self.username.get(),
                          self.password.get(),
                          self.my_name,
                          self.domain_name,
                          use_ntlm_v2=True,
                          is_direct_tcp=True)
     conn.connect(self.remote_smb_IP.get(), int(self.port.get()))
     flist = conn.listPath(service_name=self.dir,
                           path=self.display_path,
                           pattern='*')
     ml.delete(0, END)
     for i in flist:
         #print(i.filename)
         ml.insert(END, i.filename)
    def transfer(self, uid, old_code, new_code):
        """ Sync a child transfer with GP: rename pictures and log a note. """
        # Retrieve configuration
        smb_user = config.get("smb_user")
        smb_pass = config.get("smb_pwd")
        smb_ip = config.get("smb_ip")
        smb_port = int(config.get("smb_port", 0))
        if not (smb_user and smb_pass and smb_ip and smb_port):
            return False

        # Rename files in shared folder
        smb_conn = SMBConnection(smb_user, smb_pass, "openerp", "nas")
        if smb_conn.connect(smb_ip, smb_port):
            gp_old_pic_path = "{0}{1}/".format(config.get("gp_pictures"), old_code[:2])
            gp_new_pic_path = "{0}{1}/".format(config.get("gp_pictures"), new_code[:2])
            pic_files = smb_conn.listPath("GP", gp_old_pic_path)
            for file in pic_files:
                filename = file.filename
                if filename.startswith(old_code):
                    new_name = filename.replace(old_code, new_code)
                    smb_conn.rename("GP", gp_old_pic_path + filename, gp_new_pic_path + new_name)

        # Rename child code in Poles table
        self.query("UPDATE Poles SET CODESPE = %s WHERE CODESPE = %s", [old_code, new_code])
        return True
Example #8
0
 def run(self):
     print "Starting thread for " + self.ip
     net = NetBIOS()
     net_name = str(net.queryIPForName(self.ip)).strip("['").strip("']")
     net.close()
     conn = SMBConnection(self.user,
                          self.pwd,
                          'cobwebs',
                          net_name,
                          domain=self.domain,
                          use_ntlm_v2=False)
     if conn.connect(self.ip, port=139, timeout=10):
         print(
             "Connecting to %s was successful! How about a nice game of spidering %s%s?"
             % (self.ip, self.share, self.subfolder))
     else:
         print("Connection error: %s" % (self.ip))
     if self.recursive > 0:
         recurse(conn, self.ip, self.share, self.subfolder, self.pattern,
                 int(self.recursive))
     else:
         filelist = conn.listPath(self.share, self.subfolder)
         dir_list(filelist, self.ip, self.subfolder, self.pattern)
     conn.close()
     print "Exiting thread for " + self.ip
Example #9
0
def look_for_new_dumps():
    # Try to access CRADMZShare using Samba from here
    # and see if there are any dump files we have not seen before.
    try:
        logger.info("Starting to look for new dumps...")
        creds = settings.CRA_SAMBA_CREDENTIALS
        conn = SMBConnection(username=creds['username'],
                             password=creds['password'],
                             my_name=creds['client_machine_name'],
                             remote_name=creds['server_name'],
                             use_ntlm_v2=True)
        assert conn.connect(creds['server_ip'], 139)

        new_files = []
        files = conn.listPath('share', '/')
        for file in files:
            if not file.isDirectory:
                dumpfile, created = DumpFile.objects.get_or_create(
                    filename=file.filename)
                if created:
                    new_files.append(file.filename)
                    logger.info("Found a new dump %s" % file.filename)
                else:
                    logger.info("File already known: %s" % file.filename)
        if new_files:
            logger.info("Sending email about new files")
            mail_admins(
                "New dump files spotted",
                "New files: %s" % ', '.join(new_files),
            )
        else:
            logger.info("No new files")
    except:
        logger.exception(
            "Something went wrong while checking for new dump files")
    def transfer(self, uid, old_code, new_code):
        """ Sync a child transfer with GP: rename pictures and log a note. """
        # Retrieve configuration
        smb_user = config.get('smb_user')
        smb_pass = config.get('smb_pwd')
        smb_ip = config.get('smb_ip')
        smb_port = int(config.get('smb_port', 0))
        if not (smb_user and smb_pass and smb_ip and smb_port):
            return False

        # Rename files in shared folder
        smb_conn = SMBConnection(smb_user, smb_pass, 'openerp', 'nas')
        if smb_conn.connect(smb_ip, smb_port):
            gp_old_pic_path = "{0}{1}/".format(config.get('gp_pictures'),
                                               old_code[:2])
            gp_new_pic_path = "{0}{1}/".format(config.get('gp_pictures'),
                                               new_code[:2])
            pic_files = smb_conn.listPath('GP', gp_old_pic_path)
            for file in pic_files:
                filename = file.filename
                if filename.startswith(old_code):
                    new_name = filename.replace(old_code, new_code)
                    smb_conn.rename('GP', gp_old_pic_path + filename,
                                    gp_new_pic_path + new_name)

        # Rename child code in GP tables
        self.query("UPDATE ENFANTS SET CODE = %s WHERE CODE = %s",
                   [new_code, old_code])
        self.query("UPDATE Poles SET CODESPE = %s WHERE CODESPE = %s",
                   [new_code, old_code])
        self.query("UPDATE Affectat SET CODESPE = %s WHERE CODESPE = %s",
                   [new_code, old_code])
        return True
Example #11
0
    def transfer(self, uid, old_code, new_code):
        """ Sync a child transfer with GP: rename pictures and log a note. """
        # Retrieve configuration
        smb_user = config.get('smb_user')
        smb_pass = config.get('smb_pwd')
        smb_ip = config.get('smb_ip')
        smb_port = int(config.get('smb_port', 0))
        if not (smb_user and smb_pass and smb_ip and smb_port):
            return False

        # Rename files in shared folder
        smb_conn = SMBConnection(smb_user, smb_pass, 'openerp', 'nas')
        if smb_conn.connect(smb_ip, smb_port):
            gp_old_pic_path = "{0}{1}/".format(config.get('gp_pictures'),
                                               old_code[:2])
            gp_new_pic_path = "{0}{1}/".format(config.get('gp_pictures'),
                                               new_code[:2])
            pic_files = smb_conn.listPath('GP', gp_old_pic_path)
            for file in pic_files:
                filename = file.filename
                if filename.startswith(old_code):
                    new_name = filename.replace(old_code, new_code)
                    smb_conn.rename('GP', gp_old_pic_path + filename,
                                    gp_new_pic_path + new_name)

        # Rename child code in GP tables
        self.query("UPDATE ENFANTS SET CODE = %s WHERE CODE = %s",
                   [new_code, old_code])
        self.query("UPDATE Poles SET CODESPE = %s WHERE CODESPE = %s",
                   [new_code, old_code])
        self.query("UPDATE Affectat SET CODESPE = %s WHERE CODESPE = %s",
                   [new_code, old_code])
        return True
Example #12
0
    def listFiles(self, share, username='', password=''):
        try:
            smbConnect = SMBConnection(username,
                                       password,
                                       '',
                                       '',
                                       use_ntlm_v2=True)
            smbConnect.connect(str(self.target), self.port)
            sharedFiles = smbConnect.listPath(share, '/')

            files = {}

            for file in sharedFiles:
                if file.isDirectory:
                    files[str(file.filename)] = 'd'
                else:
                    files[str(file.filename)] = 'f'

            files.pop('.', None)
            files.pop('..', None)

            return {share: files}

        except:
            print(
                colored(
                    '\n[-] Network error connecting to SMB share: {}\n'.format(
                        share), 'red'))
            return None
Example #13
0
def main(argv):
    parser = argparse.ArgumentParser(description='Process SMB information.')
    parser.add_argument("-u", "--username")                 #argument to provide username
    parser.add_argument("-p", "--password")                 #argument to provide password
    parser.add_argument("-f", "--file", dest="filename")    #argument to provide file with a list of all systems you want to enumerate
    parser.add_argument("-d", "--domain")                   #argument to provide domain containing smb

    args = parser.parse_args()

###################################################################
# Opens a file that contains a list of system names to go through #
# and find all shares and the files with in those shares.         #
###################################################################

    with open(args.filename) as f:

        # Open file with system names
        for system_name in f:# Loop through file
            print('Enumerating over system: ' + system_name)
            conn = SMBConnection(args.username, args.password, 'enumerator', system_name, args.domain,
            use_ntlm_v2=True, sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, is_direct_tcp=False)
            conn.connect(system_name, 139) # Attempt to connect to the system
            Shares = conn.listShares(timeout=30)  # Set Shares variable that contains list of shares
            print('Shares for: ' + system_name)
            for i in range(len(Shares)):  # iterate through the list of shares
                if "$" in Shares[i].name:
                    continue
                print("Share: ",i," =", Shares[i].name)
                Files = conn.listPath(Shares[i].name,'/',timeout=30) # Get a list of files in the share
                print('Files for: ' + system_name + '/' + "  Share: ",i," =",Shares[i].name)
                for i in range(len(Files)):
                    print("File[",i,"] =", Files[i].filename)
            conn.close()
Example #14
0
    def connect(self, remote_server, path, user="******", password=""):
        conn = SMBConnection(user,
                             password,
                             remote_server,
                             path,
                             use_ntlm_v2=True)

        try:
            # Check of tieout is possible...
            conn.connect(remote_server, 139)
            with open('local_file', 'wb') as fp:
                # conn.retrieveFile('share', '/path/to/remote_file', fp)
                results = conn.listPath('media_source', '/')
                filenames = [(r.filename, r.isDirectory) for r in results]

                for r in results:

                    if r.isDirectory == False:

                        temp_fh = open("wsf.txt", "wb")

                        file_attributes, filesize = conn.retrieveFile('media_source', '/' + r.filename, temp_fh)

                        temp_fh.write()

                        temp_fh.close()

                "Think what to do with . and .."
                for f in filenames:
                    print(f)

                test = ""
                # documents
        except Exception as msg:
            pass
Example #15
0
def enumerateShareName(ip,shareName):
	print "Attempting to access: //"+ip+"/"+shareName
	try:
		conn = SMBConnection('guest', '', client_machine_name, remote_machine_name, use_ntlm_v2 = True) 
		conn.connect(ip, 445) 
	except:
		print "Failed to Connect"
		pass
	filelist = conn.listPath(shareName, "") 
	for y in filelist:
		if y.isDirectory:
			if y.filename!="." and y.filename!="..":
				found=False
				for z in ignoreList:
					if z in str(y.filename).lower():
						found=True
				if found==False:
					addDirList.append([ip,shareName,"\\"+y.filename])
					getShares(ip,shareName,"\\"+y.filename)
		else:
			shareName1=shareName.replace("//","/")
			fullPath = ip+"/"+shareName1+"/"+y.filename
			fullPath = fullPath.replace("///","/")
			fullPath = fullPath.replace("//","/")
			fullPath = "//"+fullPath
			print fullPath
			allFilesList.append([ip,shareName1,fullPath])
			for format in formatList:
				if format in str(y.filename).lower():
					docList.append(["docs",ip,fullPath])
			fileMatch(ip,fullPath)
Example #16
0
    def smb(self):
        userID = self.args.user[0]
        password = '******'
        client_machine_name = 'pentesting'
        server_name = 'winxpSP3'
        domain_name = 'localhost'
        server_ip = self.args.ip
        conn = SMBConnection(userID,
                             password,
                             client_machine_name,
                             server_name,
                             domain=domain_name,
                             use_ntlm_v2=True,
                             is_direct_tcp=True)
        conn.connect(server_ip, 445)
        shares = conn.listShares()

        for share in shares:
            if not share.isSpecial and share.name not in [
                    'NETLOGON', 'SYSVOL'
            ]:
                sharedfiles = conn.listPath(share.name, '/')
                for sharedfile in sharedfiles:
                    print(sharedfile.filename)

        conn.close()
        print("You are now in the smb method")
Example #17
0
def smb_conn_sync():
    share_folder = 'myshare'
    user_name = 'mytest'
    pwd = 'mytest'
    server_name = 'Legion'
    server_ip = '192.168.0.10'

    local_dir = '/home/jetson/xavier_page/target_folder/'

    #SMBConnection parameters: <username, pwd, client_name, server_name, use_ntlm_v2 = True>
    conn = SMBConnection(user_name,
                         pwd,
                         'jetson-desktop',
                         server_name,
                         use_ntlm_v2=True)
    assert conn.connect(
        server_ip, 139)  # sharing server ip address, port 45 over http/ftp

    filelist = conn.listPath(share_folder, '/')

    f_names = []
    index = 1
    for i in range(len(filelist)):
        if filelist[i].filename.endswith('.jpg'):
            filename = filelist[i].filename
            f_names.append(filename)
            with open(local_dir + filename, 'wb') as fp:
                #print(index, '.....copy:', filename)
                index += 1
                conn.retrieveFile(share_folder, '/' + filename, fp)
    print('Files copied from share to /home/jetson/xavier_page/target_folder')
Example #18
0
def getShares(ip,shareName,folderPath):
	conn = SMBConnection('guest', '', client_machine_name, remote_machine_name, use_ntlm_v2 = True)        
	conn.connect(ip, 445)
	filelist = conn.listPath(shareName, folderPath)
	for y in filelist:
		if y.isDirectory:
			if y.filename!="." and y.filename!="..":
				found=False
				for z in ignoreList:
					if z in str(y.filename).lower():
						found=True
				if found==False:
					getShares(ip,shareName,"\\"+folderPath+"\\"+y.filename)
		else:
			folderPath1=folderPath.replace("\\","/")
			folderPath1=folderPath1.replace("//","/")
			shareName1=shareName.replace("//","/")
			fullPath = ip+"/"+shareName1+folderPath1+"/"+y.filename
			fullPath = fullPath.replace("///","/")
			fullPath = fullPath.replace("//","/")
			fullPath = "//"+fullPath
			print fullPath
			allFilesList.append([ip,shareName1,fullPath])

			for format in formatList:
				if format in str(y.filename).lower():
					docList.append(["docs",ip,fullPath])
			fileMatch(ip,fullPath)
Example #19
0
def iter_shared_files(conn: SMBConnection,
                      hostname: str,
                      share_name: str,
                      pattern: List[str],
                      subdir: str = "") -> Generator[File, None, None]:

    for shared_file in conn.listPath(share_name, subdir):
        if shared_file.filename in (".", ".."):
            continue

        relative_path = f"{subdir}{shared_file.filename}"
        absolute_path = f"\\\\{hostname}\\{share_name}\\{relative_path}"

        if not fnmatch(shared_file.filename, pattern[0]):
            continue

        if shared_file.isDirectory and len(pattern) > 1:
            yield from iter_shared_files(conn,
                                         hostname,
                                         share_name,
                                         pattern[1:],
                                         subdir=f"{relative_path}\\")
            continue

        if not shared_file.isDirectory and len(pattern) == 1:
            yield File(absolute_path, shared_file)
Example #20
0
def run(username,
        password,
        ip,
        hostname='',
        Pathurl='',
        filename='',
        outfile=''):
    try:
        print Pathurl
        if '\\\\' in Pathurl:
            sharename = str(Pathurl.split('\\\\')[0])
            #print sharename
            path = str(Pathurl.split('\\\\')[1])
            #print path
        else:
            sharename = ''
            path = ''
        if hostname == '':
            hostname = ip2hostname(ip)
        if '\\' in username:
            domain = username.split('\\')[0]
            username = username.split('\\')[1]
        else:
            domain = ''
        conn = SMBConnection(username, password, '', hostname, domain)
        try:
            conn.connect(ip, 139)
        except Exception, e:
            print e
            return
        result = ''
        if sharename == '':
            if conn.auth_result:
                print 'Login Success to %s!' % hostname
                shares = conn.listShares()
                for share in shares:
                    result = result + share.name + '\r\n'
            else:
                print 'Login Fail to %s!' % hostname
        elif filename == '':
            if conn.auth_result:
                try:
                    files = conn.listPath(sharename, path)
                    result = result + 'share dir:' + sharename + '/' + path + '\r\n'
                    for f in files:
                        type = ('<DIR>' if f.isDirectory else '<File>')
                        result = result + str(
                            time.strftime(
                                "%Y-%m-%d %H:%M:%S",
                                time.localtime(f.create_time))) + '    ' + str(
                                    time.strftime(
                                        "%Y-%m-%d %H:%M:%S",
                                        time.localtime(f.last_write_time))
                                ) + '    ' + type + ' ' + str(
                                    f.file_size) + '  ' + f.filename + '\r\n'
                except Exception, e:
                    print e
            else:
                print 'Login Fail to %s!' % hostname
Example #21
0
def show_dir(path):
    conn = SMBConnection(USERNAME, PASSWORD, MY_NAME, REMOTE_NAME, use_ntlm_v2=False)
    conn.connect(SERVER_IP, PORT)
    re = conn.listPath('Share',  os.path.join('/ESAP/Hand-Out/', path))
    conn.close()
    for i in re:
        i.link = os.path.join(path, i.filename)
    return render_template('hello.html', files=re)
	def run(self):
		try:
			conn = SMBConnection(MiSeqServerData.username, MiSeqServerData.password, MiSeqServerData.myRequestIdentifier, MiSeqServerData.serverName, domain=MiSeqServerData.domain, use_ntlm_v2 = True)
			conn.connect(MiSeqServerData.host, MiSeqServerData.port)

			#Get files names of fastq.gz files that correspond to subLibraryID (e.g. one if single, two if paired-end read) 
			print("Reading fastq.gz file for "+self.subLibraryID)
			fastqFiles = []
			try:
				sharedFileObjs = conn.listPath(MiSeqServerData.sharedFolder, '/MiSeqOutput/'+self.mainLibraryFolder+'/Data/Intensities/BaseCalls')
				for a in sharedFileObjs:
					#If fastq.gz file...
					if (a.filename.endswith("fastq.gz")):
						#And correct sample ID
						if (a.filename.startswith(self.subLibraryID) or a.filename.startswith(self.subLibraryID.replace("_", "-"))): #For some reason, MiSeq sampleSheet.csv will escape hyphens
							fastqFiles.append(a.filename)
							#Now fetch and write fastq.gz files to local machine
							director = urllib.request.build_opener(SMBHandler)
							fh = director.open('smb://'+MiSeqServerData.username+':'+MiSeqServerData.password+'@secret.jbei.org/miseq/MiSeqOutput/'+self.mainLibraryFolder+'/Data/Intensities/BaseCalls/'+a.filename).read()
							f = open(self.outputFolder+"/"+a.filename, 'wb')
							f.write(fh)
							f.close()
			except SMBTimeout:
				print("SMB server timed out")
				return
			except NotReadyError:
				print("Authentication with SMB server failed")
				return
			except NotConnectedError:
				print("Disconnected from SMB server")
				return
			except Exception as ex:
				print("Error retrieving fastq.gz files "+str(ex))
				return

			print("Writing metadata for "+self.subLibraryID)
			for filename in fastqFiles:
				#Get metadata for project's pre.libraries.info file
				proposalID = self.subLibraryID
				libraryName = "libName"
				genus = "genus"
				species = "species"
				strain = "strain"
				metaData = [proposalID, libraryName, self.outputFolder+"/"+filename, genus, species, strain]
				#Save metadata to be later printed to libraries.info file
				self.metadata += ("\t").join(metaData)+"\n";
		except SMBTimeout:
			print("SMB server timed out")
			return
		except NotReadyError:
			print("Authentication with SMB server failed")
			return
		except NotConnectedError:
			print("Disconnected from SMB server")
			return
		except Exception as ex:
			print("Error retrieving libraries from SMB server "+str(ex))	
			return
Example #23
0
def test_smb(host, password):
    print("Testing SMB")
    try:
        conn = SMBConnection("smbtemp", password, "test", "INTCORP1")
        conn.connect(host, 445)
        files = conn.listPath('Shared', '/')
        print("test_smb Success")
    except Exception as e:
        pass
def test_install(vm):
    s = winrm.Session('localhost:5995', ('vagrant', 'vagrant'), read_timeout_sec=180, operation_timeout_sec=120)

    logging.info("uploading installer.msi")
    c = SMBConnection('vagrant', 'vagrant', 'PYTHON', 'vagrant-10', is_direct_tcp=True)
    c.connect('127.0.0.1', port=8445)
    with open('installer/installer.msi', 'rb') as f:
        c.storeFile('Desktop', 'installer.msi', f)
    c.close()
    logging.info("uploaded installer msi")
    s.run_cmd('start', ['/wait', r'msiexec.exe', '/i', r'C:\Users\vagrant\Desktop\installer.msi', '/qn', '/l*v', r'C:\msi.log'])

    logging.info("check if appric folder is installed")
    c = SMBConnection('vagrant', 'vagrant', 'PYTHON', 'vagrant-10', is_direct_tcp=True)
    c.connect('127.0.0.1', port=8445)
    c.listPath('C$', r'Program Files (x86)\Mobivity\AppRIC')
    c.close()
    logging.info("AppRIC is installed!")
Example #25
0
def dotransform(args):
    mt = MaltegoTransform()
    # mt.debug(pprint(args))
    mt.parseArguments(args)
    ip = mt.getVar("ip")
    port = mt.getVar("port")
    hostid = mt.getVar("hostid")
    server = mt.getVar("server")
    workgroup = mt.getVar("workgroup")
    account = mt.getVar("account_used")
    path = mt.getVar("path")
    domaindns = mt.getVar("domain_dns")
    sharename = mt.getVar("sharename")

    conn = SMBConnection('',
                         '',
                         "localhost",
                         server,
                         domain=workgroup,
                         use_ntlm_v2=True,
                         is_direct_tcp=True)
    conn.connect(ip, int(port))
    regex = re.compile("^\.{1,2}$")
    for file in conn.listPath(sharename, path):
        filename = unicodedata.normalize("NFKD", file.filename).encode(
            'ascii', 'ignore')
        if file.isDirectory:
            if not regex.match(filename):
                entityname = "msploitego.SambaShare"
                newpath = "{}/{}".format(path, filename)
            else:
                continue
        else:
            entityname = "msploitego.SambaFile"
            newpath = "{}/{}".format(path, filename)
        sambaentity = mt.addEntity(entityname,
                                   "{}/{}{}".format(ip, sharename, newpath))
        sambaentity.setValue("{}/{}{}".format(ip, sharename, newpath))
        sambaentity.addAdditionalFields("ip", "IP Address", False, ip)
        sambaentity.addAdditionalFields("port", "Port", False, port)
        sambaentity.addAdditionalFields("server", "Server", False, server)
        sambaentity.addAdditionalFields("workgroup", "Workgroup", False,
                                        workgroup)
        sambaentity.addAdditionalFields("filename", "Filename", False,
                                        filename)
        sambaentity.addAdditionalFields("path", "Path", False, newpath)
        sambaentity.addAdditionalFields("hostid", "Hostid", False, hostid)
        sambaentity.addAdditionalFields("domain_dns", "Domain DNS", False,
                                        domaindns)
        sambaentity.addAdditionalFields("sharename", "Share Name", False,
                                        sharename)
    conn.close()
    mt.returnOutput()
    mt.addUIMessage("completed!")
Example #26
0
def get_child_dirs(conn: SMBConnection, share_name: str,
                   parent_dir: str) -> Generator[str, None, None]:
    yield parent_dir

    for shared_file in conn.listPath(share_name, parent_dir):
        if shared_file.filename in (".", ".."):
            continue

        if shared_file.isDirectory:
            relative_path = f"{parent_dir}{shared_file.filename}"
            yield from get_child_dirs(conn, share_name, f"{relative_path}\\")
Example #27
0
def test(server, service, event):
    #raise NotImplementedError();
    session = Session()
    se = tables.ScoreEvent()
    se.serviceid = service.id
    se.teamserverid = server.id
    se.scoretime = datetime.now()
    se.eventid = event

    try:
        conf = ScoringEngine.utils.getServiceConfig(session, service, server)
        if 'passdb' not in conf or 'share' not in conf or 'path' not in conf or 'file' not in conf or 'remote_name' not in conf:
            logger.warning("No configuration for service %i", service.id)
            session.close()
            return
        user = ScoringEngine.utils.getRandomUser(session, conf['passdb'])
        conn = SMBConnection(user['user'].encode('ascii', 'ignore'),
                             user['pass'].encode('ascii', 'ignore'),
                             'LepusISE'.encode('ascii', 'ignore'),
                             conf['remote_name'].encode('ascii', 'ignore'),
                             user['domain'].encode('ascii', 'ignore'),
                             is_direct_tcp=service.port == 445)
        if conn.connect(server.getIP().encode('ascii', 'ignore'),
                        service.port):
            if 'regex' not in conf or conf['regex'].strip() == "":
                files = conn.listPath(conf['share'], conf['path'])
                for file in files:
                    if file.filename == conf['file']:
                        se.up = True
                        se.info = json.dumps([file.filename for file in files])
                        break
            else:
                import re
                import tempfile
                file_obj = tempfile.NamedTemporaryFile()
                file_attr, filesize = conn.retrieveFile(
                    conf['share'], conf['path'] + "/" + conf['file'], file_obj)
                file_obj.seek(0)
                content = file_obj.read()
                file_obj.close()
                if re.search(conf['regex'], content) is None:
                    se.up = False
                else:
                    se.up = True
        else:
            se.up = False
        conn.close()
    except Exception as e:
        se.up = False
        se.info = e.message
    session.add(se)
    session.commit()
    session.close()
Example #28
0
def getFileList():
    filename_list = []
    conn = SMBConnection(smb_credential['user'], smb_credential['password'], "", smb_credential['hostname'], use_ntlm_v2 = True)
    conn.connect(smb_credential['host'], 139)

    file_list = conn.listPath(short_group_name, u'/Audit/每周会议记录/%s-%s年%s学期会议记录' % (short_group_name, cur_year, cur_semester))
    for f in file_list:
        if f.filename != '.' and f.filename != '..':
            filename_list.append(f.filename)

    conn.close()
    return filename_list
Example #29
0
def getFileList():
    filename_list = []
    conn = SMBConnection(smb_credential['user'], smb_credential['password'], "", smb_credential['hostname'], use_ntlm_v2 = True)
    conn.connect(smb_credential['host'], 139)

    file_list = conn.listPath('OSV', u'/Audit/每周会议记录/OSVT-16年秋季学期会议记录')
    for f in file_list:
        if f.filename != '.' and f.filename != '..':
            filename_list.append(f.filename)

    conn.close()
    return filename_list
Example #30
0
def dotransform(args):
    mt = MaltegoTransform()
    # mt.debug(pprint(args))
    mt.parseArguments(args)
    ip = mt.getVar("ip")
    port = mt.getVar("port")
    hostid = mt.getVar("hostid")
    server = mt.getVar("server")
    if not server:
        server = mt.getVar("machinename")
    workgroup = mt.getVar("workgroup")
    path = mt.getVar("path")
    domaindns = mt.getVar("domain_dns")
    sharename = mt.getVar("sharename")

    if not workgroup:
        workgroup = "WORKGROUP"
    # conn = SMBConnection('', '', "localhost", server, domain=workgroup, use_ntlm_v2=True,is_direct_tcp=True)
    conn = SMBConnection('', '', "localhost", server, domain=workgroup, use_ntlm_v2=True)
    conn.connect(ip, int(port))
    regex = re.compile("^\.{1,2}$")
    try:
        files = conn.listPath(sharename, path)
    except NotReadyError:
        accessdenied = mt.addEntity("msploitego.AccessDenied",sharename)
        accessdenied.setValue(sharename)
    else:
        for file in files:
            filename = unicodedata.normalize("NFKD", file.filename).encode('ascii', 'ignore')
            if file.isDirectory:
                if not regex.match(filename):
                    entityname = "msploitego.SambaShare"
                    newpath = "{}/{}".format(path,filename)
                else:
                    continue
            else:
                entityname = "msploitego.SambaFile"
                newpath = "{}/{}".format(path, filename)
            sambaentity = mt.addEntity(entityname,"{}/{}{}".format(ip,sharename,newpath))
            sambaentity.setValue("{}/{}{}".format(ip,sharename,newpath))
            sambaentity.addAdditionalFields("ip", "IP Address", False, ip)
            sambaentity.addAdditionalFields("port", "Port", False, port)
            sambaentity.addAdditionalFields("server", "Server", False, server)
            sambaentity.addAdditionalFields("workgroup", "Workgroup", False, workgroup)
            sambaentity.addAdditionalFields("filename", "Filename", False, filename)
            sambaentity.addAdditionalFields("path", "Path", False, newpath)
            sambaentity.addAdditionalFields("hostid", "Hostid", False, hostid)
            if domaindns:
                sambaentity.addAdditionalFields("domain_dns", "Domain DNS", False, domaindns)
            sambaentity.addAdditionalFields("sharename", "Share Name", False, sharename)
    conn.close()
    mt.returnOutput()
    mt.addUIMessage("completed!")
Example #31
0
def check_smb(ip, port, anon, share, checkfile, filehash, domain):
    if not domain:
        domain = ""

    conn = SMBConnection("Guest",
                         "",
                         socket.gethostname(),
                         "none",
                         domain,
                         is_direct_tcp=True)
    try:
        conn.connect(ip, port)
    except Exception as e:
        return 0, "Failed to connect to ip: %s on port %s." % (ip, port)

    try:
        shareList = conn.listShares(timeout=5)
        found = False
        for i in range(len(shareList)):
            if shareList[i].name == share:
                found = True
                break

        if found == False:
            return 0, "Samba share %s not found." % (share)
    except Exception as e:
        return 0, "Failed to retrieve list of shares."

    if checkfile and filehash:
        print("[DEBUG-SMB] Grabbing file for", ip)
        tmpfile = path + "engine/tmpfiles/smb-%s-%s-%s-checkfile" % (share, ip,
                                                                     checkfile)
        try:
            with open(tmpfile, "wb") as fp:
                conn.retrieveFile(share, checkfile, fp)

            with open(tmpfile, "rb") as f:
                content = f.read()
                checkhash = hashlib.sha1(content).hexdigest()
            if checkhash != filehash:
                return (0,
                        "Hash %s does not match %s." % (checkhash, filehash))
            return 1, None
        except Exception as e:
            return 0, "Failed to read share %s." % (share)
    else:
        print("[DEBUG-SMB] Trying anonymous/noauth for", ip)
        try:
            fileList = conn.listPath(share, "/")
            return 1, None
        except Exception as e:
            return 0, "Failed to read share %s." % (share)
Example #32
0
def dotransform(args):
    mt = MaltegoTransform()
    # mt.debug(pprint(args))
    mt.parseArguments(args)
    ip = mt.getVar("ip")
    port = mt.getVar("port")
    hostid = mt.getVar("hostid")
    server = mt.getVar("server")
    if not server:
        server = mt.getVar("machinename")
    workgroup = mt.getVar("workgroup")
    path = mt.getVar("path")
    domaindns = mt.getVar("domain_dns")
    sharename = mt.getVar("sharename")

    if not workgroup:
        workgroup = "WORKGROUP"
    # conn = SMBConnection('', '', "localhost", server, domain=workgroup, use_ntlm_v2=True,is_direct_tcp=True)
    conn = SMBConnection('', '', "localhost", server, domain=workgroup, use_ntlm_v2=True)
    conn.connect(ip, int(port))
    regex = re.compile("^\.{1,2}$")
    try:
        files = conn.listPath(sharename, path)
    except NotReadyError:
        accessdenied = mt.addEntity("msploitego.AccessDenied",sharename)
        accessdenied.setValue(sharename)
    else:
        for f in files:
            filename = checkAndConvertToAscii(f.filename)
            if f.isDirectory:
                if not regex.match(filename):
                    entityname = "msploitego.SambaShare"
                    newpath = "{}/{}".format(path,filename)
                else:
                    continue
            else:
                entityname = "msploitego.SambaFile"
                newpath = "{}/{}".format(path, filename)
            sambaentity = mt.addEntity(entityname,"{}/{}{}".format(ip,sharename,newpath))
            sambaentity.setValue("{}/{}{}".format(ip,sharename,newpath))
            sambaentity.addAdditionalFields("ip", "IP Address", False, ip)
            sambaentity.addAdditionalFields("port", "Port", False, port)
            sambaentity.addAdditionalFields("server", "Server", False, server)
            sambaentity.addAdditionalFields("workgroup", "Workgroup", False, workgroup)
            sambaentity.addAdditionalFields("filename", "Filename", False, filename)
            sambaentity.addAdditionalFields("path", "Path", False, newpath)
            sambaentity.addAdditionalFields("hostid", "Hostid", False, hostid)
            if domaindns:
                sambaentity.addAdditionalFields("domain_dns", "Domain DNS", False, domaindns)
            sambaentity.addAdditionalFields("sharename", "Share Name", False, sharename)
    conn.close()
    mt.returnOutput()
Example #33
0
def get_production_backup2(path, list_filename):
    main, samba = set_samba_settings()
    conn = SMBConnection(samba['smb_username'], samba['smb_password'], os.environ['COMPUTERNAME'], getBIOSName(samba['smb_server']), use_ntlm_v2 = True)
    conn.connect(samba['smb_server'], 139)
    prod_files = filter(lambda x: x[7:17] == 'MAXIMOTEST' and x[18:25] != 'SOURCES' and x[18:24] != 'DB_OBJ', list_filename)
    file_info = {}
    for f in prod_files:
        # check directory exist on remote server
        rem_path = ('maximo/'+f[18:-len(os.path.basename(f))-1]).split('/')
        ch_path = ''
        dir_exist = True
        for item in rem_path:
            # print 'Item: '+item
            testdir = conn.listPath('c$', ch_path)
            if item == 'maximo':
                ch_path = ch_path+delimiter()+item
                continue
            if item in [x.filename for x in testdir]:
                pass
            else:
                dir_exist = False
                break
            ch_path = ch_path+delimiter()+item
        if dir_exist is False:
            continue
        # print f
        testfiles = conn.listPath('c$', 'maximo/'+f[18:-len(os.path.basename(f))])  # observe target directory
        if os.path.basename(f) in [x.filename for x in testfiles]:                  # check file exist on remote directory
            if not os.path.exists(path+delimiter()+f[:-len(os.path.basename(f))].replace('/', '\\')):
                os.makedirs(path+delimiter()+f[:-len(os.path.basename(f))].replace('/', '\\'))  # create backup dir
            with open(path+delimiter()+f.replace('/', '\\'), 'wb') as local_file:
                file_attributes, filesize = conn.retrieveFile('c$',
                                                                  delimiter()+'maximo'+delimiter()+f[18:].replace('/', '\\'),
                                                                  local_file)
            file_info[f] = []
            file_info[f].append(filesize)
            file_info[f].append(md5(open(path+delimiter()+f.replace('/', '\\'), 'rb').read()).hexdigest())
    conn.close()
    return file_info
Example #34
0
    def searchTarget(self, host, username, password, domainname):
        success = False

        try:
            self.display.debug('### Analyzing system: ' + system_name)
            # parameterize an smb connection with a system
            conn = SMBConnection(
                username,
                password,
                'enumerator',
                host,
                domainname,
                use_ntlm_v2=True,
                sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,
                is_direct_tcp=True)

            # establish the actual connection
            connected = conn.connect(system_name, 445)
            success = True

            try:
                Response = conn.listShares(
                    timeout=30)  # obtain a list of shares
                self.display.debug('Shares on: ' + system_name)
                for i in range(
                        len(Response)):  # iterate through the list of shares
                    self.display.debug("  Share[", i, "] =", Response[i].name)
                    try:
                        # list the files on each share (recursivity?)
                        Response2 = conn.listPath(Response[i].name,
                                                  '/',
                                                  timeout=30)
                        self.display.debug(
                            '    Files on: ' + system_name + '/' + "  Share[",
                            i, "] =", Response[i].name)
                        for i in range(len(Response2)):
                            for pattern in self.filepatterns:
                                match_list = fnmatch.filter(
                                    Response2[i].filename, pattern)
                                for fname in match_list:
                                    # host.download(fpath, self.config["proofsDir"] + ip + fpath.replace("/", "_"))
                                    self.display.debug("    File[", i, "] =",
                                                       Response2[i].filename)
                    except:
                        self.display.error('### can not access the resource')
            except:
                self.display.error('### can not list shares')
        except:
            self.display.error('### can not access the system')

        return success
Example #35
0
def main(argv):
    parser = OptionParser()
    parser.add_option("-u", "--username", 
            help="Username that will be used for authentication")
    parser.add_option("-p", "--password", 
            help="Password that will be used for authentication")
    parser.add_option("-f", "--file", dest="filename",
                              help="Read systems list from file")
    parser.add_option("-d", "--domain", 
            help="Domain name that will be used for authentication")
    (options, args) = parser.parse_args()

    with open(options.filename) as f:
        for system_name in f:
            try:
                print('### Analyzing system: ' + system_name)
                # parameterize an smb connection with a system
                conn = SMBConnection(options.username,
                    options.password,
                    'enumerator',
                    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:
                    print('### can not list shares')

            except:
                print('### can not access the system')
Example #36
0
def dotransform(args):
    mt = MaltegoTransform()
    mt.debug(pprint(args))
    mt.parseArguments(args)
    ip = mt.getVar("ip")
    port = mt.getVar("port")
    hostid = mt.getVar("hostid")
    server = mt.getVar("server")
    workgroup = mt.getVar("workgroup")
    account = mt.getVar("account_used")
    path = mt.getVar("sambapath")
    domaindns = mt.getVar("domain_dns")

    if not path:
        path = "/"
    conn = SMBConnection('admin', 'admin', "localhost", server, domain=workgroup, use_ntlm_v2=True,
                         is_direct_tcp=True)
    conn.connect(ip, int(port))
    shares = conn.listShares()
    regex = re.compile("^\.{1,2}$")
    for share in shares:
        if not share.isSpecial and share.name not in ['NETLOGON', 'SYSVOL']:
            sharename = unicodedata.normalize("NFKD", share.name).encode('ascii', 'ignore')
            for file in conn.listPath(share.name, path):
                filename = unicodedata.normalize("NFKD", file.filename).encode('ascii', 'ignore')
                if file.isDirectory:
                    if not regex.match(filename):
                        entityname = "msploitego.SambaShare"
                        newpath = "{}/{}/".format(path,filename)
                    else:
                        continue
                        # subpath = conn.listPath(share.name, '/{}'.format(filename))
                else:
                    entityname = "msploitego.SambaFile"
                    newpath = "{}/{}".format(path, filename)
                sambaentity = mt.addEntity(entityname,"{}/{}/{}".format(ip,sharename,filename))
                sambaentity.setValue("{}/{}/{}".format(ip,sharename,filename))
                sambaentity.addAdditionalFields("ip", "IP Address", False, ip)
                sambaentity.addAdditionalFields("port", "Port", False, port)
                sambaentity.addAdditionalFields("server", "Server", False, server)
                sambaentity.addAdditionalFields("workgroup", "Workgroup", False, workgroup)
                sambaentity.addAdditionalFields("filename", "Filename", False, filename)
                sambaentity.addAdditionalFields("path", "Path", False, newpath)
                sambaentity.addAdditionalFields("hostid", "Hostid", False, hostid)
                sambaentity.addAdditionalFields("domain_dns", "Domain DNS", False, domaindns)
                sambaentity.addAdditionalFields("sharename", "Share Name", False, sharename)

    mt.returnOutput()
    mt.addUIMessage("completed!")
Example #37
0
def getFileList(current_user, folder_name):
    userID = current_user.name
    password = current_user.password
    service_name = folder_name
    file_list = []
    try:
        conn = SMBConnection(userID,
                             password,
                             cifs_host_name,
                             cifs_server_name,
                             use_ntlm_v2=True)
        conn.connect(cifs_server_ip, 139)
        res = conn.listPath(service_name, '/', timeout=30)
        for i in range(len(res)):
            filename = res[i].filename
            if filename in [".", ".."]:
                pass
            else:
                file_size = res[i].file_size
                fil_size = file_size / 1024
                filesize = f'{fil_size:.2f}'
                last_write_time = (datetime.datetime.fromtimestamp(
                    res[i].last_write_time)).strftime('%Y-%m-%d %H:%M:%S')
                create_time = (datetime.datetime.fromtimestamp(
                    res[i].create_time)).strftime('%Y-%m-%d %H:%M:%S')
                if res[i].file_attributes == 32:
                    file_type = "file"
                elif res[i].file_attributes == 16:
                    file_type = "DIR"
                else:
                    file_type = "Other"
                file_list.append({
                    'file_name': filename,
                    'size': filesize,
                    'last_write_time': last_write_time,
                    'create_time': create_time,
                    'file_type': file_type,
                    'service_name': service_name,
                    'path': ''
                })
    except Exception as e:
        exception_type, exception_object, exception_traceback = sys.exc_info()
        filename = exception_traceback.tb_frame.f_code.co_filename
        line_number = exception_traceback.tb_lineno
        print("Exception type: ", exception_type, "\n File name: ", filename,
              "\n Line number: ", line_number)
        print("Exception: ", e)
    # print(file_list)
    return file_list
Example #38
0
 def refresh(self):
     conn = SMBConnection(self.username.get(),
                          self.password.get(),
                          self.my_name,
                          self.domain_name,
                          use_ntlm_v2=True,
                          is_direct_tcp=True)
     conn.connect(self.remote_smb_IP.get(), int(self.port.get()))
     flist = conn.listPath(service_name=self.dir,
                           path=self.display_path,
                           pattern='*')
     ml.delete(0, END)
     for i in flist:
         # print(i.filename)
         ml.insert(END, i.filename)
Example #39
0
class SMBUtils:
    """
        访问 smb共享的通用功能
    """
    def __init__(self, host, username, password, remote_name, local_name=None):
        local_name = local_name if local_name else ''
        self.conn = SMBConnection(username,
                                  password,
                                  local_name,
                                  remote_name,
                                  is_direct_tcp=True)
        self.conn.connect(host, 445)

    def list_files(self, service_name, share_path, pattern="*"):
        """
            获得共享目录下的文件
        """
        result = []
        filenames = self.conn.listPath(service_name,
                                       share_path,
                                       pattern=pattern)

        for filename in filenames:
            file_name, is_dir = filename.filename, filename.isDirectory
            if not is_dir:
                result.append(file_name)

        return result

    def download_file(self, shared_dir, shared_file_path, local_data_dir,
                      local_file_name):
        """
            下载 windows共享目录 shared_dir下的 shared_file_path 文件到本地 local_data_dir 目录下,
            保存为 local_file_name
        """
        local_file_path = path_join(local_data_dir, local_file_name)

        with open(local_file_path, 'wb') as f:
            self.conn.retrieveFile(shared_dir, shared_file_path, f)

    def delete_files(self, shared_dir, path_file_pattern):
        """
            删除远程数据
        """
        self.conn.deleteFiles(shared_dir, path_file_pattern)

    def __del__(self):
        self.conn.close()
Example #40
0
 def list_content(self, service_name, path):
     samba = SMBConnection(self.username, self.password, '', '')
     samba.connect(self.ip, self.port)
     try:
         content = 'type\tname\n----\t----\n'
         for files in samba.listPath(service_name, path)[2:]:
             if samba.getAttributes(service_name, path.rstrip('/') + '/' + files.filename).isDirectory:
                 content += ('d\t%s\n' % files.filename)
             else:
                 content += ('f\t%s\n' % files.filename)
         else:
             return content[:-1]
     except Exception:
         print('retrieve content failure! ')
     finally:
         samba.close()
Example #41
0
 def run(self):
    print "Starting thread for " + self.ip
    net = NetBIOS()
    net_name = str(net.queryIPForName(self.ip)).strip("['").strip("']")
    net.close()
    conn = SMBConnection(self.user, self.pwd, 'cobwebs', net_name, domain=self.domain, use_ntlm_v2 = False)
    if conn.connect(self.ip, port=139, timeout=10):
       print ("Connecting to %s was successful! How about a nice game of spidering %s%s?" % (self.ip, self.share, self.subfolder))
    else:
       print ("Connection error: %s" % (self.ip))
    if self.recursive > 0:
       recurse(conn,self.ip,self.share,self.subfolder,self.pattern,int(self.recursive))    
    else:
       filelist = conn.listPath(self.share, self.subfolder)
       dir_list(filelist,self.ip,self.subfolder,self.pattern)
    conn.close()
    print "Exiting thread for " + self.ip
Example #42
0
def return_mainlibraries():
	mainLibraryNames = []
	query = request.form['query']
	try:
		conn = SMBConnection(username, password, myRequestIdentifier, serverName, domain=domain, use_ntlm_v2 = True)
		conn.connect(host, port)

		mainLibraryFolders = conn.listPath(sharedFolder, '/MiSeqOutput')
		for f in mainLibraryFolders:
			#Ignore .DS_Store
			folderName = f.filename
			if folderName.startswith("."):
				continue
			if query.lower() in folderName.lower():
				mainLibraryNames.append(idtext(folderName, folderName))
	except Exception as ex:
		return jsonify(result=str(ex))
	return jsonify(result=[e.serialize() for e in mainLibraryNames])
Example #43
0
def main():
    parser = argparse.ArgumentParser('check_smbproxy')
    parser.add_argument('server_ip', metavar='server-ip')
    parser.add_argument('server_port', metavar='server-port', type=int)
    parser.add_argument('server_name', metavar='server-name')
    parser.add_argument('share_name', metavar='share-name')
    parser.add_argument('--path', default='/')
    parser.add_argument('--user', default='cluster_user')
    parser.add_argument('--password', default='cluster_user_password')


    parsed_args = parser.parse_args()

    userID = parsed_args.user
    password = parsed_args.password
    client_machine_name = 'test_client'

    server_ip = parsed_args.server_ip
    server_port = parsed_args.server_port
    server_name = parsed_args.server_name
    share_name = parsed_args.share_name
    path = parsed_args.path

    try:
        start_time = datetime.datetime.utcnow()
        conn = SMBConnection(userID, password, client_machine_name, server_name, use_ntlm_v2=False, is_direct_tcp=True)
        assert conn.connect(server_ip, server_port)

        ls = conn.listPath(share_name, path)
        num_files = len(ls)
#        for f in ls:
#            print f.filename

        conn.close()
        end_time = datetime.datetime.utcnow()
        time_spent = (end_time-start_time).total_seconds()*1000

        print "OK: %d files found in %s | connection_time=%dms" % (num_files, path, time_spent)
    except Exception:
        print "CRITICAL: Exception while trying to connect:"
        print traceback.print_exc()
        sys.exit(2)

    sys.exit(0)
Example #44
0
    def searchTarget(self, host, username, password, domainname):
        success = False

        try:
            self.display.debug('### Analyzing system: ' + system_name)
            # parameterize an smb connection with a system
            conn = SMBConnection(username,
                                 password,
                                 'enumerator',
                                 host,
                                 domainname,
                                 use_ntlm_v2=True,
                                 sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,
                                 is_direct_tcp=True)

            # establish the actual connection
            connected = conn.connect(system_name, 445)
            success = True

            try:
                Response = conn.listShares(timeout=30)  # obtain a list of shares
                self.display.debug('Shares on: ' + system_name)
                for i in range(len(Response)):  # iterate through the list of shares
                    self.display.debug("  Share[", i, "] =", Response[i].name)
                    try:
                        # list the files on each share (recursivity?)
                        Response2 = conn.listPath(Response[i].name, '/', timeout=30)
                        self.display.debug('    Files on: ' + system_name + '/' + "  Share[", i, "] =",
                                           Response[i].name)
                        for i in range(len(Response2)):
                            for pattern in self.filepatterns:
                                match_list = fnmatch.filter(Response2[i].filename, pattern)
                                for fname in match_list:
                                    # host.download(fpath, self.config["proofsDir"] + ip + fpath.replace("/", "_"))
                                    self.display.debug("    File[", i, "] =", Response2[i].filename)
                    except:
                        self.display.error('### can not access the resource')
            except:
                self.display.error('### can not list shares')
        except:
            self.display.error('### can not access the system')

        return success
Example #45
0
	def createList(self, ipList, outfile):
		with open(outfile, 'a') as out:
			for ip in ipList:
				out.write("\n----------------------------------------------------\n")
				print "Attempting to access: ", ip
				out.write("Attempting to access: %s \n" % ip)
				try:
					conn = SMBConnection('guest', '', client_machine_name, remote_machine_name, use_ntlm_v2 = True)
					conn.connect(ip, 139)
					print "Connected to: ", ip
					out.write("Connected To: %s \n" % ip)
				except:
					print "Failed to Connect"
					out.write("Failed to Connect To: %s \n" % ip)
					pass
				try:
					shareList = conn.listShares()
				except:
					out.write("Failed to open Shares\n")
					shareList = None
				if shareList != None:
					for x in shareList:
						try:
							out.write("found Share: %s \n" % x.name)
							print "Listing files in share: ", x.name
							out.write("Listing files in share: %s \n" % x.name)
							filelist = conn.listPath(x.name, '/')
							for y in filelist:
								if y.isDirectory:
									print "DIR", y.filename
								out.write("-----")
								out.write(y.filename)
								out.write('\n')
						except:
							print "failed to open share: ", x.name
							out.write("Failed to open Share: %s \n" % x.name)

		print "report written to outfile.txt"
Example #46
0
def look_for_new_dumps():
    # Try to access CRADMZShare using Samba from here
    # and see if there are any dump files we have not seen before.
    try:
        logger.info("Starting to look for new dumps...")
        creds = settings.CRA_SAMBA_CREDENTIALS
        conn = SMBConnection(
            username=creds['username'],
            password=creds['password'],
            my_name=creds['client_machine_name'],
            remote_name=creds['server_name'],
            use_ntlm_v2=True
        )
        assert conn.connect(creds['server_ip'], 139)

        new_files = []
        files = conn.listPath('share', '/')
        for file in files:
            if not file.isDirectory:
                dumpfile, created = DumpFile.objects.get_or_create(
                    filename=file.filename
                )
                if created:
                    new_files.append(file.filename)
                    logger.info("Found a new dump %s" % file.filename)
                else:
                    logger.info("File already known: %s" % file.filename)
        if new_files:
            logger.info("Sending email about new files")
            mail_admins(
                "New dump files spotted",
                "New files: %s" % ', '.join(new_files),
            )
        else:
            logger.info("No new files")
    except:
        logger.exception("Something went wrong while checking for new dump files")
Example #47
0
    def handle_JobMessage(self, msg):
        try:
            self.errors = ""

            def set_status(s):
                self.status = s
                self.log.info(s)

            set_status("Received job")
            sim = msg.get_property("msg")

            user_id = sim["user_id"]
            sim_id = sim["sim_id"]
            job_id = sim["job_id"]
            sample = sim["sample"]
            jar_hash = sim["jar_hash"]

            set_status("Starting job %d %d %s %d" % (job_id, sim_id, user_id, sample))
            self.send_status("Starting job", job_id, sim_id, user_id, sample)

            out_name = os.path.join("results", self.worker_id)
            jar_name = os.path.join("jars", "%s_%s.run" % (jar_hash, self.worker_id))
            xml_name = os.path.join("xmls", "%s_%i_%i_%i.xml" % (user_id, job_id, sim_id, sample))

            set_status("Writing input files")
            self.send_status("Writing input files", job_id, sim_id, user_id, sample)

            xml = construct_xml(sim, out_name)
            if not xml:
                self.errors = "Error constructing XML (check idrefs?)"
                set_status(self.errors)
                raise Exception(self.errors)

            with open(xml_name, "w") as xml_file:
                xml_file.write(xml)

            if not os.path.exists(jar_name):
                with open(jar_name, "wb") as jar_file:
                    jar_file.write(get_file(jar_hash))

            process = Popen(["java", "-server", "-Xmx2400M", "-jar", jar_name, xml_name], stdout=PIPE, stderr=PIPE)
            p_timer = time.time()

            # Non-blocking process io: http://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
            def enqueue_output(stream, queue, running):
                while running.value:
                    queue.put(stream.read(128))

            def create_thread_queue(stream, running):
                q = Queue()
                t = Thread(target=enqueue_output, args=(stream, q, running))
                t.daemon = True
                t.start()
                return q

            running = Value("b", True)
            out_q = create_thread_queue(process.stdout, running)
            err_q = create_thread_queue(process.stderr, running)

            set_status("Execution started")
            while process.poll() == None:
                try:
                    status = out_q.get(timeout=0.1)

                    if time.time() - p_timer > 0.3:
                        s = re.findall("\(.*?\)", status)[-1]
                        self.send_status(s, job_id, sim_id, user_id, sample)

                        p_timer = time.time()
                except:
                    pass

            # Stop the queue threads
            running.value = False

            # Get the error if it exists, only needed here because thread is constantly checking the pipe
            while not err_q.empty():
                self.errors += err_q.get(False)

            os.remove(xml_name)
            set_status("Execution finished")

            if self.errors:
                set_status(self.errors)
                raise Exception("CIlib error")

            set_status("Posting results")
            with open(out_name, "r") as result_file:
                conn = SMBConnection(SMB_USER, SMB_PWD, "", "", use_ntlm_v2=True)
                assert conn.connect(SMB_IP, timeout=SMB_TIMEOUT)

                newFile = "%s_%i_%i_%i.txt" % (user_id, job_id, sim_id, sample)
                existingFiles = [i.filename for i in conn.listPath(SMB_SHARE, "/")]

                if newFile in existingFiles:
                    conn.deleteFiles(SMB_SHARE, newFile, timeout=SMB_TIMEOUT)

                conn.storeFile(SMB_SHARE, newFile, result_file, timeout=SMB_TIMEOUT)
                conn.close()

            os.remove(out_name)

            set_status("Result notification")
            self.connect()
            self.mgr.broadcast_message(
                ResultMessage(
                    msg={
                        "job_id": job_id,
                        "sim_id": sim_id,
                        "user_id": user_id,
                        "sample": sample,
                        "jar_hash": jar_hash,
                        "worker_id": self.worker_id,
                    }
                )
            )

            set_status("Status update")
            self.send_status("Done", job_id, sim_id, user_id, sample)
            set_status("Job Done")
        except error:
            self.log.info("con error")
            self.connect()
            self.send_job_request()
            # TODO: and then? did that ^ fix it?

        except Exception, e:
            try:
                self.log.exception("Worker job error")
                self.log.exception(self.status)
                self.connect()
                self.mgr.broadcast_message(
                    JobErrorMessage(
                        msg={
                            "job_id": job_id,
                            "sim_id": sim_id,
                            "user_id": user_id,
                            "sample": sample,
                            "jar_hash": jar_hash,
                            "error": self.status.encode("zlib").encode("base64"),
                            "replenish": not self.errors,
                            "worker_id": self.worker_id,
                        }
                    )
                )

                set_status("Sending error progress thingy")
                self.send_status("Error", job_id, sim_id, user_id, sample)
            except:
                self.log.info("Sending error error")
                self.connect()
Example #48
0
class FetchFile(FetchDataBase):
	_localFolder = ''
	_serverName = ''
	_rootNode = ''
	_subPath = ''
	_conn = None
	_filename = ''
	#_folderList = []
	#_fileList = []
	def __init__(self, addr, uid, upwd):
		#super(FetchFile, self).__init__(addr, uid, upwd)
		# print 'addr:', addr
		try:
			# FetchDataBase.__init__(self, r'//CNST50066074/Root/Business_One/Projects/Dev/SDK/KnowledgeWarehouse/_Ebook/java/m2ebook-pdf.pdf', uid.lower(), upwd)
			FetchDataBase.__init__(self, str(addr), uid.lower(), upwd)
			self._genPath()
		except Exception as e:
			self._responseCode = 430
			print 'init error....'
			raise e

	def _genPath(self):
		rootPath = self._addr
		self._localFolder = self._addr
		#skip first domain path, ie "\\servername\path\
		# print 'self._addr:', self._addr
		if self._addr.startswith(PLACEHOLDER_START):
			if len(self._addr) <= 2:
				raise Exception('not support"', self._addr, '"root path')
			else:
				rootPath = self._addr[2:]

			#get servername
			index = rootPath.find(PLACEHOLDER)
			if index > 0:
				self._serverName = rootPath[0:index]
				rootPath = rootPath[index+1:]
			else:
				#ie \\servername\
				self._serverName = rootPath
				rootPath = ''
		else:
			raise Exception('invalid net work path')

		#local temp path
		self._localFolder = base64.b64encode(self._addr)

		#find root path, ie path\
		index = rootPath.find(PLACEHOLDER)
		if index > 0:
			self._rootNode = rootPath[0:index]
			self._subPath = rootPath[index:]
		else:
			self._rootNode = rootPath
			self._subPath = ''
			#raise Exception('invalid path ')
		#print 'rootNode', self._rootNode
		#print 'subpath', self._subPath

	def _connToServer(self):
		try:
			#print 'id:', self._uid
			# print 'servername', self._serverName
			self._conn = SMBConnection(self._uid, self._upwd, '', self._serverName) #use_ntlm_v2 = True
			if not self._conn.connect(self._serverName): #port = 139
				raise Exception('connect failure:', self._serverName)
		except Exception as e:
			print str(e)

	def _connClose(self):
		self._conn.close()

	FOLDERLIST = 'folderlist'
	FILELIST = 'filelist'
	def _getFileList(self, subFolder = ''):
		if subFolder == '':
			subFolder = self._subPath
		print 'rootnode:', self._rootNode
		print 'subFolder:', subFolder
		list = self._conn.listPath(self._rootNode, subFolder)
		folderList = []
		fileList = []
		for f in list:
			if f.filename in ['.','..','Thumbs.db']:
				continue
			if f.isDirectory:
				folderList.append(f.filename)
			else:
				fileList.append(f.filename)
		folderList.append('')
		#print folderList, fileList
		return {self.FOLDERLIST:folderList, self.FILELIST:fileList}

	def _getLastFilePath(self, path):
		index = path.rfind(PLACEHOLDER)
		#print 'getlastfilepath index:', index
		if index < 0:
			return {'path':'', 'file':path}
			#raise Exception('not found last path')
		if index + 1 == len(path):
			#print 'index', index
			#print 'len(path):', len(path), path
			path = path[0:index]
			return self._getLastFilePath(path)
		else:
			return {'path':path[0:index+1], 'file':path[index+1:]}


	#TYPE_FOLDER = 'folder'
	#TYPE_FILE = 'file'
	# folder or file
	def _getFileType(self):
		#get path
		dpath = self._getLastFilePath(self._subPath)
		path = dpath['path']
		file = dpath['file']
		#print 'path', path
		#print 'file', file
		self._filename = file
		#
		dlist = self._getFileList(path)
		folderList = dlist[self.FOLDERLIST]
		fileList = dlist[self.FILELIST]

		if file.decode('utf-8') in folderList:
			self._resourceType = TYPE_FOLDER
			return TYPE_FOLDER
		if file.decode('utf-8') in fileList:
			self._resourceType = TYPE_FILE
			return TYPE_FILE
		return None

	#if folder return filelist, if file return filepath.
	def _getContent(self):
		type = self._getFileType()
		#print 'content type', type
		if type == TYPE_FOLDER:
			dfiles = self._getFileList()
			#print 'dfiles', dfiles
			#dfiles['type'] = TYPE_FOLDER
			files = []
			files.extend(dfiles[self.FOLDERLIST])
			files.extend(dfiles[self.FILELIST])
			return TYPE_FOLDER, str(files)
		elif type == TYPE_FILE:
			# return {'filepath':self._retrieveFile()}
			tempResult = self._retrieveFile()
			# return TYPE_FILE, self._retrieveFile()
			return TYPE_FILE, tempResult
		else:
			return None, None

	def _retrieveFile(self):
		tempPath = SF_TEMPPATH
		self.mkdir(tempPath)
		import os
		filename =  self._addr.replace('/', '_')
		tempPath = os.path.realpath(tempPath + filename)
		import tempfile
		with open(tempPath, 'wb+') as fo:
			#fp = tempfile.NamedTemporaryFile()
			fa, filesize = self._conn.retrieveFile(self._rootNode, self._subPath, fo)
			#print 'filesize', filesize
			#for line in fp:
			#	fo.write(line)
			#fp.close()
			fo.close()
		return tempPath

	def _detectFileType(self, fileType):
		from const_file_type import GLOBAL_FILE_TYPE
		if fileType == TYPE_FOLDER:
			return GLOBAL_FILE_TYPE.SF
		elif fileType == TYPE_FILE:
			if self._addr.lower().endswith('.pdf'):
				return GLOBAL_FILE_TYPE.PDF
			elif self._addr.lower().endswith('.doc'):
				return  GLOBAL_FILE_TYPE.DOC
			elif self._addr.lower().endswith('.docx'):
				return GLOBAL_FILE_TYPE.DOCX
			elif self._addr.lower().endswith('.ppt'):
				return GLOBAL_FILE_TYPE.PPT
		return GLOBAL_FILE_TYPE.OTHERS

	def fetchData(self):
		try:
			self._connToServer()
			fileType, content = self._getContent()
			#print 'content', content
			self._connClose()
			if content == None:
				self._responseCode = 430
				e = Exception()
				e.code = 430
				e.message = "Wrong url, can not fetch url"
				raise e
			self._responseCode = 200
			return content, self._addr, self._detectFileType(fileType)
		except Exception as e:
			self._responseCode = 430
			e.code = 430
			raise e


	#using mount to open share folder, not working at now.
	def _openShareFolder(self):
		#//10.58.0.100/Root/Business_One/Projects/Dev/SDK/KnowledgeWarehouse/_Ebook/Effective STL.pdf
		tempPath = "../temp/"
		tempPath += self._localFolder
		self.mkdir(tempPath)

		#print 'absolute path'
		tempPath = os.path.realpath(tempPath)
		print tempPath

		command = r"sudo mount.cifs " + self._addr + " " + tempPath + "/ -o user="******",password="
		print command
		try:
			os.system(command + self._upwd)
		except Exception, e:
			print e
			raise e
class CommonCIFSShare(object):
    """
    Handle CIFS shares
    """

    def __init__(self):
        self.smb_conn = None

    def com_cifs_connect(self, ip_addr, user_name='guest', user_password=''):
        """
        Connect to share
        """
        server_name = 'Server'
        client_name = 'My Computer'
        self.smb_conn = SMBConnection(user_name, user_password, client_name, server_name,
                                      use_ntlm_v2=True)
        self.smb_conn.connect(ip_addr, 139)

    def com_cifs_share_list_by_connection(self):
        """
        List shares
        """
        share_names = []
        for row_data in self.smb_conn.listShares():
            share_names.append(row_data.name)
        return share_names

    def com_cifs_share_file_list_by_share(self, share_name, path_text='/'):
        """
        List files in share
        """
        file_names = []
        for row_data in self.smb_conn.listPath(share_name, path_text):
            common_global.es_inst.com_elastic_index('info', {'stuff': row_data.filename})
            file_names.append(row_data.filename)
        return file_names

    def com_cifs_share_directory_check(self, share_name, dir_path):
        """
        Verify smb directory
        """
        # try due to fact invalid file/path freaks out the connection
        try:
            return self.smb_conn.getAttributes(share_name, dir_path).isDirectory
        except:
            pass
        return False

    def com_cifs_share_file_dir_info(self, share_name, file_path):
        """
        Get specific path/file info
        """
        return self.smb_conn.getAttributes(share_name, file_path)

    def com_cifs_share_file_upload(self, file_path):
        """
        Upload file to smb
        """
        self.smb_conn.storeFile(os.path.join(
            self.sharename, file_path), open(file_path, 'rb'))

    def com_cifs_share_file_download(self, file_path):
        """
        Download from smb
        """
        self.smb_conn.retrieveFile(self.sharename, open(file_path, 'wb'))

    def com_cifs_share_file_delete(self, share_name, file_path):
        """
        Delete from smb
        """
        self.smb_conn.deleteFiles(os.path.join(share_name, file_path))

    def com_cifs_close(self):
        """
        Close connection
        """
        self.smb_conn.close()

    def com_cifs_walk(self, share_name, file_path='/'):
        """
        cifs directory walk
        """
        dirs, nondirs = [], []
        for name in self.smb_conn.listPath(share_name, file_path):
            if name.isDirectory:
                if name.filename not in ['.', '..']:
                    dirs.append(name.filename)
            else:
                nondirs.append(name.filename)
        yield file_path, dirs, nondirs
        for name in dirs:
            #           new_path = file_path + '\\' + name
            #            for ndx in self.com_cifs_walk(share_name, new_path):
            for ndx in self.com_cifs_walk(share_name, os.path.join(file_path, name)):
                yield ndx
Example #50
0
class SMB_client():
	def __init__(self,username=None,password=None,smb_name=None,print_errors=True):
		self.username     = username
		self.password     = password
		self.smb_name     = smb_name
		self.print_errors = print_errors
		self.smb_ip       = None
		self.conn         = None
		self.service_name = None
		self.my_name      = None
		self.error        = None
		self.tree         = []

	def getBIOSName(self, remote_smb_ip, timeout=5):			# unused if dynamic IP
		# ip -> smb name
		try:
			self.error = None
			bios = NetBIOS()
			srv_name = bios.queryIPForName(remote_smb_ip, timeout=timeout)
			return srv_name[0]
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
			return None
			
	def getIP(self):
		# smb name -> ip
		try:
			self.error = None
			bios = NetBIOS()
			ip = bios.queryName(self.smb_name)
			return ip[0]
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
			return None
			
	def connect(self):
		try:
			self.error = None
			self.my_name = gethostname()				# iDevice name
			self.smb_ip = self.getIP()
			smb_structs.SUPPORT_SMB2 = True
			self.conn = SMBConnection(self.username, self.password, self.my_name, self.smb_name, use_ntlm_v2 = True)
			self.conn.connect(self.smb_ip, 139)		#139=NetBIOS / 445=TCP
			if self.conn:
				shares = self.conn.listShares()
				for share in shares:
					if share.type == 0:		# 0 = DISK_TREE
						self.service_name = share.name  
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
			
	def close(self):
		try:
			self.error = None
			self.conn.close()
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
 
	def getRemoteDir(self, path, pattern):
		try:
			self.error = None
			files = self.conn.listPath(self.service_name, path, pattern=pattern)
			return files
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
			return None
				
	def getRemoteTree(self,path=''):
		try:
			self.error = None
			if path == '':
				w = ''
			else:
				w = path+'/'
			files = self.getRemoteDir(path, '*')
			if files:
				for file in files:
					if file.filename[0] == '.':
						continue
					self.tree.append({'name':w+file.filename, 'isdir':file.isDirectory, 'size':file.file_size})
					if file.isDirectory:
						self.getRemoteTree(path=w+file.filename)
			return self.tree
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
			return None

	def download(self, path, filename,buffersize=None,callback=None, local_path=None):
		try:
			self.error = None
			#print('Download = ' + path + filename)
			attr = self.conn.getAttributes(self.service_name, path+filename)
			#print('Size = %.1f kB' % (attr.file_size / 1024.0))
			#print('start download')
			file_obj = BytesIO()
			if local_path:
				fw = open(local_path+filename, 'wb')
			else:
				fw = open(filename, 'wb')
			offset = 0
			transmit =0
			while True:
				if not buffersize:
					file_attributes, filesize = self.conn.retrieveFile(self.service_name, path+filename, file_obj)
				else:
					file_attributes, filesize = self.conn.retrieveFileFromOffset(self.service_name, path+filename, file_obj,offset=offset,max_length=buffersize)
					if callback:
						transmit = transmit + filesize
						callback(transmit)
				file_obj.seek(offset)
				for line in file_obj:
					fw.write(line)
				offset = offset + filesize
				if (not buffersize) or (filesize == 0):
					break
			fw.close()
			#print('download finished')
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
			
	def upload(self, path, filename,buffersize=None,callback=None, local_path=None):
		try:
			self.error = None
			#print('Upload = ' + path + filename)
			#print('Size = %.1f kB' % (os.path.getsize(filename) / 1024.0))
			#print('start upload')
			if local_path:
				file_obj = open(local_path+filename, 'rb')
			else:
				file_obj = open(filename, 'rb')
			offset = 0
			while True:
				if not buffersize:
					filesize = self.conn.storeFile(self.service_name, path+filename, file_obj)
					break
				else:	
					buffer_obj = file_obj.read(buffersize)			
					if buffer_obj:
						buffer_fileobj = BytesIO()
						buffer_fileobj.write(buffer_obj)
						buffer_fileobj.seek(0)
						offset_new = self.conn.storeFileFromOffset(self.service_name, path+filename, buffer_fileobj, offset=offset, truncate=False)
						#return the file position where the next byte will be written.
						offset = offset_new
						if callback:
							callback(offset)
					else:
						break
			file_obj.close()
			#print('upload finished')
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)

	def delete_remote_file(self,path, filename):
		try:
			self.error = None
			self.conn.deleteFiles(self.service_name, path+filename)
			#print('Remotefile ' + path + filename + ' deleted')
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)

	def createRemoteDir(self, path):
		try:
			self.error = None
			self.conn.createDirectory(self.service_name, path)
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
  
	def removeRemoteDir(self,path):
		try:
			self.error = None
			self.conn.deleteDirectory(self.service_name, path)
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)

	def renameRemoteFileOrDir(self,old_path, new_path):
		try:
			self.error = None
			self.conn.rename(self.service_name, old_path, new_path)
		except Exception as e:
			if self.print_errors:
				print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
			else:
				self.error = 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno) + str(type(e).__name__) + str(e)
Example #51
0
class ProtocolSMB(ILocation):
	
	"""
	Doku: http://packages.python.org/pysmb/api/smb_SMBConnection.html?highlight=listpath#smb.SMBConnection.SMBConnection.listPath
	"""
	
	_conn = None
	
	def __del__(self):
		self._conn.close()
		
	
	def connect(self, username, password, ip):
		""" remote connect smb """
		from smb.SMBConnection import SMBConnection
		
		self._conn = SMBConnection(username, password, my_name = "", remote_name = "", domain='', use_ntlm_v2=True)
		return self._conn.connect(ip = ip, timeout = 5)
		

	def listDir(self, path):
		m = re.match('/.+?/(.+?)/(.+)', path)
		share = m.group(1)
		folder = m.group(2)
		
		list = []
		for item in self._conn.listPath(share, folder):
			if item.isDirectory and item.filename[0] != '.':
				list.append( item.filename )

		return list
	
	def listFile(self, path):
		m = re.match('/.+?/(.+?)/(.+)', path)
		share = m.group(1)
		folder = m.group(2)
		#filter = m.group(3)
		
		list = []
		for item in self._conn.listPath(share, folder):
			if item.isDirectory == False:
				list.append( item.filename )

		return list
		
	
	def readFile(self, filename):
		m = re.match('/.+?/(.*?)/(.*)', filename)
		share = m.group(1)
		folder = m.group(2)
		
		import tempfile
		
		tmpfile = tempfile.TemporaryFile()
		self._conn.retrieveFile(share, folder, tmpfile)
		
		tmpfile.seek(0)
		buffer = ""
		try:
			for line in tmpfile:
				buffer +=line
		finally:
			tmpfile.close()

		return buffer

	def generate_path(self, a, *p):
		chdir = a
		for path in p:
			chdir = chdir + '/' + path
			
		return chdir