-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMSAttachments.py
109 lines (103 loc) · 3.26 KB
/
SMSAttachments.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
import FileLister
import io
import hashlib
import os
import xml.etree.ElementTree as ET
import sys
from shutil import copyfile
from subprocess import call
from timeit import default_timer
#created by Luis Sanchez
def main():
backupPath = None
userPath = os.environ['HOME']
if '/' == userPath[0]:
backupPath = userPath + "/Library/Application Support/MobileSync/Backup/"
else:
#same for windows
print("windows")
dictioList = getDevicesName(backupPath).items()
print("Please select the device that you would like to extract the attachments from:")
num = 1
for name in dictioList:
print(str(num) +":" + name[0])
num +=1
device = None
while True:
try: #end program with control c, its int he try and except errr
selection = int(input(''))
if 1<= selection <= len(dictioList):
device = dictioList[selection-1]
print("Thank you. The program will do the rest. Just sit back and relax :)\n")
break
else:
print("Please try again. Select one of the option above.")
except:
print("Please enter a number from the list above.")
start = default_timer()
location, amountFiles = SMSAttachments(device)
duration = str(default_timer() - start)
print("\nThe program finished copying " + str(amountFiles) + " files to " + location + " in " + duration + " seconds.")
def SMSAttachments(device):
path = device[1]
newDirPath = makedir()
backUpFiles = FileLister.process_mbdb_file(path + "/Manifest.mbdb")
#copy files
amountFiles = 0
for hashed in backUpFiles:
file = path+"/"+hashed[0]
if os.path.isfile(file): #check if the file exists inside backup folder
fileName = hashed[1]['filename'][67:]
count = 1
copy = ""
newDes = newDirPath+"/"+fileName
while True:
if not os.path.isfile(newDes):
#print(newDes)
print("Copying "+ copy+fileName)
copyfile(file, newDes)
addProperties(hashed[1],newDes)
amountFiles +=1
break
else:
newDes = newDirPath+"/"+str(count)+fileName
copy = str(count)
count +=1
return (newDirPath, amountFiles)
def addProperties(fileInfo, filePath):
cTime = fileInfo['ctime']
mTime = fileInfo['mtime']
aTime = fileInfo['atime']
#apply ctime, and mtime. not sure if ctime is the correct one...
os.utime(filePath,(aTime, mTime))
def makedir():
#make directory for the SMSAttachemnts
original = "SMSAttachments"
path = os.environ['HOME'] + "/Desktop/" #change for windows
count = 1
while True:
if not os.path.exists(path + original):
os.makedirs(path + original)
break
else:
original = "SMSAttachments" + str(count)
count +=1
return path + original
def getDevicesName(path):
#gets the path of the backup folders
directory = os.listdir(path)
deviceNames = {}
for folder in directory:
manifest = path+folder
if folder != ".DS_Store" and os.path.exists(manifest +"/Manifest.mbdb"): #checks if there is a manifest.mbdb file, change for windows
infoPlist = path + folder + "/Info.plist"
#extracts the Device name from the info.plist file
if os.path.exists(infoPlist):
deviceNames[ET.parse(infoPlist).getroot()[0][5].text] = manifest
else:
print("There is not a Info.plist file, please back up your device correctly.")
if not deviceNames:
sys.exit("Please backup a device and run this program again.")
return deviceNames
if __name__ == "__main__":
main()