@@ -41,6 +41,7 @@ class Console::CommandDispatcher::Stdapi::Fs
41
41
@@upload_opts = Rex ::Parser ::Arguments . new (
42
42
"-h" => [ false , "Help banner." ] ,
43
43
"-r" => [ false , "Upload recursively." ] )
44
+
44
45
#
45
46
# Options for the ls command
46
47
#
@@ -54,6 +55,16 @@ class Console::CommandDispatcher::Stdapi::Fs
54
55
"-l" => [ false , "List in long format (default)" ] ,
55
56
"-R" => [ false , "Recursively list subdirectories encountered" ] )
56
57
58
+ #
59
+ # Options for the lls command
60
+ #
61
+ @@lls_opts = Rex ::Parser ::Arguments . new (
62
+ "-h" => [ false , "Help banner." ] ,
63
+ "-S" => [ true , "Search string." ] ,
64
+ "-t" => [ false , "Sort by time" ] ,
65
+ "-s" => [ false , "Sort by size" ] ,
66
+ "-r" => [ false , "Reverse sort order" ] )
67
+
57
68
#
58
69
# List of supported commands.
59
70
#
@@ -71,6 +82,7 @@ def commands
71
82
'lcd' => 'Change local working directory' ,
72
83
'lpwd' => 'Print local working directory' ,
73
84
'ls' => 'List files' ,
85
+ 'lls' => 'List local files' ,
74
86
'mkdir' => 'Make directory' ,
75
87
'pwd' => 'Print working directory' ,
76
88
'rm' => 'Delete the specified file' ,
@@ -95,6 +107,7 @@ def commands
95
107
'lcd' => [ ] ,
96
108
'lpwd' => [ ] ,
97
109
'ls' => [ 'stdapi_fs_stat' , 'stdapi_fs_ls' ] ,
110
+ 'lls' => [ ] ,
98
111
'mkdir' => [ 'stdapi_fs_mkdir' ] ,
99
112
'pwd' => [ 'stdapi_fs_getwd' ] ,
100
113
'rmdir' => [ 'stdapi_fs_delete_dir' ] ,
@@ -680,6 +693,128 @@ def cmd_ls(*args)
680
693
#
681
694
alias cmd_dir cmd_ls
682
695
696
+ def cmd_lls_help
697
+ print_line "Usage: lls [options]"
698
+ print_line
699
+ print_line "Lists contents of a local directory or file info"
700
+ print_line @@lls_opts . usage
701
+ end
702
+
703
+ #
704
+ # Get list local path information for lls command
705
+ #
706
+ def list_local_path ( path , sort , order , search_term = nil )
707
+ # Single file as path
708
+ if !::File . directory? ( path )
709
+ perms = pretty_perms ( path )
710
+ stat = ::File . stat ( path )
711
+ print_line ( "#{ perms } #{ stat . size } #{ stat . ftype [ 0 , 3 ] } #{ stat . mtime } #{ path } " )
712
+ return
713
+ end
714
+
715
+ # Enumerate each item...
716
+ # No need to sort as Table will do it for us
717
+ columns = [ 'Mode' , 'Size' , 'Type' , 'Last modified' , 'Name' ]
718
+ tbl = Rex ::Text ::Table . new (
719
+ 'Header' => "Listing Local: #{ path } " ,
720
+ 'SortIndex' => columns . index ( sort ) ,
721
+ 'SortOrder' => order ,
722
+ 'Columns' => columns )
723
+
724
+ items = 0
725
+ files = ::Dir . entries ( path )
726
+
727
+ files . each do |file |
728
+ file_path = ::File . join ( path , file )
729
+
730
+ perms = pretty_perms ( file_path )
731
+ stat = ::File . stat ( file_path )
732
+
733
+ row = [
734
+ perms ? perms : '' ,
735
+ stat . size ? stat . size . to_s : '' ,
736
+ stat . ftype ? stat . ftype [ 0 , 3 ] : '' ,
737
+ stat . mtime ? stat . mtime : '' ,
738
+ file
739
+ ]
740
+ if file != '.' && file != '..'
741
+ if row . join ( ' ' ) =~ /#{ search_term } /
742
+ tbl << row
743
+ items += 1
744
+ end
745
+ end
746
+ end
747
+ if items > 0
748
+ print_line ( tbl . to_s )
749
+ else
750
+ print_line ( "No entries exist in #{ path } " )
751
+ end
752
+ end
753
+
754
+ # Code from prettymode in lib/rex/post/file_stat.rb
755
+ # adapted for local file usage
756
+ def pretty_perms ( path )
757
+ m = ::File . stat ( path ) . mode
758
+ om = '%04o' % m
759
+ perms = ''
760
+
761
+ 3 . times {
762
+ perms = ( ( m & 01 ) == 01 ? 'x' : '-' ) + perms
763
+ perms = ( ( m & 02 ) == 02 ? 'w' : '-' ) + perms
764
+ perms = ( ( m & 04 ) == 04 ? 'r' : '-' ) + perms
765
+ m >>= 3
766
+ }
767
+
768
+ return "#{ om } /#{ perms } "
769
+ end
770
+
771
+ #
772
+ # List local files
773
+ #
774
+ def cmd_lls ( *args )
775
+ # Set Defaults
776
+ path = ::Dir . pwd
777
+ sort = 'Name'
778
+ order = :forward
779
+ search_term = nil
780
+
781
+ # Parse the args
782
+ @@lls_opts . parse ( args ) { |opt , idx , val |
783
+ case opt
784
+ # Sort options
785
+ when '-s'
786
+ sort = 'Size'
787
+ when '-t'
788
+ sort = 'Last modified'
789
+ # Output options
790
+ when '-r'
791
+ order = :reverse
792
+ # Search
793
+ when '-S'
794
+ search_term = val
795
+ if search_term . nil?
796
+ print_error ( "Enter a search term" )
797
+ return true
798
+ else
799
+ search_term = /#{ search_term } /nmi
800
+ end
801
+ # Help and path
802
+ when "-h"
803
+ cmd_lls_help
804
+ return 0
805
+ when nil
806
+ path = val
807
+ end
808
+ }
809
+
810
+ list_local_path ( path , sort , order , search_term )
811
+ end
812
+
813
+ #
814
+ # Alias the lls command to dir, for those of us who have windows muscle-memory
815
+ #
816
+ alias cmd_ldir cmd_lls
817
+
683
818
#
684
819
# Make one or more directory.
685
820
#
0 commit comments