Skip to content

Commit

Permalink
fix search list, reduce duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
zgoldman-r7 committed Oct 2, 2023
1 parent d531cab commit 7647eac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/msf/base/serializer/readable_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 16 additions & 13 deletions lib/msf/ui/console/command_dispatcher/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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|
Expand Down

0 comments on commit 7647eac

Please sign in to comment.