Module: Rex::FileUtils

Defined in:
lib/rex/file.rb

Overview

This class provides helper methods for dealing with files that are not supplied by the standard ruby API.

Class Method Summary collapse

Class Method Details

.clean_path(old) ⇒ Object

This method cleans the supplied path of directory traversal sequences It must accept path/with/..a/folder../starting/or/ending/in/two/dots but clean ../something as well as path/with/..traversal



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rex/file.rb', line 68

def self.clean_path(old)
  path = old.dup

  leading       = /\A\.\.\\|\A\.\.\//   # '..\'  or '../'  at start of string
  linux_style   = /\/\.\.\/|\/\.\.\\/   # '/../' or '/..\'
  windows_style = /\\\.\.\\|\\\.\.\//   # '\..\' or '\../'

  while(path.index(leading) != nil or path.index(linux_style) != nil or path.index(windows_style) != nil)
    path.gsub!(leading,'')
    path.gsub!(linux_style,'/')
    path.gsub!(windows_style,'\\')
  end

  path
end

.find_full_path(file_name) ⇒ Object

This method searches the PATH environment variable for a fully qualified path to the supplied file name.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rex/file.rb', line 88

def self.find_full_path(file_name)

  # Check for the absolute fast first
  if (file_name[0,1] == "/" and ::File.exist?(file_name) and ::File::Stat.new(file_name))
    return file_name
  end

  path = Rex::Compat.getenv('PATH')
  if (path)
    path.split(::File::PATH_SEPARATOR).each { |base|
      begin
        # Deal with Windows paths surrounded by quotes.  Prevents
        # silliness like trying to look for
        # '"C:\\framework\\nmap"\\nmap.exe' which will always fail.
        base = $1 if base =~ /^"(.*)"$/
        path = base + ::File::SEPARATOR + file_name
        if (::File::Stat.new(path) and not ::File.directory?(path))
          return path
        end
      rescue
      end
    }
  end
  return nil
end

.normalize_unix_path(*strs) ⇒ Object

This method joins the paths together in Unix format.



19
20
21
22
23
24
# File 'lib/rex/file.rb', line 19

def self.normalize_unix_path(*strs)
  new_str = strs * '/'
  new_str = new_str.gsub!("//", "/") while new_str.index("//")

  new_str
end

.normalize_win_path(*strs) ⇒ Object

This method joins the paths together in Windows format. All reserved characters will be filtered out, including:

" * : < > ? \ / |


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rex/file.rb', line 31

def self.normalize_win_path(*strs)
  # Convert to the same format so the parsing is easier
  s = strs * '\\'

  # Filter out double slashes
  s = s.gsub(/\\\\/, '\\') while s.index('\\\\')

  # Keep the trailing slash if exists
  trailing_s = ('\\' if s =~ /\\$/) || ''

  # Check the items (fie/dir) individually
  s = s.split(/\\/)

  # Parse the path prefix
  prefix = (s[0] || '').gsub(/[\*<>\?\/]/, '')

  # Delete the original prefix. We want the new one later.
  s.delete_at(0)

  # Filter out all the reserved characters
  s.map! {|e| e.gsub(/["\*:<>\?\\\/|]/, '') }

  # Put the modified prefix back
  s.insert(0, prefix)

  # And then safely join the items
  s *= '\\'

  # Add the trailing slash back if exists
  s << trailing_s
end