File opennms_horizon_authenticated_rce.rb
has 651 lines of code (exceeds 250 allowed). Consider refactoring. Open
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
include Msf::Exploit::Remote::HttpClient
prepend Msf::Exploit::Remote::AutoCheck
Method escalate_or_deescalate_privs
has a Cognitive Complexity of 61 (exceeds 5 allowed). Consider refactoring. Open
def escalate_or_deescalate_privs(deescalate: false)
# Establish some variables based on if we need to escalate or deescalate privileges
if deescalate
use_filesystem = @role_to_add != 'ROLE_FILESYSTEM_EDITOR'
mode = 'cleanup'
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method check
has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring. Open
def check
# Try to authenticate
success, msg_or_check_code = opennms_login('check')
return msg_or_check_code unless success
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Class MetasploitModule
has 28 methods (exceeds 20 allowed). Consider refactoring. Open
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
include Msf::Exploit::Remote::HttpClient
prepend Msf::Exploit::Remote::AutoCheck
Method opennms_login
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
def opennms_login(mode, perform_invalid_login: false)
if perform_invalid_login
user = Rex::Text.rand_text_alpha(8..12)
pass = Rex::Text.rand_text_alpha(8..12)
keep_cookies = false
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method escalate_or_deescalate_privs
has 79 lines of code (exceeds 25 allowed). Consider refactoring. Open
def escalate_or_deescalate_privs(deescalate: false)
# Establish some variables based on if we need to escalate or deescalate privileges
if deescalate
use_filesystem = @role_to_add != 'ROLE_FILESYSTEM_EDITOR'
mode = 'cleanup'
Method cleanup
has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring. Open
def cleanup
return if [@payload_file_name, @notification_name, @destination_path_name, @notification_command_name, @role_to_add].all?(&:blank?)
print_status('Attempting cleanup...')
# to be on the safe side, we'll clear the cookie jar and log in again
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method check
has 58 lines of code (exceeds 25 allowed). Consider refactoring. Open
def check
# Try to authenticate
success, msg_or_check_code = opennms_login('check')
return msg_or_check_code unless success
Method initialize
has 58 lines of code (exceeds 25 allowed). Consider refactoring. Open
def initialize(info = {})
super(
update_info(
info,
'Name' => 'OpenNMS Horizon Authenticated RCE',
Method opennms_login
has 57 lines of code (exceeds 25 allowed). Consider refactoring. Open
def opennms_login(mode, perform_invalid_login: false)
if perform_invalid_login
user = Rex::Text.rand_text_alpha(8..12)
pass = Rex::Text.rand_text_alpha(8..12)
keep_cookies = false
Method cleanup
has 44 lines of code (exceeds 25 allowed). Consider refactoring. Open
def cleanup
return if [@payload_file_name, @notification_name, @destination_path_name, @notification_command_name, @role_to_add].all?(&:blank?)
print_status('Attempting cleanup...')
# to be on the safe side, we'll clear the cookie jar and log in again
Method grab_and_parse_xml_config_file
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
def grab_and_parse_xml_config_file(file_name, root_element, element, mode, filesystem: true)
request_hash = {
'method' => 'GET',
'keep_cookies' => true
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method revert_xml_config_file
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
def revert_xml_config_file(file_name, root_element, element, element_to_remove)
# First we need to get the current #{file_name} file, so we can remove our #{element_name} from it
success, xml_doc_or_msg = grab_and_parse_xml_config_file(file_name, root_element, element, 'cleanup')
unless success
print_error(xml_doc_or_msg)
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method revert_xml_config_file
has 36 lines of code (exceeds 25 allowed). Consider refactoring. Open
def revert_xml_config_file(file_name, root_element, element, element_to_remove)
# First we need to get the current #{file_name} file, so we can remove our #{element_name} from it
success, xml_doc_or_msg = grab_and_parse_xml_config_file(file_name, root_element, element, 'cleanup')
unless success
print_error(xml_doc_or_msg)
Method grab_and_parse_xml_config_file
has 36 lines of code (exceeds 25 allowed). Consider refactoring. Open
def grab_and_parse_xml_config_file(file_name, root_element, element, mode, filesystem: true)
request_hash = {
'method' => 'GET',
'keep_cookies' => true
}
Method update_configuration
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
def update_configuration(mode)
# We need to update the configuration in order for our changes to take effect
xml_doc = Nokogiri::XML::Builder.new do |xml|
xml.event('xmlns' => 'http://xmlns.opennms.org/xsd/event') do
xml.uei('uei.opennms.org/internal/reloadDaemonConfig')
Method edit_xml_config_file
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
def edit_xml_config_file(file_name, root_element, element)
# First we need to get the current #{file_name} file, so we can edit our #{element_name} in it
_success, xml_doc = grab_and_parse_xml_config_file(file_name, root_element, element, 'exploit')
# update the xml document with a new element
Method write_payload_to_bsh_file
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
def write_payload_to_bsh_file(cmd)
# We need to write our payload to a .bsh file so that it can be executed by the notification command
post_data = generate_post_data(@payload_file_name, cmd)
Method write_payload_to_bsh_file
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
def write_payload_to_bsh_file(cmd)
# We need to write our payload to a .bsh file so that it can be executed by the notification command
post_data = generate_post_data(@payload_file_name, cmd)
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method deal_with_failure_by_mode
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def deal_with_failure_by_mode(mode, message, status)
return [false, "#{message}. Manual cleanup is required."] if mode == 'cleanup'
case status
when 'disconnected'
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method update_configuration
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
def update_configuration(mode)
# We need to update the configuration in order for our changes to take effect
xml_doc = Nokogiri::XML::Builder.new do |xml|
xml.event('xmlns' => 'http://xmlns.opennms.org/xsd/event') do
xml.uei('uei.opennms.org/internal/reloadDaemonConfig')
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method ensure_notifications_enabled
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
def ensure_notifications_enabled
res = send_request_cgi({
'method' => 'GET',
'uri' => normalize_uri(target_uri.path, 'index.jsp'),
'keep_cookies' => true
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid too many return
statements within this method. Open
return CheckCode::Appears("User #{username} has #{@highest_priv} privileges. Exploitation is likely possible via privilege escalation to ROLE_FILESYSTEM_EDITOR.")
Avoid too many return
statements within this method. Open
return CheckCode::Detected("Exploitation requires privilege escalation, which is not possible for OpenNMS version #{version}.")
Avoid too many return
statements within this method. Open
return xml_doc_or_check_code unless success # in this case xml_doc_or_check_code is a CheckCode so we can return it directly
Avoid too many return
statements within this method. Open
return [false, xml_doc_or_msg] unless success # this is only used for cleanup. for exploit this cannot happen
Avoid too many return
statements within this method. Open
return [false, 'Failed to deescalate privileges. Manual cleanup is required.']
Avoid too many return
statements within this method. Open
return [true]
Avoid too many return
statements within this method. Open
return deal_with_failure_by_mode(mode, "Failed to parse the users.xml file while attempting to #{deescalate ? 'deescalate' : 'escalate'} privileges: #{e}", 'unexpected_reply')
Avoid too many return
statements within this method. Open
return deal_with_failure_by_mode(mode, "Received unexpected reply while attempting to #{deescalate ? 'deescalate' : 'escalate'} privileges", 'unexpected_reply')
Avoid too many return
statements within this method. Open
return [false, privs_or_msg] unless success
Avoid too many return
statements within this method. Open
return
Avoid too many return
statements within this method. Open
return CheckCode::Unknown("Failed to obtain a valid OpenNMS version: #{e}")
Avoid too many return
statements within this method. Open
return CheckCode::Safe("User #{username} does not have the required privileges for exploitation to work.")
Avoid too many return
statements within this method. Open
return deal_with_failure_by_mode(mode, message, 'unexpected_reply')
Avoid too many return
statements within this method. Open
return privs_or_check_code unless success
Avoid too many return
statements within this method. Open
return CheckCode::Appears("User #{username} has the required privileges for exploitation to work without privilege escalation.")
Avoid too many return
statements within this method. Open
return [false, "Received unexpected response while attempting to trigger the notification. The payload likely wasn't executed."]
Avoid too many return
statements within this method. Open
return [true, "Successfully deescalated privileges by removing #{@role_to_add}"]
Avoid too many return
statements within this method. Open
return [true, 'Received expected response while triggering the payload. Please be patient, it may take a few seconds for the payload to execute.']
Avoid too many return
statements within this method. Open
return [false, 'Failed to parse the users.xml file while attempting to deescalate privileges. Manual cleanup is required.']
Avoid too many return
statements within this method. Open
return deal_with_failure_by_mode(mode, message, 'unexpected_reply')
Avoid too many return
statements within this method. Open
return [false, message] unless success # this is only used for cleanup. for exploit this cannot happen
Similar blocks of code found in 2 locations. Consider refactoring. Open
if find_element_via_at_css(file_name)
full_element = xml_doc_or_msg.at_css(root_element).css(element).find { |e| e.at_css('name')&.text == element_to_remove }
else
full_element = xml_doc_or_msg.at_css(root_element).css(element).find { |e| e['name'] == element_to_remove }
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 32.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76
Similar blocks of code found in 2 locations. Consider refactoring. Open
if find_element_via_at_css(file_name)
full_element = xml_doc.at_css(root_element).css(element).find { |e| e.at_css('name')&.text == new_value }
else
full_element = xml_doc.at_css(root_element).css(element).find { |e| e['name'] == new_value }
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 32.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76