File size: 5,120 Bytes
8247f19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
# Patch-overlapping Python context after the fix

## `apkleaks/apkleaks.py` — `class:APKLeaks` (lines 21-145)

```python
class APKLeaks:
	def __init__(self, args):
		self.file = args.file
		self.prefix = "apkleaks-"
		self.tempdir = tempfile.mkdtemp(prefix=self.prefix)
		self.main_dir = os.path.dirname(os.path.realpath(__file__))
		self.output = tempfile.mkstemp(suffix=".txt", prefix=self.prefix)[1] if args.output is None else args.output
		self.pattern = self.main_dir + "/../config/regexes.json" if args.pattern is None else args.pattern
		self.jadx = find_executable("jadx") if find_executable("jadx") is not None else self.main_dir + "/../jadx/bin/jadx%s" % (".bat" if os.name == "nt" else "")
		logging.config.dictConfig({"version": 1, "disable_existing_loggers": True})

	def apk_info(self):
		return APK(self.file)

	def dependencies(self):
		exter = "https://github.com/skylot/jadx/releases/download/v1.2.0/jadx-1.2.0.zip"
		with closing(urlopen(exter)) as jadx:
			with ZipFile(io.BytesIO(jadx.read())) as zfile:
				zfile.extractall(self.main_dir + "/../jadx")
		os.chmod(self.jadx, 33268)

	def write(self, message, color):
		sys.stdout.write("%s%s%s" % (color, message, clr.ENDC))

	def writeln(self, message, color):
		self.write(message + "\n", color)

	def integrity(self):
		if os.path.exists(self.jadx) is False:
			self.writeln("Can't find jadx binary.", clr.WARNING)
			valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
			while True:
				self.write("Do you want to download jadx? (Y/n) ", clr.OKBLUE)
				choice = input().lower()
				if choice == "":
					choice = valid["y"]
					break
				elif choice in valid:
					choice = valid[choice]
					break
				else:
					self.writeln("\nPlease respond with 'yes' or 'no' (or 'y' or 'n').", clr.WARNING)
			if choice:
				self.writeln("** Downloading jadx...\n", clr.OKBLUE)
				self.dependencies()
			else:
				sys.exit(self.writeln("Aborted.", clr.FAIL))

		if os.path.isfile(self.file) is True:
			try:
				self.apk = self.apk_info()
			except Exception as e:
				sys.exit(self.writeln(str(e), clr.WARNING))
			else:
				return self.apk
		else:
			sys.exit(self.writeln("It's not a valid file!", clr.WARNING))

	def decompile(self):
		self.writeln("** Decompiling APK...", clr.OKBLUE)
		with ZipFile(self.file) as zipped:
			try:
				dex = self.tempdir + "/" + self.apk.package + ".dex"
				with open(dex, "wb") as classes:
					classes.write(zipped.read("classes.dex"))
			except Exception as e:
				sys.exit(self.writeln(str(e), clr.WARNING))
		args = [self.jadx, dex, "-d", self.tempdir, "--deobf"]
		comm = "%s" % (" ".join(quote(arg) for arg in args))
		os.system(comm)
		return self.tempdir

	def unique(self, list): 
		x = numpy.array(list) 
		return (numpy.unique(x))

	def finder(self, pattern, path):
		matcher = re.compile(pattern)
		found = []
		for path, _, files in os.walk(path):
			for fn in files:
				filepath = os.path.join(path, fn)
				if mimetypes.guess_type(filepath)[0] is None:
					continue
				with open(filepath) as handle:
					for lineno, line in enumerate(handle):
						mo = matcher.search(line)
						if mo:
							found.append(mo.group())
		return self.unique(found)

	def extract(self, name, matches):
		output = open(self.output, "a+")
		if matches:
			stdout = ("[%s]" % (name))
			self.writeln("\n" + stdout, clr.OKGREEN)
			output.write(stdout + "\n")
			for secret in matches:
				if name == "LinkFinder" and re.match(r"^.(L[a-z]|application|audio|fonts|image|layout|multipart|plain|text|video).*\/.+", secret) is not None:
					continue
				stdout = ("- %s" % (secret))
				print(stdout)
				output.write(stdout + "\n")
			output.write("\n")
		output.close()

	def scanning(self):
		self.writeln("\n** Scanning against '%s'" % (self.apk.package), clr.OKBLUE)
		with open(self.pattern) as regexes:
			regex = json.load(regexes)
			for name, pattern in regex.items():
				if isinstance(pattern, list):
					for pattern in pattern:
						thread = threading.Thread(target = self.extract, args = (name, self.finder(pattern, self.tempdir)))
						thread.start()
				else:
					thread = threading.Thread(target = self.extract, args = (name, self.finder(pattern, self.tempdir)))
					thread.start()

	def __del__(self):
		print("%s\n** Results saved into '%s%s%s%s'%s" % (clr.OKBLUE, clr.ENDC, clr.OKGREEN, self.output, clr.OKBLUE, clr.ENDC))
		try:
			shutil.rmtree(self.tempdir)
		except Exception:
			return
```

## `apkleaks/apkleaks.py` — `function:APKLeaks.decompile` (lines 79-91)

```python
	def decompile(self):
		self.writeln("** Decompiling APK...", clr.OKBLUE)
		with ZipFile(self.file) as zipped:
			try:
				dex = self.tempdir + "/" + self.apk.package + ".dex"
				with open(dex, "wb") as classes:
					classes.write(zipped.read("classes.dex"))
			except Exception as e:
				sys.exit(self.writeln(str(e), clr.WARNING))
		args = [self.jadx, dex, "-d", self.tempdir, "--deobf"]
		comm = "%s" % (" ".join(quote(arg) for arg in args))
		os.system(comm)
		return self.tempdir
```

## `apkleaks/apkleaks.py` — `module:<module>@5` (lines 5-5)

```python
from pipes import quote
```