Skip to content

Commit 4d753d9

Browse files
committed
Update ignore feature; need to handle in Report too
1 parent 30d1587 commit 4d753d9

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

Analyze.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,45 @@
55
import fnmatch
66
import os
77

8+
from Permissions import Permissions
9+
810
class Analyze:
911
"""Analyze object that scrapes project source looking for permissions matches."""
1012

11-
def __init__(self, project_root, package_name, permissions):
13+
def __init__(self, project_root, package_name, permissions, ignore):
1214
"""Init method of Analyze."""
1315
self.project_root = project_root
1416
self.package_name = package_name
1517
self.permissions = permissions
1618
self.report_file_name = "reports/source_report_" + self.package_name + ".txt"
1719
self.source_files = []
1820
self.lines = []
21+
self.ignore = ignore
1922

2023
def search_project_root(self):
2124
"""Looks in the source root for matching files with permissions."""
2225
print("Analyzing from project root....")
23-
search_string = "permission"
26+
2427
source_root = self.project_root + "/app/src/"
2528
matches = []
2629

30+
# Add any ignored group permissions to the set of individual perms
31+
dangerous_permissions = Permissions().dangerous_permissions
32+
if len(self.ignore['groups']) > 0:
33+
for group in self.ignore['groups']:
34+
35+
# Get the specific list of permission group and permissions
36+
ignored_permissions = dangerous_permissions[group]
37+
for permission in ignored_permissions:
38+
dangerous_permission = "android.permission." + permission
39+
self.ignore['individual'].add(dangerous_permission)
40+
41+
# Ignore specific permissions
42+
if len(self.ignore['individual']) > 0:
43+
print("Based on config, ignoring the following permissions:")
44+
for permission in self.ignore['individual']:
45+
print("Ignoring: " + permission)
46+
2747
# Search for matching java files
2848
for root, dirnames, filenames in os.walk(source_root):
2949
for filename in fnmatch.filter(filenames, "*.java"):
@@ -32,12 +52,19 @@ def search_project_root(self):
3252
current_file = ""
3353
with open(file) as java_file:
3454
for index, line in enumerate(java_file):
35-
if search_string in line:
36-
if current_file is not java_file.name:
37-
current_file = java_file.name
38-
self.lines.append(('{} {:>4}\n'.format("\nFile: ", current_file)))
39-
self.source_files.append(current_file)
40-
self.lines.append(('{:>4} {}'.format(index, line.rstrip())))
55+
if "permission" in line:
56+
57+
# Ignore the line if it has an ignored permission,
58+
# otherwise add the line to the source_lines list
59+
for ignored_permission in self.ignore['individual']:
60+
if ignored_permission in line:
61+
break
62+
else:
63+
if current_file is not java_file.name:
64+
current_file = java_file.name
65+
self.lines.append(('{} {:>4}\n'.format("\nFile: ", current_file)))
66+
self.source_files.append(current_file)
67+
self.lines.append(('{:>4} {}'.format(index, line.rstrip())))
4168
print("Analyzing finished!")
4269

4370
# Print the source report

MPerm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def main():
110110
print("Looking in root for a config.txt...")
111111
ignore = {
112112
'groups': set(),
113-
'permissions': set()
113+
'individual': set()
114114
}
115115
try:
116116
with open("./config.txt") as config:
@@ -125,7 +125,7 @@ def main():
125125
elif line != '\n':
126126
# specific permissions
127127
sanitized = line.rstrip()
128-
ignore['permissions'].add(sanitized)
128+
ignore['individual'].add(sanitized)
129129
print("Config found. Analysis will ignore the stated permissions.")
130130

131131
except FileNotFoundError:
@@ -146,7 +146,7 @@ def main():
146146
third_party_permissions = get_third_party_permissions(manifest_tree)
147147

148148
# Scrape the source
149-
analyzer = Analyze(source_path, package_name, permissions)
149+
analyzer = Analyze(source_path, package_name, permissions, ignore)
150150
source_report = analyzer.search_project_root()
151151

152152
# Analyze and print results

0 commit comments

Comments
 (0)