From eb795514b37a9384a2048578d4a0044228fff17f Mon Sep 17 00:00:00 2001 From: James Lee Date: Thu, 2 Feb 2012 11:57:45 -0700 Subject: [PATCH 1/3] Add a SortIndex option to rex Table Allows Table#to_s to do the right thing when the first column isn't the one we want sorted. --- lib/rex/ui/text/table.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/rex/ui/text/table.rb b/lib/rex/ui/text/table.rb index 2317a064ed..cca0e75116 100644 --- a/lib/rex/ui/text/table.rb +++ b/lib/rex/ui/text/table.rb @@ -68,6 +68,8 @@ class Table self.postfix = opts['Postfix'] || '' self.colprops = [] + self.sort_index = opts['SortIndex'] || 0 + # Default column properties self.columns.length.times { |idx| self.colprops[idx] = {} @@ -100,7 +102,7 @@ class Table str << columns_to_s || '' str << hr_to_s || '' - sort_rows(0) + sort_rows rows.each { |row| if (is_hr(row)) str << hr_to_s @@ -180,7 +182,7 @@ class Table # If the supplied index is an IPv4 address, handle it differently, but # avoid actually resolving domain names. # - def sort_rows(index=0) + def sort_rows(index=sort_index) return unless rows rows.sort! do |a,b| if a[index].nil? @@ -208,6 +210,7 @@ class Table attr_accessor :columns, :rows, :colprops # :nodoc: attr_accessor :width, :indent, :cellpad # :nodoc: attr_accessor :prefix, :postfix # :nodoc: + attr_accessor :sort_index # :nodoc: protected From b347418f90ecd6dacd8c610f66ce554a6d55ee3c Mon Sep 17 00:00:00 2001 From: James Lee Date: Thu, 2 Feb 2012 12:00:08 -0700 Subject: [PATCH 2/3] Add checks for numeric column data [Fixes #6303] --- lib/rex/ui/text/table.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/rex/ui/text/table.rb b/lib/rex/ui/text/table.rb index cca0e75116..7f02080638 100644 --- a/lib/rex/ui/text/table.rb +++ b/lib/rex/ui/text/table.rb @@ -191,6 +191,8 @@ class Table 1 elsif Rex::Socket.dotted_ip?(a[index]) and Rex::Socket.dotted_ip?(b[index]) Rex::Socket::addr_atoi(a[index]) <=> Rex::Socket::addr_atoi(b[index]) + elsif a[index] =~ /^[0-9]+$/ and b[index] =~ /^[0-9]+$/ + a[index].to_i <=> b[index].to_i else a[index] <=> b[index] # assumes otherwise comparable. end From cd0a806a0663a573e2edc6032b1e037100eaa09c Mon Sep 17 00:00:00 2001 From: James Lee Date: Thu, 2 Feb 2012 12:10:27 -0700 Subject: [PATCH 3/3] Sort by filename instead of the default first column [Fixes 6336] --- .../meterpreter/ui/console/command_dispatcher/stdapi/fs.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb b/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb index 9553f46798..bac96d5ee9 100644 --- a/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb +++ b/lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb @@ -307,6 +307,7 @@ class Console::CommandDispatcher::Stdapi::Fs path = args[0] || client.fs.dir.getwd tbl = Rex::Ui::Text::Table.new( 'Header' => "Listing: #{path}", + 'SortIndex' => 4, 'Columns' => [ 'Mode', @@ -319,7 +320,8 @@ class Console::CommandDispatcher::Stdapi::Fs items = 0 # Enumerate each item... - client.fs.dir.entries_with_info(path).sort { |a,b| a['FileName'] <=> b['FileName'] }.each { |p| + # No need to sort as Table will do it for us + client.fs.dir.entries_with_info(path).each { |p| tbl << [