python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python mmpi库实现邮件检测

python 邮件检测工具mmpi的使用

作者:ddvv

这篇文章主要介绍了python 用mmpi库实现邮件检测的方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

概要介绍

mmpi,是一款使用python实现的开源邮件快速检测工具库,基于community框架设计开发。mmpi支持对邮件头、邮件正文、邮件附件的解析检测,并输出json检测报告。

mmpi,代码项目地址:https://github.com/a232319779/mmpi,pypi项目地址https://pypi.org/project/mmpi/

mmpi,邮件快速检测工具库检测逻辑:

ole文件格式:如doc、xls等,提取其中的vba宏代码、模板注入链接
zip文件格式:提取压缩文件列表,统计文件名、文件格式等
rtf文件格式:解析内嵌ole对象等
其他文件格式:如PE可执行文件

基础信息规则检测方式
yara规则检测方式

适用前提

mmpi的分析判定检测前提:邮件系统环境。脱离邮件环境上下文,检测规则的依据就不可靠了。

使用方式

1. 安装

$ pip install mmpi

备注:windows安装yara-python,可以从这里下载

2. 命令执行

$ mmpi-run $email_path

3. 快速开始

from mmpi import mmpi


def main():
  emp = mmpi()
  emp.parse('test.eml')
  report = emp.get_report()
  print(report)


if __name__ == "__main__":
  main()

4. 输出格式

{
	 // 固定字段
  "headers": [],
  "body": [],
  "attachments": [],
  "signatures": []
  // 动态字段
  "vba": [],
  "rtf": [],
}

工具特色

mmpi完全基于python开发,使用python原生email、html、zip库进行解析,基于oletool做定制化修改,支持对office文档和rtf文档的解析,再结合yara实现对其他文件的检测。

项目代码结构

.
├── mmpi
│   ├── common
│   ├── core
│   ├── data
│   │   ├── signatures
│   │   │   ├── eml
│   │   │   ├── html
│   │   │   ├── ole
│   │   │   ├── other
│   │   │   ├── rtf
│   │   │   └── zip
│   │   ├── white
│   │   └── yara
│   │     ├── exe
│   │     ├── pdf
│   │     └── vba
│   └── processing
└── tests
  └── samples

检测规则示例说明

1. PE文件伪装文档类检测
检测规则:压缩包中文件名以.exe结尾,并且中间插入20个以上空格的

class PEFakeDocument(Signature):
  authors = ["ddvv"]
  sig_type = 'zip'
  name = "pe_fake_document"
  severity = 9
  description = "PE File Fake Document"

  def on_complete(self):
    results = self.get_results()
    for result in results:
      if result.get('type', '') == self.sig_type:
        infos = result.get('value', {}).get('infos', [])
        for info in infos:
          file_type = info.get('type')
          file_name = info.get('name')
          space_count = file_name.count(' ')
          if 'exe' == file_type and space_count > 20:
            self.mark(type="zip", tag=self.name, data=info.get('name'))
            return self.has_marks()
    return None

2. DLL劫持检测
检测规则:压缩包中同时存在exe和dll文件

class DLLHijacking(Signature):
  authors = ["ddvv"]
  sig_type = 'zip'
  name = "dll_hijacking"
  severity = 9
  description = "DLL Hijacking"

  def on_complete(self):
    results = self.get_results()
    for result in results:
      if result.get('type', '') == self.sig_type:
        infos = result.get('value', {}).get('infos', [])
        file_types = [info.get('type') for info in infos]
        if set(['exe', 'dll']).issubset(file_types):
          self.mark(type="zip", tag=self.name)
          return self.has_marks()
    return None

3. RTF漏洞利用检测
检测规则:RTF文档中存在OLE对象,并且class_name是OLE2Link或者以equation开头

class RTFExploitDetected(Signature):
  authors = ["ddvv"]
  sig_type = 'rtf'
  name = "rtf_exploit_detected"
  severity = 9
  description = "RTF Exploit Detected"

  def on_complete(self):
    results = self.get_results()
    for result in results:
      if result.get('type', '') == self.sig_type:
        infos = result.get('value', {}).get('infos', [])
        for info in infos:
          if info.get('is_ole', False):
            class_name = info.get('class_name', '')
            if class_name == 'OLE2Link' or class_name.lower().startswith('equation'):
              self.mark(type="rtf", tag=self.name)
              return self.has_marks()
    return None

结果示例

结果说明:邮件包含漏洞利用的RTF文档,属于恶意邮件。

{
  "headers": [
    {
      "From": [
        {
          "name": "Mohd Mukhriz Ramli (MLNG/GNE)",
          "addr": "info@vm1599159.3ssd.had.wf"
        }
      ],
      "To": [
        {
          "name": "",
          "addr": ""
        }
      ],
      "Subject": "Re: Proforma Invoice",
      "Date": "2020-11-24 12:37:38 UTC+01:00",
      "X-Originating-IP": []
    }
  ],
  "body": [
    {
      "type": "text",
      "content": " \nDEAR SIR, \n\nPLEASE SIGN THE PROFORMA INVOICE SO THAT I CAN PAY AS SOON AS POSSIBLE.\n\nATTACHED IS THE PROFORMA INVOICE,\n\nPLEASE REPLY QUICKLY, \n\nTHANKS & REGARDS' \n\nRAJASHEKAR \n\n Dubai I Kuwait I Saudi Arabia I India I Egypt \nKuwait: +965 22261501 \nSaudi Arabia: +966 920033029 \nUAE: +971 42431343 \nEmail ID: help@rehlat.co [1]m\n \n\nLinks:\n------\n[1]\nhttps://deref-mail.com/mail/client/OV1N7sILlK8/dereferrer/?redirectUrl=https%3A%2F%2Fe.mail.ru%2Fcompose%2F%3Fmailto%3Dmailto%253ahelp%40rehlat.com"
    }
  ],
  "attachments": [
    {
      "type": "doc",
      "filename": "Proforma Invoice.doc",
      "filesize": 1826535,
      "md5": "558c4aa596b0c4259182253a86b35e8c",
      "sha1": "63982d410879c09ca090a64873bc582fcc7d802b"
    }
  ],
  "vba": [],
  "rtf": [
    {
      "is_ole": true,
      "format_id": 2,
      "format_type": "Embedded",
      "class_name": "EQUATion.3",
      "data_size": 912305,
      "md5": "a5cee525de80eb537cfea247271ad714"
    }
  ],
  "signatures": [
    {
      "name": "rtf_suspicious_detected",
      "description": "RTF Suspicious Detected",
      "severity": 3,
      "marks": [
        {
          "type": "rtf",
          "tag": "rtf_suspicious_detected"
        }
      ],
      "markcount": 1
    },
    {
      "name": "rtf_exploit_detected",
      "description": "RTF Exploit Detected",
      "severity": 9,
      "marks": [
        {
          "type": "rtf",
          "tag": "rtf_exploit_detected"
        }
      ],
      "markcount": 1
    }
  ]
}

以上就是python 邮件检测工具mmpi的使用的详细内容,更多关于python mmpi库实现邮件检测的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文