Skip to content

Commit f31d4bd

Browse files
committed
scripts: test for PE control flow instrumentation
1 parent 0445e1a commit f31d4bd

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

contrib/devtools/security-check.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ def check_PE_RELOC_SECTION(binary) -> bool:
121121
'''Check for a reloc section. This is required for functional ASLR.'''
122122
return binary.has_relocations
123123

124+
def check_PE_control_flow(binary) -> bool:
125+
'''
126+
Check for control flow instrumentation
127+
'''
128+
main = binary.get_symbol('main').value
129+
130+
section_addr = binary.section_from_rva(main).virtual_address
131+
virtual_address = binary.optional_header.imagebase + section_addr + main
132+
133+
content = binary.get_content_from_virtual_address(virtual_address, 4, lief.Binary.VA_TYPES.VA)
134+
135+
if content == [243, 15, 30, 250]: # endbr64
136+
return True
137+
return False
138+
124139
def check_MACHO_NOUNDEFS(binary) -> bool:
125140
'''
126141
Check for no undefined references.
@@ -177,7 +192,8 @@ def check_control_flow(binary) -> bool:
177192
('DYNAMIC_BASE', check_PE_DYNAMIC_BASE),
178193
('HIGH_ENTROPY_VA', check_PE_HIGH_ENTROPY_VA),
179194
('NX', check_NX),
180-
('RELOC_SECTION', check_PE_RELOC_SECTION)
195+
('RELOC_SECTION', check_PE_RELOC_SECTION),
196+
('CONTROL_FLOW', check_PE_control_flow),
181197
],
182198
'MACHO': [
183199
('PIE', check_PIE),

contrib/devtools/test-security-check.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,18 @@ def test_PE(self):
7070
write_testcode(source)
7171

7272
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--no-nxcompat','-Wl,--disable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
73-
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION'))
73+
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA NX RELOC_SECTION CONTROL_FLOW'))
7474
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--disable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
75-
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION'))
75+
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA RELOC_SECTION CONTROL_FLOW'))
7676
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-no-pie','-fno-PIE']),
77-
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA'))
77+
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW'))
7878
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--no-dynamicbase','-Wl,--no-high-entropy-va','-pie','-fPIE']),
79-
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA')) # -pie -fPIE does nothing unless --dynamicbase is also supplied
79+
(1, executable+': failed PIE DYNAMIC_BASE HIGH_ENTROPY_VA CONTROL_FLOW')) # -pie -fPIE does nothing unless --dynamicbase is also supplied
8080
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--no-high-entropy-va','-pie','-fPIE']),
81-
(1, executable+': failed HIGH_ENTROPY_VA'))
81+
(1, executable+': failed HIGH_ENTROPY_VA CONTROL_FLOW'))
8282
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--high-entropy-va','-pie','-fPIE']),
83+
(1, executable+': failed CONTROL_FLOW'))
84+
self.assertEqual(call_security_check(cc, source, executable, ['-Wl,--nxcompat','-Wl,--enable-reloc-section','-Wl,--dynamicbase','-Wl,--high-entropy-va','-pie','-fPIE', '-fcf-protection=full']),
8385
(0, ''))
8486

8587
clean_files(source, executable)

0 commit comments

Comments
 (0)