Chuyển đến nội dung chính

Bảo vệ và phân tích một tệp

Trong Chế độ Bảo vệ, các policy quản lý nội dung cho phép kiểm soát nhiều loại nội dung tệp khác nhau như mã thực thi, nội dung biểu mẫu tương tác và một số hành động (ví dụ: liên kết ngoài hoặc việc thực thi JavaScript). Các phần tử tệp này được biết là những vectơ tấn công phổ biến khi xuất hiện trong một tệp. policy quản lý nội dung sẽ xác định cách Glasswall Embedded Engine nên xử lý các cấu trúc này. Trong Chế độ Phân tích, các cấu trúc này được báo cáo là SanitisationItems. policy quản lý nội dung khác nhau giữa các loại tệp được hỗ trợ.

Các chỉnh sửa tự động để đưa tệp trở lại đúng đặc tả tệp cũng được thực hiện khi tái tạo tệp. Mục đích của việc này là để cho phép Glasswall Embedded Engine loại bỏ các mối đe dọa ẩn trong cấu trúc tệp, đồng thời ngăn chặn khả năng kích hoạt khai thác thông qua việc lạm dụng các thành phần cấu trúc trong tệp. Trong Chế độ Phân tích, các nội dung này được báo cáo là RemedyItems.

Có thể bảo vệ từng tệp riêng lẻ từ đường dẫn tệp hoặc trong bộ nhớ bằng các phương thức protect_file hoặc protect_directory.

Ví dụ

Bảo vệ

Bảo vệ từ đường dẫn tệp sang đường dẫn tệp

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

Bảo vệ từ đường dẫn tệp sang bộ nhớ

protect_file trả về các byte của tệp đã được bảo vệ. Ví dụ bên dưới minh họa việc gán biến file_bytes. Chúng ta có thể thấy rằng sau khi làm sạch, 8 byte đầu tiên của file_bytes khớp với chữ ký tệp của định dạng 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'

Bảo vệ từ bộ nhớ

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'

Bảo vệ các tệp trong một thư mục

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

Bảo vệ các tệp trong một thư mục có thể chứa các loại tệp không được hỗ trợ

Hành vi mặc định của trình bao bọc Python của Glasswall là đưa ra ngoại lệ tương ứng (xem: glasswall.libraries.editor.errors) nếu quá trình xử lý thất bại. Việc truyền raise_unsupported=False sẽ ngăn không cho ngoại lệ được đưa ra và có thể hữu ích khi làm việc với một thư mục chứa hỗn hợp cả loại tệp được hỗ trợ và không được hỗ trợ, trong trường hợp mong muốn xử lý càng nhiều tệp càng tốt thay vì chấm dứt ở lần thất bại đầu tiên.

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
)

Bảo vệ các tệp trong một thư mục bằng policy quản lý nội dung tùy chỉnh

Sử dụng 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",
}
}
)
)

Bảo vệ các tệp trong một thư mục theo điều kiện dựa trên định dạng tệp

Ví dụ dưới đây minh họa việc chỉ xử lý các tệp .doc.docx từ một thư mục lồng nhau chứa nhiều định dạng tệp.

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)


Phân tích

Báo cáo Embedded Engine cung cấp mô tả chi tiết, không phụ thuộc loại tệp, về dữ liệu và được ghi lại ở định dạng XML. Cấu trúc của báo cáo này tuân theo một Analysis Report XSD, được thiết kế để đơn giản hóa việc phân tích cú pháp và xử lý, giúp việc tích hợp và phân tích dữ liệu trở nên dễ dàng hơn. Xem Báo cáo Engine.

Có thể phân tích từng tệp riêng lẻ từ đường dẫn tệp hoặc trong bộ nhớ bằng phương thức analyse_file, hoặc có thể phân tích tất cả tệp trong một thư mục bằng phương thức analyse_directory.

Phân tích từ đường dẫn tệp sang đường dẫn tệp

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

Phân tích từ đường dẫn tệp vào bộ nhớ

analyse_file trả về bytes của tệp xml báo cáo phân tích. Ví dụ dưới đây minh họa việc gán biến analysis_report và kiểm tra nội dung ở phần đầu của một báo cáo phân tích 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'

Phân tích từ bộ nhớ

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'

Phân tích tệp trong một thư mục

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

Phân tích tệp trong một thư mục có thể chứa các loại tệp không được hỗ trợ

Hành vi mặc định của trình bao bọc Python của Glasswall là đưa ra ngoại lệ tương ứng (xem: glasswall.libraries.editor.errors) nếu quá trình xử lý thất bại. Việc truyền raise_unsupported=False sẽ ngăn không cho ngoại lệ được đưa ra và có thể hữu ích khi làm việc với một thư mục chứa hỗn hợp cả loại tệp được hỗ trợ và không được hỗ trợ, trong trường hợp mong muốn xử lý càng nhiều tệp càng tốt thay vì chấm dứt ở lần thất bại đầu tiên.

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
)

Phân tích các tệp trong một thư mục bằng policy quản lý nội dung tùy chỉnh

Sử dụng 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
)

Phân tích các tệp trong một thư mục theo điều kiện dựa trên định dạng tệp

Ví dụ dưới đây minh họa việc chỉ xử lý các tệp .doc.docx từ một thư mục lồng nhau chứa nhiều định dạng tệp.

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)


Bảo vệ và Phân tích

Các hàm cấp cao này cho phép bạn chạy các phương thức Protect with Analysis trong một phiên duy nhất. Để biết thêm thông tin, hãy xem các liên kết tài liệu bên dưới.