class GmpCreateConfigTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_create_config(self): self.gmp.create_config('a1', 'foo') self.connection.send.has_been_called_with('<create_config>' '<copy>a1</copy>' '<name>foo</name>' '</create_config>') def test_missing_config_id(self): with self.assertRaises(RequiredArgument): self.gmp.create_config(config_id='', name='foo') with self.assertRaises(RequiredArgument): self.gmp.create_config(config_id=None, name='foo') def test_missing_name(self): with self.assertRaises(RequiredArgument): self.gmp.create_config(config_id='c1', name=None) with self.assertRaises(RequiredArgument): self.gmp.create_config(config_id='c1', name='')
class GmpHelpTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_help(self): self.gmp.help() self.connection.send.has_been_called_with('<help type=""/>') def test_invalid_help_type(self): with self.assertRaises(InvalidArgument): self.gmp.help(help_type='foo') def test_help_type_brief(self): self.gmp.help(help_type='brief') self.connection.send.has_been_called_with('<help type="brief"/>') def test_invalid_format(self): with self.assertRaises(InvalidArgument): self.gmp.help(format='foo') def test_html_format(self): self.gmp.help(format='html') self.connection.send.has_been_called_with( '<help type="" format="html"/>')
class GmpGetRolesTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_roles(self): self.gmp.get_roles() self.connection.send.has_been_called_with('<get_roles/>') def test_get_roles_with_filter(self): self.gmp.get_roles(filter='foo=bar') self.connection.send.has_been_called_with( '<get_roles filter="foo=bar"/>' ) def test_get_roles_with_filter_id(self): self.gmp.get_roles(filter_id='f1') self.connection.send.has_been_called_with('<get_roles filt_id="f1"/>') def test_get_roles_with_trash(self): self.gmp.get_roles(trash=True) self.connection.send.has_been_called_with('<get_roles trash="1"/>') self.gmp.get_roles(trash=False) self.connection.send.has_been_called_with('<get_roles trash="0"/>')
class GmpGetCredentialTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_credential_simple(self): self.gmp.get_credential('id') self.connection.send.has_been_called_with( '<get_credentials credential_id="id"/>') def test_fail_without_credential_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_credential(None) def test_fail_with_empty_credential_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_credential('') def test_get_credential_with_valid_format(self): self.gmp.get_credential('id', credential_format='key') self.connection.send.has_been_called_with( '<get_credentials credential_id="id" format="key"/>') def test_get_credential_with_invalid_format(self): with self.assertRaises(InvalidArgument): self.gmp.get_credential('id', credential_format='foo')
class GmpGetFeedsTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_feeds(self): self.gmp.get_feeds() self.connection.send.has_been_called_with('<get_feeds/>')
class GmpEmptyTrashcanCommandTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_empty_trashcan(self): self.gmp.empty_trashcan() self.connection.send.has_been_called_with('<empty_trashcan/>')
class GMPCreateFilterCommandTestCase(unittest.TestCase): FILTER_NAME = "special filter" def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_all_available_filters_types_correct(self): for filter_type in FILTER_TYPES: self.gmp.create_filter( name=self.FILTER_NAME, term='sort-reverse=threat first=1 rows=1000', filter_type=filter_type, ) self.connection.send.has_been_called_with( '<create_filter>' '<name>{0}</name>' '<term>sort-reverse=threat first=1 rows=1000</term>' '<type>{1}</type>' '</create_filter>'.format(self.FILTER_NAME, filter_type), ) def test_invalid_filters_type(self): with self.assertRaises(InvalidArgument): self.gmp.create_filter( name=self.FILTER_NAME, term='sort-reverse=threat result_hosts_only=1 ' 'notes=1 overrides=1 levels=hml first=1 rows=1000', filter_type='foo', ) def test_all_arguments(self): self.gmp.create_filter( name=self.FILTER_NAME, make_unique=True, term='sort-reverse=threat result_hosts_only=1 ' 'notes=1 overrides=1 levels=hml first=1 rows=1000', filter_type='task', comment='foo', ) self.connection.send.has_been_called_with( '<create_filter>' '<name>{0}<make_unique>1</make_unique></name>' '<comment>foo</comment>' '<term>sort-reverse=threat result_hosts_only=1 notes=1 ' 'overrides=1 levels=hml first=1 rows=1000</term>' '<type>task</type>' '</create_filter>'.format(self.FILTER_NAME), ) def test_missing_name(self): with self.assertRaises(RequiredArgument): self.gmp.create_filter('') with self.assertRaises(RequiredArgument): self.gmp.create_filter(None)
class GmpSyncScapCommandTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_sync_scap(self): self.gmp.sync_scap() self.connection.send.has_been_called_with('<sync_scap/>')
class GmpGetCredentialsTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_credentials_simple(self): self.gmp.get_credentials() self.connection.send.has_been_called_with('<get_credentials/>')
class GmpDescribeAuthCommandTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_describe_auth(self): self.gmp.describe_auth() self.connection.send.has_been_called_with('<describe_auth/>')
class GmpGetSettingTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_setting_simple(self): self.gmp.get_setting('id') self.connection.send.has_been_called_with( '<get_settings setting_id="id"/>')
class GmpGetVersionCommandTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_version(self): self.gmp.get_version() self.connection.connect.has_been_called() self.connection.read.has_been_called() self.connection.send.has_been_called() self.connection.send.has_been_called_with('<get_version/>')
class GmpDeleteAssetTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_delete_asset(self): self.gmp.delete_asset(asset_id='a1') self.connection.send.has_been_called_with( '<delete_asset asset_id="a1"/>') def test_delete_from_report(self): self.gmp.delete_asset(report_id='a1') self.connection.send.has_been_called_with( '<delete_asset report_id="a1"/>') def test_delete_passing_asset_and_report(self): self.gmp.delete_asset(report_id='r1', asset_id='a1') self.connection.send.has_been_called_with( '<delete_asset asset_id="a1"/>') def test_missing_arguments(self): with self.assertRaises(GvmError): self.gmp.delete_asset()
class GmpGetRoleTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_role(self): self.gmp.get_role('r1') self.connection.send.has_been_called_with( '<get_roles role_id="r1"/>' ) self.gmp.get_role(role_id='r1') self.connection.send.has_been_called_with( '<get_roles role_id="r1"/>' ) def test_get_role_missing_role_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_role(role_id=None) with self.assertRaises(RequiredArgument): self.gmp.get_role('')
class GmpCreateHostTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_missing_name(self): with self.assertRaises(RequiredArgument): self.gmp.create_host(name=None) with self.assertRaises(RequiredArgument): self.gmp.create_host(name='') def test_create_host_asset(self): self.gmp.create_host(name='ipsum') self.connection.send.has_been_called_with('<create_asset>' '<asset>' '<type>host</type>' '<name>ipsum</name>' '</asset>' '</create_asset>') def test_create_asset_with_comment(self): self.gmp.create_host(name='ipsum', comment='lorem') self.connection.send.has_been_called_with('<create_asset>' '<asset>' '<type>host</type>' '<name>ipsum</name>' '<comment>lorem</comment>' '</asset>' '</create_asset>')
class GmpGetTargetTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_user(self): self.gmp.get_user('u1') self.connection.send.has_been_called_with( '<get_users user_id="u1"/>' ) self.gmp.get_user(user_id='u1') self.connection.send.has_been_called_with( '<get_users user_id="u1"/>' ) def test_get_user_missing_user_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_user(user_id=None) with self.assertRaises(RequiredArgument): self.gmp.get_user('')
class GmpDeletePortListTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_delete(self): self.gmp.delete_port_list('a1') self.connection.send.has_been_called_with( '<delete_port_list port_list_id="a1" ultimate="0"/>' ) def test_delete_ultimate(self): self.gmp.delete_port_list('a1', ultimate=True) self.connection.send.has_been_called_with( '<delete_port_list port_list_id="a1" ultimate="1"/>' ) def test_missing_id(self): with self.assertRaises(GvmError): self.gmp.delete_port_list(None) with self.assertRaises(GvmError): self.gmp.delete_port_list('')
class GmpGetReportFormatTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_report_format(self): self.gmp.get_report_format('rf1') self.connection.send.has_been_called_with( '<get_report_formats report_format_id="rf1" details="1"/>' ) self.gmp.get_report_format(report_format_id='rf1') self.connection.send.has_been_called_with( '<get_report_formats report_format_id="rf1" details="1"/>' ) def test_get_report_format_missing_report_format_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_report_format(report_format_id=None) with self.assertRaises(RequiredArgument): self.gmp.get_report_format('')
class GMPCreateContainerTaskCommandTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_create_task(self): self.gmp.create_container_task(name='foo') self.connection.send.has_been_called_with('<create_task>' '<name>foo</name>' '<target id="0"/>' '</create_task>') def test_create_task_missing_name(self): with self.assertRaises(RequiredArgument): self.gmp.create_container_task(name=None) with self.assertRaises(RequiredArgument): self.gmp.create_container_task(name='') def test_create_task_with_comment(self): self.gmp.create_container_task(name='foo', comment='bar') self.connection.send.has_been_called_with('<create_task>' '<name>foo</name>' '<target id="0"/>' '<comment>bar</comment>' '</create_task>')
class GmpImportConfigTestCase(unittest.TestCase): CONFIG_XML_STRING = '<get_configs_response status="200" status_text="OK">' \ '<config id="c4aa21e4-23e6-4064-ae49-c0d425738a98">' \ '<name>Foobar</name>' \ '<comment>Foobar config</comment>' \ '<creation_time>2018-11-09T10:48:03Z</creation_time>' \ '<modification_time>2018-11-09T10:48:03Z</modification_time>' \ '</config>' \ '</get_configs_response>' def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_import_config(self): self.gmp.import_config(self.CONFIG_XML_STRING) self.connection.send.has_been_called_with( '<create_config>' '{config}' '</create_config>'.format(config=self.CONFIG_XML_STRING)) def test_import_missing_config_xml(self): with self.assertRaises(RequiredArgument): self.gmp.import_config(None) with self.assertRaises(RequiredArgument): self.gmp.import_config('') def test_import_invalid_xml(self): with self.assertRaises(InvalidArgument): self.gmp.import_config('abcdef')
class GmpGetSettingsTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_settings(self): self.gmp.get_settings() self.connection.send.has_been_called_with('<get_settings/>') def test_get_settings_with_filter(self): self.gmp.get_settings(filter="foo=bar") self.connection.send.has_been_called_with( '<get_settings filter="foo=bar"/>')
class GmpGetNvtFamiliesTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_nvt_families(self): self.gmp.get_nvt_families() self.connection.send.has_been_called_with('<get_nvt_families/>') def test_get_nvt_families_with_sort_order(self): self.gmp.get_nvt_families(sort_order='foo') self.connection.send.has_been_called_with( '<get_nvt_families sort_order="foo"/>')
class GmpCloneTargetCommandTestCase(unittest.TestCase): TARGET_ID = '00000000-0000-0000-0000-000000000000' def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_clone(self): self.gmp.clone_target(self.TARGET_ID) self.connection.send.has_been_called_with( '<create_target>' '<copy>{copy}</copy>' '</create_target>'.format(copy=self.TARGET_ID) )
def create_connection(): """ **Creates the connection* This function allows user to create a connection with openvas manager using gvm. The UnixSocketConnection function takes as a parameter a path that leads to openvasmd socket file (/var/run/openvasmd.sock) that exists in the VM that runs OpenVAS manager. Read rights must be granted (sudo chmod a+rwx openvasmd.sock) :return: The connection that gives access to the OpenVAS interface. """ subprocess.call(["sudo", "./config.sh"]) connection = UnixSocketConnection(path='/var/run/gvmd.sock') # transform = EtreeTransform() gmp = Gmp(connection) # version = gmp.get_version() gmp.authenticate('admin', 'admin') return gmp
class GmpGetUserTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_users(self): self.gmp.get_users() self.connection.send.has_been_called_with( '<get_users/>' ) def test_get_users_with_filter(self): self.gmp.get_users(filter='foo=bar') self.connection.send.has_been_called_with( '<get_users filter="foo=bar"/>' ) def test_get_users_with_filter_id(self): self.gmp.get_users(filter_id='f1') self.connection.send.has_been_called_with( '<get_users filt_id="f1"/>' )
def __post_init__(self): connection: TLSConnection = TLSConnection(hostname=self.hostname.ip, timeout=5) self.gmp: Gmp = Gmp(connection) try: response: str = self.gmp.authenticate(self.user, self.password) soup: BeautifulSoup = BeautifulSoup(response, 'xml') if int(soup.authenticate_response['status']) != 200: # print(soup.authenticate_response.attrs) self.print_and_exit(soup.authenticate_response['status_text']) except OSError: self.print_and_exit(f"Timeout connect Openvas {self.hostname.ip}") self.export = { 'PDF': 'c402cc3e-b531-11e1-9163-406186ea4fc5', 'XML': 'a994b278-1f62-11e1-96ac-406186ea4fc5', 'LATEX': 'a684c02c-b531-11e1-bdc2-406186ea4fc5', 'HTML': '6c248850-1f62-11e1-b082-406186ea4fc5' }
class GmpGetTagTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_tag(self): self.gmp.get_tag('t1') self.connection.send.has_been_called_with('<get_tags tag_id="t1"/>') self.gmp.get_tag(tag_id='t1') self.connection.send.has_been_called_with('<get_tags tag_id="t1"/>') def test_get_tag_missing_tag_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_tag(tag_id=None) with self.assertRaises(RequiredArgument): self.gmp.get_tag('')
class GmpGetNvtTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_nvt_with_nvt_oid(self): self.gmp.get_nvt(nvt_oid='nvt_oid') self.connection.send.has_been_called_with( '<get_nvts nvt_oid="nvt_oid" details="1"/>') def test_get_nvt_missing_nvt_oid(self): with self.assertRaises(RequiredArgument): self.gmp.get_nvt(nvt_oid=None) with self.assertRaises(RequiredArgument): self.gmp.get_nvt(nvt_oid='') with self.assertRaises(RequiredArgument): self.gmp.get_nvt('')
class GmpGetGroupTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_group(self): self.gmp.get_group('f1') self.connection.send.has_been_called_with('<get_groups group_id="f1"/>') self.gmp.get_group(group_id='f1') self.connection.send.has_been_called_with('<get_groups group_id="f1"/>') def test_get_group_missing_group_id(self): with self.assertRaises(RequiredArgument): self.gmp.get_group(group_id=None) with self.assertRaises(RequiredArgument): self.gmp.get_group('')
class GmpGetPreferenceTestCase(unittest.TestCase): def setUp(self): self.connection = MockConnection() self.gmp = Gmp(self.connection) def test_get_preference(self): self.gmp.get_preference(name='foo') self.connection.send.has_been_called_with( '<get_preferences preference="foo"/>') def test_get_preference_missing_name(self): with self.assertRaises(RequiredArgument): self.gmp.get_preference(name=None) with self.assertRaises(RequiredArgument): self.gmp.get_preference(name='') with self.assertRaises(RequiredArgument): self.gmp.get_preference('')