Skip to content

Commit

Permalink
Improvements to os write restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
w4ffl35 committed Aug 25, 2024
1 parent 29fb9da commit 2be4a78
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="darklock",
version="0.1.5",
version="0.1.6",
author="Capsize LLC",
description="",
long_description=open("README.md", "r", encoding="utf-8").read(),
Expand Down
7 changes: 5 additions & 2 deletions src/darklock/log_disc_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ def __call__(self, *args, **kwargs):
print(f"Write attempt: {self.total_write_attempts}")

# show where write came from:
stack = traceback.extract_stack()
print(f"Write attempt from: {stack[-2]}")
filename = kwargs.get("filename", None)
if not filename:
stack = traceback.extract_stack()
filename = stack[-2].filename
print(f"Write attempt from: {filename}")
13 changes: 11 additions & 2 deletions src/darklock/restrict_os_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __init__(self):
self.blacklisted_filenames = []
self.whitelisted_directories = []

self.log_disc_writter = LogDiscWriter()

self.logger = logging.getLogger(__name__)
self.logger.setLevel(logging.DEBUG)
self.logger.addHandler(logging.StreamHandler())
Expand All @@ -33,7 +35,14 @@ def is_directory_whitelisted(self, directory: str) -> bool:
return False

def restrict_os_write(self, *args, **kwargs):
return self.original_os_write(*args, **kwargs)
stack = traceback.extract_stack()
filename = stack[-2].filename
if (
self.is_directory_whitelisted(args[0]) or
'write' in self.whitelisted_directories or self.check_stack_trace()
):
return self.original_os_write(*args, **kwargs)
self.log_disc_writter(filename=filename)

def restricted_open(self, *args, **kwargs):
if (
Expand Down Expand Up @@ -144,7 +153,7 @@ def activate(

#builtins.open = self.restricted_open

self.log_writes()
#self.log_writes()

def deactivate(self):
"""
Expand Down

0 comments on commit 2be4a78

Please sign in to comment.