-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCrackProcess.cs
272 lines (244 loc) · 9.67 KB
/
CrackProcess.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using Ionic.Zip;
using Renci.SshNet;
using Renci.SshNet.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Brake
{
public partial class CrackProcess : Form
{
private static Container xml;
private Queue<IPAInfo> queue;
private String DeviceTempDir;
delegate void LogCallback(string text);
PasswordConnectionInfo connectionInfo;
SftpClient sftp;
SshClient ssh;
private void log(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (logBox.InvokeRequired)
{
LogCallback d = new LogCallback(log);
this.Invoke(d, new object[] { text });
}
else
{
logBox.AppendText(text + Environment.NewLine);
Console.WriteLine("log: " + text);
}
}
delegate void PercentStatusCallback(string text, int percent);
private void PercentStatus(string text, int percent)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (logBox.InvokeRequired)
{
PercentStatusCallback d = new PercentStatusCallback(PercentStatus);
this.Invoke(d, new object[] { text, percent });
}
else
{
statusLabel.Text = "status: " + text + " (" + percent + "%)";
progressBar1.Value = percent;
}
}
private void crackIPA(IPAInfo ipaInfo)
{
log("beginning cracking process..");
PercentStatus("Establishing SSH connection", 5);
PercentStatus("Establishing SFTP connection", 10);
log("Cracking " + ipaInfo.AppName);
PercentStatus("Preparing IPA", 25);
/*String ipalocation = AppHelper.extractIPA(ipaInfo);
using (var file = File.OpenRead(ipalocation))
{
log("Uploading IPA to device..");
PercentStatus("Uploading IPA", 40);
sftp.UploadFile(file, "Upload.ipa");
}*/
log("Cracking! (This might take a while)");
PercentStatus("Cracking", 50);
String binaryLocation = ipaInfo.BinaryLocation.Replace("Payload/", "");
String TempDownloadBinary = Path.Combine(AppHelper.GetTemporaryDirectory(), "crackedBinary");
var crack = ssh.RunCommand("Clutch -i '" + ipaInfo.IPALocationOnDevice +"' " + binaryLocation + " /tmp/crackedBinary");
log("Clutch -i '" + ipaInfo.IPALocationOnDevice +"' " + binaryLocation + " /tmp/crackedBinary");
log("cracking output: " + crack.Result);
using (var file = File.OpenWrite(TempDownloadBinary))
{
log("Downloading cracked binary..");
PercentStatus("Downloading cracked binary", 80);
try
{
sftp.DownloadFile("/tmp/crackedBinary", file);
}
catch (SftpPathNotFoundException e)
{
log("Could not find file, help!!!!!");
return;
}
}
PercentStatus("Repacking IPA", 90);
String repack = AppHelper.repack(ipaInfo, TempDownloadBinary);
PercentStatus("Done!", 100);
log("Cracking completed, file at " + repack);
}
public CrackProcess()
{
xml = Brake.Container.getContainer();
InitializeComponent();
queue = new Queue<IPAInfo>();
connectionInfo = new PasswordConnectionInfo(xml.Config.host, xml.Config.port, "root", xml.Config.Password);
}
public void queueIPA(IPAInfo ipaInfo)
{
queue.Enqueue(ipaInfo);
}
public void runQueue()
{
SetAppStatusText("Establishing SSH connection...");
connectSSH();
prepareIPAs();
SetAppStatusBar(0);
int i = 1;
foreach (IPAInfo ipa in queue) {
SetAppStatusText("Cracking app: " + ipa.AppName + " (" + i + "/" + queue.Count + ")");
crackIPA(ipa);
SetAppStatusBar((i* 100) / queue.Count);
i++;
}
try
{
sftp.DeleteDirectory(DeviceTempDir);
}
catch (Exception e)
{
}
ssh.Disconnect();
sftp.Disconnect();
AppHelper.DeleteDirectory(AppHelper.GetWorkingDirectory());
MessageBox.Show("Cracking complete!");
CloseWindow();
}
private void connectSSH()
{
sftp = new SftpClient(connectionInfo);
sftp.Connect();
ssh = new SshClient(connectionInfo);
ssh.Connect();
}
public void prepareIPAs()
{
DeviceTempDir = "/tmp/Brake-" + RandString();
try
{
sftp.CreateDirectory(DeviceTempDir);
}
catch
{
}
using (ZipFile packagedIPAs = new ZipFile())
{
packagedIPAs.CompressionLevel = Ionic.Zlib.CompressionLevel.Level0;
int i = 1;
foreach (IPAInfo ipaInfo in queue)
{
ipaInfo.IPALocation = AppHelper.extractIPA(ipaInfo);
ipaInfo.IPALocationOnDevice = DeviceTempDir + "/" + Path.GetFileName(ipaInfo.IPALocation);
Console.WriteLine("ipalocation on device " + ipaInfo.IPALocationOnDevice);
packagedIPAs.AddFile(ipaInfo.IPALocation, String.Empty);
Console.WriteLine("Added ipa into zip: " + ipaInfo.IPALocation);
SetAppStatusText("Preparing IPAs: " + ipaInfo.AppName + " (" + i + "/" + queue.Count + ")");
SetAppStatusBar((i * 100) / queue.Count);
i++;
}
packagedIPAs.Save("Brake-Upload.zip");
}
SetAppStatusText("Uploading IPAs..");
try
{
log("device temp dir: " + DeviceTempDir);
using (var file = File.OpenRead("Brake-Upload.zip"))
{
sftp.UploadFile(file, DeviceTempDir + "/Brake-Upload.zip");
}
ssh.RunCommand("cd '" + DeviceTempDir + "'; unzip Brake-Upload.zip");
Console.WriteLine("ssh command: cd '" + DeviceTempDir + "'; unzip Brake-Upload.zip");
}
catch (Exception e)
{
Console.WriteLine("exception occured " + e.ToString());
}
}
delegate void SetAppStatusCallback(string text);
delegate void CloseWindowCallback();
delegate void SetAppStatusBarCallback(int value);
public static String RandString()
{
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var result = new string(
Enumerable.Repeat(chars, 8)
.Select(s => s[random.Next(s.Length)])
.ToArray());
return result;
}
private void SetAppStatusBar(int value)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.appProgressBar.InvokeRequired)
{
SetAppStatusBarCallback d = new SetAppStatusBarCallback(SetAppStatusBar);
this.Invoke(d, new object[] { value });
}
else
{
this.appProgressBar.Value = value;
Console.WriteLine("progress bar value " + value);
}
}
private void CloseWindow()
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.appProgressBar.InvokeRequired)
{
CloseWindowCallback d = new CloseWindowCallback(CloseWindow);
this.Invoke(d);
}
else
{
this.Close();
}
}
private void SetAppStatusText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.appStatusLabel.InvokeRequired)
{
SetAppStatusCallback d = new SetAppStatusCallback(SetAppStatusText);
this.Invoke(d, new object[] { text });
}
else
{
this.appStatusLabel.Text = text;
}
}
}
}