-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnigmaScope.py
291 lines (232 loc) · 9.47 KB
/
EnigmaScope.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.fernet import Fernet
from tabulate import tabulate
import argparse
import requests
import datetime
import bcrypt
import base64
import json
import zlib
import os
import re
class Printer:
@staticmethod
def log(data: str):
print(f"[+] {data.capitalize()}")
@staticmethod
def err(data: str):
print(f"[-] {str(data).capitalize()}")
class Encryptor:
@staticmethod
def passwordToKey(password: bytes):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
iterations=100000,
salt=b"EnigmaScope-salt",
length=32
)
key = base64.urlsafe_b64encode(kdf.derive(password))
return key
@staticmethod
def encrypt(password: bytes, data: bytes):
try:
cipher = Fernet(Encryptor.passwordToKey(password))
return cipher.encrypt(data)
except:
Printer.err("encryption failed.")
return False
@staticmethod
def decrypt(password: bytes, data: bytes):
try:
cipher = Fernet(Encryptor.passwordToKey(password))
return cipher.decrypt(data)
except:
Printer.err("decryption failed.")
return False
class EnigmaScope:
def __init__(self, imageFilePath) -> None:
self.imageFilePath = imageFilePath
self.imageName = os.path.basename(self.imageFilePath)
self.outputFolder = os.path.join(os.path.expanduser('~'), "Documents", "EnigmaScope", self.imageName.split(".")[0])
self.TAG = zlib.compress(b"(ENIGMASCOPE-START)[ENIGMASCOPE-DATA](ENIGMASCOPE-END)")
if not os.path.exists(self.outputFolder):
os.makedirs(self.outputFolder)
def sourceBufferWriter(self, jsonData):
with open(self.imageFilePath, "rb") as f:
data = f.read().split(self.TAG)
jsonData = json.dumps(jsonData).encode()
data = data[0] + self.TAG + zlib.compress(jsonData)
with open(self.imageFilePath, "wb") as f:
f.write(data)
def sourceBufferReader(self):
with open(self.imageFilePath, "rb") as f:
data = f.read().split(self.TAG)
if len(data) == 1:
Printer.log("creating new source.")
newPassword = input("Enter a new password (you cannot recover data without this password): ").encode()
if len(newPassword) == 0:
Printer.err("password length is 0.")
return False
token = bcrypt.hashpw(newPassword, bcrypt.gensalt())
config = {
"token": token.decode(),
"data": {}
}
self.password = newPassword
self.sourceBufferWriter(config)
return "new"
elif len(data) == 2:
return json.loads(zlib.decompress(data[1]))
return False
def login(self):
res = self.sourceBufferReader()
if res == "new":
Printer.log("new secure source was created.")
return True
elif not res:
return False
password = input("Enter password: ").encode()
if not bcrypt.checkpw(password, res['token'].encode()):
Printer.err("incorrect password.")
return False
Printer.log("password match success.")
self.password = password
return True
def run(self):
self.helpMenu()
while True:
userInput = input(f"[{self.imageName}]> ")
if userInput == "q":
break
elif userInput == "help":
self.helpMenu()
elif userInput == "list":
self.listSource()
elif userInput.startswith("write "):
self.writeSource(userInput[6:])
elif userInput.startswith("read "):
self.readSource(userInput[5:])
elif userInput.startswith("delete "):
self.deleteSource(userInput[7:])
def listSource(self):
data = self.sourceBufferReader()['data']
tableData = []
for index, fileName in enumerate(data):
tableData.append([
index,
fileName,
data[fileName]['time'],
data[fileName]['size']
])
print(f'\n{tabulate(tableData, headers=["ID", "FILE", "TIME", "SIZE"])}\n')
def writeSource(self, filePath):
if os.path.exists(filePath) and os.path.isfile(filePath):
fileName = os.path.basename(filePath)
fileName = self.filterFilename(fileName)
with open(filePath, "rb") as f:
binData = f.read()
encryptedData = Encryptor.encrypt(self.password, binData)
if not encryptedData: return
oldData = self.sourceBufferReader()
oldData['data'][fileName] = {
"size": "{:.2f}".format(len(binData) / (1024 * 1024)),
"time": str(datetime.datetime.now()),
"bin": encryptedData.decode()
}
self.sourceBufferWriter(oldData)
Printer.log(f"write '{fileName}' successfully.")
return
if 'http' in filePath:
try:
fileName = filePath.split("/")[-1]
fileName = self.filterFilename(fileName)
res = requests.get(filePath, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"})
if res.status_code == 200:
binData = res.content
encryptedData = Encryptor.encrypt(self.password, binData)
if not encryptedData: return
oldData = self.sourceBufferReader()
oldData['data'][fileName] = {
"size": "{:.2f}".format(len(binData) / (1024 * 1024)),
"time": str(datetime.datetime.now()),
"bin": encryptedData.decode()
}
self.sourceBufferWriter(oldData)
Printer.log(f"write '{fileName}' successfully.")
else:
Printer.err(f"STATUS CODE: {res.status_code}")
except Exception as e:
Printer.err(e)
return
Printer.err(f"URL or FILE '{filePath}' not found.")
def readSource(self, fileId):
data = self.sourceBufferReader()['data']
if fileId == "*":
for fileName in data:
decrypedData = Encryptor.decrypt(self.password, data[fileName]['bin'])
if not decrypedData: continue
filePath = os.path.join(self.outputFolder, fileName)
with open(filePath, "wb") as f:
f.write(decrypedData)
Printer.log(f"Read success. Saved on '{filePath}'")
return
for fileIndex, fileName in enumerate(data):
if int(fileId) == fileIndex:
decrypedData = Encryptor.decrypt(self.password, data[fileName]['bin'])
if not decrypedData: return
filePath = os.path.join(self.outputFolder, fileName)
with open(filePath, "wb") as f:
f.write(decrypedData)
Printer.log(f"Read success. Saved on '{filePath}'")
return
Printer.err(f"ID not found")
def deleteSource(self, fileId):
data = self.sourceBufferReader()
if fileId == "*":
data['data'] = {}
self.sourceBufferWriter(data)
Printer.log(f"all deleted successfully.")
return
for fileIndex, fileName in enumerate(data['data']):
if int(fileId) == fileIndex:
del data['data'][fileName]
self.sourceBufferWriter(data)
Printer.log(f"file '{fileName}' deleted successfully.")
return
Printer.err(f"ID not found")
def filterFilename(self, filename: str):
filename = filename.replace(' ', '_')
filename = re.sub(r'[^\w\s.-]', '_', filename)
return filename
def helpMenu(self):
print('''
+======== COMMANDS ========+
help Help menu.
q Exit.
list List all files.
write <URL or FILE> ex: write /path/to/file, write https://example.com/data.zip
read <ID or *> ex: read 0, read *
delete <ID or *> ex: delete 0, delete *
''')
def main():
parser = argparse.ArgumentParser(description="EnigmaScope is a versatile Python tool designed to conceal and encapsulate diverse elements within a unified framework.")
parser.add_argument("-l", "--load", required=True, help="Load the source file. ex: wallpaper.jpg, icon.png ....")
args = parser.parse_args()
imageFilePath = args.load
if not os.path.exists(imageFilePath):
Printer.err("source file not found.")
return
try:
enigmaScope = EnigmaScope(imageFilePath)
if enigmaScope.login():
enigmaScope.run()
except KeyboardInterrupt:
Printer.log("KeyboardInterrupt - enigmaScope exit.")
exit(0)
except Exception as e:
Printer.err(e)
Printer.log("enigmaScope exit.")
if __name__ == "__main__":
main()