safety checks
This commit is contained in:
		
							parent
							
								
									f0e613919a
								
							
						
					
					
						commit
						4727f389ed
					
				
							
								
								
									
										43
									
								
								raps.py
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								raps.py
									
									
									
									
									
								
							| @ -3,9 +3,10 @@ | |||||||
| import random | import random | ||||||
| import secrets | import secrets | ||||||
| import os | import os | ||||||
|  | import sys | ||||||
| import hashlib | import hashlib | ||||||
| 
 | 
 | ||||||
| SECRET_FILE = 'secret' | DEFAULT_SECRET_PATH = 'secret' | ||||||
| 
 | 
 | ||||||
| def create_secret(): | def create_secret(): | ||||||
|     return secrets.token_hex(128) |     return secrets.token_hex(128) | ||||||
| @ -27,28 +28,44 @@ def generate_seed(secret): | |||||||
|     m.update(str(month_timestamp()).encode()) |     m.update(str(month_timestamp()).encode()) | ||||||
|     return m.digest() |     return m.digest() | ||||||
| 
 | 
 | ||||||
|  | def generate_secret(secret_path): | ||||||
|  |     print('Generating a new secret... ', end='') | ||||||
|  |     secret = create_secret() | ||||||
|  |     with open(secret_path, 'w') as f: | ||||||
|  |         f.write(secret) | ||||||
|  |     print('done. Send this to the other party:') | ||||||
|  |     print(secret) | ||||||
|  | 
 | ||||||
| def main(): | def main(): | ||||||
|     import argparse |     import argparse | ||||||
|     parser = argparse.ArgumentParser(prog='Remote Admin Password Solution', description='Generate rotating passwords based on a shared secret') |     parser = argparse.ArgumentParser( | ||||||
|  |                 prog='Remote Admin Password Solution', | ||||||
|  |                 description='Generate rotating passwords based on a shared secret' | ||||||
|  |     ) | ||||||
|     parser.add_argument('--secret', action='store', help='path to secret file') |     parser.add_argument('--secret', action='store', help='path to secret file') | ||||||
|     parser.add_argument('--new-secret', action='store_true', help='generate a new secret') |     parser.add_argument('--new-secret', action='store_true', help='generate a new secret') | ||||||
| 
 | 
 | ||||||
|     args = parser.parse_args() |     args = parser.parse_args() | ||||||
| 
 | 
 | ||||||
|     secret_path = args.secret if args.secret else SECRET_FILE |     secret_path = args.secret if args.secret else DEFAULT_SECRET_PATH | ||||||
| 
 | 
 | ||||||
|     secret = '' |     secret = '' | ||||||
|     if not os.path.isfile(secret_path) or args.new_secret: |     if args.new_secret: | ||||||
|         print('Generating a new secret... ', end='') |         if os.path.isfile(secret_path): | ||||||
|         secret = create_secret() |             choice = '' | ||||||
|         with open(secret_path, 'w') as f: |             while choice.lower() != 'y' and choice.lower() != 'n': | ||||||
|             f.write(secret) |                 choice = input(f'Secret {secret_path} exists! Overwrite? [y/n]: ') | ||||||
|         print('done. Send this to the other party:') |             if choice.lower() == 'y': | ||||||
|         print(secret) |                 generate_secret(secret_path) | ||||||
|  |         else: | ||||||
|  |             generate_secret(secret_path) | ||||||
| 
 | 
 | ||||||
|     else: |     try: | ||||||
|         secret = open(SECRET_FILE, 'r').read().strip() |         secret = open(secret_path, 'r').read().strip() | ||||||
|         print('Secret loaded.') |     except FileNotFoundError: | ||||||
|  |         print(f'ERROR: Secret file {secret_path} could not be found.') | ||||||
|  |         sys.exit(1) | ||||||
|  |     print(f'Secret {secret_path} loaded.') | ||||||
| 
 | 
 | ||||||
|     random.seed(generate_seed(secret)) |     random.seed(generate_seed(secret)) | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user