We have a bunch of document files, and some of them might be something more than just documents. Without opening them (hopefully), could we identify the malicious ones from the clean ones?
For the graded task, start the assignment workspace below and download your generated task2-documents.zip. The shared maldocs_task1.zip is only an example and can be used for signature detection and its filenames and indicators are not the answers to your generated task, while you need to do identical process to identify them. Keep your generated documents private and analyze them statically.
One great paper about PDF and Office malware on a general level is available here. Only available on the University Network (can be accessed via VPN or SSH tunnel).
There are various tools for doing analysis partially for you, and we are going to try out some of them.
The shared example files can be found in maldocs_task1.zip. Its password is infected. Your generated workspace download is a separate ZIP and is not password protected.
Warning Do not extract the zip files directly into the Windows machine. Antivirus might flag and remove them, even though there are not really malicious files. They just look like and include some patterns malicious files could have.
Used libraries
Most of the used libraries for tasks 2. and 3. can be installed with pacman and a Python virtual environment:
sudo pacman -S --needed clamav yara python-pip git base-devel
python -m venv .venv
source .venv/bin/activate
pip install oletools eml-parser olefile
The system Python on modern distributions is marked as externally managed, so installing with pip directly is refused. Create a virtual environment instead, and activate it in each new shell (source .venv/bin/activate).
oledump, pdfid, pdf-parser Python scripts can be installed from GitHub using:
git clone https://github.com/DidierStevens/DidierStevensSuite.git
Getting started
When we face a suspicious file, what should we do first? The first thing we can do is to see if someone has already wondered the same thing and has done the analysis already!
Many anti-virus tools and other databases are signature-based - they might have an exact signature hash or some other regular pattern database for detecting malware. Someone/something might have submitted the file to these places and classified it already.
Some tools/utilities for general analysis; do they exist already?
- ClamAV - The biggest Open Source antivirus engine
- VirusTotal - analyse URL, file, or search by hashes against many AV engines. CLI is available here.
- YARA - the pattern matching Swiss knife, installed as the classic
yaratool. It can be used as a CLI or as a Python library, powered by a list of rules. Its successor, YARA-X, is also available. - Microsoft Defender Antivirus - often underestimated, but possesses one of the largest signature and heuristic pattern databases among Windows-based endpoint protection engines. Not used directly in this lab, but notable for its scale. (Even scary good at collecting samples)
We could try to use ClamAV:
sudo freshclam
clamscan <FILENAME>
freshclam updates the virus database. It is required before the first scan: with an
empty database clamscan reports Infected files: 0 without scanning anything.
This will scan the file against ClamAV’s current database.
A similar example with yara:
yara -w <Rules> <FILE>
To search with all predefined rules of yara. (See the package here.)
We could also try to use VirusTotal to see if there is already a report about some of our file(s). We could use the graphical Web interface or CLI tool for that.
In the case of the CLI tool, we should calculate the hash (SHA256) of the file and search based on that at first - in general, uploading is quite a permanent thing and then the file is identified by everyone. By searching with file hash, we can see if the file has been already submitted there.
By using the VirusTotal website or using API calls with vt or vt-cli packages we can search for example matches.
If you don’t want to create an account for VirusTotal, you can use the following API key: cc36b7771c0c738d7923b3209b6ae6c37a868e9412b3c2dd4109b10bda8f4f6e A key is required for the CLI, but a single key can make only 4 requests per minute.
Try signature-based detection on the shared examples
Use the files in the shared maldocs_task1.zip and not the ones generated specifically for you. You can try ClamAV, YARA, or VirusTotal hash searches. These shared examples are already known to detection systems and may therefore be flagged. Your generated files are unique to the assignment, so signature-based services are not expected to identify them.
You may use a table to summarize the tools and results, but you do not need to submit explicit answers for this preliminary exercise. While testing, consider:
- Have any of the shared files already been identified, and which tools flag them as suspicious?
- Why might a genuinely malicious file still produce no detections?
- What are the limitations of signature-based identification, especially for new, modified, or obfuscated samples?
- What dangerous action—disabled by default—should you avoid enabling in a suspicious Microsoft Office document?
- How do malicious PDF files commonly compromise a victim’s machine?
Making the final manual analysis
Some tools for analysing PDF files are listed below. They attempt to identify some known patterns, which usually can be seen in malicious files, and rarely in clean files. You can install them by yourself; they are also available as normal packages in Kali.
At first, ideally, we want to find some suspicious keywords from PDFs such as /JavaScript, /OpenAction, /GoTo, /URI and /RichMedia. PDF format is based on a specific structure with possible keyword elements.
Pdf-parser and pdfid are capable of doing that.
See --help of each tool for creating the correct command. Pdf-parser can print each section of the PDF file.
E.g. pdfid is simple to use at a basic level:
python3 DidierStevensSuite/pdfid.py <FILENAME>
One of the PDF files is ‘malicious’. What it attempts to do is not working in most PDF readers anymore.
Tools for analysing Microsoft Office documents (OLE2 format, also known as Compound File Binary Format) are listed below. Usually, these files end with the file extensions ‘.doc’ or ‘.xls’.
For example, the tool olevba from oletools can be used for analysing VBA macros from the document, and detecting some suspicious keywords:
olevba <FILENAME>
.
.
.
.
+----------+--------------------+---------------------------------------------+
|Type |Keyword |Description |
+----------+--------------------+---------------------------------------------+
|AutoExec |AutoOpen |Runs when the Word document is opened |
|Suspicious|powershell |May run PowerShell commands |
|Suspicious|EncodedCommand |May run PowerShell commands |
|Suspicious|Base64 Strings |Base64-encoded strings were detected, may be |
| | |used to obfuscate strings (option --decode to|
| | |see all) |
|IOC |calc.exe |Executable file name |
|IOC |wonderful.ps1 |Executable file name |
+----------+--------------------+---------------------------------------------+
One of the .doc files is ‘malicious’.
Which files might seem to do something abnormal? What do they attempt to do on a general level? Are they executing some code?
Record your findings
Use your generated document set for the questions below. Enter just the requested indicator or decoded text in each field. Keep notes on the commands and code you use to understand each document, but no separate write-up is required when you complete this task through automated grading.
Sign in required
Sign in with the GitHub account linked to your university email.Document investigation
Prepare and download your task2-documents.zip, then trace what each suspicious document tries to do, using the previously mentioned tools. Record the evidence as you find it.Follow the PDF action
Which PDF is suspicious?
Compare the PDF files in your generated set and identify the one with abnormal behavior.What triggers the opening action?
Find the PDF catalog key that causes an action to run when the document opens.What kind of action runs?
Inspect the suspicious action dictionary. What is the exact value of its /S entry?What message does the JavaScript reconstruct?
Recover the original text from the encoded data, without opening or executing the document.Which method handles the target?
Identify the JavaScript method that attempts to export or launch the target.What does the PDF try to launch?
Follow the action to its target executable.Trace the Office macro
Which Office document is suspicious?
Inspect the DOC files in your generated set and identify the one containing the suspicious macro.Where does the macro begin?
Which VBA procedure runs automatically?Which interpreter does it start?
Follow the command launched by the macro and identify its command interpreter.How is the encoded command passed?
Which exact PowerShell option carries the encoded command?What script is written to disk?
Decode the command and find the filename of the script written by the macro.What does that script launch?
Read the written script and identify the executable it starts.Note that these examples are not sophisticated malware. They are just demonstrating the basic actions. You can never fully trust the tools.