oletools: OLE Analysis Tools

0.60.2 · active · verified Fri Apr 10

oletools is a Python package providing a collection of tools to analyze Microsoft Office and OLE (Object Linking and Embedding) files, also known as Structured Storage or Compound Document File Format. It's primarily used for malware analysis and incident response (DFIR). The current version is 0.60.2, with an active release cadence focused on bug fixes and new detection capabilities.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `olevba` to detect and analyze VBA macros in a (dummy) OLE file. In a real scenario, `dummy_file_path` would point to an actual Office document. The `VBA_Parser` provides methods for macro detection, analysis, and extraction of suspicious keywords and IOCs. Remember to close the parser with `vbaparser.close()` to release file handles.

import os
from oletools.olevba import VBA_Parser, get_filepath_or_data

# Create a dummy OLE file for demonstration
dummy_file_path = 'dummy_macro.doc'
with open(dummy_file_path, 'wb') as f:
    # A very basic, non-functional OLE header structure
    # In a real scenario, this would be a proper MS Office file
    f.write(b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1\x00\x00\x00\x00\x00\x00\x00\x00')
    f.write(b'\x00' * 500) # Padding to make it somewhat resemble a file

try:
    # Initialize VBA_Parser with the file path
    # olevba handles file paths and file-like objects
    vbaparser = VBA_Parser(dummy_file_path)

    if vbaparser.detect_vba_macros():
        print(f"VBA Macros detected in {dummy_file_path}:")
        # Analyze and print macro details
        results = vbaparser.analyze_macros()
        for kw, description, typ, filename, original_code, code in results:
            print(f"  [{typ}] {kw}: {description}")
            if code:
                print(f"    Code:\n{code[:200]}...")
    else:
        print(f"No VBA macros detected in {dummy_file_path}.")

    vbaparser.close()

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(dummy_file_path):
        os.remove(dummy_file_path)

view raw JSON →