metasploit-framework/modules/auxiliary/scanner/smtp/smtp_relay.rb

122 lines
4.8 KiB
Ruby
Raw Normal View History

2013-06-25 15:22:51 +00:00
##
# This module requires Metasploit: http//metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
2013-06-25 15:22:51 +00:00
##
2013-06-26 03:09:45 +00:00
2013-06-25 15:22:51 +00:00
require 'msf/core'
class Metasploit3 < Msf::Auxiliary
2013-08-30 21:28:54 +00:00
include Msf::Exploit::Remote::Smtp
include Msf::Auxiliary::Scanner
include Msf::Auxiliary::Report
2013-06-25 15:22:51 +00:00
2013-08-30 21:28:54 +00:00
def initialize
super(
'Name' => 'SMTP Open Relay Detection',
'Description' => %q{
This module tests if an SMTP server will accept (via a code 250)
2014-02-12 08:44:53 +00:00
an e-mail by using a variation of testing methods.
Some of the extended methods will try to abuse configuration or mailserver flaws.
2013-08-30 21:28:54 +00:00
},
'References' =>
[
2014-02-12 08:44:53 +00:00
['URL', 'https://svn.nmap.org/nmap/scripts/smtp-open-relay.nse'],
2013-08-30 21:28:54 +00:00
],
2014-02-12 08:44:53 +00:00
'Author' => 'xistence <xistence[at]0x90.nl>',
2013-08-30 21:28:54 +00:00
'License' => MSF_LICENSE
)
2014-02-12 08:44:53 +00:00
register_options(
[
OptBool.new('EXTENDED', [true, 'Do all the 16 extended checks', false]),
], self.class)
2013-08-30 21:28:54 +00:00
end
2013-06-25 15:22:51 +00:00
2013-08-30 21:28:54 +00:00
def peer
"#{rhost}:#{rport}"
end
2013-06-26 03:09:45 +00:00
2013-08-30 21:28:54 +00:00
def run_host(ip)
begin
connect
2014-02-12 08:44:53 +00:00
banner_sanitized = Rex::Text.to_hex_ascii(banner.to_s)
print_status("#{peer} - SMTP #{banner_sanitized}")
report_service(:host => rhost, :port => rport, :name => "smtp", :info => banner)
if datastore['EXTENDED']
if banner_sanitized =~ /220 (.*) /
serverhost = $1
end
mailfromuser = datastore['MAILFROM'].split("@").first
mailfromdomain = datastore['MAILFROM'].split("@").last
mailtouser = datastore['MAILTO'].split("@").first
mailtodomain = datastore['MAILTO'].split("@").last
do_test_relay(1, "MAIL FROM:<>", "RCPT TO:<#{datastore['MAILTO']}>")
do_test_relay(2, "MAIL FROM:<#{datastore['MAILFROM']}>", "RCPT TO:<#{datastore['MAILTO']}>")
do_test_relay(3, "MAIL FROM:<#{mailfromuser}@#{serverhost}>", "RCPT TO:<#{datastore['MAILTO']}>")
do_test_relay(4, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtouser}@[#{rhost}]>")
do_test_relay(5, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtouser}\%#{mailtodomain}@[#{rhost}]>")
do_test_relay(6, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtouser}\%#{mailtodomain}@#{serverhost}>")
do_test_relay(7, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<\"#{mailtouser}@#{mailtodomain}\">")
do_test_relay(8, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<\"#{mailtouser}\%#{mailtodomain}\">")
do_test_relay(9, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtouser}@#{mailtodomain}@[#{rhost}]>")
do_test_relay(10, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<\"#{mailtouser}@#{mailtodomain}\"@[#{rhost}]>")
do_test_relay(11, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtouser}@#{mailtodomain}@#{serverhost}>")
do_test_relay(12, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<@[#{rhost}]:#{mailtouser}@#{mailtodomain}>")
do_test_relay(13, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<@#{serverhost}:#{mailtouser}@#{mailtodomain}>")
do_test_relay(14, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtodomain}!#{mailtouser}>")
do_test_relay(15, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtodomain}!#{mailtouser}@[#{rhost}]>")
do_test_relay(16, "MAIL FROM:<#{mailfromuser}@[#{rhost}]>", "RCPT TO:<#{mailtodomain}!#{mailtouser}@#{serverhost}>")
else
do_test_relay(nil, "MAIL FROM:<#{datastore['MAILFROM']}>", "RCPT TO:<#{datastore['MAILTO']}>")
end
2013-08-30 21:28:54 +00:00
rescue
print_error("#{peer} - Unable to establish an SMTP session")
return
end
end
2013-06-25 15:22:51 +00:00
2014-02-12 08:44:53 +00:00
def do_test_relay(testnumber, mailfrom, mailto)
begin
connect
res = raw_send_recv("EHLO X\r\n")
vprint_status("#{peer} - #{res.inspect}")
2013-06-26 03:09:45 +00:00
2014-02-12 08:44:53 +00:00
res = raw_send_recv("#{mailfrom}\r\n")
vprint_status("#{peer} - #{res.inspect}")
2013-06-26 03:09:45 +00:00
2014-02-12 08:44:53 +00:00
res = raw_send_recv("#{mailto}\r\n")
vprint_status("#{peer} - #{res.inspect}")
2013-06-26 03:09:45 +00:00
2014-02-12 08:44:53 +00:00
res = raw_send_recv("DATA\r\n")
vprint_status("#{peer} - #{res.inspect}")
2013-06-26 03:09:45 +00:00
2014-02-12 08:44:53 +00:00
res = raw_send_recv("#{Rex::Text.rand_text_alpha(rand(10)+5)}\r\n.\r\n")
vprint_status("#{peer} - #{res.inspect}")
2013-06-26 03:09:45 +00:00
2014-02-12 08:44:53 +00:00
if res =~ /250/
if testnumber.nil?
print_good("#{peer} - Potential open SMTP relay detected: - #{mailfrom} -> #{mailto}")
else
print_good("#{peer} - Test ##{testnumber} - Potential open SMTP relay detected: - #{mailfrom} -> #{mailto}")
end
else
if testnumber.nil?
print_status "#{peer} - No relay detected"
else
print_status "#{peer} - Test ##{testnumber} - No relay detected"
end
end
rescue
print_error("#{peer} - Test ##{testnumber} - Unable to establish an SMTP session")
return
2013-08-30 21:28:54 +00:00
end
end
2013-07-01 20:37:37 +00:00
end