Skip to content

Commit 955140b

Browse files
committed
contrib: consolidate PIE and NX security checks
1 parent 2aa1631 commit 955140b

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

contrib/devtools/security-check.py

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,6 @@ def check_ELF_separate_code(executable):
134134
return False
135135
return True
136136

137-
def check_PE_PIE(executable) -> bool:
138-
'''
139-
Check for position independent executable (PIE),
140-
allowing for address space randomization.
141-
'''
142-
binary = lief.parse(executable)
143-
return binary.is_pie
144-
145137
def check_PE_DYNAMIC_BASE(executable) -> bool:
146138
'''PIE: DllCharacteristics bit 0x40 signifies dynamicbase (ASLR)'''
147139
binary = lief.parse(executable)
@@ -159,32 +151,13 @@ def check_PE_RELOC_SECTION(executable) -> bool:
159151
binary = lief.parse(executable)
160152
return binary.has_relocations
161153

162-
def check_PE_NX(executable) -> bool:
163-
'''NX: DllCharacteristics bit 0x100 signifies nxcompat (DEP)'''
164-
binary = lief.parse(executable)
165-
return binary.has_nx
166-
167-
def check_MACHO_PIE(executable) -> bool:
168-
'''
169-
Check for position independent executable (PIE), allowing for address space randomization.
170-
'''
171-
binary = lief.parse(executable)
172-
return binary.is_pie
173-
174154
def check_MACHO_NOUNDEFS(executable) -> bool:
175155
'''
176156
Check for no undefined references.
177157
'''
178158
binary = lief.parse(executable)
179159
return binary.header.has(lief.MachO.HEADER_FLAGS.NOUNDEFS)
180160

181-
def check_MACHO_NX(executable) -> bool:
182-
'''
183-
Check for no stack execution
184-
'''
185-
binary = lief.parse(executable)
186-
return binary.has_nx
187-
188161
def check_MACHO_LAZY_BINDINGS(executable) -> bool:
189162
'''
190163
Check for no lazy bindings.
@@ -200,6 +173,21 @@ def check_MACHO_Canary(executable) -> bool:
200173
binary = lief.parse(executable)
201174
return binary.has_symbol('___stack_chk_fail')
202175

176+
def check_PIE(executable) -> bool:
177+
'''
178+
Check for position independent executable (PIE),
179+
allowing for address space randomization.
180+
'''
181+
binary = lief.parse(executable)
182+
return binary.is_pie
183+
184+
def check_NX(executable) -> bool:
185+
'''
186+
Check for no stack execution
187+
'''
188+
binary = lief.parse(executable)
189+
return binary.has_nx
190+
203191
CHECKS = {
204192
'ELF': [
205193
('PIE', check_ELF_PIE),
@@ -209,16 +197,16 @@ def check_MACHO_Canary(executable) -> bool:
209197
('separate_code', check_ELF_separate_code),
210198
],
211199
'PE': [
212-
('PIE', check_PE_PIE),
200+
('PIE', check_PIE),
213201
('DYNAMIC_BASE', check_PE_DYNAMIC_BASE),
214202
('HIGH_ENTROPY_VA', check_PE_HIGH_ENTROPY_VA),
215-
('NX', check_PE_NX),
203+
('NX', check_NX),
216204
('RELOC_SECTION', check_PE_RELOC_SECTION)
217205
],
218206
'MACHO': [
219-
('PIE', check_MACHO_PIE),
207+
('PIE', check_PIE),
220208
('NOUNDEFS', check_MACHO_NOUNDEFS),
221-
('NX', check_MACHO_NX),
209+
('NX', check_NX),
222210
('LAZY_BINDINGS', check_MACHO_LAZY_BINDINGS),
223211
('Canary', check_MACHO_Canary)
224212
]

0 commit comments

Comments
 (0)