11from collections import namedtuple
22from pathlib import Path
3+ from typing import Set
34
45import lief
5- from lief .PE import DLL_CHARACTERISTICS , HEADER_CHARACTERISTICS , MACHINE_TYPES
6+ from lief .PE import DLL_CHARACTERISTICS , HEADER_CHARACTERISTICS , MACHINE_TYPES , GUARD_CF_FLAGS
67
78from .binary import BinarySecurity
89
1920 "safe_seh" ,
2021 "force_integrity" ,
2122 "guard_cf" ,
23+ "rfg" ,
2224 "isolation" ,
2325 ],
2426)
@@ -104,7 +106,24 @@ def has_guard_cf(self) -> bool:
104106
105107 # code integrity: November 2015 (Windows 10 1511)
106108
107- # Return Flow Guard: October 2016 (Windows 10 Redstone 2)
109+ @property
110+ def has_return_flow_guard (self ) -> bool :
111+ """Whether Return Flow Guard is enabled"""
112+ # Return Flow Guard: October 2016 (Windows 10 Redstone 2)
113+ # winchecksec:
114+ # https://github.com/trailofbits/winchecksec/blob/v2.0.0/checksec.cpp#L262
115+ # Tencent lab article
116+ # https://xlab.tencent.com/en/2016/11/02/return-flow-guard/
117+ try :
118+ guard_flags : Set [GUARD_CF_FLAGS ] = self .bin .load_configuration .guard_cf_flags_list
119+ return (
120+ True
121+ if GUARD_CF_FLAGS .GRF_INSTRUMENTED in guard_flags
122+ and (GUARD_CF_FLAGS .GRF_ENABLE in guard_flags or GUARD_CF_FLAGS .GRF_STRICT in guard_flags )
123+ else False
124+ )
125+ except (lief .not_found , AttributeError ):
126+ return False
108127
109128 @property
110129 def has_isolation (self ) -> bool :
@@ -126,5 +145,6 @@ def checksec_state(self) -> PEChecksecData:
126145 safe_seh = self .has_safe_seh ,
127146 force_integrity = self .has_force_integrity ,
128147 guard_cf = self .has_guard_cf ,
148+ rfg = self .has_return_flow_guard ,
129149 isolation = self .has_isolation ,
130150 )
0 commit comments