How to Check SharePoint Files for Scripts, Executables & Macros in Python
Using ready-to-run Python code examples, we can easily determine if suspicious SharePoint files contain malicious content.
We’ll be using the below Python code to call a free threat scanning API that simultaneously checks files for viruses, malware, scripts, executables, macros, XML external entities, and other threatening content types (with in-depth file type verification).
To use this API for free, we’ll just need a free API key, which will allow us to make up to 800 API calls per month with no additional commitments.
Before we copy any Python code, let’s first gather the below SharePoint details:
- Client ID & Client Secret (these require a few steps to obtain; google “how to get client ID and client secret” from SharePoint for a quick walkthrough)
- SharePoint Domain Name
- Site ID
- Tenant ID (optional)
- File Path (if this contains Unicode characters, please base64 encode the file path and prepend with ‘base64’)
- Item ID
Let’s now go ahead and install the client SDK with pip install:
pip install cloudmersive-virus-api-client
Next, let’s add the imports and call the function. We can customize threat rules by setting our own values in the request variables below our SharePoint details. If, for example, we set allowExecutables
to “False”, files containing executable content will return a CleanResult: “False”
response from the threat scanning service. This is the same response that files infected with viruses and malware would receive.
from __future__ import print_function
import time
import cloudmersive_virus_api_client
from cloudmersive_virus_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_virus_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_virus_api_client.ScanCloudStorageApi(cloudmersive_virus_api_client.ApiClient(configuration))
client_id = 'client_id_example' # str | Client ID access credentials; see description above for instructions on how to get the Client ID from the Azure Active Directory portal.
client_secret = 'client_secret_example' # str | Client Secret access credentials; see description above for instructions on how to get the Client Secret from the Azure Active Directory portal
sharepoint_domain_name = 'sharepoint_domain_name_example' # str | SharePoint Online domain name, such as mydomain.sharepoint.com
site_id = 'site_id_example' # str | Site ID (GUID) of the SharePoint site you wish to retrieve the file from
tenant_id = 'tenant_id_example' # str | Optional; Tenant ID of your Azure Active Directory (optional)
file_path = 'file_path_example' # str | Path to the file within the drive, such as 'hello.pdf' or '/folder/subfolder/world.pdf'. If the file path contains Unicode characters, you must base64 encode the file path and prepend it with 'base64:', such as: 'base64:6ZWV6ZWV6ZWV6ZWV6ZWV6ZWV'. (optional)
item_id = 'item_id_example' # str | SharePoint itemID, such as a DriveItem Id (optional)
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)
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 Virus Scan a file in a SharePoint Online Site Drive
api_response = api_instance.scan_cloud_storage_scan_share_point_online_file_advanced(client_id, client_secret, sharepoint_domain_name, site_id, tenant_id=tenant_id, file_path=file_path, item_id=item_id, 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, restrict_file_types=restrict_file_types)
pprint(api_response)
except ApiException as e:
print("Exception when calling ScanCloudStorageApi->scan_cloud_storage_scan_share_point_online_file_advanced: %s\n" % e)
It’s also worth noting we can effectively whitelist file types using the restrictFileTypes
parameter. If, for example, we wanted to ensure a SharePoint site only contained verified Excel, PDF and Word documents, we could enter the string ‘.xlsx,.pdf,.docx’
to flag all file types beyond these three.
That’s all there is to it — now we can easily scan SharePoint files for viruses, malware, scripts, executables, macros, and other threats using minimal Python code.