-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent Local Privilege Escalation via Config & Untrusted Aliases
* Mitigated local config exploitability * Re-ordered precedence to sys locations * Added warning if local location is used * Mitigated untrusted alias execution
- Loading branch information
1 parent
d3956bc
commit 2c11575
Showing
3 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,34 @@ | ||
import os | ||
import subprocess | ||
|
||
__version__ = "0.9.3" | ||
|
||
from os import PathLike | ||
from typing import Union | ||
|
||
BIN_LOCATIONS = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"] | ||
|
||
|
||
def _check_file_exists_and_executable(path: Union[PathLike, str]) -> bool: | ||
if not os.path.isfile(path): | ||
return False | ||
else: | ||
return os.access(path, os.X_OK) | ||
|
||
|
||
def safe_exec(binary_name: str, args: list) -> bytes: | ||
""" | ||
Executes the given binary with the given arguments as a subprocess. What makes this safe is that the binary name | ||
is not executed as an alias, and only binaries that live in trusted system locations are executed. This means that | ||
only system-wide binaries are executable. | ||
""" | ||
exec_path = None | ||
for prefix in BIN_LOCATIONS: | ||
bin_path = os.path.join(prefix, binary_name) | ||
if _check_file_exists_and_executable(os.path.join(prefix, binary_name)): | ||
This comment was marked as outdated.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
thinkst-az
Contributor
|
||
exec_path = bin_path | ||
break | ||
if exec_path is None: | ||
raise Exception(f"Could not find executable ${binary_name}") | ||
else: | ||
return subprocess.check_output(args, shell=True, executable=exec_path) | ||
This comment was marked as outdated.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
thinkst-az
Contributor
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Consider handling relative paths in binary_name.
Eg:
../../../tmp/foo