파일 보호 및 분석
Protect Mode에서는 콘텐츠 관리 policy를 통해 실행 코드, 대화형 양식 콘텐츠, 그리고 여러 작업(예: 외부 링크 또는 JavaScript 실행)과 같은 다양한 파일 콘텐츠 유형을 제어할 수 있습니다. 이러한 파일 요소는 파일 내에서 발견될 때 일반적인 공격 벡터로 알려져 있습니다. 콘텐츠 관리 policy는 Glasswall Embedded Engine이 이러한 구조를 어떻게 처리해야 하는지 정의합니다. Analysis Mode에서는 이러한 항목이 SanitisationItems로 보고됩니다. 콘텐츠 관리 policy는 지원되는 파일 유형마다 다릅니다.
파일을 재생성할 때 파일 사양에 맞도록 자동 수정도 수행됩니다. 이는 Glasswall Embedded Engine이 파일 구조 내에 숨겨진 위협을 제거하고, 파일 내 구조적 구성 요소의 오용을 통해 익스플로잇이 활성화될 가능성을 방지하기 위한 것입니다. Analysis Mode에서는 이러한 항목이 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는 보호된 파일의 바이트를 반환합니다. 아래 예제는 file_bytes 변수에 할당하는 방법을 보여줍니다. sanitisation 후 file_bytes의 처음 8바이트가 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
)
사용자 지정 content management 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는 분석 보고서 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를 사용하여 디렉터리의 파일 analyse
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
)
파일 형식에 따라 조건부로 디렉터리의 파일 analyse
아래 예제는 여러 파일 형식이 포함된 중첩 디렉터리에서 .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
이러한 고수준 함수는 단일 세션 내에서 Analysis 메서드와 함께 Protect를 실행할 수 있게 해줍니다. 자세한 내용은 아래 문서 링크를 참조하세요.