-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbdecrypt.py
More file actions
77 lines (72 loc) · 3.27 KB
/
dbdecrypt.py
File metadata and controls
77 lines (72 loc) · 3.27 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
#!/usr/bin/env python
#
# Funf: Open Sensing Framework
# Copyright (C) 2010-2011 Nadav Aharony, Wei Pan, Alex Pentland.
# Acknowledgments: Alan Gardner
# Contact: nadav@media.mit.edu
#
# This file is part of Funf.
#
# Funf is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# Funf is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Funf. If not, see <http://www.gnu.org/licenses/>.
#
'''Decrypt one or more sqlite3 files using the provided key. Checks to see if it is readable
'''
from optparse import OptionParser
import decrypt
import sqlite3
import shutil
_random_table_name = 'jioanewvoiandoasdjf'
def is_funf_database(file_name):
try:
conn = sqlite3.connect(file_name)
conn.execute('create table %s (nothing text)' % _random_table_name)
conn.execute('drop table %s' % _random_table_name)
except (sqlite3.OperationalError, sqlite3.DatabaseError):
return False
else:
return True
finally:
if conn is not None: conn.close()
def decrypt_if_not_db_file(file_name, key, extension=None):
if is_funf_database(file_name):
print "Already decrypted: '%s'" % file_name
return True
else:
print ("Attempting to decrypt: '%s'..." % file_name),
decrypt.decrypt([file_name], key, extension)
if is_funf_database(file_name):
print "Success!"
return True
else:
print "FAILED!!!!"
print "File is either encrypted with another method, another key, or is not a valid sqlite3 db file."
print "Keeping original file."
shutil.move(decrypt.backup_file(file_name, extension), file_name)
return False
if __name__ == '__main__':
usage = "%prog [options] [sqlite_file1.db [sqlite_file2.db...]]"
description = "Safely decrypt Sqlite3 db files. Checks to see if the file can be opened by Sqlite. If so, the file is left alone, otherwise the file is decrypted. Uses the decrypt script, so it always keeps a backup of the original encrypted files. "
parser = OptionParser(usage="%s\n\n%s" % (usage, description))
parser.add_option("-i", "--inplace", dest="extension", default=None,
help="The extension to rename the original file to. Will not overwrite file if it already exists. Defaults to '%s'." % decrypt.default_extension,)
parser.add_option("-k", "--key", dest="key", default=None,
help="The DES key used to decrypt the files. Uses the default hard coded one if one is not supplied.",)
(options, args) = parser.parse_args()
key = options.key if options.key else decrypt.key_from_password(decrypt.prompt_for_password())
try:
for file_name in args:
decrypt_if_not_db_file(file_name, key, options.extension)
except Exception as e:
import sys
sys.exit("ERROR: " + str(e))