How to Quarantine a Dangerous File in Python

Cloudmersive
2 min readJan 6, 2023

--

Malicious files can wreak havoc on our system if we aren’t careful. One easy way to prevent this from happening is to quickly enclose the malicious file in a zip archive, temporarily locking it away.

The below API solution will make it simple and easy to quarantine dangerous files in your Python applications via the zip method. After copying from the complementary, ready-to-run Python code examples provided below, you’ll be able to programmatically password-protect and encrypt dangerous files during your file processing & security workflows.

To get started, we need to run the below command to install the Python SDK:

pip install cloudmersive-convert-api-client

Next up, we need to copy in the remaining code, starting with the imports & finishing with the function:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint

# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'



# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ZipArchiveApi(cloudmersive_convert_api_client.ApiClient(configuration))
password = 'password_example' # str | Password to place on the Zip file; the longer the password, the more secure
input_file1 = '/path/to/inputfile' # file | First input file to perform the operation on.
encryption_algorithm = 'encryption_algorithm_example' # str | Encryption algorithm to use; possible values are AES-256 (recommended), AES-128, and PK-Zip (not recommended; legacy, weak encryption algorithm). Default is AES-256. (optional)

try:
# Create an encrypted zip file to quarantine a dangerous file
api_response = api_instance.zip_archive_zip_create_quarantine(password, input_file1, encryption_algorithm=encryption_algorithm)
pprint(api_response)
except ApiException as e:
print("Exception when calling ZipArchiveApi->zip_archive_zip_create_quarantine: %s\n" % e)

To complete our call, we now need to include a Cloudmersive API key, which we can get by registering a free account here (this account will yield a limit of 800 API calls per month with no additional commitments).

You can now zip quarantine dangerous files with ease!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet