Last active
April 23, 2018 11:28
-
-
Save operatorequals/64375aabe09e1da3fe59ffddad3448db to your computer and use it in GitHub Desktop.
Context that replaces the Access/Modification times after file operations (TimeStomping)
This file contains hidden or 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
#!/usr/bin/env python | |
import sys | |
from stealthy_opener import stealth_open as open | |
try: | |
file_path = sys.argv[1] | |
with open(file_path) as f: | |
print (f.read()) | |
except IndexError: | |
print ("Usage: %s <filename>" % sys.argv[0]) | |
sys.exit(1) | |
except Exception as e: | |
print (e) | |
sys.exit(2) |
This file contains hidden or 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
from contextlib import contextmanager | |
import os | |
@contextmanager | |
def stealth_open(file_path, options='r'): | |
# Get current Access/Modification time of the file | |
stat_struct = os.stat(file_path) | |
atime = stat_struct.st_atime | |
mtime = stat_struct.st_mtime | |
# Open the file with provided 'options' ('r[b][w]') | |
handle = open(file_path, options) | |
# Return the file in 'with' statement | |
yield handle | |
# Close the Accessed/Modified file | |
handle.close() | |
# Revert Access/Modification times of the file | |
os.utime(file_path, (atime, mtime)) | |
Author
operatorequals
commented
Apr 20, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment