Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PE: Add Return Flow Guard support #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion checksec/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self):
self.table_pe.add_column("SafeSEH", justify="center")
self.table_pe.add_column("Force Integrity", justify="center")
self.table_pe.add_column("Control Flow Guard", justify="center")
self.table_pe.add_column("Return Flow Guard", justify="center")
self.table_pe.add_column("Isolation", justify="center")

# init console
Expand Down Expand Up @@ -270,6 +271,11 @@ def add_checksec_result(self, filepath: Path, checksec: Union[ELFChecksecData, P
else:
guard_cf_res = "[green]Yes"

if not checksec.rfg:
rfg_res = "[red]No"
else:
rfg_res = "[green]Yes"

if not checksec.isolation:
isolation_res = "[red]No"
else:
Expand All @@ -286,6 +292,7 @@ def add_checksec_result(self, filepath: Path, checksec: Union[ELFChecksecData, P
safe_seh_res,
force_integrity_res,
guard_cf_res,
rfg_res,
isolation_res,
)
else:
Expand Down Expand Up @@ -345,7 +352,8 @@ def add_checksec_result(self, filepath: Path, checksec: Union[ELFChecksecData, P
"seh": checksec.seh,
"safe_seh": checksec.safe_seh,
"guard_cf": checksec.guard_cf,
"force_integrity": checksec.force_integrity
"rfg": checksec.rfg,
"force_integrity": checksec.force_integrity,
}
else:
raise NotImplementedError
Expand Down
24 changes: 22 additions & 2 deletions checksec/pe.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from collections import namedtuple
from pathlib import Path
from typing import Set

import lief
from lief.PE import DLL_CHARACTERISTICS, HEADER_CHARACTERISTICS, MACHINE_TYPES
from lief.PE import DLL_CHARACTERISTICS, HEADER_CHARACTERISTICS, MACHINE_TYPES, GUARD_CF_FLAGS

from .binary import BinarySecurity

Expand All @@ -19,6 +20,7 @@
"safe_seh",
"force_integrity",
"guard_cf",
"rfg",
"isolation",
],
)
Expand Down Expand Up @@ -104,7 +106,24 @@ def has_guard_cf(self) -> bool:

# code integrity: November 2015 (Windows 10 1511)

# Return Flow Guard: October 2016 (Windows 10 Redstone 2)
@property
def has_return_flow_guard(self) -> bool:
"""Whether Return Flow Guard is enabled"""
# Return Flow Guard: October 2016 (Windows 10 Redstone 2)
# winchecksec:
# https://github.com/trailofbits/winchecksec/blob/v2.0.0/checksec.cpp#L262
# Tencent lab article
# https://xlab.tencent.com/en/2016/11/02/return-flow-guard/
try:
guard_flags: Set[GUARD_CF_FLAGS] = self.bin.load_configuration.guard_cf_flags_list
return (
True
if GUARD_CF_FLAGS.GRF_INSTRUMENTED in guard_flags
and (GUARD_CF_FLAGS.GRF_ENABLE in guard_flags or GUARD_CF_FLAGS.GRF_STRICT in guard_flags)
else False
)
except (lief.not_found, AttributeError):
return False

@property
def has_isolation(self) -> bool:
Expand All @@ -126,5 +145,6 @@ def checksec_state(self) -> PEChecksecData:
safe_seh=self.has_safe_seh,
force_integrity=self.has_force_integrity,
guard_cf=self.has_guard_cf,
rfg=self.has_return_flow_guard,
isolation=self.has_isolation,
)