From 82dee78837d1d21fa80271befa909efd86554567 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Thu, 12 Oct 2023 14:59:23 +0200 Subject: Show how to create a Python plugin for ROST. --- python-plugins/zipbomb/__init__.py | 2 ++ python-plugins/zipbomb/plugin.py | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 python-plugins/zipbomb/__init__.py create mode 100644 python-plugins/zipbomb/plugin.py diff --git a/python-plugins/zipbomb/__init__.py b/python-plugins/zipbomb/__init__.py new file mode 100644 index 0000000..a9d4527 --- /dev/null +++ b/python-plugins/zipbomb/__init__.py @@ -0,0 +1,2 @@ + +from zipbomb.plugin import ZipBombChecker as AutoLoad diff --git a/python-plugins/zipbomb/plugin.py b/python-plugins/zipbomb/plugin.py new file mode 100644 index 0000000..be8c12f --- /dev/null +++ b/python-plugins/zipbomb/plugin.py @@ -0,0 +1,71 @@ + +import io +import zipfile + +from pychrysalide.analysis.scan import ScanRegisteredItem +from pychrysalide.analysis.scan.exprs import ScanLiteralExpression +from pychrysalide.core import get_rost_root_namespace +from pychrysalide.plugins import PluginModule + + +class ZipBombKeyword(ScanRegisteredItem): + """Introduce a new keyword in the ROST grammar.""" + + _name = 'is_zip_bomb' + + + def _reduce(self, ctx, scope): + + expr = None + + try: + + has_bomb = False + + # compress < 10% plain => bomb + ZIP_BOMB_RATIO = 0.01 + + data = io.BytesIO(ctx.content.data) + + zf = zipfile.ZipFile(data) + + info = zf.infolist() + + for i in info: + + ratio = i.compress_size / i.file_size + + has_bomb = ratio < ZIP_BOMB_RATIO + + if has_bomb: + break + + expr = ScanLiteralExpression(has_bomb) + + except: + pass + + return expr + + +class ZipBombChecker(PluginModule): + """Check for ZIP bombs.""" + + _name = 'ZipBombChecker' + _desc = 'Check if a ZIP archive may contain heavy files on extraction' + _version = '0.1' + _url = 'https://www.chrysalide.re/' + + _actions = ( ) + + + def __init__(self): + """Create a ZipBombChecker plugin instance.""" + + super().__init__() + + ns = get_rost_root_namespace() + + kw = ZipBombKeyword() + + ns.register_item(kw) -- cgit v0.11.2-87-g4458