Module: Msf::Ui::Console::LocalFileSystem

Included in:
CommandDispatcher::LocalFileSystem, Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Stdapi::Fs
Defined in:
lib/msf/ui/console/local_file_system.rb

Overview

This module provides commands for the local file system

Constant Summary collapse

@@lls_opts =

Options for the lls command

Rex::Parser::Arguments.new(
  '-h' => [ false, 'Help banner' ],
  '-S' => [ true, 'Search string on filename (as regular expression)' ],
  '-t' => [ false, 'Sort by time' ],
  '-s' => [ false, 'Sort by size' ],
  '-r' => [ false, 'Reverse sort order' ]
)

Instance Method Summary collapse

Instance Method Details

#cmd_lcat(*args) ⇒ TrueClass

Reads the contents of a local file and prints them to the screen.

Parameters:

  • args (Array)

Returns:

  • (TrueClass)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/msf/ui/console/local_file_system.rb', line 186

def cmd_lcat(*args)
  if args.empty? || args.include?('-h') || args.include?('--help')
    print_line('Usage: lcat file')
    return true
  end

  path = args[0]
  path = ::File.expand_path(path) if path =~ path_expand_regex

  if ::File.stat(path).directory?
    print_error("#{path} is a directory")
  else
    fd = ::File.new(path, 'rb')
    begin
      print(fd.read) until fd.eof?
      # EOFError is raised if file is empty, do nothing, just catch
    rescue EOFError
    end
    fd.close
  end

  true
end

#cmd_lcat_tabs(str, words) ⇒ Array

Tab completion for the lcat command

Parameters:

  • str (Object)
  • words (Object)

Returns:

  • (Array)

    List of matches



216
217
218
# File 'lib/msf/ui/console/local_file_system.rb', line 216

def cmd_lcat_tabs(str, words)
  tab_complete_filenames(str, words)
end

#cmd_lcd(*args) ⇒ TrueClass

Change the local working directory.

Parameters:

  • args (Array)

Returns:

  • (TrueClass)


104
105
106
107
108
109
110
111
112
113
# File 'lib/msf/ui/console/local_file_system.rb', line 104

def cmd_lcd(*args)
  if args.empty?
    print_line('Usage: lcd directory')
    return true
  end

  ::Dir.chdir(args[0])

  true
end

#cmd_lcd_tabs(str, words) ⇒ Object Also known as: cmd_lls_tabs

Tab completion for the lcd command

Parameters:

  • str (String)
  • words (Array)


120
121
122
# File 'lib/msf/ui/console/local_file_system.rb', line 120

def cmd_lcd_tabs(str, words)
  tab_complete_directory(str, words)
end

#cmd_lls(*args) ⇒ Rex::Text::Table Also known as: cmd_ldir

List local files

Parameters:

  • args (Array)

Returns:

  • (Rex::Text::Table)

    The results lls command



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/msf/ui/console/local_file_system.rb', line 44

def cmd_lls(*args)
  # Set Defaults
  path = ::Dir.pwd
  sort = 'Name'
  order = :forward
  search_term = nil

  # Parse the args
  @@lls_opts.parse(args) do |opt, _idx, val|
    case opt
      # Sort options
    when '-s'
      sort = 'Size'
    when '-t'
      sort = 'Last modified'
      # Output options
    when '-r'
      order = :reverse
      # Search
    when '-S'
      search_term = val
      if search_term.nil?
        print_error('Enter a search term')
        return true
      else
        search_term = /#{search_term}/nmi
      end
      # Help and path
    when '-h'
      cmd_lls_help
      return 0
    when nil
      path = val
    end
  end

  list_local_path(path, sort, order, search_term)
end

#cmd_lls_helpRex::Parser::Arguments

Help output for lss command



87
88
89
90
91
92
# File 'lib/msf/ui/console/local_file_system.rb', line 87

def cmd_lls_help
  print_line 'Usage: lls [options]'
  print_line
  print_line 'Lists contents of a local directory or file info'
  print_line @@lls_opts.usage
end

#cmd_lmkdir(*args) ⇒ Array

Create new directory on local machine

Parameters:

  • args (Array)

Returns:

  • (Array)


225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/msf/ui/console/local_file_system.rb', line 225

def cmd_lmkdir(*args)
  if args.empty?
    print_line('Usage: lmkdir </path/to/directory>')
    return
  end

  args.each do |path|
    ::FileUtils.mkdir_p(path)
    print_line("Directory '#{path}' created successfully.")
  rescue ::StandardError => e
    print_error("Error creating #{path} directory: #{e}")
  end
end

#cmd_lpwd(*args) ⇒ TrueClass Also known as: cmd_getlwd

Display the local working directory.

Parameters:

  • args (Array)

Returns:

  • (TrueClass)


244
245
246
247
# File 'lib/msf/ui/console/local_file_system.rb', line 244

def cmd_lpwd(*args)
  print_line(::Dir.pwd)
  true
end

#list_local_path(path, sort, order, search_term = nil) ⇒ Rex::Text::Table, String

Get list local path information for lls command

Parameters:

  • path (String)
  • sort (String)
  • order (Symbol)
  • search_term (nil) (defaults to: nil)

Returns:

  • (Rex::Text::Table, String)

    The results lcd command



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/msf/ui/console/local_file_system.rb', line 134

def list_local_path(path, sort, order, search_term = nil)
  # Single file as path
  unless ::File.directory?(path)
    perms = pretty_perms(path)
    stat = ::File.stat(path)
    print_line("#{perms}  #{stat.size}  #{stat.ftype[0, 3]}  #{stat.mtime}  #{path}")
    return
  end

  # Enumerate each item...
  # No need to sort as Table will do it for us
  columns = [ 'Mode', 'Size', 'Type', 'Last modified', 'Name' ]
  tbl = Rex::Text::Table.new(
    'Header' => "Listing Local: #{path}",
    'SortIndex' => columns.index(sort),
    'SortOrder' => order,
    'Columns' => columns
  )

  items = 0
  files = ::Dir.entries(path)

  files.each do |file|
    file_path = ::File.join(path, file)

    perms = pretty_perms(file_path)
    stat = ::File.stat(file_path)

    row = [
      perms || '',
      stat.size ? stat.size.to_s : '',
      stat.ftype ? stat.ftype[0, 3] : '',
      stat.mtime || '',
      file
    ]
    if file != '.' && file != '..' && (row.join(' ') =~ /#{search_term}/)
      tbl << row
      items += 1
    end
  end
  if items > 0
    print_line(tbl.to_s)
  else
    print_line("No entries exist in #{path}")
  end
end

#local_fs_commandsHash

List of supported local commands.

Returns:

  • (Hash)

    Hash of local commands



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/msf/ui/console/local_file_system.rb', line 27

def local_fs_commands
  {
    'getlwd' => 'Print local working directory (alias for lpwd)',
    'lcat' => 'Read the contents of a local file to the screen',
    'lcd' => 'Change local working directory',
    'lmkdir' => 'Create new directory on local machine',
    'lpwd' => 'Print local working directory',
    'lls' => 'List local files',
    'ldir' => 'List local files (alias for lls)'
  }
end

#pretty_perms(path) ⇒ String

Code from prettymode in lib/rex/post/file_stat.rb adapted for local file usage

Parameters:

  • path (Object)

Returns:

  • (String)


257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/msf/ui/console/local_file_system.rb', line 257

def pretty_perms(path)
  m = ::File.stat(path).mode
  om = '%04o' % m
  perms = ''

  3.times do
    perms = ((m & 0o1) == 0o1 ? 'x' : '-') + perms
    perms = ((m & 0o2) == 0o2 ? 'w' : '-') + perms
    perms = ((m & 0o4) == 0o4 ? 'r' : '-') + perms
    m >>= 3
  end

  "#{om}/#{perms}"
end