-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLogLeaks.py
145 lines (111 loc) · 4.78 KB
/
LogLeaks.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
import subprocess
import os
import time
import io
import re
import csv
import sys
from datetime import datetime
STIGMA_GENERATED_MESSAGE = "LEAK via LOGD OCCURING!" # message stigma generates when it detects a leak
TEST_TAG_START = "D stigmatestapp" # start of each test's tag according to above format
END_REACHED_FLAG = "endstigmatestapp" # StigmaTestApp Logs this to help detect end of testing
sourceDict = {}
sourceDict['A'] = "IMEI"
sourceDict['B'] = "Phone Number"
sourceDict['C'] = "Location"
sinkDict = {}
sinkDict['Z'] = "Write"
sinkDict['Y'] = "Logd"
def main():
## Note: run emulator before running test
# get apk path from console input
if(not os.path.exists(sys.argv[1])):
raise ValueError("Input file (" + sys.argv[1] + ") was not found or was not readable.")
return sys.argv[1]
path = sys.argv[1]
# Clear logcat to remove past test logs
clear_completed = False
while not clear_completed:
try:
print("Clearing Logcat")
clearLog = ["adb", "logcat", "-c"]
completedProcess = subprocess.run(clearLog)
completedProcess.check_returncode()
clear_completed = True
except Exception as e:
print("Clear failed. Retrying...")
#adb logcat -b all -c
clearLog = ["adb", "logcat", "-b", "all", "-c"]
completedProcess = subprocess.run(clearLog)
completedProcess.check_returncode()
# Reinstall instrumented StigmaTestApp (Note: Needs to be installed manually once)
# the -r means "re-install" it also means that
# the permissions for the app are not wiped
installApk = ["adb", "install", "-r", path]
completedProcess2 = subprocess.run(installApk)
completedProcess2.check_returncode()
'''# Run StigmaTestApp and read tests from logcat
runApk = ["adb", "shell", "am", "start", "-n", "com.example.stigmatestapp/com.example.stigmatestapp.MainActivity"]
completedProcess3 = subprocess.run(runApk)
completedProcess3.check_returncode()'''
# -d means to "dump" i.e., just give all the logcat output up until this point
# and then stop the process
#time.sleep(3)
#os.system("adb logcat -d | python3 ReadLogCat.py")
logcatProcess = subprocess.Popen(["adb", "logcat"], stdout=subprocess.PIPE)
ReadLogCat(logcatProcess, path)
def ReadLogCat(logcatProcess, inputPath):
# Logcat Leak Detection Protocol
#Tag: STIGMA__
#1st Pos:
#A = Source
#Z = Sink
#2nd Pos:
#A-Z for sources
#A-Z for sinks
#A = IMEI
#B = Phone Number
#Z = Write
#Y = Logd
#remove apk extension with -4. make more robust
outputPath = inputPath[:len(inputPath)-4] + "_Leaks.csv"
if(not os.path.exists(outputPath)): #So we dont override previous data
outputFile = open(outputPath, 'w', newline='')
fieldnames = ['date_time', 'apk_path', 'source', 'sink']
writer = csv.DictWriter(outputFile, fieldnames=fieldnames)
writer.writeheader()
outputFile.close()
print ("LEAK DETECTION STARTED")
print()
for line in io.TextIOWrapper(logcatProcess.stdout, encoding="utf-8"):
if "STIGMAZ" in line:
# Data leaked out of sink
#So we can check csv file while LogLeaks is running
outputFile = open(outputPath, 'a', newline='')
fieldnames = ['date_time', 'apk_path', 'source', 'sink']
writer = csv.DictWriter(outputFile, fieldnames=fieldnames)
now = datetime.now()
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
sinkKey = re.findall("STIGMAZ(\S)", line)[0]
sink = sinkDict[sinkKey]
print("Wrote: ")
print({'date_time' : date_time, 'apk_path': inputPath, 'source': '', 'sink': sink})
print()
writer.writerow({'date_time' : date_time, 'apk_path': inputPath, 'source': '', 'sink': sink})
outputFile.close()
if "STIGMAA" in line:
# Data entered from source
#So we can check csv file while LogLeaks is running
outputFile = open(outputPath, 'a', newline='')
fieldnames = ['date_time', 'apk_path', 'source', 'sink']
writer = csv.DictWriter(outputFile, fieldnames=fieldnames)
now = datetime.now()
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
sourceKey = re.findall("STIGMAA(\S)", line)[0]
source = sourceDict[sourceKey]
print("Wrote: ")
print({'date_time' : date_time, 'apk_path': inputPath, 'source': source, 'sink': ''})
print()
writer.writerow({'date_time' : date_time, 'apk_path': inputPath, 'source': source, 'sink': ''})
outputFile.close()
main()