Skip to content

Commit 65a8efc

Browse files
authored
Merge pull request #86 from Wenzel/libc_detect_windows
utils: add error handling for Windows when detecting libc
2 parents 47f2199 + cbaa047 commit 65a8efc

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

checksec/elf.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .binary import BinarySecurity
1010
from .errors import ErrorParsingFailed
11-
from .utils import find_libc
11+
from .utils import LibcNotFoundError, find_libc
1212

1313
FORTIFIED_END_MARKER = "_chk"
1414
FORTFIED_START_MARKER = "__"
@@ -34,10 +34,6 @@
3434
__LIBC_OBJ = {}
3535

3636

37-
class LibcNotFoundError(Exception):
38-
pass
39-
40-
4137
def get_libc(libc_path: Optional[Path] = None) -> Optional["Libc"]:
4238
"""This function initializes a Libc using LIEF
4339

checksec/utils.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
import lddwrap
1010

11+
12+
class LibcNotFoundError(Exception):
13+
pass
14+
15+
1116
LIBC_PATH_POSSIBILITIES = [
1217
"/lib/libc.so.6",
1318
"/lib/libc.so.7",
@@ -26,19 +31,21 @@ def find_libc():
2631
libc_path = None
2732
try:
2833
libc_path = find_library_full("c")
29-
except FileNotFoundError:
34+
except (FileNotFoundError, AttributeError, RuntimeError):
3035
# ldconfig is not accessible as user
36+
# or running on Windows
37+
# or other errors
3138
try:
3239
libc_path = find_libc_ldd()
3340
except FileNotFoundError:
3441
# test hardcoded paths
3542
logging.debug("Finding libc path: hardcoded paths")
3643
for maybe_libc in LIBC_PATH_POSSIBILITIES:
37-
if Path(maybe_libc).exists():
44+
if Path(maybe_libc).resolve().exists():
3845
libc_path = maybe_libc
3946
break
4047
if libc_path is None:
41-
raise RuntimeError("Cannot find a suitable libc path on your system")
48+
raise LibcNotFoundError("Cannot find a suitable libc path on your system")
4249
logging.debug("Found libc: %s", libc_path)
4350
return libc_path
4451

@@ -64,9 +71,14 @@ def find_libc_ldd():
6471

6572

6673
def find_library_full(name):
67-
"""https://stackoverflow.com/a/29227195/3017219"""
74+
"""https://stackoverflow.com/a/29227195/3017219
75+
76+
:raise:
77+
AttributeError: if an attribute is not found on OS module
78+
RuntimeError"""
6879
logging.debug("Finding libc path: ldconfig")
6980
# see ctypes.find_library code
81+
# Note: os.uname is OS dependant, will raise AttributeError
7082
uname = os.uname()[4]
7183
if uname.startswith("arm"):
7284
uname = "arm"

0 commit comments

Comments
 (0)