ZipFile

Zip a file and upload to war room.

python · Common Scripts

Details

IDZipFile
Languagepython
From Version5.0.0
Docker Imagedemisto/py3-tools:1.0.0.11597437
TagsUtility file

README

Zip a file and upload to war room

Supported Cortex XSOAR versions: 5.0.0 and later.

Script Data


Name Description
Script Type python3
Tags Utility, file
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
entryID CSV list of entry ids for the files to zip.
zipName Name of the output file, for example: zipName=”test” would result in output file “test.zip”
password Used to create a password protected zip file. Example: password=”abcd”

Outputs


Path Description Type
ZipFile.ZippedFile The zipped file string
File.Name The full file name (including file extension). String
File.EntryID The ID for locating the file in the War Room. String
File.Size The size of the file in bytes. Number
File.MD5 The MD5 hash of the file. String
File.SHA1 The SHA1 hash of the file. String
File.SHA256 The SHA1 hash of the file. String
File.SHA512 The SHA512 hash of the file. String
File.SSDeep The ssdeep hash of the file (same as displayed in file entries). String
File.Extension The file extension, for example: ‘xls’. String
File.Type The file type, as determined by libmagic (same as displayed in file entries). String

Troubleshooting

Because of security reasons we support only AES encryption which is not supported on the Windows OS without 3rd party unzip applications. For more information about the encryption methods, see https://en.wikipedia.org/wiki/ZIP_(file_format)#Encryption.

import os
import tempfile

import pytest
import pyzipper
from ZipFile import compress_multiple, escape_illegal_characters_in_file_name

ESCAPE_CHARACTERS_PACK = [
    ("/Users/user/Downloads/b/a/testingfile.txt", "-Users-user-Downloads-b-a-testingfile.txt"),
    ("/Users/user/Downloads/?///testingfile.txt", "-Users-user-Downloads-testingfile.txt"),
    ("/Users/user/Downloads/b/a/testingfile*.txt", "-Users-user-Downloads-b-a-testingfile-.txt"),
    ("abcde", "abcde"),
]


def unzip(zip_file_path: str, password: str = None):
    with tempfile.TemporaryDirectory() as unzip_dir, pyzipper.AESZipFile(zip_file_path) as zf:
        zf.pwd = bytes(password, "utf-8") if password else None
        zf.extractall(path=unzip_dir)


@pytest.mark.parametrize(("input_name", "output_name"), ESCAPE_CHARACTERS_PACK)
def test_escape_characters_in_file_name(input_name, output_name):
    assert escape_illegal_characters_in_file_name(input_name) == output_name


def test_compress_multiple_with_password():
    """
    Given:
        - A directory with files to zip.
    When:
        - Calling the function compress_multiple.
    Then:
        - The function should not raise an exception.
    """
    test_data_dir = "./test_data"
    file_names = [
        os.path.join(test_data_dir, f) for f in os.listdir(test_data_dir) if os.path.isfile(os.path.join(test_data_dir, f))
    ]
    with tempfile.NamedTemporaryFile(suffix=".zip") as tmp_zip:
        zip_name = tmp_zip.name
        compress_multiple(file_names=file_names, zip_name=zip_name, password="123")


def test_zip_and_unzip_with_password():
    """
    Given:
        - A directory with files to zip.
    When:
        - Calling the function compress_multiple with a password.
    Then:
        - We can unzip the file with the correct password.
    """
    test_data_dir = "./test_data"
    file_names = [
        os.path.join(test_data_dir, f) for f in os.listdir(test_data_dir) if os.path.isfile(os.path.join(test_data_dir, f))
    ]
    with tempfile.NamedTemporaryFile(suffix=".zip") as tmp_zip:
        zip_name = tmp_zip.name
        compress_multiple(file_names=file_names, zip_name=zip_name, password="123")
        unzip(zip_name, "123")


def test_unzip_wrong_password():
    """
    Given:
        - A directory with files to zip.
    When:
        - Calling the function compress_multiple with a password.
    Then:
        - We can not unzip the file with the wrong password.
    """
    test_data_dir = "./test_data"
    file_names = [
        os.path.join(test_data_dir, f) for f in os.listdir(test_data_dir) if os.path.isfile(os.path.join(test_data_dir, f))
    ]
    with tempfile.NamedTemporaryFile(suffix=".zip") as tmp_zip:
        zip_name = tmp_zip.name
        compress_multiple(file_names=file_names, zip_name=zip_name, password="123")
        with pytest.raises(Exception) as e:
            unzip(zip_name, "1234")

        assert "Bad password" in e.value.args[0]