ข้ามไปยังเนื้อหาหลัก

ปกป้องและวิเคราะห์ไฟล์

ในโหมด Protect นโยบายการจัดการเนื้อหาช่วยให้สามารถควบคุมประเภทเนื้อหาไฟล์ต่าง ๆ เช่น โค้ดที่เรียกใช้งานได้ เนื้อหาฟอร์มแบบโต้ตอบ และการดำเนินการหลายประเภท (เช่น ลิงก์ภายนอกหรือการเรียกใช้ JavaScript) องค์ประกอบของไฟล์เหล่านี้เป็นที่ทราบกันว่าเป็นช่องทางการโจมตีที่พบบ่อยเมื่อพบอยู่ภายในไฟล์ นโยบายการจัดการเนื้อหาจะกำหนดว่า Glasswall Embedded Engine ควรประมวลผลโครงสร้างเหล่านี้อย่างไร ใน โหมด Analysis สิ่งเหล่านี้จะถูกรายงานเป็น SanitisationItems นโยบายการจัดการเนื้อหาจะแตกต่างกันไปตามประเภทไฟล์ที่รองรับ

ยังมีการแก้ไขกลับไปให้เป็นไปตามข้อกำหนดของไฟล์โดยอัตโนมัติเมื่อมีการสร้างไฟล์ใหม่ จุดประสงค์คือเพื่อให้ Glasswall Embedded Engine สามารถลบภัยคุกคามที่ซ่อนอยู่ภายในโครงสร้างไฟล์ได้ รวมถึงป้องกันความเป็นไปได้ในการกระตุ้นการโจมตีผ่านการใช้ส่วนประกอบเชิงโครงสร้างในไฟล์ในทางที่ผิด ใน โหมด Analysis สิ่งเหล่านี้จะถูกรายงานเป็น RemedyItems

สามารถปกป้องไฟล์แต่ละไฟล์ได้จากพาธไฟล์หรือในหน่วยความจำโดยใช้เมธอด protect_file หรือ protect_directory

ตัวอย่าง

ป้องกัน

ป้องกันจากพาธไฟล์ไปยังพาธไฟล์

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",
)

ป้องกันจากพาธไฟล์ไปยังหน่วยความจำ

protect_file จะส่งคืน bytes ของไฟล์ที่ได้รับการป้องกัน ตัวอย่างด้านล่างสาธิตการกำหนดค่าให้ตัวแปร file_bytes เราจะเห็นได้ว่าหลังจากการ sanitisation แล้ว 8 bytes แรกของ file_bytes ตรงกับfile signatureของรูปแบบ Microsoft Compound File Binary (CFB) คือ 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'

ป้องกันจากหน่วยความจำ

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'

ป้องกันไฟล์ในไดเรกทอรี

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"
)

ป้องกันไฟล์ในไดเรกทอรีที่อาจมีประเภทไฟล์ที่ไม่รองรับ

พฤติกรรมเริ่มต้นของ Glasswall Python wrapper คือการส่งข้อยกเว้นที่เกี่ยวข้อง (ดู: glasswall.libraries.editor.errors) หากการประมวลผลล้มเหลว การส่งค่า raise_unsupported=False จะป้องกันไม่ให้มีการส่งข้อยกเว้น และอาจมีประโยชน์เมื่อทำงานกับไดเรกทอรีที่มีทั้งประเภทไฟล์ที่รองรับและไม่รองรับปะปนกัน ในกรณีที่ต้องการประมวลผลไฟล์ให้ได้มากที่สุด แทนที่จะยุติการทำงานเมื่อเกิดความล้มเหลวครั้งแรก

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
)

ปกป้องไฟล์ในไดเรกทอรีโดยใช้ policy การจัดการเนื้อหาแบบกำหนดเอง

โดยใช้ 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",
}
}
)
)

ปกป้องไฟล์ในไดเรกทอรีแบบมีเงื่อนไขตามรูปแบบไฟล์

ตัวอย่างด้านล่างสาธิตการประมวลผลเฉพาะไฟล์ .doc และ .docx จากไดเรกทอรีแบบซ้อนที่มีหลายรูปแบบไฟล์

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)


การวิเคราะห์

รายงาน Embedded Engine ให้คำอธิบายข้อมูลโดยละเอียดแบบไม่ยึดติดกับประเภทไฟล์ และถูกบันทึกในรูปแบบ XML โครงสร้างของรายงานนี้เป็นไปตาม Analysis Report XSD ซึ่งออกแบบมาเพื่อให้การแยกวิเคราะห์และการประมวลผลง่ายขึ้น ช่วยให้การผสานรวมและการวิเคราะห์ข้อมูลทำได้สะดวกยิ่งขึ้น ดู Engine Reporting.

สามารถวิเคราะห์ไฟล์ทีละไฟล์ได้จากพาธของไฟล์หรือในหน่วยความจำโดยใช้เมธอด analyse_file หรือสามารถวิเคราะห์ไฟล์ทั้งหมดจากไดเรกทอรีได้โดยใช้เมธอด analyse_directory

วิเคราะห์จากพาธไฟล์ไปยังพาธไฟล์

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",
)

วิเคราะห์จากพาธไฟล์ไปยังหน่วยความจำ

analyse_file จะส่งคืน bytes ของไฟล์ xml รายงานการวิเคราะห์ ตัวอย่างด้านล่างสาธิตการกำหนดค่าให้ตัวแปร analysis_report และตรวจสอบเนื้อหาช่วงต้นของรายงานการวิเคราะห์ของ Editor

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'

วิเคราะห์จากหน่วยความจำ

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'

วิเคราะห์ไฟล์ในไดเรกทอรี

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"
)

วิเคราะห์ไฟล์ในไดเรกทอรีที่อาจมีชนิดไฟล์ที่ไม่รองรับ

พฤติกรรมเริ่มต้นของ Glasswall Python wrapper คือการส่งข้อยกเว้นที่เกี่ยวข้อง (ดู: glasswall.libraries.editor.errors) หากการประมวลผลล้มเหลว การส่งค่า raise_unsupported=False จะป้องกันไม่ให้มีการส่งข้อยกเว้น และอาจมีประโยชน์เมื่อทำงานกับไดเรกทอรีที่มีทั้งประเภทไฟล์ที่รองรับและไม่รองรับปะปนกัน ในกรณีที่ต้องการประมวลผลไฟล์ให้ได้มากที่สุด แทนที่จะยุติการทำงานเมื่อเกิดความล้มเหลวครั้งแรก

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
)

วิเคราะห์ไฟล์ในไดเรกทอรีโดยใช้ policy การจัดการเนื้อหาแบบกำหนดเอง

โดยใช้ 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
)

วิเคราะห์ไฟล์ในไดเรกทอรีแบบมีเงื่อนไขตามรูปแบบไฟล์

ตัวอย่างด้านล่างสาธิตการประมวลผลเฉพาะไฟล์ .doc และ .docx จากไดเรกทอรีแบบซ้อนที่มีหลายรูปแบบไฟล์

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 และ Analyse

ฟังก์ชันระดับสูงเหล่านี้ช่วยให้คุณเรียกใช้เมธอด Protect พร้อม Analysis ภายในเซสชันเดียวได้ สำหรับข้อมูลเพิ่มเติม โปรดดูลิงก์เอกสารด้านล่าง