· 4 years ago · May 20, 2021, 09:10 PM
1using System;
2using System.Security.Cryptography;
3using System.Security.Cryptography.X509Certificates;
4using System.Collections.Specialized;
5using System.Text;
6using System.Net;
7using System.IO;
8using System.Net.Security;
9using System.Runtime.InteropServices;
10using System.Runtime.Serialization;
11using System.Runtime.Serialization.Json;
12using System.Diagnostics;
13using System.Security.Principal;
14using System.Threading;
15using System.Windows.Forms;
16
17namespace KeyAuth
18{
19 public class api
20 {
21 public string name, ownerid, secret, version;
22 public api(string name, string ownerid, string secret, string version)
23 {
24
25 if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(ownerid) || string.IsNullOrWhiteSpace(secret) || string.IsNullOrWhiteSpace(version))
26 {
27 MessageBox.Show("Application not setup correctly. Please watch video link found in Login.cs");
28 Environment.Exit(0);
29 }
30
31 this.name = name;
32
33 this.ownerid = ownerid;
34
35 this.secret = secret;
36
37 this.version = version;
38 }
39
40 #region structures
41 [DataContract]
42 private class response_structure
43 {
44 [DataMember]
45 public bool success { get; set; }
46
47 [DataMember]
48 public string response { get; set; }
49
50 [DataMember]
51 public string message { get; set; }
52
53 [DataMember]
54 public string download { get; set; }
55
56 [DataMember(IsRequired = false, EmitDefaultValue = false)]
57 public user_data_structure info { get; set; }
58 }
59
60 [DataContract]
61 private class user_data_structure
62 {
63 [DataMember]
64 public string key { get; set; }
65
66 [DataMember]
67 public string expiry { get; set; } //timestamp
68
69 [DataMember]
70 public int level { get; set; }
71
72 [DataMember]
73 public string note { get; set; }
74 }
75 #endregion
76
77 public void init()
78 {
79 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
80
81 var values_to_upload = new NameValueCollection
82 {
83 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("init")),
84 ["ver"] = encryption.encrypt(version, secret, init_iv),
85 ["hash"] = checksum(Process.GetCurrentProcess().MainModule.FileName),
86 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
87 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
88 ["init_iv"] = init_iv
89 };
90
91 var response = req(values_to_upload);
92
93 response = encryption.decrypt(response, secret, init_iv);
94 var json = response_decoder.string_to_generic<response_structure>(response);
95
96 if (json.success)
97 {
98 // optional success message
99 }
100 else if (json.message == "invalidver")
101 {
102 Process.Start(json.download);
103 Environment.Exit(0);
104 }
105 else
106 {
107 MessageBox.Show(json.message);
108 Environment.Exit(0);
109 }
110
111 }
112
113 public void register(string username, string pass, string key)
114 {
115 string hwid = WindowsIdentity.GetCurrent().User.Value;
116
117 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
118
119 var values_to_upload = new NameValueCollection
120 {
121 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("register")),
122 ["username"] = encryption.encrypt(username, secret, init_iv),
123 ["pass"] = encryption.encrypt(pass, secret, init_iv),
124 ["key"] = encryption.encrypt(key, secret, init_iv),
125 ["hwid"] = encryption.encrypt(hwid, secret, init_iv),
126 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
127 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
128 ["init_iv"] = init_iv
129 };
130
131 var response = req(values_to_upload);
132
133 response = encryption.decrypt(response, secret, init_iv);
134 var json = response_decoder.string_to_generic<response_structure>(response);
135
136 if (!json.success)
137 {
138 MessageBox.Show(json.message);
139 }
140 else
141 {
142 // optional success msg
143 }
144 }
145
146 public void login(string username, string pass)
147 {
148 string hwid = WindowsIdentity.GetCurrent().User.Value;
149
150 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
151
152 var values_to_upload = new NameValueCollection
153 {
154 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("login")),
155 ["username"] = encryption.encrypt(username, secret, init_iv),
156 ["pass"] = encryption.encrypt(pass, secret, init_iv),
157 ["hwid"] = encryption.encrypt(hwid, secret, init_iv),
158 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
159 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
160 ["init_iv"] = init_iv
161 };
162
163 var response = req(values_to_upload);
164
165 response = encryption.decrypt(response, secret, init_iv);
166 var json = response_decoder.string_to_generic<response_structure>(response);
167
168 if (!json.success)
169 {
170 MessageBox.Show(json.message);
171 }
172 else
173 {
174 // optional success msg
175 }
176 }
177
178 public bool license(string key)
179 {
180 string hwid = WindowsIdentity.GetCurrent().User.Value;
181
182 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
183
184 var values_to_upload = new NameValueCollection
185 {
186 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("license")),
187 ["key"] = encryption.encrypt(key, secret, init_iv),
188 ["hwid"] = encryption.encrypt(hwid, secret, init_iv),
189 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
190 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
191 ["init_iv"] = init_iv
192 };
193
194 var response = req(values_to_upload);
195
196 response = encryption.decrypt(response, secret, init_iv);
197 var json = response_decoder.string_to_generic<response_structure>(response);
198
199 if (!json.success)
200 {
201 MessageBox.Show(json.message);
202 return false;
203 }
204 else
205 {
206 load_user_data(json.info);
207 File.WriteAllText(@"C:\ProgramData\" + name, key);
208 return true;
209 }
210 }
211
212 public string var(string varid)
213 {
214 string hwid = WindowsIdentity.GetCurrent().User.Value;
215
216 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
217
218 var values_to_upload = new NameValueCollection
219 {
220 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("var")),
221 ["key"] = encryption.encrypt(user_data.key, secret, init_iv),
222 ["varid"] = encryption.encrypt(varid, secret, init_iv),
223 ["hwid"] = encryption.encrypt(hwid, secret, init_iv),
224 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
225 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
226 ["init_iv"] = init_iv
227 };
228
229 var response = req(values_to_upload);
230
231 response = encryption.decrypt(response, secret, init_iv);
232 var json = response_decoder.string_to_generic<response_structure>(response);
233
234 if (!json.success)
235 {
236 MessageBox.Show(json.message);
237 return "";
238 }
239 else
240 {
241 return json.message;
242 }
243 }
244
245 public void webhook(string webid, string param)
246 {
247 string hwid = WindowsIdentity.GetCurrent().User.Value;
248
249 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
250
251 var values_to_upload = new NameValueCollection
252 {
253 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("webhook")),
254 ["key"] = encryption.encrypt(user_data.key, secret, init_iv),
255 ["webid"] = encryption.encrypt(webid, secret, init_iv),
256 ["params"] = encryption.encrypt(param, secret, init_iv),
257 ["hwid"] = encryption.encrypt(hwid, secret, init_iv),
258 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
259 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
260 ["init_iv"] = init_iv
261 };
262
263 var response = req(values_to_upload);
264
265 response = encryption.decrypt(response, secret, init_iv);
266 var json = response_decoder.string_to_generic<response_structure>(response);
267
268 if (!json.success)
269 {
270 MessageBox.Show(json.message);
271 }
272 else
273 {
274 // optional success message
275 }
276 }
277
278 public void download(string fileid, string path)
279 {
280 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
281
282 var values_to_upload = new NameValueCollection
283 {
284 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("file")),
285 ["fileid"] = encryption.encrypt(fileid, secret, init_iv),
286 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
287 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
288 ["init_iv"] = init_iv
289 };
290
291 var response = dl(values_to_upload, path);
292 }
293
294 public void log(string message)
295 {
296 var init_iv = encryption.sha256(encryption.iv_key()); // can be changed to whatever you want
297 var values_to_upload = new NameValueCollection
298 {
299 ["type"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes("log")),
300 ["key"] = encryption.encrypt(user_data.key, secret, init_iv),
301 ["message"] = encryption.encrypt(message, secret, init_iv),
302 ["name"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(name)),
303 ["ownerid"] = encryption.byte_arr_to_str(Encoding.Default.GetBytes(ownerid)),
304 ["init_iv"] = init_iv
305 };
306
307 req(values_to_upload);
308 }
309
310 public static string checksum(string filename)
311 {
312 string result;
313 using (MD5 md = MD5.Create())
314 {
315 using (FileStream fileStream = File.OpenRead(filename))
316 {
317 byte[] value = md.ComputeHash(fileStream);
318 result = BitConverter.ToString(value).Replace("-", "").ToLowerInvariant();
319 }
320 }
321 return result;
322 }
323
324 private static string req(NameValueCollection post_data)
325 {
326 try
327 {
328 using (WebClient client = new WebClient())
329 {
330 client.Headers["User-Agent"] = "KeyAuth";
331
332 ServicePointManager.ServerCertificateValidationCallback = others.pin_public_key;
333
334 var raw_response = client.UploadValues("https://keyauth.com/api/v5/", post_data);
335
336 ServicePointManager.ServerCertificateValidationCallback += (send, certificate, chain, sslPolicyErrors) => { return true; };
337
338 return Encoding.Default.GetString(raw_response);
339 }
340 }
341 catch
342 {
343
344 MessageBox.Show("SSL Pin Error. Please try again with apps that modify network activity closed/disabled.");
345 Environment.Exit(0);
346 return "lmao";
347 }
348 }
349
350 private static string dl(NameValueCollection post_data, string path)
351 {
352 try
353 {
354 using (WebClient client = new WebClient())
355 {
356 client.Headers["User-Agent"] = "KeyAuth";
357
358 ServicePointManager.ServerCertificateValidationCallback = others.pin_public_key;
359
360 byte[] result = client.UploadValues("https://keyauth.com/api/v3/", post_data);
361
362 ServicePointManager.ServerCertificateValidationCallback += (send, certificate, chain, sslPolicyErrors) => { return true; };
363
364 File.WriteAllBytes(path, result);
365 return "";
366 }
367 }
368 catch
369 {
370
371 MessageBox.Show("SSL Pin Error. Please try again with apps that modify network activity closed/disabled.");
372 Environment.Exit(0);
373 return "lmao";
374 }
375 }
376
377
378 #region user_data
379 public user_data_class user_data = new user_data_class();
380
381 public class user_data_class
382 {
383 public string key { get; set; }
384 public DateTime expiry { get; set; }
385 public int level { get; set; }
386 public string note { get; set; }
387 }
388 private void load_user_data(user_data_structure data)
389 {
390 user_data.key = data.key;
391
392 user_data.expiry = others.unix_to_date(Convert.ToDouble(data.expiry));
393
394 user_data.level = data.level;
395
396 user_data.note = data.note;
397 }
398 #endregion
399
400 private json_wrapper response_decoder = new json_wrapper(new response_structure());
401 }
402
403 public static class others
404 {
405 public static DateTime unix_to_date(double unixTimeStamp) =>
406 new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc).AddSeconds(unixTimeStamp).ToLocalTime();
407
408 public static bool pin_public_key(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
409 certificate.GetPublicKeyString() == "0480126A944139DFDCF7808EF35430F592F6C1BDDEF3AB693563B3521FFBBA907E0A44F99FF43B8A1D68CA89778AA06BEA97A72EFF4C1BBAB49F9B84F154D57944";
410 }
411
412 public static class encryption
413 {
414 public static string byte_arr_to_str(byte[] ba)
415 {
416 StringBuilder hex = new StringBuilder(ba.Length * 2);
417 foreach (byte b in ba)
418 hex.AppendFormat("{0:x2}", b);
419 return hex.ToString();
420 }
421
422 public static byte[] str_to_byte_arr(string hex)
423 {
424 int NumberChars = hex.Length;
425 byte[] bytes = new byte[NumberChars / 2];
426 for (int i = 0; i < NumberChars; i += 2)
427 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
428 return bytes;
429 }
430
431 public static string encrypt_string(string plain_text, byte[] key, byte[] iv)
432 {
433 Aes encryptor = Aes.Create();
434
435 encryptor.Mode = CipherMode.CBC;
436 encryptor.Key = key;
437 encryptor.IV = iv;
438
439 using (MemoryStream mem_stream = new MemoryStream())
440 {
441 using (ICryptoTransform aes_encryptor = encryptor.CreateEncryptor())
442 {
443 using (CryptoStream crypt_stream = new CryptoStream(mem_stream, aes_encryptor, CryptoStreamMode.Write))
444 {
445 byte[] p_bytes = Encoding.Default.GetBytes(plain_text);
446
447 crypt_stream.Write(p_bytes, 0, p_bytes.Length);
448
449 crypt_stream.FlushFinalBlock();
450
451 byte[] c_bytes = mem_stream.ToArray();
452
453 return byte_arr_to_str(c_bytes);
454 }
455 }
456 }
457 }
458
459 public static string decrypt_string(string cipher_text, byte[] key, byte[] iv)
460 {
461 Aes encryptor = Aes.Create();
462
463 encryptor.Mode = CipherMode.CBC;
464 encryptor.Key = key;
465 encryptor.IV = iv;
466
467 using (MemoryStream mem_stream = new MemoryStream())
468 {
469 using (ICryptoTransform aes_decryptor = encryptor.CreateDecryptor())
470 {
471 using (CryptoStream crypt_stream = new CryptoStream(mem_stream, aes_decryptor, CryptoStreamMode.Write))
472 {
473 byte[] c_bytes = str_to_byte_arr(cipher_text);
474
475 crypt_stream.Write(c_bytes, 0, c_bytes.Length);
476
477 crypt_stream.FlushFinalBlock();
478
479 byte[] p_bytes = mem_stream.ToArray();
480
481 return Encoding.Default.GetString(p_bytes, 0, p_bytes.Length);
482 }
483 }
484 }
485 }
486
487 public static string iv_key() =>
488 Guid.NewGuid().ToString().Substring(0, Guid.NewGuid().ToString().IndexOf("-", StringComparison.Ordinal));
489
490 public static string sha256(string r) =>
491 byte_arr_to_str(new SHA256Managed().ComputeHash(Encoding.Default.GetBytes(r)));
492
493 public static string encrypt(string message, string enc_key, string iv)
494 {
495 byte[] _key = Encoding.Default.GetBytes(sha256(enc_key).Substring(0, 32));
496
497 byte[] _iv = Encoding.Default.GetBytes(sha256(iv).Substring(0, 16));
498
499 return encrypt_string(message, _key, _iv);
500 }
501
502 public static string decrypt(string message, string enc_key, string iv)
503 {
504 byte[] _key = Encoding.Default.GetBytes(sha256(enc_key).Substring(0, 32));
505
506 byte[] _iv = Encoding.Default.GetBytes(sha256(iv).Substring(0, 16));
507
508 return decrypt_string(message, _key, _iv);
509 }
510
511 public static DateTime unix_to_date(double unixTimeStamp) =>
512 new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc).AddSeconds(unixTimeStamp).ToLocalTime();
513
514 public static bool pin_public_key(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
515 certificate.GetPublicKeyString() == "0480126A944139DFDCF7808EF35430F592F6C1BDDEF3AB693563B3521FFBBA907E0A44F99FF43B8A1D68CA89778AA06BEA97A72EFF4C1BBAB49F9B84F154D57944";
516 }
517
518 public class json_wrapper
519 {
520 public static bool is_serializable(Type to_check) =>
521 to_check.IsSerializable || to_check.IsDefined(typeof(DataContractAttribute), true);
522
523 public json_wrapper(object obj_to_work_with)
524 {
525 current_object = obj_to_work_with;
526
527 var object_type = current_object.GetType();
528
529 serializer = new DataContractJsonSerializer(object_type);
530
531 if (!is_serializable(object_type))
532 throw new Exception($"the object {current_object} isn't a serializable");
533 }
534
535 public string to_json_string()
536 {
537 using (var mem_stream = new MemoryStream())
538 {
539 serializer.WriteObject(mem_stream, current_object);
540
541 mem_stream.Position = 0;
542
543 using (var reader = new StreamReader(mem_stream))
544 return reader.ReadToEnd();
545 }
546 }
547
548 public object string_to_object(string json)
549 {
550 var buffer = Encoding.Default.GetBytes(json);
551
552 //SerializationException = session expired
553
554 using (var mem_stream = new MemoryStream(buffer))
555 return serializer.ReadObject(mem_stream);
556 }
557
558 #region extras
559
560 public dynamic string_to_dynamic(string json) =>
561 (dynamic)string_to_object(json);
562
563 public T string_to_generic<T>(string json) =>
564 (T)string_to_object(json);
565
566 public dynamic to_json_dynamic() =>
567 string_to_object(to_json_string());
568
569 #endregion
570
571 private DataContractJsonSerializer serializer;
572
573 private object current_object;
574 }
575}
576