Lumaktaw sa pangunahing nilalaman

Protektahan at suriin ang isang file

Sa Protect Mode, ang mga policy sa pamamahala ng nilalaman ay nagbibigay-daan sa pagkontrol ng iba't ibang uri ng nilalaman ng file gaya ng executable code, interactive form content, at ilang aksyon (hal., external links o ang pagpapatakbo ng JavaScript). Kilala ang mga elementong ito ng file bilang mga karaniwang attack vector kapag natatagpuan ang mga ito sa loob ng isang file. Tutukuyin ng content management policy kung paano dapat iproseso ng Glasswall Embedded Engine ang mga istrukturang ito. Sa Analysis Mode, iniuulat ang mga ito bilang SanitisationItems. Nagkakaiba ang content management policy sa iba't ibang sinusuportahang uri ng file.

Isinasagawa rin ang mga awtomatikong pagwawasto pabalik sa file specification kapag nire-regenerate ang file. Ang layunin nito ay upang bigyang-daan ang Glasswall Embedded Engine na alisin ang mga banta na nakatago sa loob ng istruktura ng file, pati na rin maiwasan ang posibilidad na ma-activate ang mga exploit sa pamamagitan ng maling paggamit ng mga structural component sa file. Sa Analysis Mode, iniuulat ang mga ito bilang RemedyItems.

Maaaring protektahan ang mga file nang paisa-isa mula sa isang file path o sa memory gamit ang mga paraang protect_file o protect_directory.

Mga Halimbawa

Protektahan

Protektahan mula sa file path papunta sa file path

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to sanitise a file, writing the sanitised file to a new path
editor.protect_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
output_file=r"C:\gwpw\output\editor\protect_f2f\TestFile_11.doc",
)

Protektahan mula sa file path papuntang memory

Ang protect_file ay nagbabalik ng bytes ng protektadong file. Ipinapakita ng halimbawa sa ibaba ang pag-assign sa variable na file_bytes. Makikita natin na pagkatapos ng sanitisation, ang unang 8 bytes ng file_bytes ay tumutugma sa file signature para sa Microsoft Compound File Binary (CFB) format, D0 CF 11 E0 A1 B1 1A E1.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to sanitise a file in memory, returning the file bytes in memory
file_bytes = editor.protect_file(
input_file=r"C:\gwpw\input\TestFile_11.doc"
)

assert file_bytes[:8] == b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'

Protektahan mula sa memory

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Read file from disk to memory
with open(r"C:\gwpw\input\TestFile_11.doc", "rb") as f:
input_bytes = f.read()

# Use the default policy to sanitise a file
file_bytes = editor.protect_file(
input_file=input_bytes,
)

assert file_bytes[:8] == b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'

Protektahan ang mga file sa isang directory

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to protect a directory of files, writing the sanitised files to a new directory.
editor.protect_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\protect_directory"
)

Protektahan ang mga file sa isang directory na maaaring naglalaman ng mga hindi suportadong uri ng file

Ang default na pag-uugali ng Glasswall Python wrapper ay mag-raise ng kaugnay na exception (tingnan ang: glasswall.libraries.editor.errors) kung mabigo ang pagproseso. Ang pagpasa ng raise_unsupported=False ay pipigil sa pag-raise ng exception at maaaring maging kapaki-pakinabang kapag nagtatrabaho sa isang directory na naglalaman ng halo ng parehong supported at unsupported na mga file type kapag nais na maproseso ang pinakamarami sa mga file hangga't maaari sa halip na tumigil sa unang pagkabigo.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to protect a directory of files, writing the sanitised files to a new directory.
editor.protect_directory(
input_directory=r"C:\gwpw\input_with_unsupported_file_types",
output_directory=r"C:\gwpw\output\editor\protect_directory_unsupported",
raise_unsupported=False
)

Protektahan ang mga file sa isang directory gamit ang custom na content management policy

Gamit ang glasswall.content_management.policies.Editor:

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use a custom Editor policy to sanitise all files in the input directory
# and write them to the input_sanitised directory. If macros are present
# in ppt or word files, the file will be marked as non-conforming and blocked.
# If internal or external hyperlinks are present in word files they will not
# be sanitised, and will remain in the regenerated document.
editor.protect_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\protect_directory_custom",
content_management_policy=glasswall.content_management.policies.Editor(
default="sanitise",
config={
"pptConfig": {
"macros": "disallow",
},
"wordConfig": {
"internal_hyperlinks": "allow",
"external_hyperlinks": "allow",
"macros": "disallow",
}
}
)
)

Kondisyonal na protektahan ang mga file sa isang directory batay sa format ng file

Ipinapakita ng halimbawa sa ibaba ang pagproseso lamang ng mga .doc at .docx file mula sa isang nested directory na naglalaman ng maraming format ng file.

import os

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

input_directory = r"C:\gwpw\input"
output_directory = r"C:\gwpw\output\editor\protect_directory_file_format"

# Iterate relative file paths from input_directory
for relative_file in glasswall.utils.list_file_paths(input_directory, absolute=False):
# Construct absolute paths
input_file = os.path.join(input_directory, relative_file)
output_file = os.path.join(output_directory, relative_file)

# Get the file type of the file
file_type = editor.determine_file_type(
input_file=input_file,
as_string=True,
raise_unsupported=False
)

# Protect only doc and docx files
if file_type in ["doc", "docx"]:
editor.protect_file(input_file, output_file)


Pagsusuri

Ang isang ulat ng Embedded Engine ay nagbibigay ng detalyado at file-type agnostic na paglalarawan ng data at nilo-log sa XML format. Ang istruktura ng ulat na ito ay sumusunod sa isang Analysis Report XSD, na idinisenyo upang pasimplehin ang parsing at processing, na tinitiyak ang mas madaling integration at analysis ng data. Tingnan ang Engine Reporting.

Maaaring suriin ang mga file nang paisa-isa mula sa isang file path o nasa memory gamit ang paraang analyse_file, o maaaring suriin ang lahat ng file mula sa isang directory gamit ang paraang analyse_directory.

Magsuri mula file path papuntang file path

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to analyse a file, writing the analysis report to a new path
editor.analyse_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
output_file=r"C:\gwpw\output\editor\analyse_f2f\TestFile_11.doc.xml",
)

Magsuri mula file path papuntang memory

Ang analyse_file ay nagbabalik ng bytes ng xml file ng analysis report. Ipinapakita ng halimbawa sa ibaba ang pag-assign sa variable na analysis_report at ang pagsuri sa nilalaman ng simula ng isang Editor analysis report.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to analyse a file
analysis_report = editor.analyse_file(
input_file=r"C:\gwpw\input\TestFile_11.doc",
)

assert analysis_report[:500] == b'<?xml version="1.0" encoding="utf-8"?>\n<gw:GWallInfo xsi:schemaLocation="http://glasswall.com/namespace/gwallInfo.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gw="http://glasswall.com/namespace">\n\t<gw:DocumentStatistics>\n\t\t<gw:DocumentSummary>\n\t\t\t<gw:TotalSizeInBytes>35840</gw:TotalSizeInBytes>\n\t\t\t<gw:FileType>doc</gw:FileType>\n\t\t\t<gw:Version>Not Applicable</gw:Version>\n\t\t\t<gw:InputSHA256>9FDE85B8800C1019D2865FA298A7F75873E09870B71F9825827E354B865686A6</gw:InputSHA256>\n\t\t\t<gw'

Magsuri mula sa memory

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Read file from disk to memory
with open(r"C:\gwpw\input\TestFile_11.doc", "rb") as f:
input_bytes = f.read()

# Use the default policy to analyse a file
analysis_report = editor.analyse_file(
input_file=input_bytes,
)

assert analysis_report[:500] == b'<?xml version="1.0" encoding="utf-8"?>\n<gw:GWallInfo xsi:schemaLocation="http://glasswall.com/namespace/gwallInfo.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gw="http://glasswall.com/namespace">\n\t<gw:DocumentStatistics>\n\t\t<gw:DocumentSummary>\n\t\t\t<gw:TotalSizeInBytes>35840</gw:TotalSizeInBytes>\n\t\t\t<gw:FileType>doc</gw:FileType>\n\t\t\t<gw:Version>Not Applicable</gw:Version>\n\t\t\t<gw:InputSHA256>9FDE85B8800C1019D2865FA298A7F75873E09870B71F9825827E354B865686A6</gw:InputSHA256>\n\t\t\t<gw'

Suriin ang mga file sa isang directory

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to analyse a directory of files, writing the analysis reports to a new directory.
editor.analyse_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\analyse_directory"
)

I-analyse ang mga file sa isang directory na maaaring naglalaman ng mga hindi suportadong uri ng file

Ang default na pag-uugali ng Glasswall Python wrapper ay mag-raise ng kaugnay na exception (tingnan ang: glasswall.libraries.editor.errors) kung mabigo ang pagproseso. Ang pagpasa ng raise_unsupported=False ay pipigil sa pag-raise ng exception at maaaring maging kapaki-pakinabang kapag nagtatrabaho sa isang directory na naglalaman ng halo ng parehong supported at unsupported na mga file type kapag nais na maproseso ang pinakamarami sa mga file hangga't maaari sa halip na tumigil sa unang pagkabigo.

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use the default policy to analyse a directory of files, writing the analysis reports to a new directory.
editor.analyse_directory(
input_directory=r"C:\gwpw\input_with_unsupported_file_types",
output_directory=r"C:\gwpw\output\editor\analyse_directory_unsupported",
raise_unsupported=False
)

I-analyse ang mga file sa isang directory gamit ang custom na content management policy

Gamit ang glasswall.content_management.policies.Editor:

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

# Use a custom Editor policy to analyse all files in the input directory
# and write them to analyse_directory_custom directory. If macros are
# present in ppt or word files, a GeneralFail exception will be raised if the
# raise_unsupported argument is left at its default value of False, but the
# analysis report will still be written to file and will contain IssueItems.
# If internal or external hyperlinks are present in word files they will not
# be sanitised, and will remain in the regenerated document.
editor.analyse_directory(
input_directory=r"C:\gwpw\input",
output_directory=r"C:\gwpw\output\editor\analyse_directory_custom",
content_management_policy=glasswall.content_management.policies.Editor(
default="sanitise",
config={
"pptConfig": {
"macros": "disallow",
},
"wordConfig": {
"internal_hyperlinks": "allow",
"external_hyperlinks": "allow",
"macros": "disallow",
}
}
),
raise_unsupported=False
)

I-analyse ang mga file sa isang directory nang may kondisyon batay sa format ng file

Ipinapakita ng halimbawa sa ibaba ang pagproseso lamang ng mga .doc at .docx file mula sa isang nested directory na naglalaman ng maraming format ng file.

import os

import glasswall


# Load the Glasswall Editor library
editor = glasswall.Editor(r"C:\gwpw\libraries\10.0")

input_directory = r"C:\gwpw\input"
output_directory = r"C:\gwpw\output\editor\analyse_directory_file_format"

# Iterate relative file paths from input_directory
for relative_file in glasswall.utils.list_file_paths(input_directory, absolute=False):
# Construct absolute paths
input_file = os.path.join(input_directory, relative_file)
output_file = os.path.join(output_directory, relative_file + ".xml")

# Get the file type of the file
file_type = editor.determine_file_type(
input_file=input_file,
as_string=True,
raise_unsupported=False
)

# Analyse only doc and docx files
if file_type in ["doc", "docx"]:
editor.analyse_file(input_file, output_file)


Protect at Analyse

Hinahayaan ka ng mga high-level function na ito na patakbuhin ang Protect gamit ang mga Analysis method sa loob ng iisang session. Para sa higit pang impormasyon, tingnan ang mga link ng dokumentasyon sa ibaba.