def __init__( self, connection: GvmConnection, *, transform: Optional[Callable[[str], Any]] = None ): super().__init__(connection, transform=EtreeCheckCommandTransform()) self._gmp_transform = transform
def connect_gvm(username, password): """connect via TLS and create a target for gvm this assumes you have connected to gvmd through local ssh forward, if not configure hostname to match hostname or ip address and change the port if needed, this will use the default port 9390""" transform = EtreeCheckCommandTransform() connection = TLSConnection() connection.hostname = '127.0.0.1' connection.port = '9390' gmp = Gmp(connection=connection, transform=transform) gmp.authenticate(username, password) return gmp
def test_no_success_status_transform(self): transform = EtreeCheckCommandTransform() root = etree.Element('foo_response') root.set('status', '400') root.set('status_text', 'Foo error') response = etree.tostring(root).decode('utf-8') with self.assertRaises(GvmError): transform(response)
def test_no_success_status_transform(self): transform = EtreeCheckCommandTransform() root = etree.Element("foo_response") root.set("status", "400") root.set("status_text", "Foo error") response = etree.tostring(root).decode("utf-8") with self.assertRaises(GvmError): transform(response)
def __init__(self, password, socket_path='/var/run/gvmd.sock', user='******', timeout=10, loglevel=logging.ERROR): logging.basicConfig(level=loglevel) self.connection_errors = 0 self.container_tasks = {} self.password = password self.user = user self.socketconnection = UnixSocketConnection(path=socket_path, timeout=timeout) self.connection = DebugConnection(self.socketconnection) self.transform = EtreeCheckCommandTransform() self.gmp = Gmp(connection=self.connection, transform=self.transform) self.connect()
def test_success_response(self): transform = EtreeCheckCommandTransform() root = etree.Element('foo_response') root.set('status', '200') response = etree.tostring(root).decode('utf-8') result = transform(response) self.assertTrue(etree.iselement(result)) self.assertEqual(result.tag, 'foo_response') self.assertEqual(result.get('status'), '200')
def test_success_response(self): transform = EtreeCheckCommandTransform() root = etree.Element("foo_response") root.set("status", "200") response = etree.tostring(root).decode("utf-8") result = transform(response) self.assertTrue(etree.iselement(result)) self.assertEqual(result.tag, "foo_response") self.assertEqual(result.get("status"), "200")
def test_no_status_transform(self): transform = EtreeCheckCommandTransform() with self.assertRaises(GvmError): transform('<foo/')
def main(): do_not_run_as_root() parser = create_parser(description=HELP_TEXT, logfilename='gvm-pyshell.log') parser.add_protocol_argument() parser.add_argument( '-i', '--interactive', action='store_true', default=False, help='Start an interactive Python shell', ) parser.add_argument( 'scriptname', nargs='?', metavar="SCRIPT", help='Path to script to be preloaded (example: myscript.gmp.py)', ) parser.add_argument( 'scriptargs', nargs='*', metavar="ARG", help='Arguments for preloaded script', ) args = parser.parse_args() connection = create_connection(**vars(args)) transform = EtreeCheckCommandTransform() global_vars = { 'help': Help(), '__version__': __version__, '__api_version__': __api_version__, } username = None password = None if args.protocol == PROTOCOL_OSP: protocol_class = Osp name = 'osp' else: protocol_class = Gmp name = 'gmp' with protocol_class(connection, transform=transform) as protocol: global_vars[name] = protocol global_vars['__name__'] = '__{}__'.format(name) if args.protocol == PROTOCOL_GMP: if args.gmp_username: (username, password) = authenticate( protocol, username=args.gmp_username, password=args.gmp_password, ) shell_args = Namespace(username=username, password=password) global_vars['args'] = shell_args with_script = args.scriptname and len(args.scriptname) > 0 if with_script: argv = [os.path.abspath(args.scriptname), *args.scriptargs] shell_args.argv = argv # for backwards compatibility we add script here shell_args.script = argv no_script_no_interactive = not args.interactive and not with_script script_and_interactive = args.interactive and with_script only_interactive = not with_script and args.interactive only_script = not args.interactive and with_script if only_interactive or no_script_no_interactive: enter_interactive_mode(global_vars) if script_and_interactive or only_script: if only_script: print( 'Using gvm-pyshell for running scripts only is deprecated. ' 'Please use gvm-script instead', file=sys.stderr, ) script_name = args.scriptname run_script(script_name, global_vars) if not only_script: enter_interactive_mode(global_vars)
def main(): parser = create_parser(description=HELP_TEXT, logfilename='gvm-script.log') parser.add_protocol_argument() parser.add_argument( 'scriptname', metavar="SCRIPT", help='Path to script to be executed (example: myscript.gmp)', ) parser.add_argument('scriptargs', nargs='*', metavar="ARG", help='Arguments for the script') args = parser.parse_args() if 'socket' in args.connection_type and args.sockpath: print( 'The --sockpath parameter has been deprecated. Please use ' '--socketpath instead', file=sys.stderr, ) connection = create_connection(**vars(args)) transform = EtreeCheckCommandTransform() global_vars = { '__version__': __version__, '__api_version__': __api_version__, } username = None password = None if args.protocol == PROTOCOL_OSP: protocol = Osp(connection, transform=transform) global_vars['osp'] = protocol global_vars['__name__'] = '__osp__' else: protocol = Gmp(connection, transform=transform) global_vars['gmp'] = protocol global_vars['__name__'] = '__gmp__' if args.gmp_username: (username, password) = authenticate(protocol, username=args.gmp_username, password=args.gmp_password) argv = [os.path.abspath(args.scriptname), *args.scriptargs] shell_args = Namespace( username=username, password=password, argv=argv, # for backwards compatibility we add script here script=argv, ) global_vars['args'] = shell_args run_script(args.scriptname, global_vars)
def main(): parser = create_parser(description=HELP_TEXT, logfilename='gvm-pyshell.log') parser.add_protocol_argument() parser.add_argument('-i', '--interactive', action='store_true', default=False, help='Start an interactive Python shell') parser.add_argument( 'scriptname', nargs='?', metavar="SCRIPT", help='Path to script to be preloaded (example: myscript.gmp)') parser.add_argument('scriptargs', nargs='*', metavar="ARG", help='Arguments for preloaded script') args = parser.parse_args() if 'socket' in args.connection_type and args.sockpath: print( 'The --sockpath parameter has been deprecated. Please use ' '--socketpath instead', file=sys.stderr) connection = create_connection(**vars(args)) transform = EtreeCheckCommandTransform() global_vars = { 'help': Help(), '__version__': __version__, '__api_version__': __api_version__, } username = None password = None if args.protocol == PROTOCOL_OSP: protocol = Osp(connection, transform=transform) global_vars['osp'] = protocol global_vars['__name__'] = '__osp__' else: protocol = Gmp(connection, transform=transform) global_vars['gmp'] = protocol global_vars['__name__'] = '__gmp__' if args.gmp_username: (username, password) = authenticate(protocol, username=args.gmp_username, password=args.gmp_password) shell_args = Arguments(username=username, password=password) global_vars['args'] = shell_args with_script = args.scriptname and len(args.scriptname) > 0 if with_script: argv = [os.path.abspath(args.scriptname), *args.scriptargs] shell_args.argv = argv # for backwards compatibility we add script here shell_args.script = argv no_script_no_interactive = not args.interactive and not with_script script_and_interactive = args.interactive and with_script only_interactive = not with_script and args.interactive only_script = not args.interactive and with_script if only_interactive or no_script_no_interactive: enter_interactive_mode(global_vars) if script_and_interactive or only_script: script_name = args.scriptname load(script_name, global_vars) if not only_script: enter_interactive_mode(global_vars) protocol.disconnect()
def __init__(self, socket_path='/var/run/openvasmd.sock', username='******', password='******'): self.username = username self.password = password connection = UnixSocketConnection(path=socket_path) transform = EtreeCheckCommandTransform() self.gmp = Gmp(connection=connection, transform=transform)
def main(): do_not_run_as_root() parser = create_parser(description=HELP_TEXT, logfilename='gvm-script.log') parser.add_protocol_argument() parser.add_argument( 'scriptname', metavar="SCRIPT", help='Path to script to be executed (example: myscript.gmp.py)', ) parser.add_argument('scriptargs', nargs='*', metavar="ARG", help='Arguments for the script') args, script_args = parser.parse_known_args() connection = create_connection(**vars(args)) transform = EtreeCheckCommandTransform() global_vars = { '__version__': __version__, '__api_version__': __api_version__, } username = None password = None if args.protocol == PROTOCOL_OSP: protocol_class = Osp name = 'osp' else: protocol_class = Gmp name = 'gmp' try: with protocol_class(connection, transform=transform) as protocol: global_vars[name] = protocol global_vars['__name__'] = f'__{name}__' if args.protocol == PROTOCOL_GMP: if args.gmp_username: (username, password) = authenticate( protocol, username=args.gmp_username, password=args.gmp_password, ) argv = [os.path.abspath(args.scriptname), *args.scriptargs] shell_args = Namespace( username=username, password=password, argv=argv, # for backwards compatibility we add script here script=argv, # the unknown args, which are owned by the script. script_args=script_args, ) global_vars['args'] = shell_args run_script(args.scriptname, global_vars) except Exception as e: # pylint: disable=broad-except print(e, file=sys.stderr) sys.exit(1) sys.exit(0)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.settings = getattr(settings, 'SELENE', DEFAULT_SETTINGS) self.transform = EtreeCheckCommandTransform()
#!/usr/bin/env python3 import gvm from gvm.protocols.latest import Gmp from gvm.transforms import EtreeCheckCommandTransform from gvm.errors import GvmError connection = gvm.connections.TLSConnection(hostname='localhost') username = '******' password = '******' transform = EtreeCheckCommandTransform() try: with Gmp(connection=connection, transform=transform) as gmp: gmp.authenticate(username, password) users = gmp.get_users() tasks = gmp.get_tasks() targets = gmp.get_targets() scanners = gmp.get_scanners() configs = gmp.get_configs() feeds = gmp.get_feeds() nvts = gmp.get_nvts() print("Users\n------------") for user in users.xpath('user'): print(user.find('name').text) print("\nTasks\n------------") for task in tasks.xpath('task'): print(task.find('name').text)