-
Notifications
You must be signed in to change notification settings - Fork 786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create secure custom PBA directory on Windows #1270
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
75a2f1b
island: Use `create_secure_directory()` for custom PBA directory crea…
shreyamalviya 7211d59
tests: Add unit test for custom PBA dir permissions on Windows
shreyamalviya 7afe081
tests: Use `is_windows_os()` while skipping tests in test_post_breach…
shreyamalviya 3bea4bb
tests: Refactor duplicate code for checking secure Windows permissions
shreyamalviya 37a7344
tests: Add extra line in tests/monkey_island/utils.py to pass formatt…
shreyamalviya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -2,8 +2,17 @@ | |
|
||
import pytest | ||
|
||
from monkey_island.cc.server_utils.file_utils import is_windows_os | ||
from monkey_island.cc.services.post_breach_files import PostBreachFilesService | ||
|
||
if is_windows_os(): | ||
import win32api | ||
import win32security | ||
|
||
FULL_CONTROL = 2032127 | ||
ACE_ACCESS_MODE_GRANT_ACCESS = win32security.GRANT_ACCESS | ||
ACE_INHERIT_OBJECT_AND_CONTAINER = 3 | ||
|
||
|
||
def raise_(ex): | ||
raise ex | ||
|
@@ -32,13 +41,42 @@ def dir_is_empty(dir_path): | |
return len(dir_contents) == 0 | ||
|
||
|
||
@pytest.mark.skipif(os.name != "posix", reason="Tests Posix (not Windows) permissions.") | ||
def test_custom_pba_dir_permissions(): | ||
@pytest.mark.skipif(is_windows_os(), reason="Tests Posix (not Windows) permissions.") | ||
def test_custom_pba_dir_permissions_linux(): | ||
st = os.stat(PostBreachFilesService.get_custom_pba_directory()) | ||
|
||
assert st.st_mode == 0o40700 | ||
|
||
|
||
def _get_acl_and_sid_from_path(path: str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also duplicated from test_file_utils.py. |
||
sid, _, _ = win32security.LookupAccountName("", win32api.GetUserName()) | ||
security_descriptor = win32security.GetNamedSecurityInfo( | ||
path, win32security.SE_FILE_OBJECT, win32security.DACL_SECURITY_INFORMATION | ||
) | ||
acl = security_descriptor.GetSecurityDescriptorDacl() | ||
return acl, sid | ||
|
||
|
||
@pytest.mark.skipif(not is_windows_os(), reason="Tests Windows (not Posix) permissions.") | ||
mssalvatore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_custom_pba_dir_permissions_windows(): | ||
pba_dir = PostBreachFilesService.get_custom_pba_directory() | ||
|
||
acl, user_sid = _get_acl_and_sid_from_path(pba_dir) | ||
|
||
assert acl.GetAceCount() == 1 | ||
|
||
ace = acl.GetExplicitEntriesFromAcl()[0] | ||
|
||
ace_access_mode = ace["AccessMode"] | ||
ace_permissions = ace["AccessPermissions"] | ||
ace_inheritance = ace["Inheritance"] | ||
ace_sid = ace["Trustee"]["Identifier"] | ||
|
||
assert ace_sid == user_sid | ||
assert ace_permissions == FULL_CONTROL and ace_access_mode == ACE_ACCESS_MODE_GRANT_ACCESS | ||
assert ace_inheritance == ACE_INHERIT_OBJECT_AND_CONTAINER | ||
|
||
|
||
def test_remove_failure(monkeypatch): | ||
monkeypatch.setattr(os, "remove", lambda x: raise_(OSError("Permission denied"))) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also use this in test_file_utils.py. Can we put this constant somewhere common for tests? Maybe tests/monkey_island/utils.py.