From d7c8836f3b5d3b5142c9595e9e101dfa943f01b0 Mon Sep 17 00:00:00 2001 From: adfoster-r7 Date: Tue, 22 Oct 2024 23:10:01 +0100 Subject: [PATCH] Fix crash when importing Metasploit xml file --- .../db_manager/import/metasploit_framework.rb | 3 +- .../import/metasploit_framework/xml.rb | 2 +- .../import/basic_host_data_set.xml | 62 +++++++++++++++++++ .../import/metasploit_framework/xml.rb | 16 +++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 spec/file_fixtures/import/basic_host_data_set.xml diff --git a/lib/msf/core/db_manager/import/metasploit_framework.rb b/lib/msf/core/db_manager/import/metasploit_framework.rb index 5e12fad48b0f..fb2c1f3e700d 100644 --- a/lib/msf/core/db_manager/import/metasploit_framework.rb +++ b/lib/msf/core/db_manager/import/metasploit_framework.rb @@ -8,6 +8,7 @@ module Msf::DBManager::Import::MetasploitFramework include Msf::DBManager::Import::MetasploitFramework::Zip # Convert the string "NULL" to actual nil + # @param [String] str def nils_for_nulls(str) str == "NULL" ? nil : str end @@ -42,4 +43,4 @@ def unserialize_object(xml_elem, allow_yaml = false) end end end -end \ No newline at end of file +end diff --git a/lib/msf/core/db_manager/import/metasploit_framework/xml.rb b/lib/msf/core/db_manager/import/metasploit_framework/xml.rb index 53c2a497697b..e37f04ab57ea 100644 --- a/lib/msf/core/db_manager/import/metasploit_framework/xml.rb +++ b/lib/msf/core/db_manager/import/metasploit_framework/xml.rb @@ -307,7 +307,7 @@ def parse_host(host, wspace, blacklist, allow_yaml, btag, args, &block) # A regression resulted in the address field being serialized in some cases. # Lets handle both instances to keep things happy. See #5837 & #5985 - addr = nils_for_nulls(host.at('address')) + addr = nils_for_nulls(host.at('address')&.text&.to_s&.strip) return 0 unless addr # No period or colon means this must be in base64-encoded serialized form diff --git a/spec/file_fixtures/import/basic_host_data_set.xml b/spec/file_fixtures/import/basic_host_data_set.xml new file mode 100644 index 000000000000..bd9d16ea39a7 --- /dev/null +++ b/spec/file_fixtures/import/basic_host_data_set.xml @@ -0,0 +1,62 @@ + + + + + + 75 + 2012-05-30 17:12:44 UTC +
10.6.200.2
+ 00:0C:29:63:B3:95 + + + alive + Linux + 2.6.X + + + + 4 + 2012-05-30 17:14:37 UTC + device + + + + VMWare + + + 641 + 75 + 2012-05-30 17:12:44 UTC + 514 + tcp + open + shell + 2012-05-30 17:12:44 UTC + + + + + + 349 + 2012-05-30 17:14:17 UTC + smb.shares + 4 + 640 + 75 + 2012-05-30 17:14:17 UTC + + + BAh7BjoLc2hhcmVzWwlbCCILcHJpbnQkIglESVNLIhRQcmludGVyIERyaXZlcnNbCCILcHVibGljIglESVNLIhFTaGFyZWQgRmlsZXNbCCIMc2NyYXRjaCIJRElTSyIUVGVtcG9yYXJ5IEZpbGVzWwgiCUlQQyQiCElQQyIeSVBDIFNlcnZpY2UgKEZpbGUgU2VydmVyKQ== + + + + + + + + + + +
+
+
diff --git a/spec/support/shared/examples/msf/db_manager/import/metasploit_framework/xml.rb b/spec/support/shared/examples/msf/db_manager/import/metasploit_framework/xml.rb index f492fbefa15d..f5bd33cd6246 100644 --- a/spec/support/shared/examples/msf/db_manager/import/metasploit_framework/xml.rb +++ b/spec/support/shared/examples/msf/db_manager/import/metasploit_framework/xml.rb @@ -1035,6 +1035,22 @@ def with_info import_msf_xml end + context 'with host elements present' do + let(:data) do + File.binread(File.join(FILE_FIXTURES_PATH, 'import', 'basic_host_data_set.xml')) + end + + it 'import the host' do + expect { + import_msf_xml + }.to( + change(Mdm::Host, :count).by(1) + .and change(Mdm::Service, :count).by(1) + .and change(Mdm::Note, :count).by(1) + ) + end + end + context 'with web_forms/web_form elements' do let(:data) do xml.tag!('MetasploitV4') do