|
| 1 | +# Import the necessary libraries. |
| 2 | +importpyzipper,argparse,sys,re,getpass |
| 3 | +fromcoloramaimportFore,init |
| 4 | + |
| 5 | +init() |
| 6 | + |
| 7 | +# Define a function to get CLI commands. |
| 8 | +defget_cli_arguments(): |
| 9 | +parser=argparse.ArgumentParser(description="A program to lock a ZIP File.") |
| 10 | +# Collect user arguments. |
| 11 | +parser.add_argument('--zipfile','-z',dest='zip_file',help='Specify the ZIP file to create or update.') |
| 12 | +parser.add_argument('--addfile','-a',dest='add_files',nargs='+',help='Specify one or more files to add to the ZIP file(s).') |
| 13 | + |
| 14 | +# Parse the collected arguments. |
| 15 | +args=parser.parse_args() |
| 16 | + |
| 17 | +# Check if arguments are missing, print appropriate messages and exit the program. |
| 18 | +ifnotargs.zip_file: |
| 19 | +parser.print_help() |
| 20 | +sys.exit() |
| 21 | +ifnotargs.add_files: |
| 22 | +parser.print_help() |
| 23 | +sys.exit() |
| 24 | + |
| 25 | +returnargs |
| 26 | + |
| 27 | +# Function to check password strength. |
| 28 | +defcheck_password_strength(password): |
| 29 | +# Check for minimum length. In our case, 8. |
| 30 | +iflen(password)<8: |
| 31 | +returnFalse |
| 32 | + |
| 33 | +# Check for at least one uppercase letter, one lowercase letter, and one digit. |
| 34 | +ifnot (re.search(r'[A-Z]',password)andre.search(r'[a-z]',password)andre.search(r'\d',password)): |
| 35 | +returnFalse |
| 36 | + |
| 37 | +returnTrue |
| 38 | + |
| 39 | +# Call the arguments function. |
| 40 | +arguments=get_cli_arguments() |
| 41 | + |
| 42 | +# Get user password |
| 43 | +password=getpass.getpass("[?] Enter your password > ") |
| 44 | + |
| 45 | +# If password is weak, tell the user and exit the program. |
| 46 | +ifnotcheck_password_strength(password): |
| 47 | +print(f"{Fore.RED}[-] Password is not strong enough. It should have at least 8 characters and contain at least one uppercase letter, one lowercase letter, and one digit.") |
| 48 | +sys.exit() |
| 49 | + |
| 50 | +# Create a password-protected ZIP file. |
| 51 | +withpyzipper.AESZipFile(arguments.zip_file,'w',compression=pyzipper.ZIP_LZMA,encryption=pyzipper.WZ_AES)aszf: |
| 52 | +zf.setpassword(password.encode()) |
| 53 | + |
| 54 | +# Add files to the ZIP file. |
| 55 | +forfile_to_addinarguments.add_files: |
| 56 | +zf.write(file_to_add) |
| 57 | + |
| 58 | +# Print a Success message. |
| 59 | +print(f"{Fore.GREEN}[+] ZIP file is locked with a strong password.") |