-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyCloud.py
More file actions
executable file
·437 lines (346 loc) · 15 KB
/
PyCloud.py
File metadata and controls
executable file
·437 lines (346 loc) · 15 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python3
import os
import glob
import time
import pathlib
import pyAesCrypt
import configparser
def displayBanner():
os.system('clear')
print("""
_____ _____ _ _
| __ \ / ____| | | |
| |__) | _| | | | ___ _ _ __| |
| ___/ | | | | | |/ _ \| | | |/ _` |
| | | |_| | |____| | (_) | |_| | (_| |
|_| \__, |\_____|_|\___/ \__,_|\__,_|
__/ |
|___/
""")
return
# -------------------------------------------> encryptIni
def encryptIni(inputFile,ouputFile,password):
# Function to encrypt the ini configuration file
try:
# Buffer initialization
bufferSize = 64 * 1024
# Crypt the file
pyAesCrypt.encryptFile(inputFile,ouputFile,password,bufferSize)
# Remove the original file
os.remove(inputFile)
except Exception as error:
print(f"[!] {error}")
exit()
return
# -------------------------------------------> decryptIni
def decryptIni(inputFile,outputFile,password):
# Function to decrypt ini file
try:
# Buffer initialization
bufferSize = 64 * 1024
# Decrypt ini file
pyAesCrypt.decryptFile(inputFile,outputFile,password,bufferSize)
# Remove the original file
os.remove(inputFile)
# If the password is incorrect...or other exception
except Exception as error:
print(f"[!] {error}")
exit()
return
# -------------------------------------------> checkConfig
def checkConfig(cryptedIni,uncryptedIni,folder):
# Function to check if initiale config is present
# If PyCloud folder does not exist, create it and create the ini encrypted file too
if not os.path.isdir(folder):
print(f"[+] PyCloud folder does not exist yet")
print(f"[+] Create {folder}")
print(f"[+] Add ini file {cryptedIni}")
os.mkdir(folder)
file = open(uncryptedIni,'w')
file.close()
password = input("[?] Choose a password for the ini file encryption : ")
# Add error condition if the input password is empty
if len(password) == 0:
notValid = True
else:
notValid = False
# While the password is empty, make a while loop
while notValid:
displayBanner()
print("[!] The encryption password cannot be empty")
password = input("[?] Choose a password for the ini file encryption : ")
if len(password) > 0:
notValid = False
encryptIni(uncryptedIni,cryptedIni,password)
# Else if PyCloud folder exist yet but ini encrypted file does not exist
elif os.path.isdir(folder) and not os.path.isfile(cryptedIni):
print(f"[+] Ini file does not exist yet")
print(f"[+] Create {uncryptedIni}")
file = open(uncryptedIni,'w')
file.close()
password = input("[?] Choose a password for the ini file encryption : ")
# Add error condition if the input password is empty
if len(password) == 0:
notValid = True
else:
notValid = False
# While the password is empty, make a while loop
while notValid:
displayBanner()
print("[!] The encryption password cannot be empty")
password = input("[?] Choose a password for the ini file encryption : ")
if len(password) > 0:
notValid = False
encryptIni(uncryptedIni,cryptedIni,password)
return
# -------------------------------------------> changePassword
def validIni(iniFile):
reader = configparser.ConfigParser()
try:
reader.read(iniFile)
return True
except configparser.ParsingError as error:
print("The ini configuration file looks wrong : ")
print(error)
time.sleep(4)
return False
def addSite(decrypt,crypt):
try:
# Display banner
displayBanner()
# Get password to decrypt ini gile
password = input("[?] Please, enter the password to unlock ini file : ")
# Decrypt the ini file to check if the synta is right
decryptIni(crypt,decrypt,password)
# If the ini file have incorrect syntax, go to menu
if not validIni(decrypt):
return main()
# Else, add a new site to backup
else:
# Initiate config reader
configReader = configparser.ConfigParser(delimiters=':')
# Simple error condition with loop to get user answer
# Choose folder to backup
loop = True
displayBanner()
folderBackup = input("[?] Which folder do you want backup : ")
while loop:
displayBanner()
if not os.path.isdir(folderBackup):
print(f"[!] The folder {folderBackup} does not exist")
folderBackup = input("[?] Which folder do you want backup : ")
else:
configReader.add_section(folderBackup)
loop = False
# Choose the targeted server where upload backup
displayBanner()
site = input("[?] Targeted server to save backup (hostname or IP) : ")
configReader.set(folderBackup,'site',site)
# Choose the kind of backup
# Mirror will make a simple mirror of local folder to a remote folder
# Newest will upload only newest and news files
loop = True
displayBanner()
mode = input("[?] What kind of backup do you want to do (mirror/newest) : ")
while loop:
displayBanner()
if mode != 'mirror' and mode != 'newest':
print("[!] Please, make choice between (mirror/newest)")
mode = input("[?] What kind of backup do you want to do (mirror/newest) : ")
else:
configReader.set(folderBackup,'mode',mode)
loop = False
# Choose the backup protocol
loop = True
displayBanner()
protocole = input("[?] Which protocole use (ssh/ftp) : ")
while loop:
displayBanner()
if protocole != 'ssh' and protocole != 'ftp':
print(f"[!] The {protocole} protocole is not available...")
protocole = input("[?] Which protocole use (ssh/ftp) : ")
else:
configReader.set(folderBackup,'protocole',protocole)
loop = False
# Choose the port of the protocol
loop = True
displayBanner()
port = input(f"[?] Which port do you want to use with {protocole} : ")
while loop:
displayBanner()
if int(port) < 1 or int(port) > 65535:
print("[!] Please, choose a port between 1 and 65535")
port = input(f"[?] Which port do you want to use with {protocole} : ")
else:
configReader.set(folderBackup,'port',port)
loop = False
#---------------------------------------
# If the user choose the FTP protocole |
#---------------------------------------
if protocole == 'ftp':
# Want activation SSL/TLS with FTP ?
loop = True
displayBanner()
ssl = input("[?] Do you want to use SSL/TLS (y/n) : ")
while loop:
displayBanner()
if ssl != 'y' and ssl != 'n':
print("[!] Please, make choice between (y/n)")
ssl = input("[?] Do you want to use SSL/TLS (y/n) : ")
else:
configReader.set(folderBackup,'SSL/TLS',ssl)
loop = False
# Which user to use with FTP ?
loop = True
displayBanner()
user = input("[?] Which user use for FTP login : ")
while loop:
displayBanner()
if len(user) == 0:
print("[!] Please, add a non empty username")
user = input("[?] Which user use for FTP login : ")
else:
configReader.set(folderBackup,'user',user)
loop = False
# Which password ?
displayBanner()
passwd = input("[?] Which password use for FTP login : ")
configReader.set(folderBackup,'password',passwd)
# ---------------------
# If protocole is SSH |
# ---------------------
elif protocole == 'ssh':
# Use a private key ?
loop = True
displayBanner()
privateKey = input("[?] Do you want to use a private key with SSH (y/n) : ")
while loop:
displayBanner()
if privateKey != 'y' and privateKey != 'n':
print("[!] Please make a choice between (y/n)")
privateKey = input("[?] Do you want to use a private key with SSH (y/n) : ")
else:
if str(privateKey) == 'y':
loop = True
displayBanner()
keyPath = input("[?] Specify the path of the private key : ")
while loop:
displayBanner()
if not os.path.isfile:
print(f"[!] The key {keyPath} does not exist")
keyPath = input("[?] Specify the path of the private key : ")
else:
loop = False
configReader.set(folderBackup,'key',keyPath)
elif str(privateKey) == 'n':
# If don't want to use a private key, which username to login ?
loop = True
displayBanner()
user = input("[?] Which user use for SSH login : ")
while loop:
displayBanner()
if len(user) == 0:
print("[!] Please, add a non empty username")
user = input("[?] Which user use for SSH login : ")
else:
configReader.set(folderBackup,'user',user)
loop = False
# Which password use to login
displayBanner()
passwd = input("[?] Which password use for SSH login : ")
configReader.set(folderBackup,'password',passwd)
# Add a comment for this backup configuration ?
loop = True
displayBanner()
comment = input("[?] Do you want to add a comment (y/n) : ")
while loop:
displayBanner()
if comment != 'y' and comment != 'n':
print("[!] Please make a choice between (y/n)")
comment = input("[?] Do you want to add a comment (y/n) : ")
else:
loop = False
if str(comment) == 'y':
readComment = input("[?] Add your comment to the backup configuration : \n")
configReader.set(folderBackup,'comment',readComment)
# Make a global view of the configuration
loop = True
while loop:
displayBanner()
print(f"[{folderBackup}]")
for name,value in configReader.items(folderBackup):
print(f"{name} : {value}")
response = input("\nConfirm backup configuration creation (y/n)")
if response != 'y' and response != 'n':
displayBanner()
print(f"[{folderBackup}]")
for name,value in configReader.items(folderBackup):
print(f"{name} : {value}")
response = input("\nConfirm backup configuration creation (y/n)")
else:
loop = False
# Finally write the configuration in the ini file
with open(decrypt,'a') as iniFile:
configReader.write(iniFile)
# Encrypt ini file at the end
encryptIni(decrypt,crypt,password)
return
except KeyboardInterrupt:
displayBanner()
encryptIni(decrypt,crypt,password)
print("\n[+] Successfully encrypted INI file before return to the menu")
time.sleep(3)
return main()
def main():
# -------------------------------------------------------
# Define some variables
# -------------------------------------------------------
# Get home folder
home = os.path.expanduser('~')
# Define PyCloud folder
pycloudFolder = f"{home}/.PyCloud"
# Define INI file
# Extension can be change for the encrypted ini file
# The decrypt ini file must have INI extension (for configparser checking)
encryptedIni = f"{pycloudFolder}/config.aes"
decryptedIni = f"{pycloudFolder}/config.ini"
# --------------------------------------------------------
# ------------------ Begin of the script -----------------
# --------------------------------------------------------
# It's better with a banner ! \o/
displayBanner()
# Check the installation at the begining
checkConfig(encryptedIni,decryptedIni, pycloudFolder)
# Diplay banner and initial decrypt INI file
displayBanner()
#password = input("[?] Use password to decrypt ini file : ")
#decryptIni(encryptedIni,decryptedIni,password)
# Large try
try:
displayBanner()
print(
"""
1. Launch backup
2. Add a new folder for backup
3. Change setting for a folder (modify a parameter)
4. Remove a folder
5. View backup settings
6. Change encryption password
99. Quit
"""
)
# Force type to choice
choice = int(input("[?] Choose an action : "))
if choice == 2:
addSite(decryptedIni,encryptedIni)
if choice == 5:
displayBanner()
password = input("[?] Please, enter the password to unlock ini file : ")
elif choice == 99:
exit()
# If user quit the script with escape keys, encrypt ini file before quit
except KeyboardInterrupt:
encryptIni(decryptedIni,encryptedIni,password)
print("\n[+] Successfully encrypted INI file before quit the script")
if __name__ == '__main__':
main()