My next class:

Dissecting Malicious Office Documents with Linux

Published: 2018-10-26. Last Updated: 2018-10-26 06:11:54 UTC
by Xavier Mertens (Version: 1)
6 comment(s)

A few months ago, Rob wrote a nice diary[1] to explain how to dissect a (malicious) Office document (.docx). The approach was to use the OpenXML SDK[2] with Powershell. This is nice but how to achieve the same on a Linux system? One of our readers (thanks Mike!) provided us with the steps to perform the same kind of analysis but on a Kali instance (replace Kali with your preferred distribution). 

The idea remains the same: To use Powershell and the SDK. Yes, if you don’t know yet, the integration between the Linux and Windows worlds is becoming very deep. If it’s possible to run bash on Windows, it’s also possible to run Powershell on a Linux box[3]! Here are the steps to install the required software components.

Install Powershell:

# apt update && apt -y install curl gnupg apt-transport-https
# curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
# echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/powershell.list
# apt-get update
# apt-get install powershell

Install Mono (Mono is an open source implementation of Microsoft's .NET Framework[4])

# apt-get install mono-complete
# curl -o /usr/local/bin/nuget.exe https://dist.nuget.org/win-x86-commandline/latest/nuget.exe

Install the OpenXML SDK:

# mkdir /opt/MSPackages
# cd /opt/MSPackages
# mono /usr/local/bin/nuget.exe install DocumentFormat.OpenXml -Version 2.8.1

Now you are able to follow the same analysis path as described by Rob in his diary. Here is an example:

# file 14266eed4ebd3d4653c27f150e5717a763f13365525966186cfd7b390d21685f.docx
14266eed4ebd3d4653c27f150e5717a763f13365525966186cfd7b390d21685f.docx: Microsoft Word 2007+
# pwsh
PowerShell 6.1.0
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs

Type 'help' to get help.

PS /tmp> [System.Reflection.Assembly]::LoadFrom("/opt/MSPackages/DocumentFormat.OpenXml.2.8.1/lib/net35/DocumentFormat.OpenXml.dll")

GAC    Version        Location
---    -------        --------
False  v2.0.50727     /opt/MSPackages/DocumentFormat.OpenXml.2.8.1/lib/net35/DocumentFormat.OpenXml.dll

PS /tmp> $file='14266eed4ebd3d4653c27f150e5717a763f13365525966186cfd7b390d21685f.docx'
PS /tmp> $doc = [DocumentFormat.OpenXml.Packaging.WordprocessingDocument]::Open($file,$true)
PS /tmp> $doc.MainDocumentPart.Document | grep InnerText | grep http                                    
InnerText                 :                         HYPERLINK "hxxps://bozeqb[.]com/redirect.php"READ  FULL CODE OF CONDUCT AND SIGN HERE
PS /tmp> $doc.Close()

An easy way to safely extract malicious URLs. Happy document analysis!

[1] https://isc.sans.edu/forums/diary/Dissecting+Malicious+MS+Office+Docs/24108/
[2] https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
[3] https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6
[4] https://www.mono-project.com/

Xavier Mertens (@xme)
Senior ISC Handler - Freelance Cyber Security Consultant
PGP Key

6 comment(s)
My next class:

Comments

When attempting to open the document, powershell gives a warning that the file is not found (although it is there, accessible). Tried with relative and absolutes paths. Just wondering if anyone else has had this issue? I would love to be able to use something like this.

@dsplice
Strange, I can't reproduce this. Is the file readable by the user running Powershell? Did you try as root?
(I'm using Kali and my default user remains root, I know - bad behaviour :-)
Works now. Must be a user error :P

And yes, bad behavior (that I am doing too)..

@dsplice
Had to add this to powershell.list to get it to work on Kali:

deb http://deb.debian.org/debian stretch main
Does not work with Kali 2018.3.
Thank you Xavier

Diary Archives