Skip to content

Instantly share code, notes, and snippets.

@operatorequals
Last active April 23, 2018 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save operatorequals/64375aabe09e1da3fe59ffddad3448db to your computer and use it in GitHub Desktop.
Save operatorequals/64375aabe09e1da3fe59ffddad3448db to your computer and use it in GitHub Desktop.
Context that replaces the Access/Modification times after file operations (TimeStomping)
#!/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)
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))
@operatorequals
Copy link
Author

$ cat /tmp/test;  stat /tmp/test; sleep 2; ./stealthy_cat.py /tmp/test; stat /tmp/test
  File: /tmp/test
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 2384626     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  unused)   Gid: ( 1000/  unused)
Access: 2018-04-20 09:44:40.722821173 -0400
Modify: 2018-04-20 09:25:41.896007000 -0400
Change: 2018-04-20 09:44:29.650297635 -0400
 Birth: -

  File: /tmp/test
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 2384626     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/  unused)   Gid: ( 1000/  unused)
Access: 2018-04-20 09:44:40.722821000 -0400
Modify: 2018-04-20 09:25:41.896007000 -0400
Change: 2018-04-20 09:44:42.734916697 -0400
 Birth: -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment