Advertisement
Falexom

Untitled

Dec 2nd, 2022
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. import os
  2. import glob
  3. import argparse
  4. import linecache
  5. import shutil
  6. import re
  7. import sys
  8. from hashlib import sha512
  9.  
  10. if __name__ == '__main__':
  11.     parser = argparse.ArgumentParser(description='file filter')
  12.     parser.add_argument('-k', '--kill', help='terminate the programm execution', default=False, action='store_true')
  13.     args = parser.parse_args()
  14.     directory = '/home/kali/Desktop/lab1'
  15.     password = input('enter password: ').encode()
  16.     hash = sha512(password).hexdigest()
  17.  
  18.     try:
  19.         if not args.kill:
  20.             if not os.access("template.tbl", os.W_OK):
  21.                 print('Dont change P4ss')
  22.                 sys.exit(0)
  23.             open('names.txt', 'x')
  24.  
  25.             for filename in os.listdir(directory):
  26.                 f = os.path.join(directory, filename)
  27.                 with open('names.txt', 'a') as the_file:
  28.                     the_file.write(filename)
  29.                     the_file.write("\n")
  30.  
  31.             with open('names.txt', 'r') as f:
  32.                 old_data = f.read()
  33.  
  34.             new_data = old_data.replace('lab1.py\n', '')
  35.  
  36.             with open('names.txt', 'w') as f:
  37.                 f.write(new_data)
  38.  
  39.             with open('names.txt', 'r') as f:
  40.                 old_data = f.read()
  41.  
  42.             new_data = old_data.replace('names.txt\n', '')
  43.  
  44.             with open('names.txt', 'w') as f:
  45.                 f.write(new_data)
  46.  
  47.             with open('names.txt', 'r') as f:
  48.                 old_data = f.read()
  49.  
  50.             new_data = old_data.replace('template.tbl\n', '')
  51.  
  52.             with open('names.txt', 'w') as f:
  53.                 f.write(new_data)
  54.  
  55.             with open('template.tbl', 'r') as the_file:
  56.                 myString = linecache.getline('names.txt', 1)
  57.                 if not myString:
  58.                     os.remove('names.txt')
  59.                     raise KeyError
  60.  
  61.             with open('template.tbl', 'w') as the_file:
  62.                 the_file.write(hash)
  63.  
  64.             with open('template.tbl', 'a') as the_file:
  65.                 the_file.write("\n")
  66.  
  67.             with open('template.tbl', "a") as concat_file:
  68.                 shutil.copyfileobj(open('names.txt', "r"), concat_file)
  69.  
  70.             file1 = open("names.txt", "r")
  71.             while True:
  72.                 line = file1.readline()
  73.                 if not line:
  74.                     break
  75.                 os.chmod(line.strip(), 0o000)
  76.                 print(line.strip() + ' is cl0seD')
  77.  
  78.             file1.close()
  79.             os.chmod('template.tbl', 0o0400)
  80.             print('template.tbl file is open for the creator to read only')
  81.             os.chmod('lab1.py', 0o0400)
  82.             print('lab1.py file is open for the creator to read only')
  83.             os.remove('names.txt')
  84.  
  85.         else:
  86.             with open('template.tbl', 'r') as the_file:
  87.                 myString = linecache.getline('template.tbl', 1)
  88.                 myString1 = myString.rstrip()
  89.  
  90.             if hash != myString1:
  91.                 raise ValueError
  92.  
  93.             if hash == myString1:
  94.                 open('names1.txt', 'x')
  95.  
  96.                 for filename in os.listdir(directory):
  97.                     f = os.path.join(directory, filename)
  98.  
  99.                 with open('names1.txt', 'a') as the_file:
  100.                     the_file.write(filename)
  101.                     the_file.write("\n")
  102.                 file1 = open("names1.txt", "r")
  103.  
  104.                 while True:
  105.                     line = file1.readline()
  106.  
  107.                     if not line:
  108.                         break
  109.                     os.chmod(line.strip(), 0o777)
  110.                     print(line.strip() + ' is 0p3n')
  111.  
  112.             if os.path.isfile('names1.txt'):
  113.                 os.remove('names1.txt')
  114.  
  115.             if os.path.isfile('names.txt'):
  116.                 os.remove('names.txt')
  117.  
  118.     except KeyError:
  119.         print('no files in directory...')
  120.        
  121.     except ValueError:
  122.         print('invalid pass...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement