Module: Msf::Post::File
- Includes:
- Common
- Included in:
- Exploit::Remote::HTTP::Kubernetes, Android::System, Linux::BusyBox, Linux::Compile, Linux::Kernel, Linux::Priv, Linux::System, OSX::System, Process, Solaris::System, Vcenter::Database, Vcenter::Vcenter, Windows::Packrat, Windows::Runas, Windows::UserProfiles, Windows::WMIC, Sessions::CommandShell::FileTransfer, Rex::Post::Meterpreter::Extensions::Stdapi::Webcam::Webcam
- Defined in:
- lib/msf/core/post/file.rb
Defined Under Namespace
Classes: FileStat
Instance Method Summary collapse
- #_append_file_powershell(file_name, data) ⇒ Object protected
-
#_append_file_unix_shell(file_name, data) ⇒ void
protected
Append
datato the remote filefile_name. -
#_can_echo?(data) ⇒ Boolean
protected
Checks to see if there are non-printable characters in a given string.
-
#_read_file_meterpreter(file_name) ⇒ String
protected
Meterpreter-specific file read.
- #_read_file_powershell(filename) ⇒ Object protected
- #_read_file_powershell_fragment(filename, chunk_size, offset = 0) ⇒ Object protected
- #_shell_command_with_success_code(cmd) ⇒ Object protected
- #_shell_process_with_success_code(executable, args) ⇒ Object protected
-
#_unix_max_line_length ⇒ Integer
protected
Calculate the maximum line length for a unix shell.
-
#_win_ansi_append_file(file_name, data, chunk_size = 5000) ⇒ void
protected
Windows ansi file append for shell sessions.
-
#_win_ansi_write_file(file_name, data, chunk_size = 5000) ⇒ void
protected
Windows ANSI file write for shell sessions.
-
#_win_bin_append_file(file_name, data, chunk_size = 5000) ⇒ void
protected
Windows binary file append for shell sessions.
-
#_win_bin_write_file(file_name, data, chunk_size = 5000) ⇒ void
protected
Windows binary file write for shell sessions.
-
#_writable_powershell?(path) ⇒ Boolean
protected
Check writability via PowerShell session by attempting to create/open a file.
-
#_writable_unix?(path) ⇒ Boolean
protected
Check writability on Unix using test(1) builtins.
-
#_writable_windows_meterpreter?(path) ⇒ Boolean
protected
Check writability on Windows via Meterpreter by attempting to open a file handle.
-
#_writable_windows_shell?(path) ⇒ Boolean
protected
Check writability on Windows via a shell session using cmd.exe redirects.
-
#_write_file_meterpreter(file_name, data, mode = 'wb') ⇒ Object
protected
Meterpreter-specific file write.
- #_write_file_powershell(file_name, data, append = false) ⇒ Object protected
- #_write_file_powershell_fragment(file_name, data, offset, chunk_size, append = false) ⇒ Object protected
-
#_write_file_unix_shell(file_name, data, append = false) ⇒ void
protected
Write
datato the remote filefile_name. -
#append_file(file_name, data) ⇒ Object
Platform-agnostic file append.
-
#attributes(path) ⇒ Object
Retrieve file attributes for
pathon the remote system. -
#cd(path) ⇒ void
Change directory in the remote session to
path, which may be relative or absolute. -
#chmod(path, mode = 0o700) ⇒ Object
Sets the permissions on a remote file.
-
#copy_file(src_file, dst_file) ⇒ Boolean
(also: #cp_file)
Copy a remote file.
-
#dir(directory) ⇒ Array
(also: #ls)
Returns a list of the contents of the specified directory.
-
#directory?(path) ⇒ Boolean
See if
pathexists on the remote system and is a directory. -
#executable?(path) ⇒ Boolean
See if
pathon the remote system exists and is executable. -
#exist?(path) ⇒ Boolean
(also: #exists?)
Check for existence of
pathon the remote file system. -
#expand_path(path) ⇒ String
Expand any environment variables to return the full path specified by
path. -
#exploit_data(data_directory, file) ⇒ Object
Read a local exploit file binary from the data directory.
-
#exploit_source(source_directory, file) ⇒ Object
Read a local exploit source file from the external exploits directory.
-
#file?(path) ⇒ Boolean
(also: #file_exist?)
See if
pathexists on the remote system and is a regular file. -
#file_local_write(local_file_name, data) ⇒ void
Writes a given string to a given local file.
-
#file_remote_digestmd5(file_name, util: nil) ⇒ String
Returns a MD5 checksum of a given remote file.
-
#file_remote_digestsha1(file_name, util: nil) ⇒ String
Returns a SHA1 checksum of a given remote file.
-
#file_remote_digestsha2(file_name) ⇒ String
Returns a SHA2 checksum of a given remote file.
-
#find_writable_directories(path: '/', max_depth: 2, timeout: 15) ⇒ Array<String>?
Find writable directories under
pathon a Unix system. -
#immutable?(path) ⇒ Boolean
See if
pathon the remote system exists and is immutable. - #initialize(info = {}) ⇒ Object
-
#mkdir(path, cleanup: true) ⇒ Object
create and mark directory for cleanup.
-
#pwd ⇒ String
Returns the current working directory in the remote session.
-
#read_file(file_name) ⇒ String, Array
Platform-agnostic file read.
-
#readable?(path) ⇒ Boolean
See if
pathon the remote system exists and is readable. -
#rename_file(old_file, new_file) ⇒ Boolean
(also: #move_file, #mv_file)
Renames a remote file.
-
#rm_f(*remote_files) ⇒ void
(also: #file_rm)
Delete remote files.
-
#rm_rf(*remote_dirs) ⇒ void
(also: #dir_rm)
Delete remote directories.
-
#setuid?(path) ⇒ Boolean
See if
pathon the remote system is a setuid file. - #stat(filename) ⇒ Object protected
-
#upload_and_chmodx(path, data) ⇒ Object
Upload a binary and write it as an executable file
remoteon the remote filesystem. -
#upload_file(remote, local) ⇒ Object
Read a local file
localand write it asremoteon the remote file system. -
#writable?(path) ⇒ Boolean
See if
pathon the remote system exists and is writable. -
#write_file(file_name, data) ⇒ Object
Platform-agnostic file write.
Methods included from Common
#clear_screen, #cmd_exec, #cmd_exec_get_pid, #cmd_exec_with_result, #command_exists?, #create_process, #get_env, #get_envs, #peer, #report_virtualization, #rhost, #rport
Instance Method Details
#_append_file_powershell(file_name, data) ⇒ Object (protected)
820 821 822 |
# File 'lib/msf/core/post/file.rb', line 820 def _append_file_powershell(file_name, data) _write_file_powershell(file_name, data, true) end |
#_append_file_unix_shell(file_name, data) ⇒ void (protected)
This method returns an undefined value.
Append data to the remote file file_name.
You should never call this method directly. Instead, call #append_file which will call this method if it is appropriate for the given session.
1080 1081 1082 |
# File 'lib/msf/core/post/file.rb', line 1080 def _append_file_unix_shell(file_name, data) _write_file_unix_shell(file_name, data, true) end |
#_can_echo?(data) ⇒ Boolean (protected)
Checks to see if there are non-printable characters in a given string
916 917 918 919 920 921 922 923 924 |
# File 'lib/msf/core/post/file.rb', line 916 def _can_echo?(data) # Ensure all bytes are between ascii 0x20 to 0x7e (ie. [[:print]]), excluding quotes etc data.bytes.all? do|b| (b >= 0x20 && b <= 0x7e) && b != '"'.ord && b != '%'.ord && b != '$'.ord end end |
#_read_file_meterpreter(file_name) ⇒ String (protected)
Meterpreter-specific file read. Returns contents of remote file file_name as a String or nil if there was an error
You should never call this method directly. Instead, call #read_file which will call this if it is appropriate for the given session.
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 |
# File 'lib/msf/core/post/file.rb', line 945 def _read_file_meterpreter(file_name) fd = session.fs.file.new(file_name, 'rb') data = ''.b loop do chunk = fd.read break if chunk.nil? || chunk.empty? data << chunk end data rescue EOFError data rescue ::Rex::Post::Meterpreter::RequestError => e print_error("Failed to open file: #{file_name}: #{e}") return nil ensure fd.close if fd end |
#_read_file_powershell(filename) ⇒ Object (protected)
879 880 881 882 883 884 885 886 887 888 889 890 891 892 |
# File 'lib/msf/core/post/file.rb', line 879 def _read_file_powershell(filename) data = '' offset = 0 chunk_size = 65536 loop do chunk = _read_file_powershell_fragment(filename, chunk_size, offset) break if chunk.nil? data << chunk offset += chunk_size break if chunk.length < chunk_size end return data end |
#_read_file_powershell_fragment(filename, chunk_size, offset = 0) ⇒ Object (protected)
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 |
# File 'lib/msf/core/post/file.rb', line 894 def _read_file_powershell_fragment(filename, chunk_size, offset = 0) pwsh_code = <<~PSH $mstream = New-Object System.IO.MemoryStream; $gzipstream = New-Object System.IO.Compression.GZipStream($mstream, [System.IO.Compression.CompressionMode]::Compress); $get_bytes = [System.IO.File]::ReadAllBytes(\"#{filename}\")[#{offset}..#{offset + chunk_size - 1}]; $gzipstream.Write($get_bytes, 0, $get_bytes.Length); $gzipstream.Close(); [System.Convert]::ToBase64String($mstream.ToArray()); PSH b64_data = cmd_exec(pwsh_code) return nil if b64_data.empty? uncompressed_fragment = Zlib::GzipReader.new(StringIO.new(Base64.decode64(b64_data))).read return uncompressed_fragment end |
#_shell_command_with_success_code(cmd) ⇒ Object (protected)
1220 1221 1222 1223 1224 1225 |
# File 'lib/msf/core/post/file.rb', line 1220 def _shell_command_with_success_code(cmd) token = "_#{::Rex::Text.rand_text_alpha(32)}" result = session.shell_command_token("#{cmd} && echo #{token}") return result&.include?(token) end |
#_shell_process_with_success_code(executable, args) ⇒ Object (protected)
1227 1228 1229 1230 1231 1232 1233 |
# File 'lib/msf/core/post/file.rb', line 1227 def _shell_process_with_success_code(executable, args) cmd = session.to_cmd([executable] + args) token = "_#{::Rex::Text.rand_text_alpha(32)}" result = session.shell_command_token("#{cmd} && echo #{token}") return result&.include?(token) end |
#_unix_max_line_length ⇒ Integer (protected)
Calculate the maximum line length for a unix shell.
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
# File 'lib/msf/core/post/file.rb', line 1240 def _unix_max_line_length # Based on autoconf's arg_max calculator, see # http://www.in-ulm.de/~mascheck/various/argmax/autoconf_check.html calc_line_max = 'i=0 max= new= str=abcd; \ while (test "X"`echo "X$str" 2>/dev/null` = "XX$str") >/dev/null 2>&1 && \ new=`expr "X$str" : ".*" 2>&1` && \ test "$i" != 17 && \ max=$new; do \ i=`expr $i + 1`; str=$str$str;\ done; echo $max' line_max = session.shell_command_token(calc_line_max).to_i # Fall back to a conservative 4k which should work on even the most # restrictive of embedded shells. line_max = (line_max == 0 ? 4096 : line_max) vprint_status("Max line length is #{line_max}") line_max end |
#_win_ansi_append_file(file_name, data, chunk_size = 5000) ⇒ void (protected)
This method returns an undefined value.
Windows ansi file append for shell sessions. Writes given object content to a remote file.
NOTE: *This is not binary-safe on Windows shell sessions!*
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
# File 'lib/msf/core/post/file.rb', line 995 def _win_ansi_append_file(file_name, data, chunk_size = 5000) start_index = 0 write_length = [chunk_size, data.length].min while start_index < data.length begin success = _shell_command_with_success_code("echo | set /p x=\"#{data[start_index, write_length]}\">> \"#{file_name}\"") unless success print_warning("Write partially succeeded then failed. May need to manually clean up #{file_name}") unless start_index == 0 return false end start_index += write_length write_length = [chunk_size, data.length - start_index].min rescue ::Exception => e print_error("Exception while running #{__method__}: #{e}") print_warning("May need to manually clean up #{file_name}") unless start_index == 0 file_rm(file_name) return false end end true end |
#_win_ansi_write_file(file_name, data, chunk_size = 5000) ⇒ void (protected)
This method returns an undefined value.
Windows ANSI file write for shell sessions. Writes given object content to a remote file.
NOTE: *This is not binary-safe on Windows shell sessions!*
974 975 976 977 978 979 980 981 982 983 984 985 |
# File 'lib/msf/core/post/file.rb', line 974 def _win_ansi_write_file(file_name, data, chunk_size = 5000) start_index = 0 write_length = [chunk_size, data.length].min success = _shell_command_with_success_code("echo | set /p x=\"#{data[0, write_length]}\"> \"#{file_name}\"") return false unless success if data.length > write_length # just use append to finish the rest return _win_ansi_append_file(file_name, data[write_length, data.length], chunk_size) end true end |
#_win_bin_append_file(file_name, data, chunk_size = 5000) ⇒ void (protected)
This method returns an undefined value.
Windows binary file append for shell sessions. Appends given object content to a remote file.
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 |
# File 'lib/msf/core/post/file.rb', line 1049 def _win_bin_append_file(file_name, data, chunk_size = 5000) b64_data = Base64.strict_encode64(data) b64_filename = "#{file_name}.b64" tmp_filename = "#{file_name}.tmp" begin success = _win_ansi_write_file(b64_filename, b64_data, chunk_size) return false unless success vprint_status("Uploaded Base64-encoded file. Decoding using certutil") success = _shell_process_with_success_code('certutil', ['-decode', b64_filename, tmp_filename]) return false unless success vprint_status("Certutil succeeded. Appending using copy") success = _shell_process_with_success_code('copy', ['/b', "#{file_name}+#{tmp_filename}", file_name]) return false unless success rescue ::Exception => e print_error("Exception while running #{__method__}: #{e}") return false ensure file_rm(b64_filename) file_rm(tmp_filename) end true end |
#_win_bin_write_file(file_name, data, chunk_size = 5000) ⇒ void (protected)
This method returns an undefined value.
Windows binary file write for shell sessions. Writes given object content to a remote file.
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'lib/msf/core/post/file.rb', line 1024 def _win_bin_write_file(file_name, data, chunk_size = 5000) b64_data = Base64.strict_encode64(data) b64_filename = "#{file_name}.b64" begin success = _win_ansi_write_file(b64_filename, b64_data, chunk_size) return false unless success vprint_status("Uploaded Base64-encoded file. Decoding using certutil") success = _shell_process_with_success_code('certutil', ['-f', '-decode', b64_filename, file_name]) return false unless success rescue ::Exception => e print_error("Exception while running #{__method__}: #{e}") return false ensure file_rm(b64_filename) end true end |
#_writable_powershell?(path) ⇒ Boolean (protected)
Check writability via PowerShell session by attempting to create/open a file.
757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/msf/core/post/file.rb', line 757 def _writable_powershell?(path) verification_token = Rex::Text.rand_text_alpha_upper(8) if directory?(path) tmp_file = "#{path}\\#{Rex::Text.rand_text_alpha(8)}.tmp" script = "$f=[System.IO.File]::Create('#{tmp_file}');if($?){$f.Close();[System.IO.File]::Delete('#{tmp_file}');echo #{verification_token}}" return create_process('powershell.exe', args: ['-NoProfile', '-Command', script]).to_s.include?(verification_token) end return false unless file?(path) script = "$a=[System.IO.File]::OpenWrite('#{path}');if($?){echo #{verification_token}};$a.Close()" create_process('powershell.exe', args: ['-NoProfile', '-Command', script]).to_s.include?(verification_token) end |
#_writable_unix?(path) ⇒ Boolean (protected)
Check writability on Unix using test(1) builtins.
816 817 818 |
# File 'lib/msf/core/post/file.rb', line 816 def _writable_unix?(path) create_process('sh', args: ['-c', "(test -w '#{path}' || test -O '#{path}') && echo true"]).to_s.include?('true') end |
#_writable_windows_meterpreter?(path) ⇒ Boolean (protected)
Check writability on Windows via Meterpreter by attempting to open a file handle.
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
# File 'lib/msf/core/post/file.rb', line 774 def _writable_windows_meterpreter?(path) if directory?(path) tmp_file = "#{path}\\#{Rex::Text.rand_text_alpha(8)}.tmp" begin fd = session.fs.file.new(tmp_file, 'wb') fd.close session.fs.file.rm(tmp_file) return true rescue ::Rex::Post::Meterpreter::RequestError return false end end return false unless file?(path) begin fd = session.fs.file.new(path, 'wb') fd.close true rescue ::Rex::Post::Meterpreter::RequestError false end end |
#_writable_windows_shell?(path) ⇒ Boolean (protected)
Check writability on Windows via a shell session using cmd.exe redirects.
801 802 803 804 805 806 807 808 809 810 |
# File 'lib/msf/core/post/file.rb', line 801 def _writable_windows_shell?(path) verification_token = Rex::Text.rand_text_alpha_upper(8) if directory?(path) tmp_file = "#{path}\\#{Rex::Text.rand_text_alpha(8)}.tmp" return create_process('cmd.exe', args: ['/C', "type nul >> \"#{tmp_file}\" 2>nul && del \"#{tmp_file}\" && echo #{verification_token}"]).to_s.include?(verification_token) end return false unless file?(path) create_process('cmd.exe', args: ['/C', "type nul >> \"#{path}\" 2>nul && echo #{verification_token}"]).to_s.include?(verification_token) end |
#_write_file_meterpreter(file_name, data, mode = 'wb') ⇒ Object (protected)
Meterpreter-specific file write. Returns true on success
929 930 931 932 933 934 935 936 |
# File 'lib/msf/core/post/file.rb', line 929 def _write_file_meterpreter(file_name, data, mode = 'wb') fd = session.fs.file.new(file_name, mode) fd.write(data) fd.close return true rescue ::Rex::Post::Meterpreter::RequestError => e return false end |
#_write_file_powershell(file_name, data, append = false) ⇒ Object (protected)
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/msf/core/post/file.rb', line 824 def _write_file_powershell(file_name, data, append = false) offset = 0 chunk_size = 1000 loop do success = _write_file_powershell_fragment(file_name, data, offset, chunk_size, append) unless success unless offset == 0 print_warning("Write partially succeeded then failed. May need to manually clean up #{file_name}") end return false end # Future writes will then append, regardless of whether this is an append or write operation append = true offset += chunk_size break if offset >= data.length end true end |
#_write_file_powershell_fragment(file_name, data, offset, chunk_size, append = false) ⇒ Object (protected)
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/msf/core/post/file.rb', line 845 def _write_file_powershell_fragment(file_name, data, offset, chunk_size, append = false) token = "_#{::Rex::Text.rand_text_alpha(32)}" chunk = data[offset..(offset + chunk_size-1)] length = chunk.length compressed_chunk = Rex::Text.gzip(chunk) encoded_chunk = Base64.strict_encode64(compressed_chunk) if append file_mode = 'Append' else file_mode = 'Create' end file_name = file_name.gsub("'","''") pwsh_code = <<~PSH try { $encoded='#{encoded_chunk}'; $gzip_bytes=[System.Convert]::FromBase64String($encoded); $mstream = New-Object System.IO.MemoryStream(,$gzip_bytes); $gzipstream = New-Object System.IO.Compression.GzipStream $mstream, ([System.IO.Compression.CompressionMode]::Decompress); $filestream = [System.IO.File]::Open('#{file_name}', [System.IO.FileMode]::#{file_mode}); $file_bytes=[System.Byte[]]::CreateInstance([System.Byte],#{length}); $gzipstream.Read($file_bytes,0,$file_bytes.Length); $filestream.Write($file_bytes,0,$file_bytes.Length); $filestream.Close(); $gzipstream.Close(); echo Done } catch { echo #{token} } PSH result = cmd_exec(pwsh_code) return result.include?(length.to_s) && !result.include?(token) && result.include?('Done') end |
#_write_file_unix_shell(file_name, data, append = false) ⇒ void (protected)
This method returns an undefined value.
Write data to the remote file file_name.
Truncates if append is false, appends otherwise.
You should never call this method directly. Instead, call #write_file or #append_file which will call this if it is appropriate for the given session.
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/msf/core/post/file.rb', line 1094 def _write_file_unix_shell(file_name, data, append = false) redirect = (append ? '>>' : '>') # Short-circuit an empty string. The : builtin is part of posix # standard and should theoretically exist everywhere. if data.empty? return _shell_command_with_success_code(": #{redirect} #{session.escape_arg(file_name)}") end d = data.dup d.force_encoding('binary') if d.respond_to? :force_encoding chunks = [] command = nil encoding = :hex cmd_name = '' line_max = _unix_max_line_length # Leave plenty of room for the filename we're writing to and the # command to echo it out line_max -= file_name.length line_max -= 64 # Ordered by descending likeliness to work [ # POSIX standard requires %b which expands octal (but not hex) # escapes in the argument. However, some versions (notably # FreeBSD) truncate input on nulls, so "printf %b '\0\101'" # produces a 0-length string. Some also allow octal escapes # without a format string, and do not truncate, so start with # that and try %b if it doesn't work. The standalone version seems # to be more likely to work than the builtin version, so try it # first. # # Both of these work for sure on Linux and FreeBSD { cmd: %q{/usr/bin/printf 'CONTENTS'}, enc: :octal, name: 'printf' }, { cmd: %q{printf 'CONTENTS'}, enc: :octal, name: 'printf' }, # Works on Solaris { cmd: %q{/usr/bin/printf %b 'CONTENTS'}, enc: :octal, name: 'printf' }, { cmd: %q{printf %b 'CONTENTS'}, enc: :octal, name: 'printf' }, # Perl supports both octal and hex escapes, but octal is usually # shorter (e.g. 0 becomes \0 instead of \x00) { cmd: %q{perl -e 'print("CONTENTS")'}, enc: :octal, name: 'perl' }, # POSIX awk doesn't have \xNN escapes, use gawk to ensure we're # getting the GNU version. { cmd: %q^gawk 'BEGIN {ORS="";print "CONTENTS"}' </dev/null^, enc: :hex, name: 'awk' }, # xxd's -p flag specifies a postscript-style hexdump of unadorned hex # digits, e.g. ABCD would be 41424344 { cmd: %q{echo 'CONTENTS'|xxd -p -r}, enc: :bare_hex, name: 'xxd' }, # Use echo as a last resort since it frequently doesn't support -e # or -n. bash and zsh's echo builtins are apparently the only ones # that support both. Most others treat all options as just more # arguments to print. In particular, the standalone /bin/echo or # /usr/bin/echo appear never to have -e so don't bother trying # them. { cmd: %q{echo -ne 'CONTENTS'}, enc: :hex }, ].each do |foo| # Some versions of printf mangle %. test_str = "\0\xff\xfe#{Rex::Text.rand_text_alpha_upper(4)}\x7f%%\r\n" # test_str = "\0\xff\xfe" case foo[:enc] when :hex cmd = foo[:cmd].sub('CONTENTS') { Rex::Text.to_hex(test_str) } when :octal cmd = foo[:cmd].sub('CONTENTS') { Rex::Text.to_octal(test_str) } when :bare_hex cmd = foo[:cmd].sub('CONTENTS') { Rex::Text.to_hex(test_str, '') } end a = session.shell_command_token(cmd.to_s) if test_str == a command = foo[:cmd] encoding = foo[:enc] cmd_name = foo[:name] break else vprint_status("#{cmd} Failed: #{a.inspect} != #{test_str.inspect}") end end if command.nil? raise RuntimeError, "Can't find command on the victim for writing binary data", caller end # each byte will balloon up to 4 when we encode # (A becomes \x41 or \101) max = line_max / 4 i = 0 while (i < d.length) slice = d.slice(i...(i + max)) case encoding when :hex chunks << Rex::Text.to_hex(slice) when :octal chunks << Rex::Text.to_octal(slice) when :bare_hex chunks << Rex::Text.to_hex(slice, '') end i += max end vprint_status("Writing #{d.length} bytes in #{chunks.length} chunks of #{chunks.first.length} bytes (#{encoding}-encoded), using #{cmd_name}") # The first command needs to use the provided redirection for either # appending or truncating. cmd = command.sub('CONTENTS') { chunks.shift } succeeded = _shell_command_with_success_code("#{cmd} #{redirect} #{session.escape_arg(file_name)}") return false unless succeeded # After creating/truncating or appending with the first command, we # need to append from here on out. chunks.each do |chunk| vprint_status("Next chunk is #{chunk.length} bytes") cmd = command.sub('CONTENTS') { chunk } succeeded = _shell_command_with_success_code("#{cmd} >> #{session.escape_arg(file_name)}") unless succeeded print_warning("Write partially succeeded then failed. May need to manually clean up #{file_name}") return false end end true end |
#append_file(file_name, data) ⇒ Object
Platform-agnostic file append. Appends given object content to a remote file.
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/msf/core/post/file.rb', line 535 def append_file(file_name, data) if session.type == 'meterpreter' return _write_file_meterpreter(file_name, data, 'ab') elsif session.type == 'powershell' return _append_file_powershell(file_name, data) elsif session.respond_to? :shell_command_token if session.platform == 'windows' if _can_echo?(data) return _win_ansi_append_file(file_name, data) else return _win_bin_append_file(file_name, data) end else return _append_file_unix_shell(file_name, data) end end end |
#attributes(path) ⇒ Object
Retrieve file attributes for path on the remote system
334 335 336 337 338 |
# File 'lib/msf/core/post/file.rb', line 334 def attributes(path) raise "`attributes' method does not support Windows systems" if session.platform == 'windows' cmd_exec("lsattr -l '#{path}'").to_s.scan(/^#{path}\s+(.+)$/).flatten.first.to_s.split(', ') end |
#cd(path) ⇒ void
This method returns an undefined value.
Change directory in the remote session to path, which may be relative or absolute.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/msf/core/post/file.rb', line 43 def cd(path) e_path = begin (path) rescue StandardError path end if session.type == 'meterpreter' session.fs.dir.chdir(e_path) elsif session.type == 'powershell' cmd_exec("Set-Location -Path \"#{e_path}\";[System.IO.Directory]::SetCurrentDirectory($(Get-Location))") else session.shell_command_token("cd \"#{e_path}\"") end nil end |
#chmod(path, mode = 0o700) ⇒ Object
Sets the permissions on a remote file
581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/msf/core/post/file.rb', line 581 def chmod(path, mode = 0o700) if session.platform == 'windows' raise "`chmod' method does not support Windows systems" end if session.type == 'meterpreter' && session.commands.include?(Rex::Post::Meterpreter::Extensions::Stdapi::COMMAND_ID_STDAPI_FS_CHMOD) session.fs.file.chmod(path, mode) else create_process('chmod', args: [mode.to_s(8), path]) end end |
#copy_file(src_file, dst_file) ⇒ Boolean Also known as: cp_file
Copy a remote file.
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 |
# File 'lib/msf/core/post/file.rb', line 691 def copy_file(src_file, dst_file) return false if directory?(dst_file) || directory?(src_file) verification_token = Rex::Text.rand_text_alpha_upper(8) if session.type == 'meterpreter' begin return (session.fs.file.cp(src_file, dst_file).result == 0) rescue Rex::Post::Meterpreter::RequestError => e # when the source file is not present meterpreter will raise an error return false end elsif session.type == 'powershell' cmd_exec("Copy-Item \"#{src_file}\" -Destination \"#{dst_file}\"; if($?){echo #{verification_token}}").include?(verification_token) elsif session.platform == 'windows' cmd_exec(%(copy /y "#{src_file}" "#{dst_file}" & if not errorlevel 1 echo #{verification_token})).include?(verification_token) else cmd_exec(%(cp -f "#{src_file}" "#{dst_file}" && echo #{verification_token})).include?(verification_token) end end |
#dir(directory) ⇒ Array Also known as: ls
Returns a list of the contents of the specified directory
86 87 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 113 114 115 116 |
# File 'lib/msf/core/post/file.rb', line 86 def dir(directory) if session.type == 'meterpreter' return session.fs.dir.entries(directory) end if session.type == 'powershell' return cmd_exec("Get-ChildItem \"#{directory}\" -Name").split(/[\r\n]+/) end if session.platform == 'windows' return session.shell_command_token("dir /b \"#{directory}\"")&.split(/[\r\n]+/) end if command_exists?('ls') return session.shell_command_token("ls #{directory}").split(/[\r\n]+/) end # Result on systems without ls command if directory[-1] != '/' directory += '/' end result = [] data = session.shell_command_token("for fn in #{directory}*; do echo $fn; done") parts = data.split("\n") parts.each do |line| line = line.split('/')[-1] result.insert(-1, line) end result end |
#directory?(path) ⇒ Boolean
See if path exists on the remote system and is a directory
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/msf/core/post/file.rb', line 143 def directory?(path) if session.type == 'meterpreter' stat = begin session.fs.file.stat(path) rescue StandardError nil end return false unless stat return stat.directory? elsif session.type == 'powershell' return cmd_exec("Test-Path -Path \"#{path}\" -PathType Container").include?('True') else if session.platform == 'windows' f = cmd_exec("cmd.exe /C IF exist \"#{path}\\*\" ( echo true )") else f = session.shell_command_token("test -d '#{path}' && echo true") end return false if f.nil? || f.empty? return false unless f =~ /true/ true end end |
#executable?(path) ⇒ Boolean
See if path on the remote system exists and is executable
234 235 236 237 238 |
# File 'lib/msf/core/post/file.rb', line 234 def executable?(path) raise "`executable?' method does not support Windows systems" if session.platform == 'windows' cmd_exec("test -x '#{path}' && echo true").to_s.include? 'true' end |
#exist?(path) ⇒ Boolean Also known as: exists?
Check for existence of path on the remote file system
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/msf/core/post/file.rb', line 305 def exist?(path) if session.type == 'meterpreter' stat = begin session.fs.file.stat(path) rescue StandardError nil end return !!stat elsif session.type == 'powershell' return cmd_exec("Test-Path \"#{path}\"")&.include?('True') else if session.platform == 'windows' f = cmd_exec("cmd.exe /C IF exist \"#{path}\" ( echo true )") else f = cmd_exec("test -e \"#{path}\" && echo true") end return false if f.nil? || f.empty? return false unless f =~ /true/ true end end |
#expand_path(path) ⇒ String
Expand any environment variables to return the full path specified by path.
172 173 174 175 176 177 178 179 180 |
# File 'lib/msf/core/post/file.rb', line 172 def (path) if session.type == 'meterpreter' return session.fs.file.(path) elsif session.type == 'powershell' return cmd_exec("[Environment]::ExpandEnvironmentVariables(\"#{path}\")") else return cmd_exec("echo #{path}") end end |
#exploit_data(data_directory, file) ⇒ Object
Read a local exploit file binary from the data directory
598 599 600 601 |
# File 'lib/msf/core/post/file.rb', line 598 def exploit_data(data_directory, file) file_path = ::File.join(::Msf::Config.data_directory, 'exploits', data_directory, file) ::File.binread(file_path) end |
#exploit_source(source_directory, file) ⇒ Object
Read a local exploit source file from the external exploits directory
608 609 610 611 |
# File 'lib/msf/core/post/file.rb', line 608 def exploit_source(source_directory, file) file_path = ::File.join( Msf::Config.install_root, 'external', 'source', 'exploits', source_directory, file) ::File.read(file_path) end |
#file?(path) ⇒ Boolean Also known as: file_exist?
See if path exists on the remote system and is a regular file
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/msf/core/post/file.rb', line 186 def file?(path) return false if path.nil? if session.type == 'meterpreter' stat = begin session.fs.file.stat(path) rescue StandardError nil end return false unless stat return stat.file? elsif session.type == 'powershell' return cmd_exec("[System.IO.File]::Exists( \"#{path}\")")&.include?('True') else if session.platform == 'windows' f = cmd_exec("cmd.exe /C IF exist \"#{path}\" ( echo true )") if f =~ /true/ f = cmd_exec("cmd.exe /C IF exist \"#{path}\\\\\" ( echo false ) ELSE ( echo true )") end else f = session.shell_command_token("test -f \"#{path}\" && echo true") end return false if f.nil? || f.empty? return false unless f =~ /true/ true end end |
#file_local_write(local_file_name, data) ⇒ void
This method returns an undefined value.
Writes a given string to a given local file
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/msf/core/post/file.rb', line 346 def file_local_write(local_file_name, data) fname = Rex::FileUtils.clean_path(local_file_name) unless ::File.exist?(fname) ::FileUtils.touch(fname) end output = ::File.open(fname, 'a') data.each_line do |d| output.puts(d) end output.close end |
#file_remote_digestmd5(file_name, util: nil) ⇒ String
For shell sessions, this method downloads the file from the remote host unless a hashing utility for use on the remote host is specified.
Returns a MD5 checksum of a given remote file
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/msf/core/post/file.rb', line 368 def file_remote_digestmd5(file_name, util: nil) if session.type == 'meterpreter' begin return session.fs.file.md5(file_name)&.unpack('H*').flatten.first rescue StandardError => e print_error("Exception while running #{__method__}: #{e}") return nil end end # Note: This will fail on files larger than 2GB if session.type == 'powershell' data = cmd_exec("$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider; [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes('#{file_name}')))") return unless data chksum = data.scan(/^([A-F0-9-]+)$/).flatten.first return chksum&.gsub('-', '')&.downcase end case util when 'md5' chksum = session.shell_command_token("md5 -q '#{file_name}'")&.strip when 'md5sum' chksum = session.shell_command_token("md5sum '#{file_name}'")&.strip.split.first when 'certutil' data = session.shell_command_token("certutil -hashfile \"#{file_name}\" MD5") return unless data chksum = data.scan(/^([a-f0-9 ]{47})\r?\n/).flatten.first&.gsub(/\s*/, '') else data = read_file(file_name) return unless data chksum = Digest::MD5.hexdigest(data) end return unless chksum =~ /\A[a-f0-9]{32}\z/ chksum end |
#file_remote_digestsha1(file_name, util: nil) ⇒ String
For shell sessions, this method downloads the file from the remote host unless a hashing utility for use on the remote host is specified.
Returns a SHA1 checksum of a given remote file
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/msf/core/post/file.rb', line 417 def file_remote_digestsha1(file_name, util: nil) if session.type == 'meterpreter' begin return session.fs.file.sha1(file_name)&.unpack('H*').flatten.first rescue StandardError => e print_error("Exception while running #{__method__}: #{e}") return nil end end # Note: This will fail on files larger than 2GB if session.type == 'powershell' data = cmd_exec("$sha1 = New-Object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider; [System.BitConverter]::ToString($sha1.ComputeHash([System.IO.File]::ReadAllBytes('#{file_name}')))") return unless data chksum = data.scan(/^([A-F0-9-]+)$/).flatten.first return chksum&.gsub('-', '')&.downcase end case util when 'sha1' chksum = session.shell_command_token("sha1 -q '#{file_name}'")&.strip when 'sha1sum' chksum = session.shell_command_token("sha1sum '#{file_name}'")&.strip.split.first when 'certutil' data = session.shell_command_token("certutil -hashfile \"#{file_name}\" SHA1") return unless data chksum = data.scan(/^([a-f0-9 ]{59})\r?\n/).flatten.first&.gsub(/\s*/, '') else data = read_file(file_name) return unless data chksum = Digest::SHA1.hexdigest(data) end return unless chksum =~ /\A[a-f0-9]{40}\z/ chksum end |
#file_remote_digestsha2(file_name) ⇒ String
THIS DOWNLOADS THE FILE
Returns a SHA2 checksum of a given remote file
461 462 463 464 465 466 467 468 |
# File 'lib/msf/core/post/file.rb', line 461 def file_remote_digestsha2(file_name) data = read_file(file_name) chksum = nil if data chksum = Digest::SHA256.hexdigest(data) end return chksum end |
#find_writable_directories(path: '/', max_depth: 2, timeout: 15) ⇒ Array<String>?
Find writable directories under path on a Unix system.
Uses find’s -writable flag which checks effective access for the current user.
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 |
# File 'lib/msf/core/post/file.rb', line 723 def find_writable_directories(path: '/', max_depth: 2, timeout: 15) raise "`find_writable_directories' method does not support Windows systems" if session.platform == 'windows' path = path.to_s raise ArgumentError, 'path must be an absolute path' unless path.start_with?('/') max_depth = max_depth.to_i raise ArgumentError, 'max_depth must not be negative' if max_depth < 0 timeout = timeout.to_i if max_depth > 2 print_warning("Large max_depth (#{max_depth}) may cause the find command to run for a long time and hang the session") end exec_timeout = timeout > 0 ? timeout : 15 find_args = ['-maxdepth', max_depth.to_s, '-type', 'd', '-writable'] begin output = create_process('find', args: [path] + find_args, time_out: exec_timeout) output.to_s.lines.map(&:strip).select { |p| p.start_with?('/') } rescue ::StandardError => e elog("Failed to find writable directories in #{path}", error: e) print_error("Failed to find writable directories in #{path}") nil end end |
#immutable?(path) ⇒ Boolean
See if path on the remote system exists and is immutable
270 271 272 273 274 |
# File 'lib/msf/core/post/file.rb', line 270 def immutable?(path) raise "`immutable?' method does not support Windows systems" if session.platform == 'windows' attributes(path).include?('Immutable') end |
#initialize(info = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/msf/core/post/file.rb', line 9 def initialize(info = {}) super( update_info( info, 'Compat' => { 'Meterpreter' => { 'Commands' => %w[ core_channel_eof core_channel_open core_channel_read core_channel_write stdapi_fs_chdir stdapi_fs_chmod stdapi_fs_delete_dir stdapi_fs_delete_file stdapi_fs_file_expand_path stdapi_fs_file_move stdapi_fs_getwd stdapi_fs_ls stdapi_fs_mkdir stdapi_fs_separator stdapi_fs_stat ] } } ) ) end |
#mkdir(path, cleanup: true) ⇒ Object
create and mark directory for cleanup
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/msf/core/post/file.rb', line 121 def mkdir(path, cleanup: true) result = nil vprint_status("Creating directory #{path}") if session.type == 'meterpreter' # behave like mkdir -p and don't throw an error if the directory exists result = session.fs.dir.mkdir(path) unless directory?(path) elsif session.type == 'powershell' result = cmd_exec("New-Item \"#{path}\" -itemtype directory") elsif session.platform == 'windows' result = cmd_exec("mkdir \"#{path}\"") else result = cmd_exec("mkdir -p '#{path}'") end vprint_status("#{path} created") register_dir_for_cleanup(path) if cleanup result end |
#pwd ⇒ String
This may be inaccurate on shell sessions running on Windows before XP/2k3
Returns the current working directory in the remote session
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/msf/core/post/file.rb', line 66 def pwd if session.type == 'meterpreter' return session.fs.dir.getwd elsif session.type == 'powershell' return cmd_exec('(Get-Location).Path').strip elsif session.platform == 'windows' return session.shell_command_token('echo %CD%').to_s.strip # XXX: %CD% only exists on XP and newer, figure something out for NT4 # and 2k elsif command_exists?('pwd') return session.shell_command_token('pwd').to_s.strip else # Result on systems without pwd command return session.shell_command_token('echo $PWD').to_s.strip end end |
#read_file(file_name) ⇒ String, Array
Platform-agnostic file read. Returns contents of remote file file_name as a String.
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/msf/core/post/file.rb', line 479 def read_file(file_name) if session.type == 'meterpreter' return _read_file_meterpreter(file_name) end return unless %w[shell powershell].include?(session.type) if session.type == 'powershell' return _read_file_powershell(file_name) end if session.platform == 'windows' return session.shell_command_token("type \"#{file_name}\"") end return nil unless readable?(file_name) if command_exists?('cat') return session.shell_command_token("cat \"#{file_name}\"") end # Result on systems without cat command session.shell_command_token("while read line; do echo $line; done <#{file_name}") end |
#readable?(path) ⇒ Boolean
See if path on the remote system exists and is readable
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/msf/core/post/file.rb', line 283 def readable?(path) verification_token = Rex::Text.rand_text_alpha(8) return false unless exists?(path) if session.type == 'powershell' if directory?(path) return cmd_exec("[System.IO.Directory]::GetFiles('#{path}'); if($?) {echo #{verification_token}}").include?(verification_token) else return cmd_exec("[System.IO.File]::OpenRead(\"#{path}\");if($?){echo\ #{verification_token}}").include?(verification_token) end end raise "`readable?' method does not support Windows systems" if session.platform == 'windows' cmd_exec("test -r '#{path}' && echo #{verification_token}").to_s.include?(verification_token) end |
#rename_file(old_file, new_file) ⇒ Boolean Also known as: move_file, mv_file
Renames a remote file. If the new file path is a directory, the file will be moved into that directory with the same name.
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
# File 'lib/msf/core/post/file.rb', line 662 def rename_file(old_file, new_file) verification_token = Rex::Text.rand_text_alphanumeric(8) if session.type == 'meterpreter' begin new_file = new_file + session.fs.file.separator + session.fs.file.basename(old_file) if directory?(new_file) return (session.fs.file.mv(old_file, new_file).result == 0) rescue Rex::Post::Meterpreter::RequestError => e return false end elsif session.type == 'powershell' cmd_exec("Move-Item \"#{old_file}\" \"#{new_file}\" -Force; if($?){echo #{verification_token}}").include?(verification_token) elsif session.platform == 'windows' return false unless file?(old_file) # adding this because when the old_file is not present it hangs for a while, should be removed after this issue is fixed. cmd_exec(%(move #{directory?(new_file) ? '' : '/y'} "#{old_file}" "#{new_file}" & if not errorlevel 1 echo #{verification_token})).include?(verification_token) else cmd_exec(%(mv #{directory?(new_file) ? '' : '-f'} "#{old_file}" "#{new_file}" && echo #{verification_token})).include?(verification_token) end end |
#rm_f(*remote_files) ⇒ void Also known as: file_rm
This method returns an undefined value.
Delete remote files
619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/msf/core/post/file.rb', line 619 def rm_f(*remote_files) remote_files.each do |remote| if session.type == 'meterpreter' session.fs.file.delete(remote) if file?(remote) elsif session.type == 'powershell' cmd_exec("[System.IO.File]::Delete(\"#{remote}\")") if file?(remote) elsif session.platform == 'windows' cmd_exec("del /q /f \"#{remote}\"") else cmd_exec("rm -f \"#{remote}\"") end end end |
#rm_rf(*remote_dirs) ⇒ void Also known as: dir_rm
This method returns an undefined value.
Delete remote directories
639 640 641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/msf/core/post/file.rb', line 639 def rm_rf(*remote_dirs) remote_dirs.each do |remote| if session.type == 'meterpreter' session.fs.dir.rmdir(remote) if exist?(remote) elsif session.type == 'powershell' cmd_exec("Remove-Item -Path \"#{remote}\" -Force -Recurse") elsif session.platform == 'windows' cmd_exec("rd /s /q \"#{remote}\"") else cmd_exec("rm -rf \"#{remote}\"") end end end |
#setuid?(path) ⇒ Boolean
See if path on the remote system is a setuid file
222 223 224 225 |
# File 'lib/msf/core/post/file.rb', line 222 def setuid?(path) stat = stat(path) stat.setuid? end |
#stat(filename) ⇒ Object (protected)
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 |
# File 'lib/msf/core/post/file.rb', line 1260 def stat(filename) if session.type == 'meterpreter' return session.fs.file.stat(filename) else raise NotImplementedError if session.platform == 'windows' raise "`stat' command doesn't exist on target system" unless command_exists?('stat') return FileStat.new(filename, session) end end |
#upload_and_chmodx(path, data) ⇒ Object
Upload a binary and write it as an executable file remote on the remote filesystem.
570 571 572 573 574 |
# File 'lib/msf/core/post/file.rb', line 570 def upload_and_chmodx(path, data) print_status "Writing '#{path}' (#{data.size} bytes) ..." write_file path, data chmod(path) end |
#upload_file(remote, local) ⇒ Object
Read a local file local and write it as remote on the remote file system
560 561 562 |
# File 'lib/msf/core/post/file.rb', line 560 def upload_file(remote, local) write_file(remote, ::File.read(local, mode: 'rb')) end |
#writable?(path) ⇒ Boolean
See if path on the remote system exists and is writable
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/msf/core/post/file.rb', line 247 def writable?(path) if session.type == 'powershell' return _writable_powershell?(path) end if session.platform == 'windows' if session.type == 'meterpreter' return _writable_windows_meterpreter?(path) end return _writable_windows_shell?(path) end _writable_unix?(path) end |
#write_file(file_name, data) ⇒ Object
Platform-agnostic file write. Writes given object content to a remote file.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/msf/core/post/file.rb', line 509 def write_file(file_name, data) if session.type == 'meterpreter' return _write_file_meterpreter(file_name, data) elsif session.type == 'powershell' return _write_file_powershell(file_name, data) elsif session.respond_to? :shell_command_token if session.platform == 'windows' if _can_echo?(data) return _win_ansi_write_file(file_name, data) else return _win_bin_write_file(file_name, data) end else return _write_file_unix_shell(file_name, data) end else return false end end |