Advertisement
DeaD_EyE

ADS ntfs

Mar 26th, 2021
841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. r"""
  2. ADS are alternative data streams of files in NTFS on Windows.
  3.  
  4. This is just a demonstration, that you can use a regular open
  5. function to write and read this files.
  6.  
  7.  
  8. I got the Idea from: https://www.youtube.com/watch?v=vz15OqiYYXo
  9.  
  10. After you run the programm, you can comment out the write of the file.
  11. Then repeat the process and you'll see the content again.
  12. Then look on your desktop and look if you see more than one file.txt
  13.  
  14. You can show this files with dir. Here some output from my vm:
  15.  
  16. C:\Users\andre\Desktop>dir /R
  17.  
  18. ... removed ...
  19.  
  20. 26.03.2021  18:40    <DIR>          .
  21. 26.03.2021  18:40    <DIR>          ..
  22. 26.03.2021  18:30               632 ads.py
  23. 26.03.2021  18:30                 4 file.txt
  24.                                  3 file.txt:bar:$DATA
  25.                                  4 file.txt:buzz:$DATA
  26.                                  4 file.txt:fizz:$DATA
  27.                                  3 file.txt:foo:$DATA
  28.                                  7 file.txt:my_data:$DATA
  29.  
  30. ... removed ...
  31. """
  32.  
  33. from pathlib import Path
  34. from typing import Optional
  35.  
  36.  
  37. def open_ads(
  38.     file: Path,
  39.     mode: str = "r",
  40.     stream: Optional[str] = None,
  41.     **kwargs,
  42. ):
  43.     """
  44.    Open alternate data streams on Windows.
  45.  
  46.    if stream is None, then the default stream is used,
  47.    which is visible to the user.
  48.    """
  49.     file = str(file)
  50.     if stream:
  51.         file += f":{stream}:$data"
  52.     return open(file, mode, **kwargs)
  53.  
  54.  
  55. # place to put the file is yor Desktop with
  56. # the current user, who runs the program
  57. desktop = Path.home() / "Desktop"
  58. text_file = desktop / "file.txt"
  59.  
  60. # name of streams
  61. # None is a regular file
  62. STREAMS = (None, "my_data", "foo", "bar", "fizz", "buzz")
  63.  
  64.  
  65. def write():
  66.     for stream in STREAMS:
  67.         print("Writing to stream", stream or "default")
  68.         with open_ads(text_file, "w", stream=stream) as fd:
  69.             fd.write(stream or "None")
  70.  
  71.  
  72. def read():
  73.     for stream in STREAMS:
  74.         print(f"Content of {stream or 'default'} is:", end=" ")
  75.         with open_ads(text_file, stream=stream) as fd:
  76.             print(fd.read())
  77.  
  78.  
  79. if __name__ == "__main__":
  80.     print(f"'{text_file}' will be created.", end="\n\n")
  81.     yesno = input("Am I allowed to do this? [y/n]: ")
  82.     if yesno.lower().strip().startswith("y"):
  83.         write()
  84.         print()
  85.     try:
  86.         read()
  87.     except FileNotFoundError:
  88.         print(f"'{text_file}' does not exsit")
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement