From 7647eac28a4bb353a7728e136e7234d704abc03a Mon Sep 17 00:00:00 2001 From: Zach Goldman Date: Mon, 2 Oct 2023 17:08:41 -0500 Subject: [PATCH] fix search list, reduce duplicated code --- lib/msf/base/serializer/readable_text.rb | 2 ++ lib/msf/ui/console/command_dispatcher/core.rb | 29 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/msf/base/serializer/readable_text.rb b/lib/msf/base/serializer/readable_text.rb index a34d2c09e296b..309b7c44cb528 100644 --- a/lib/msf/base/serializer/readable_text.rb +++ b/lib/msf/base/serializer/readable_text.rb @@ -834,6 +834,7 @@ def self.dump_datastore(name, ds, indent = DefaultIndent, col = DefaultColumnWra def self.dump_sessions(framework, opts={}) output = "" verbose = opts[:verbose] || false + session_ids = opts[:session_ids] || nil show_active = opts[:show_active] || false show_inactive = opts[:show_inactive] || false # if show_active and show_inactive are false the caller didn't @@ -860,6 +861,7 @@ def self.dump_sessions(framework, opts={}) 'Columns' => columns, 'Indent' => indent) framework.sessions.each_sorted { |k| + next unless session_ids.nil? || session_ids.include?(framework.sessions.get(k).sid) session = framework.sessions[k] row = create_msf_session_row(session, show_extended) tbl << row diff --git a/lib/msf/ui/console/command_dispatcher/core.rb b/lib/msf/ui/console/command_dispatcher/core.rb index ee7db4e1da851..2f9b64fcb6611 100644 --- a/lib/msf/ui/console/command_dispatcher/core.rb +++ b/lib/msf/ui/console/command_dispatcher/core.rb @@ -1549,12 +1549,7 @@ def cmd_sessions(*args) return false end if search_term - matching_sessions = [] - terms = search_term.split - terms.each do |term| - matching_session = filter_sessions_by_search(term) - matching_sessions << matching_session.to_i if matching_session - end + matching_sessions = get_matching_sessions(search_term) if matching_sessions == [] print_error("No matching sessions.") return @@ -1675,12 +1670,7 @@ def cmd_sessions(*args) end when 'killall' if search_term - matching_sessions = [] - terms = search_term.split - terms.each do |term| - matching_session = filter_sessions_by_search(term) - matching_sessions << matching_session if matching_session - end + matching_sessions = get_matching_sessions(search_term) if matching_sessions == [] print_status("No matching sessions.") return @@ -1793,8 +1783,11 @@ def cmd_sessions(*args) end end when 'list', 'list_inactive', nil + if search_term + matching_sessions = get_matching_sessions(search_term) + end print_line - print(Serializer::ReadableText.dump_sessions(framework, show_active: show_active, show_inactive: show_inactive, show_extended: show_extended, verbose: verbose, search_term: search_term, ids: session_list, search: search)) + print(Serializer::ReadableText.dump_sessions(framework, show_active: show_active, show_inactive: show_inactive, show_extended: show_extended, verbose: verbose, session_ids: matching_sessions)) print_line when 'name' if session_name.blank? @@ -1833,6 +1826,16 @@ def cmd_sessions(*args) true end + def get_matching_sessions(search_term) + matching_sessions = [] + terms = search_term.split + terms.each do |term| + matching_session = filter_sessions_by_search(term) + matching_sessions << matching_session.to_i if matching_session + end + matching_sessions + end + def filter_sessions_by_search(search_term) matching_sessions = [] framework.sessions.each do |session|