Skip to content

Commit 7f05c8c

Browse files
committed
Major refactoring for including hash generation
1 parent c9c1b88 commit 7f05c8c

File tree

3 files changed

+76
-50
lines changed

3 files changed

+76
-50
lines changed

acquisitionGUI.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def extract_thread():
103103
sg.Push(),
104104
sg.Text('ADB Extractor', size=(30, 1), justification='center', font=("Helvetica", 25)),
105105
sg.Push(),
106-
sg.Button('?', size=(1, 1), key='-INFO-'),
106+
#sg.Button('?', size=(1, 1), key='-INFO-'),
107107
],
108108
[sg.VerticalSeparator(pad=((0, 0), (10, 10)))],
109109
*options_layout,
@@ -114,16 +114,19 @@ def extract_thread():
114114
],
115115
[sg.VerticalSeparator(pad=((0, 0), (10, 10)))],
116116
[
117+
sg.Push(),
117118
sg.Column(packages_layout, size=(350, 300)),
118119
sg.VSeperator(),
119120
sg.Column(output_layout, size=(400, 300)),
121+
sg.Push()
120122
],
121123
[sg.VerticalSeparator(pad=((0, 0), (10, 10)))],
122124
[
123125
sg.Push(),
124126
sg.Button('Extract', size=(15, 1), key='-EXTRACT-'),
125127
sg.Button('Exit', size=(15, 1), key='-EXIT-'),
126-
sg.Text('LabCIF - 2023', size=(30, 1), justification='right', font=("Helvetica", 10)),
128+
sg.Push(),
129+
#sg.Text('LabCIF - 2023', size=(30, 1), justification='right', font=("Helvetica", 10)),
127130
]
128131
]
129132

modules/adb_acquistion.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33
import time
44
from datetime import datetime
5+
import modules.sha_hashes as sha_hashes
56

67

78
class Bcolors:
@@ -109,21 +110,28 @@ def get_acquistion(APP, DEVICE, DATA, callback=None, folder=''):
109110
FILENAME = APP + "-v" + str(VERSION) + "-" + DATA + "--" + DEVNAME + str(ANDROID) + "-u" + str(
110111
USER) + "--" + datetime.now().strftime("%Y%m%d-%H%M%S") + ".tar"
111112

113+
OUTPUT_FOLDER = FILENAME[:-4]
114+
if folder != '':
115+
OUTPUT_FOLDER = folder + "/" + OUTPUT_FOLDER
116+
OUTPUT_FILE = OUTPUT_FOLDER + "/" + "sha256_hashes.txt"
117+
# create the folder
118+
if not os.path.exists(OUTPUT_FOLDER):
119+
os.makedirs(OUTPUT_FOLDER)
120+
112121
print_message(callback, "[Info ] " + APP + " version: " + str(VERSION))
113122
print_message(callback, "[Info ] Android version: " + str(ANDROID))
114123

115124
print_message(callback, "[Info ] Copying data from " + APP + " version " + VERSION + " ...")
116125

117126
if DATA == "private" or DATA == "public":
118127
if DATA == "private":
119-
print("[Info ] Calculating hashes...")
120-
hashes_output = subprocess.run(ADB + " " + DEVICE + " shell " + CMD + "'find /data/data/" + APP + END + " -type f -exec sha256sum {} \;'", stdout=subprocess.PIPE, shell=True)
121-
output = hashes_output.stdout.decode("utf-8").strip()
122128
print_message(callback, "[Info ] Acquiring private data")
123129
# Primary method used to copy the data from the application
124130
# In windows this process was better when dealing with threads such as in the GUI
125131
if callback is not None and os.name == 'nt':
126-
subprocess.run(ADB + " " + DEVICE + " shell " + CMD + " tar -cvzf /sdcard/Download/" + FILENAME + " /data/data/" + APP + END, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
132+
subprocess.run(
133+
ADB + " " + DEVICE + " shell " + CMD + " tar -cvzf /sdcard/Download/" + FILENAME + " /data/data/" + APP + END,
134+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
127135
else:
128136
# Method used in the bash script to copy the data from the application
129137
# Check for filename with spaces
@@ -138,23 +146,27 @@ def get_acquistion(APP, DEVICE, DATA, callback=None, folder=''):
138146
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
139147

140148
elif DATA == "public":
141-
print("[Info ] Calculating hashes...")
142-
hashes_output = subprocess.run(
143-
ADB + " " + DEVICE + " shell " + CMD + "'find /storage/emulated/0/Android/data/" + APP + END + " -type f -exec sha256sum {} \;'",
144-
stdout=subprocess.PIPE, shell=True)
145-
output = hashes_output.stdout.decode("utf-8").strip()
146149
print_message(callback, "[Info ] Acquiring public data")
147150
# Primary method used to copy the data from the application
148151
# In windows this process was better when dealing with threads such as in the GUI
149152
if callback is not None and os.name == 'nt':
150-
subprocess.run(ADB + " " + DEVICE + " shell " + CMD + " tar -cvzf /sdcard/Download/" + FILENAME + " /storage/emulated/0/Android/data/" + APP + END, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
153+
subprocess.run(
154+
ADB + " " + DEVICE + " shell " + CMD + " tar -cvzf /sdcard/Download/" + FILENAME + " /storage/emulated/0/Android/data/" + APP + END,
155+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=True)
151156
else:
152157
# Method used in the bash script to copy the data from the application
153158
# Check for filename with spaces
154159
subprocess.run(
155160
ADB + " " + DEVICE + " shell " + CMD + " tar -cvzf /sdcard/Download/" + FILENAME + " /storage/emulated/0/Android/data/" + APP + END,
156161
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, shell=SHELL)
157162

163+
OUTPUT_FOLDER = FILENAME[:-4]
164+
if folder != '':
165+
OUTPUT_FOLDER = folder + "/" + OUTPUT_FOLDER
166+
OUTPUT_FILE = OUTPUT_FOLDER + "/" + "sha256_hashes.txt"
167+
# create the folder
168+
if not os.path.exists(OUTPUT_FOLDER):
169+
os.makedirs(OUTPUT_FOLDER)
158170
# Retrieve the file from the device and save it to the current directory and remove the file from the device
159171
print_message(callback, "[Info ] Copying to local storage ...")
160172
print_message(callback, "[Info ] Compressing " + FILENAME + " ...")
@@ -163,25 +175,25 @@ def get_acquistion(APP, DEVICE, DATA, callback=None, folder=''):
163175
print_message(callback, "[Info] Compressing Terminated.")
164176

165177
print_message(callback, "[Info ] Copying to local storage ...")
166-
if folder == '':
167-
subprocess.run(ADB + " " + DEVICE + " pull /sdcard/Download/" + FILENAME + ".gz", stdout=subprocess.DEVNULL,
168-
shell=True)
169-
else:
170-
subprocess.run(ADB + " " + DEVICE + " pull /sdcard/Download/" + FILENAME + ".gz " + folder,
171-
stdout=subprocess.DEVNULL, shell=True)
178+
subprocess.run(ADB + " " + DEVICE + " pull /sdcard/Download/" + FILENAME + ".gz " + OUTPUT_FOLDER,
179+
stdout=subprocess.DEVNULL,
180+
shell=True)
172181
print_message(callback, "[Info ] Copy Terminated.")
173182
print("[Info ] Creating hashes file...")
174183
# Check if the command executed successfully
175-
if output == '':
176-
print_message(callback, "[Info ] No hashes to store.", "error")
177-
return
178184
# Save the output to a file
179-
#remove any \r\n
180-
H_APP = APP.replace('\r', '')
181-
H_FILENAME = H_APP + "-" + DATA + "--sha256_hashes"
182-
OUTPUT_FILE = folder + "/" + H_FILENAME + ".txt"
183-
with open(OUTPUT_FILE, 'w') as f:
184-
f.write(output)
185+
186+
# unzip the file
187+
print_message(callback, "[Info ] Unzipping file...")
188+
subprocess.run("gzip -d " + OUTPUT_FOLDER + "/" + FILENAME + ".gz", stdout=subprocess.DEVNULL, shell=True)
189+
print_message(callback, "[Info ] Unzip Terminated.")
190+
# untar the file
191+
print_message(callback, "[Info ] Untaring file...")
192+
subprocess.run("tar -xvf " + OUTPUT_FOLDER + "/" + FILENAME + " -C " + OUTPUT_FOLDER, stdout=subprocess.DEVNULL,
193+
stderr=subprocess.DEVNULL, shell=True)
194+
print_message(callback, "[Info ] Untar Terminated.")
195+
print_message(callback, "[Info ] Calculating hashes.")
196+
sha_hashes.calculate_sha256_for_directory(OUTPUT_FOLDER, OUTPUT_FILE)
185197

186198
print_message(callback, "[Info ] Cleaning acquisition files from phone...")
187199
subprocess.run(ADB + " " + DEVICE + " shell rm /sdcard/Download/" + FILENAME + ".gz", stdout=subprocess.DEVNULL,
@@ -195,31 +207,20 @@ def get_acquistion(APP, DEVICE, DATA, callback=None, folder=''):
195207
APK = APK.stdout.decode("utf-8").strip()
196208
APK = APK.split(":")[1]
197209
print_message(callback, "[Info ] APK: " + APK)
198-
if folder == '':
199-
subprocess.run(ADB + " " + DEVICE + " pull " + APK + " " + APP + ".apk", shell=True)
200-
else:
201-
subprocess.run(ADB + " " + DEVICE + " pull " + APK + " " + folder + "/" + APP + ".apk", shell=True)
202210

203-
#calculate sha256 of the apk
211+
subprocess.run(ADB + " " + DEVICE + " pull " + APK + " " + APP + ".apk", stdout=subprocess.DEVNULL, shell=True)
212+
os.rename("base.apk", APP + ".apk")
213+
# move the apk to the output folder
214+
os.rename(APP + ".apk", OUTPUT_FOLDER + "/" + APP + ".apk")
215+
216+
# calculate sha256 of the apk
204217
print_message(callback, "[Info ] Calculating hashes...")
205-
hashes_output = subprocess.run(
206-
ADB + " " + DEVICE + " shell " + CMD + "'sha256sum " + APK + "'",
207-
stdout=subprocess.PIPE, shell=True)
208-
output = hashes_output.stdout.decode("utf-8").strip()
209-
print_message(callback, "[Info ] Creating hashes file...")
210-
# Check if the command executed successfully
211-
if output == '':
212-
print_message(callback, "[Info ] No hashes to store.", "error")
213-
return
214-
# Save the output to a file
215-
# remove any \r\n
216-
H_APP = APP.replace('\r', '')
217-
H_FILENAME = H_APP + "-" + DATA + "--sha256_hashes"
218-
OUTPUT_FILE = folder + "/" + H_FILENAME + ".txt"
219-
with open(OUTPUT_FILE, 'w') as f:
220-
f.write(output)
221-
print_message(callback, "[Done ] Operation Completed with success, generated file: " + APP + ".apk", "ok")
218+
sha256 = sha_hashes.calculate_sha256(OUTPUT_FOLDER + "/" + APP + ".apk")
219+
# save the sha256 to a file
220+
with open(OUTPUT_FILE, "w") as file:
221+
file.write(f"{sha256} *{APP}.apk\n")
222222

223+
print_message(callback, "[Done ] Operation Completed with success, generated file: " + APP + ".apk", "ok")
223224
else:
224-
print_message(callback, "[ERROR] Invalid data type!", "error")
225+
print_message(callback, "[ERROR] Invalid data type!", "error")
225226
return

modules/sha_hashes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import hashlib
2+
import os
3+
4+
5+
def calculate_sha256(file_path):
6+
sha256_hash = hashlib.sha256()
7+
with open(file_path, "rb") as file:
8+
for chunk in iter(lambda: file.read(4096), b""):
9+
sha256_hash.update(chunk)
10+
return sha256_hash.hexdigest()
11+
12+
13+
def calculate_sha256_for_directory(root_dir, output_file):
14+
with open(output_file, "w") as file:
15+
for foldername, subfolders, filenames in os.walk(root_dir):
16+
for filename in filenames:
17+
file_path = os.path.join(foldername, filename)
18+
sha256 = calculate_sha256(file_path)
19+
relative_path = os.path.relpath(file_path, root_dir)
20+
#ignore if the file is the sha256 file itself
21+
if "sha256_hashes.txt" not in relative_path:
22+
file.write(f"{sha256} *{relative_path}\n")

0 commit comments

Comments
 (0)