-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis-cli.py
More file actions
186 lines (158 loc) · 6.61 KB
/
redis-cli.py
File metadata and controls
186 lines (158 loc) · 6.61 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import redis
import getpass
import json
import chardet
from pathlib import Path
from termcolor import colored
from base64 import b64encode, b64decode
# AES
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Hash import SHA256
def is_text(bytes_data):
result = chardet.detect(bytes_data)
return result['encoding'] is not None
def print_in_color(message, color):
print(colored(message, color))
def outline_with_stars(message, color):
print_in_color('*' * 100, 'blue')
print_in_color(message, color)
print_in_color('*' * 100, 'blue')
# AES encryption and decryption
def encrypt_decrypt(action, data, key):
# hash the key using SHA-256
key = SHA256.new(key.encode('utf-8')).digest()
cipher = AES.new(key, AES.MODE_CBC)
if action == 'encrypt':
cipher_text = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
return b64encode(cipher.iv + cipher_text).decode('utf-8')
elif action == 'decrypt':
decoded = b64decode(data)
iv = decoded[:16]
cipher_text = decoded[16:]
decipher = AES.new(key, AES.MODE_CBC, iv=iv)
return unpad(decipher.decrypt(cipher_text), AES.block_size).decode('utf-8')
# Store and retrieve credentials from filesystem
def handle_credentials(action):
credential_file = Path('.rediscli')
if action == 'store':
host = input('Enter Redis server hostname: ')
port = int(input('Enter Redis server port: '))
password = getpass.getpass('Enter Redis server password: ')
tls = input('Use TLS? (y/n): ').lower() == 'y'
credentials = {'host': host, 'port': port, 'password': password, 'tls': tls}
passkey = getpass.getpass('Enter your passkey: ')
encrypted_credentials = encrypt_decrypt('encrypt', json.dumps(credentials), passkey)
with open('.rediscli', 'w') as f:
f.write(encrypted_credentials)
return credentials
elif action == 'retrieve' and credential_file.is_file():
passkey = getpass.getpass('Enter your passkey: ')
with open('.rediscli', 'r') as f:
encrypted_credentials = f.read()
credentials = json.loads(encrypt_decrypt('decrypt', encrypted_credentials, passkey))
return credentials
art = '''
________ _____________
___ __ \__________ /__(_)_______
__ /_/ / _ \ __ /__ /__ ___/
_ _, _// __/ /_/ / _ / _(__ )
/_/ |_| \___/\__,_/ /_/ /____/
'''
print_in_color(art, 'red')
# Check if .rediscli exists
credential_file = Path('.rediscli')
while credential_file.is_file():
try:
credentials = handle_credentials('retrieve')
outline_with_stars('Redis connection details has been read from file', 'yellow')
break
except:
print_in_color("Invalid passkey. Please try again.", 'red')
else:
outline_with_stars('Enter Redis server details', 'yellow')
credentials = handle_credentials('store')
# connect to Redis server
print_in_color('Connecting to Redis server... \n', 'cyan')
r = redis.Redis(host=credentials['host'], port=credentials['port'], password=credentials['password'], ssl=credentials['tls'])
# scan all populated databases
dbs = []
for db in range(16): # Redis has 16 databases by default
r.execute_command('SELECT', db)
if r.dbsize() > 0:
dbs.append(db)
# print populated databases
print_in_color('Populated databases:', 'green')
for i, db in enumerate(dbs):
r.execute_command('SELECT', db)
print_in_color(f"db#{db}:", 'light_green')
keys = r.keys()[:5]
for key in keys:
db_display=f"{key.decode('utf-8')}";
print_in_color(db_display, 'light_cyan')
if len(r.keys()) > 5:
print_in_color("...", 'light_cyan')
# select DB
db = int(input('\nEnter db index to select: \r\n'))
r.execute_command('SELECT', db)
# display the keys with their corresponding indexes
def keys_management():
keys_dict = {i: key for i, key in enumerate(r.keys())}
print_in_color('\nKeys and their values:', 'light_green')
for i, key in keys_dict.items():
value = r.get(key)
if is_text(value):
value = value.decode('utf-8')
else:
value = value.hex()
print_in_color(f"{i}: {key.decode('utf-8')}: {value}", 'light_cyan')
# select a key
key_index = int(input('\nEnter key index to select: \r\n'))
selected_key = keys_dict[key_index]
# define and print possible operations
operations = {'g': 'GET', 's': 'SET', 'd': 'DELETE', 't': 'TTL', 'y': 'TYPE', '..': 'BACK', 'q': 'QUIT'}
print_in_color('\nAvailable operations:', 'light_green')
ops = ', '.join(f"{op}: {desc}" for op, desc in operations.items())
print_in_color(ops, 'light_cyan')
while True:
# prompt user to choose an operation
selected_op = input('\n> ').lower()
# check if entered operation is valid
if selected_op not in operations:
print_in_color('Invalid operation. Try again.', 'red')
continue
# check if quit
if selected_op == 'q':
break
# get operation name
selected_op_name = operations[selected_op]
# execute the selected operation
if selected_op_name == 'GET':
value = r.get(selected_key)
if is_text(value):
value = value.decode('utf-8')
else:
value = value.hex()
print_in_color(f"Value: {value}", 'light_cyan')
elif selected_op_name == 'SET':
new_value = input('Enter new value for the key: \r\n')
r.set(selected_key, new_value)
print_in_color(f"Key {selected_key.decode('utf-8')} has been set to {new_value}.", 'light_cyan')
elif selected_op_name == 'DELETE':
r.delete(selected_key)
print_in_color(f"Key {selected_key.decode('utf-8')} has been deleted.", 'light_cyan')
keys_management() # yes, it's bad
elif selected_op_name == 'TTL':
ttl = r.ttl(selected_key)
if ttl == -1:
print_in_color(f"Key {selected_key.decode('utf-8')} does not have an expire set.", 'light_cyan')
elif ttl == -2:
print_in_color(f"Key {selected_key.decode('utf-8')} does not exist.", 'light_cyan')
else:
print_in_color(f"Time to live for key {selected_key.decode('utf-8')}: {ttl} seconds.", 'light_cyan')
elif selected_op_name == 'BACK':
keys_management() # yes, it's bad
elif selected_op_name == 'TYPE':
key_type = r.type(selected_key)
print_in_color(f"Type of key {selected_key.decode('utf-8')}: {key_type}", 'light_cyan')
keys_management()