forked from nerdunit/androidsideloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spoofer.cs
243 lines (219 loc) · 10.8 KB
/
spoofer.cs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using AndroidSideloader;
using AndroidSideloader.Utilities;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
namespace Spoofer
{
internal class spoofer
{
public static string alias = string.Empty;
public static string password = string.Empty;
public static void Init()
{
//If there is no keystore or details (user and pw for keystore) generate them!
if ((File.Exists("keystore.key") == false || File.Exists("details.txt") == false) && HasDependencies())
{
var rand = new Random();
alias = GeneralUtilities.randomString(8);
password = GeneralUtilities.randomString(16);
string subject = $"CN = {GeneralUtilities.randomString(rand.Next(2, 6))}, OU = {GeneralUtilities.randomString(rand.Next(2, 6))}, O = {GeneralUtilities.randomString(rand.Next(2, 6))}, L = {GeneralUtilities.randomString(rand.Next(2, 6))}, ST = {GeneralUtilities.randomString(rand.Next(2, 6))}, C = {GeneralUtilities.randomString(rand.Next(2, 6))}";
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine($"keytool -genkeypair -alias {alias} -keyalg RSA -keysize 2048 -keystore keystore.key -keypass {password} -storepass {password} -dname \"{subject}\"");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
string keyerror = cmd.StandardError.ReadToEnd();
string keyoutput = cmd.StandardOutput.ReadToEnd();
Logger.Log($"Output: {keyoutput} Error: {keyerror}");
File.WriteAllText("details.txt", $"{alias};{password}");
}
else
{
var temp = File.ReadAllText("details.txt").Split(';');
alias = temp[0];
password = temp[1];
}
}
public static string folderPath = string.Empty;
public static string decompiledPath = string.Empty;
public static string newPackageName = string.Empty;
public static string originalPackageName = string.Empty;
public static string spoofedApkPath = string.Empty;
//public static ProcessOutput ResignAPK(string apkPath)
//{
// string output = "";
// string oldGameName = Path.GetFileName(apkPath);
// folderPath = apkPath.Replace(Path.GetFileName(apkPath), "");
// File.Move(apkPath, $"{folderPath}temp.apk");
// apkPath = $"{folderPath}temp.apk";
// decompiledPath = apkPath.Replace(".apk", "");
// string packagename = PackageName(apkPath);
//}
public static ProcessOutput SpoofApk(string apkPath, string newPackageName, string obbPath = "", string spoofedFileName = "spoofed.apk")
{
//Rename
ProcessOutput output = new ProcessOutput("", "");
string oldGameName = Path.GetFileName(apkPath);
folderPath = apkPath.Replace(Path.GetFileName(apkPath), "");
File.Move(apkPath, $"{folderPath}temp.apk");
apkPath = $"{folderPath}temp.apk";
decompiledPath = apkPath.Replace(".apk", "");
//newPackageName = $"com.{Utilities.randomString(rand.Next(3, 8))}.{Utilities.randomString(rand.Next(3, 8))}";
originalPackageName = PackageName(apkPath);
Logger.Log($"Your app will be spoofed as {newPackageName}");
Logger.Log($"Folderpath: {folderPath} decompiledPaht: {decompiledPath} ");
if (obbPath.Length > 1)
{
RenameObb(obbPath, newPackageName, originalPackageName);
}
output += DecompileApk(apkPath);
//Rename APK Packagename
string foo = File.ReadAllText($"{decompiledPath}\\AndroidManifest.xml").Replace(originalPackageName, newPackageName);
File.WriteAllText($"{decompiledPath}\\AndroidManifest.xml", foo);
foreach (string file in Directory.EnumerateFiles(decompiledPath, "*.*", SearchOption.AllDirectories))
{
if (Path.GetFileName(file) == "BuildConfig.smali")
{
foo = File.ReadAllText(file).Replace(originalPackageName, newPackageName);
File.WriteAllText(file, foo);
}
}
//spoofedApkPath = $"{Path.GetFileName(apkPath).Replace(".apk", "")}_Spoofed as {newPackageName}.apk";
spoofedApkPath = Path.GetDirectoryName(apkPath) + "\\" + spoofedFileName;
string apkDecompiledPath = Path.GetFileName(apkPath).Replace(".apk", "");
output += GeneralUtilities.startProcess("cmd.exe", folderPath, $"apktool b \"{apkDecompiledPath}\" -o \"{spoofedApkPath}\"");
//Sign the new apk
if (File.Exists(folderPath + "keystore.key") == false)
File.Copy("keystore.key", $"{folderPath}keystore.key");
output += SignApk(spoofedApkPath);
//Delete the copy of the key and the decompiled apk folder
if (string.Equals(folderPath, Environment.CurrentDirectory + "\\") == false)
File.Delete($"{folderPath}keystore.key");
Directory.Delete(decompiledPath, true);
File.Delete(apkPath);
return output;
}
public static ProcessOutput SignApk(string path)
{
//Logger.Log($"jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore keystore.key \"{path}\" {alias}");
Process cmdSign = new Process();
cmdSign.StartInfo.FileName = "cmd.exe";
cmdSign.StartInfo.RedirectStandardInput = true;
cmdSign.StartInfo.WorkingDirectory = folderPath;
cmdSign.StartInfo.CreateNoWindow = true;
cmdSign.StartInfo.UseShellExecute = false;
cmdSign.StartInfo.RedirectStandardOutput = true; //
cmdSign.StartInfo.RedirectStandardError = true; //
cmdSign.Start();
cmdSign.StandardInput.WriteLine($"jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore keystore.key \"{path}\" {alias}");
cmdSign.StandardInput.WriteLine(password);
cmdSign.StandardInput.Flush();
cmdSign.StandardInput.Close();
cmdSign.StandardOutput.Close();
cmdSign.WaitForExit();
//For some reason it hangs when also reading output...
//string output = cmdSign.StandardOutput.ReadToEnd();
//string error = cmdSign.StandardError.ReadToEnd();
//Logger.Log("Jarsign Output " + output);
//Logger.Log("Error: " + error);
//return new ProcessOutput(output, error);
return new ProcessOutput("", "");
}
public static ProcessOutput DecompileApk(string path)
{
ProcessOutput output = GeneralUtilities.startProcess("cmd.exe", folderPath, $"apktool d -f \"{path}\"");
return output;
}
public static bool HasDependencies()
{
if (ExistsOnPath("jarsigner") && ExistsOnPath("apktool") && ExistsOnPath("aapt"))
return true;
return false;
}
public static bool ExistsOnPath(string exeName)
{
try
{
using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "where";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.Arguments = exeName;
p.Start();
p.WaitForExit();
return p.ExitCode==0;
}
}
catch
{
throw new Exception("'where' command is not on path");
}
}
public static string GetFullPath(string exeName)
{
try
{
using (Process p = new Process())
{
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "where";
p.StartInfo.Arguments = exeName;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
if (p.ExitCode != 0)
return null;
// just return first match
return output.Substring(0, output.IndexOf(Environment.NewLine));
}
}
catch
{
throw new Exception("'where' command is not on path");
}
}
//Renames obb to new obb according to packagename
public static void RenameObb(string obbPath, string newPackageName, string originalPackageName)
{
Directory.Move(obbPath, obbPath.Replace(originalPackageName, newPackageName));
obbPath = obbPath.Replace(originalPackageName, newPackageName);
foreach (string file in Directory.GetFiles(obbPath))
{
if (Path.GetExtension(file) == ".obb")
{
File.Move(file, file.Replace(originalPackageName, newPackageName));
}
}
}
public static string PackageName(string path)
{
Logger.Log($"aapt dump badging \"{path}\"");
string originalPackageName = GeneralUtilities.startProcess("cmd.exe", path.Replace(Path.GetFileName(path), string.Empty), $"aapt dump badging \"{path}\" | findstr -i \"package: name\"").Output;
Logger.Log($"originalPackageName: {originalPackageName}");
try
{
originalPackageName = originalPackageName.Substring(originalPackageName.IndexOf("package: name='") + 15);
originalPackageName = originalPackageName.Substring(0, originalPackageName.IndexOf("'"));
}
catch
{
return "PackageName ERROR";
}
return originalPackageName;
}
}
}