Beispiel #1
0
    async def scan_host(self, atarget):
        try:
            tid, target = atarget
            #spneg = AuthenticatorBuilder.to_spnego_cred(self.credential, target)
            connection = self.smb_mgr.create_connection_newtarget(target)
            async with connection:
                await connection.login()

                extra_info = connection.get_extra_info()
                if extra_info is not None:
                    try:
                        f = SMBFinger.from_extra_info(tid, extra_info)
                        await self.out_q.put((tid, connection.target, f, None))
                    except:
                        traceback.print_exc()

                machine = SMBMachine(connection)

                if 'all' in self.gather or 'shares' in self.gather:
                    async for smbshare, err in machine.list_shares():
                        if err is not None:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to list shares. Reason: %s' %
                                 format_exc(err)))
                            continue
                        share = NetShare()
                        share.machine_sid = tid
                        share.ip = connection.target.get_ip()
                        share.netname = smbshare.name
                        share.type = smbshare.type
                        r = None
                        try:
                            r = smbshare.remark.decode()
                        except:
                            r = smbshare.remark
                        share.remark = r

                        await self.out_q.put(
                            (tid, connection.target, share, None))

                if 'all' in self.gather or 'sessions' in self.gather:
                    async for session, err in machine.list_sessions():
                        if err is not None:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to get sessions. Reason: %s' %
                                 format_exc(err)))
                            continue

                        sess = NetSession()
                        sess.machine_sid = tid
                        sess.source = connection.target.get_ip()
                        sess.ip = session.ip_addr.replace('\\', '').strip()
                        sess.username = session.username

                        await self.out_q.put(
                            (tid, connection.target, sess, None))

                if 'all' in self.gather or 'localgroups' in self.gather:
                    for group_name in self.localgroups:
                        async for domain_name, user_name, sid, err in machine.list_group_members(
                                'Builtin', group_name):
                            if err is not None:
                                await self.out_q.put((
                                    tid, connection.target, None,
                                    'Failed to connect to poll group memeberships. Reason: %s'
                                    % format_exc(err)))
                                continue

                            lg = LocalGroup()
                            lg.machine_sid = tid
                            lg.ip = connection.target.get_ip()
                            lg.hostname = connection.target.get_hostname()
                            lg.sid = sid
                            lg.groupname = group_name
                            lg.domain = domain_name
                            lg.username = user_name
                            await self.out_q.put(
                                (tid, connection.target, lg, None))

        except asyncio.CancelledError:
            return

        except Exception as e:
            await self.out_q.put(
                (tid, connection.target, None,
                 'Failed to connect to host. Reason: %s' % format_exc(e)))
            return

        finally:
            await self.out_q.put(
                (tid, connection.target, None, None))  #target finished
Beispiel #2
0
	async def scan_host(self, target):
		try:
			#spneg = AuthenticatorBuilder.to_spnego_cred(self.credential, target)
			connection = self.smb_mgr.create_connection_newtarget(target)
			async with connection:
				await connection.login()
				machine = SMBMachine(connection)

				if 'all' in self.gather or 'shares' in self.gather:
					async for smbshare, err in machine.list_shares():
						if err is not None:
							await self.out_q.coro_put((connection.target, None, 'Failed to list shares. Reason: %s' % format_exc(err)))
							continue
						share = NetShare()
						share.ip = connection.target.get_ip()
						share.netname = smbshare.name
						share.type = smbshare.type
						share.remark = smbshare.remark

						await self.out_q.coro_put((connection.target, share, None))
					
				
				if 'all' in self.gather or 'sessions' in self.gather:
					async for session, err in machine.list_sessions():
						if err is not None:
							await self.out_q.coro_put((connection.target, None, 'Failed to get sessions. Reason: %s' % format_exc(err)))
							continue

						sess = NetSession()
						sess.source = connection.target.get_ip()
						sess.ip = session.ip_addr.replace('\\','').strip()
						sess.username = session.username

						await self.out_q.coro_put((connection.target, sess, None))

				if 'all' in self.gather or 'localgroups' in self.gather:
					for group_name in self.localgroups:
						async for domain_name, user_name, sid, err in machine.list_group_members(domain_name, group_name):
							if err is not None:
								await self.out_q.coro_put((connection.target, None, 'Failed to connect to poll group memeberships. Reason: %s' % format_exc(err)))
								continue

							lg = LocalGroup()
							lg.ip = connection.target.get_ip()
							lg.hostname = connection.target.get_hostname()
							lg.sid = sid
							lg.groupname = group_name
							lg.domain = domain_name
							lg.username = user_name
							await self.out_q.coro_put((connection.target, lg, None))
		
		except Exception as e:
			await self.out_q.coro_put((connection.target, None, 'Failed to connect to host. Reason: %s' % format_exc(e)))
			return

		finally:
			await self.out_q.coro_put((connection.target, None, None)) #target finished
Beispiel #3
0
	async def enum_host(self, atarget):
		connection = None
		try:
			tid, target = atarget

			connection = self.smb_mgr.create_connection_newtarget(target)
			async with connection:
				_, err = await connection.login()
				if err is not None:
					raise err

				machine = SMBMachine(connection)
				maxerr = self.host_max_errors
				async for obj, otype, err in machine.enum_all_recursively(depth = self.depth, fetch_share_sd = self.fetch_share_sd, fetch_dir_sd = self.fetch_dir_sd, fetch_file_sd=self.fetch_file_sd, maxentries = self.max_entries):
					otype = otype.lower()
					if err is not None:
						await self.out_q.put((tid, connection.target, None, 'Failed to perform file enum. Reason: %s' % format_exc(err)))
						break
							
					else:
						try:
							if otype not in ['share', 'file', 'dir']:
								continue
							
							sf = SMBFile()
							sf.machine_sid = tid
							sf.unc = obj.unc_path
							sf.otype = otype

							if otype == 'share':
								continue
							
							if otype in ['file', 'dir']:
								sf.creation_time = obj.creation_time
								sf.last_access_time = obj.last_access_time
								sf.last_write_time = obj.last_write_time
								sf.change_time = obj.change_time
								if obj.security_descriptor is not None and obj.security_descriptor != '':
									sf.sddl = obj.security_descriptor.to_sddl()
								if otype == 'file':
									sf.size = obj.size
									sf.size_ext = sizeof_fmt(sf.size)
								await self.out_q.put((tid, connection.target, sf, None))
						except Exception as e:
							maxerr -= 1
							await self.out_q.put((tid, connection.target, None, 'Failed to format file result. Reason: %s' % format_exc(e)))
							if maxerr == 0:
								await self.out_q.put((tid, connection.target, None, 'File Results too many errors. Reason: %s' % format_exc(e)))
								break
				
		except asyncio.CancelledError:
			return

		except Exception as e:
			await self.out_q.put((tid, connection.target, None, 'Failed to connect to host. Reason: %s' % format_exc(e)))
			return

		finally:
			await self.out_q.put((tid, connection.target, None, None)) #target finished
Beispiel #4
0
    async def scan_host(self, atarget):
        try:
            tid, target = atarget

            try:
                if 'all' in self.gather or 'protocols' in self.gather:
                    for protocol in self.protocols:
                        connection = self.smb_mgr.create_connection_newtarget(
                            target)
                        res, _, _, _, err = await connection.protocol_test(
                            [protocol])
                        if err is not None:
                            raise err
                        if res is True:
                            pr = SMBProtocols()
                            pr.machine_sid = tid
                            pr.protocol = protocol.name if protocol != NegotiateDialects.WILDCARD else 'SMB1'
                            await self.out_q.put(
                                (tid, connection.target, pr, None))
            except Exception as e:
                await self.out_q.put(
                    (tid, connection.target, None,
                     'Failed to enumerate supported protocols. Reason: %s' %
                     format_exc(e)))

            connection = self.smb_mgr.create_connection_newtarget(target)
            async with connection:
                _, err = await connection.login()
                if err is not None:
                    raise err

                try:
                    extra_info = connection.get_extra_info()
                    if extra_info is not None:
                        f = SMBFinger.from_extra_info(tid, extra_info)
                        await self.out_q.put((tid, connection.target, f, None))
                except Exception as e:
                    await self.out_q.put(
                        (tid, connection.target, None,
                         'Failed to get finger data. Reason: %s' %
                         format_exc(e)))

                machine = SMBMachine(connection)

                if 'all' in self.gather or 'shares' in self.gather:
                    async for smbshare, err in machine.list_shares():
                        if err is not None:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to list shares. Reason: %s' %
                                 format_exc(err)))
                            break
                        else:
                            share = NetShare()
                            share.machine_sid = tid
                            share.ip = connection.target.get_ip()
                            share.netname = smbshare.name
                            share.type = smbshare.type
                            #share.remark = smbshare.remark
                            #if smbshare.remark is not None:
                            #	r = None
                            #	try:
                            #		r = smbshare.remark.decode('utf-16-le')
                            #	except:
                            #		try:
                            #			r = smbshare.remark.decode('latin-1')
                            #		except:
                            #			try:
                            #				r = smbshare.remark.decode('utf-8')
                            #			except:
                            #				r = smbshare.remark
                            #
                            #	if isinstance(r, str):
                            #		r = r.replace('\x00','')
                            #		share.remark = r

                            await self.out_q.put(
                                (tid, connection.target, share, None))

                if 'all' in self.gather or 'sessions' in self.gather:
                    async for session, err in machine.list_sessions():
                        if err is not None:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to get sessions. Reason: %s' %
                                 format_exc(err)))
                            break
                        else:
                            try:
                                sess = NetSession()
                                sess.machine_sid = tid
                                sess.source = connection.target.get_ip()
                                sess.ip = session.ip_addr.replace('\\',
                                                                  '').strip()
                                sess.username = session.username

                                await self.out_q.put(
                                    (tid, connection.target, sess, None))
                            except Exception as e:
                                await self.out_q.put(
                                    (tid, connection.target, None,
                                     'Failed to format session. Reason: %s' %
                                     format_exc(e)))

                if 'all' in self.gather or 'localgroups' in self.gather:
                    for group_name in self.localgroups:
                        async for domain_name, user_name, sid, err in machine.list_group_members(
                                'Builtin', group_name):
                            if err is not None:
                                await self.out_q.put((
                                    tid, connection.target, None,
                                    'Failed to connect to poll group memeberships. Reason: %s'
                                    % format_exc(err)))
                                break
                            else:
                                lg = LocalGroup()
                                lg.machine_sid = tid
                                lg.ip = connection.target.get_ip()
                                lg.hostname = connection.target.get_hostname()
                                lg.sid = sid
                                lg.groupname = group_name
                                lg.domain = domain_name
                                lg.username = user_name
                                await self.out_q.put(
                                    (tid, connection.target, lg, None))

        except asyncio.CancelledError:
            return

        except Exception as e:
            await self.out_q.put(
                (tid, connection.target, None,
                 'Failed to connect to host. Reason: %s' % format_exc(e)))
            return

        finally:
            await self.out_q.put(
                (tid, connection.target, None, None))  #target finished
Beispiel #5
0
    async def scan_host(self, atarget):
        try:
            tid, target = atarget
            connection = self.smb_mgr.create_connection_newtarget(target)
            async with connection:
                _, err = await connection.login()
                if err is not None:
                    raise err

                machine = SMBMachine(connection)

                if 'all' in self.gather or 'shares' in self.gather:
                    async for smbshare, err in machine.list_shares():
                        if err is not None:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to list shares. Reason: %s' %
                                 format_exc(err)))
                            break
                        else:
                            share = NetShare()
                            share.machine_sid = tid
                            share.ip = connection.target.get_ip()
                            share.netname = smbshare.name
                            share.type = smbshare.type
                            await self.out_q.put(
                                (tid, connection.target, share, None))

                if 'all' in self.gather or 'sessions' in self.gather:
                    async for session, err in machine.list_sessions():
                        if err is not None:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to get sessions. Reason: %s' %
                                 format_exc(err)))
                            break
                        else:
                            try:
                                sess = NetSession()
                                sess.machine_sid = tid
                                sess.source = connection.target.get_ip()
                                sess.ip = session.ip_addr.replace('\\',
                                                                  '').strip()
                                sess.username = session.username

                                await self.out_q.put(
                                    (tid, connection.target, sess, None))
                            except Exception as e:
                                await self.out_q.put(
                                    (tid, connection.target, None,
                                     'Failed to format session. Reason: %s' %
                                     format_exc(e)))
                if 'all' in self.gather or 'localgroups' in self.gather:
                    for group_name in self.localgroups:
                        async for domain_name, user_name, sid, err in machine.list_group_members(
                                'Builtin', group_name):
                            if err is not None:
                                await self.out_q.put((
                                    tid, connection.target, None,
                                    'Failed to poll group memeberships. Reason: %s'
                                    % format_exc(err)))
                                break
                            else:
                                lg = LocalGroup()
                                lg.machine_sid = tid
                                lg.ip = connection.target.get_ip()
                                lg.hostname = connection.target.get_hostname()
                                lg.sid = sid
                                lg.groupname = group_name
                                lg.domain = domain_name
                                lg.username = user_name
                                await self.out_q.put(
                                    (tid, connection.target, lg, None))

                if 'all' in self.gather or 'regsessions' in self.gather:
                    users, err = await machine.reg_list_users()
                    if err is not None:
                        await self.out_q.put(
                            (tid, connection.target, None,
                             'Failed to get sessions. Reason: %s' %
                             format_exc(err)))

                    else:
                        try:
                            for usersid in users:
                                if usersid in self.regusers_filter:
                                    continue
                                if usersid.find('_') != -1:
                                    continue
                                sess = RegSession()
                                sess.machine_sid = tid
                                sess.user_sid = usersid

                                await self.out_q.put(
                                    (tid, connection.target, sess, None))
                        except Exception as e:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to format session. Reason: %s' %
                                 format_exc(e)))

                if 'all' in self.gather or 'interfaces' in self.gather:
                    interfaces, err = await machine.list_interfaces()
                    if err is not None:
                        await self.out_q.put(
                            (tid, connection.target, None,
                             'Failed to get interfaces. Reason: %s' %
                             format_exc(err)))

                    else:
                        try:
                            for interface in interfaces:
                                iface = SMBInterface()
                                iface.machine_sid = tid
                                iface.address = interface['address']

                                await self.out_q.put(
                                    (tid, connection.target, iface, None))
                        except Exception as e:
                            await self.out_q.put(
                                (tid, connection.target, None,
                                 'Failed to format interface. Reason: %s' %
                                 format_exc(e)))

                if 'all' in self.gather or 'share_1' in self.gather:
                    ctr = self.share_max_files
                    maxerr = 10
                    async for obj, otype, err in machine.enum_all_recursively(
                            depth=1, fetch_share_sd=False, fetch_dir_sd=True):
                        otype = otype.lower()
                        ctr -= 1
                        if ctr == 0:
                            break

                        if err is not None:
                            await self.out_q.put((
                                tid, connection.target, None,
                                'Failed to perform first-level file enum. Reason: %s'
                                % format_exc(err)))
                            break

                        else:
                            try:
                                if otype == 'share':
                                    continue
                                if otype in ['file', 'dir']:
                                    sf = SMBFile()
                                    sf.machine_sid = tid
                                    sf.unc = obj.unc_path
                                    sf.otype = otype
                                    sf.creation_time = obj.creation_time
                                    sf.last_access_time = obj.last_access_time
                                    sf.last_write_time = obj.last_write_time
                                    sf.change_time = obj.change_time
                                    if obj.security_descriptor is not None and obj.security_descriptor != '':
                                        sf.sddl = obj.security_descriptor.to_sddl(
                                        )
                                    if otype == 'file':
                                        sf.size = obj.size
                                        sf.size_ext = sizeof_fmt(sf.size)

                                    await self.out_q.put(
                                        (tid, connection.target, sf, None))
                            except Exception as e:
                                maxerr -= 1
                                await self.out_q.put(
                                    (tid, connection.target, None,
                                     'Failed to format file result. Reason: %s'
                                     % format_exc(e)))
                                if maxerr == 0:
                                    await self.out_q.put((
                                        tid, connection.target, None,
                                        'File Results too many errors. Reason: %s'
                                        % format_exc(e)))
                                    break

            try:
                if 'all' in self.gather or 'finger' in self.gather:
                    connection = self.smb_mgr.create_connection_newtarget(
                        target)
                    extra_info, err = await connection.fake_login()
                    if extra_info is not None:
                        f = SMBFinger.from_fake_login(tid,
                                                      extra_info.to_dict())
                        await self.out_q.put((tid, connection.target, f, None))
            except Exception as e:
                await self.out_q.put(
                    (tid, connection.target, None,
                     'Failed to get finger data. Reason: %s' % format_exc(e)))

            try:
                if 'all' in self.gather or 'protocols' in self.gather:
                    for protocol in self.protocols:
                        connection = self.smb_mgr.create_connection_newtarget(
                            target)
                        res, _, _, _, err = await connection.protocol_test(
                            [protocol])
                        if err is not None:
                            raise err
                        if res is True:
                            pr = SMBProtocols()
                            pr.machine_sid = tid
                            pr.protocol = protocol.name if protocol != NegotiateDialects.WILDCARD else 'SMB1'
                            await self.out_q.put(
                                (tid, connection.target, pr, None))
            except Exception as e:
                await self.out_q.put(
                    (tid, connection.target, None,
                     'Failed to enumerate supported protocols. Reason: %s' %
                     format_exc(e)))

        except asyncio.CancelledError:
            return

        except Exception as e:
            await self.out_q.put(
                (tid, connection.target, None,
                 'Failed to connect to host. Reason: %s' % format_exc(e)))
            return

        finally:
            await self.out_q.put(
                (tid, connection.target, None, None))  #target finished