How to Scan PostScript (.PS) Files for Threats in your Python App
PostScript (.ps
) files are vector-based page description documents often used in publishing workflows. Their scripting capabilities introduce a few security risks we should remain vigilant against.
Because .ps
files can contain embedded commands, they’ve been exploited at times to execute arbitrary code — or even launch denial-of-service attacks during post-upload rendering or conversion processes.
If your Python application handles .ps
file uploads in print queues, conversion tools, document preview services, or any other workflow, scanning them before processing is a crucial security measure.
Detecting PostScript Threats with an API
To that end, we can take advantage of a reliable, free virus scanning API to inspect .ps
file contents for signs of exploit behavior, malicious instructions, or formatting corruptions intended to disrupt our app services. We can implement this API directly into our Python app with minimal code changes using code examples provided below.
To get started, we can use the following command to install the SDK:
pip install cloudmersive-virus-api-client
Next, we can add the necessary imports to the top of our file:
from __future__ import print_function
import time
import cloudmersive_virus_api_client
from cloudmersive_virus_api_client.rest import ApiException
from pprint import pprint
To set up authentication, we can create a new configuration object and assign our API key string to the api_key dictionary entry (to get a free API key, we can register a free account on the Cloudmersive website):
# Configure API key authorization: Apikey
configuration = cloudmersive_virus_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
Following that, we can create an instance of the API class and configure several optional “threat rule” parameters. We can log the API response (or any possible errors) in a simple try/except block:
# create an instance of the API class
api_instance = cloudmersive_virus_api_client.ScanApi(cloudmersive_virus_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
allow_executables = true # bool | Set to false to block executable files (program code) from being allowed in the input file. Default is false (recommended). (optional)
allow_invalid_files = true # bool | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document. Default is false (recommended). (optional)
allow_scripts = true # bool | Set to false to block script files, such as a PHP files, Python scripts, and other malicious content or security threats that can be embedded in the file. Set to true to allow these file types. Default is false (recommended). (optional)
allow_password_protected_files = true # bool | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords. Set to true to allow these file types. Default is false (recommended). (optional)
allow_macros = true # bool | Set to false to block macros and other threats embedded in document files, such as Word, Excel and PowerPoint embedded Macros, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
allow_xml_external_entities = true # bool | Set to false to block XML External Entities and other threats embedded in XML files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
allow_insecure_deserialization = true # bool | Set to false to block Insecure Deserialization and other threats embedded in JSON and other object serialization files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
allow_html = true # bool | Set to false to block HTML input in the top level file; HTML can contain XSS, scripts, local file accesses and other threats. Set to true to allow these file types. Default is false (recommended) [for API keys created prior to the release of this feature default is true for backward compatability]. (optional)
restrict_file_types = 'restrict_file_types_example' # str | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files. All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false. Set restrictFileTypes parameter to null or empty string to disable; default is disabled. (optional)
try:
# Advanced Scan a file for viruses
api_response = api_instance.scan_file_advanced(input_file, allow_executables=allow_executables, allow_invalid_files=allow_invalid_files, allow_scripts=allow_scripts, allow_password_protected_files=allow_password_protected_files, allow_macros=allow_macros, allow_xml_external_entities=allow_xml_external_entities, allow_insecure_deserialization=allow_insecure_deserialization, allow_html=allow_html, restrict_file_types=restrict_file_types)
pprint(api_response)
except ApiException as e:
print("Exception when calling ScanApi->scan_file_advanced: %s\n" % e)
For reference, we can expect our API response to follow the below model:
{
"CleanResult": true,
"ContainsExecutable": true,
"ContainsInvalidFile": true,
"ContainsScript": true,
"ContainsPasswordProtectedFile": true,
"ContainsRestrictedFileFormat": true,
"ContainsMacros": true,
"ContainsXmlExternalEntities": true,
"ContainsInsecureDeserialization": true,
"ContainsHtml": true,
"ContainsUnsafeArchive": true,
"ContainsOleEmbeddedObject": true,
"VerifiedFileFormat": "string",
"FoundViruses": [
{
"FileName": "string",
"VirusName": "string"
}
],
"ContentInformation": {
"ContainsJSON": true,
"ContainsXML": true,
"ContainsImage": true,
"RelevantSubfileName": "string",
"IsAuthenticodeSigned": true
}
}
That’s all there is to it — we’ve now implemented a simple, effective solution to protect our Python app from insecure .ps
content.
It’s worth noting that this API can handle dozens of other file types — including PDF, MS Office documents, image formats, and compressed archive formats.