-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_codeSimulator.py
73 lines (58 loc) · 2.22 KB
/
core_codeSimulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import mysql.connector
from cryptography.fernet import Fernet, InvalidToken
import os
# Function to load the encryption key
def get_or_generate_key():
key_file = 'encryption_key.key'
if os.path.exists(key_file):
with open(key_file, 'rb') as file:
key = file.read()
return key
else:
print('DecryptionKey Not Found')
raise FileNotFoundError('Decryption key file not found. Please save the key and save it in "encryption_key.key".')
# Initialize Fernet cipher with the key
encryption_key = get_or_generate_key()
cipher_suite = Fernet(encryption_key)
# Function to decrypt a password
def decrypt_password(encrypted_password):
try:
decrypted_password = cipher_suite.decrypt(encrypted_password).decode()
return decrypted_password
except InvalidToken:
return "Invalid token (possibly corrupted or tampered data)"
# Establish a connection to the MySQL database
connection = mysql.connector.connect(
host='localhost',
user='root',
password='hack',
database='passwords_db'
)
# Create a cursor object to interact with the database
cursor = connection.cursor(dictionary=True)
# Specify the group you want to filter
group_name = 'testt'
# Query to retrieve devices based on group name
query = ("SELECT * FROM passwords WHERE group_name = %s")
values = (group_name,)
# Execute the query
cursor.execute(query, values)
# Retrieve the devices
devices = cursor.fetchall()
# Process devices
for device in devices:
username = device['username']
device_name = device['device']
encrypted_password = device['encrypted_password']
decrypted_password = decrypt_password(encrypted_password) # Decrypt the password
device_type = device['type']
# Process devices based on their type
if device_type == 'ASA':
# Process devices of type 'asdad'
print(f"Processing device {device_name} of type 'ASA' with username {username} and decrypted password {decrypted_password}")
elif device_type == 'IOS':
# Process devices of type 'IOS'
print(f"Processing device {device_name} of type 'IOS' with username {username} and decrypted password {decrypted_password}")
# Close the cursor and connection
cursor.close()
connection.close()