-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyauth.js
More file actions
848 lines (713 loc) · 20.1 KB
/
keyauth.js
File metadata and controls
848 lines (713 loc) · 20.1 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
import { Buffer } from "buffer"
import { createInterface } from "readline"
import { execSync, exec } from "child_process"
import { verifyKey } from "discord-interactions"
import os from "os"
import fs from "fs"
import crypto from "crypto"
import * as QRCode from "qrcode"
const readline = createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
export default class KeyAuth {
url = "https://keyauth.win/api/1.3/"
public_key =
"5586b4bc69c7a4b487e4563a4cd96afd39140f919bd31cea7d1c6a1e8439422b"
loggingEnabled = true
initialized = false
user_data = null
app_data = null
constructor(options) {
this.name = options.name
this.ownerid = options.ownerid
this.version = options.version
this.hash_to_check = options.hash_to_check
this.url = options.url ?? "https://keyauth.win/api/1.3/"
this.path = options.path
if (!this.name || !this.ownerid || !this.version) {
throw new Error("Name, ownerid, and version are required")
}
}
async init() {
if (this.sessionid && this.initialized) {
console.log("Application already initialized")
return
}
let token = ""
if (this.path) {
try {
token = fs.readFileSync(this.path, "utf-8").trim()
} catch (error) {
console.error(`Failed to read file at path ${this.path}:`, error)
this.sleep(5000)
process.exit(0)
}
}
const post_data = {
type: "init",
name: this.name,
ownerid: this.ownerid,
version: this.version,
hash: this.hash_to_check,
...(this.path && {
token: token,
thash: crypto
.createHash("sha256")
.update(token)
.digest("hex")
})
}
const response = await this.__do_request(post_data)
if (response === "KeyAuth_Invalid") {
console.log("This application does not exist")
this.sleep(5000)
process.exit(0)
}
if (response["message"] === "invalidver") {
if (response["download"]) {
console.log("Your application is outdated.")
exec(`start ${response["download"]}`, error => {
if (error) {
console.error("Failed to open the download link:", error)
}
})
this.sleep(5000)
process.exit(0)
} else {
console.log(
"Your application is outdated and no download link was provided, contact the owner for the latest app version."
)
this.sleep(5000)
process.exit(0)
}
}
if (response["success"] === false) {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
this.sessionid = response["sessionid"]
this.initialized = true
}
async register(username, password, license, hwid) {
this.checkinit()
if (!hwid) hwid = this.get_hwid()
const post_data = {
type: "register",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
username: username,
pass: password,
key: license,
hwid: hwid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
console.log(response["message"])
this.__load_user_data(response["info"])
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async upgrade(username, license) {
this.checkinit()
const post_data = {
type: "upgrade",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
username: username,
key: license
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
console.log(response["message"])
console.log("Restart the application to apply the changes.")
this.sleep(5000)
process.exit(0)
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async login(username, password, code, hwid) {
this.checkinit()
if (!hwid) hwid = this.get_hwid()
const post_data = {
type: "login",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
username: username,
pass: password,
hwid: hwid,
...(code && { code: code })
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
console.log(response["message"])
this.__load_user_data(response["info"])
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async license(license, code, hwid) {
this.checkinit()
if (!hwid) hwid = this.get_hwid()
const post_data = {
type: "license",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
key: license,
hwid: hwid,
...(code && { code: code })
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
console.log(response["message"])
this.__load_user_data(response["info"])
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async var(name) {
this.checkinit()
const post_data = {
type: "var",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
varid: name
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return response["message"]
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async getvar(name) {
this.checkinit()
const post_data = {
type: "getvar",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
var: name
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return response["message"]
} else {
console.log(
'NOTE: This is commonly misunderstood. This is for user variables, not the normal variables.\nUse KeyAuthApp.var("{var_name}") for normal variables'
)
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async setvar(name, value) {
this.checkinit()
const post_data = {
type: "setvar",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
varid: name,
data: value
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return true
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async ban() {
this.checkinit()
const post_data = {
type: "ban",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return true
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async file(id) {
this.checkinit()
const post_data = {
type: "file",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
fileid: id
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return Buffer.from(response["contents"], "hex")
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async webhook(id, param, body, conttype) {
this.checkinit()
const post_data = {
type: "webhook",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
webid: id,
params: param,
body: body,
conttype: conttype
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return response["message"]
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async check() {
this.checkinit()
const post_data = {
type: "check",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return true
} else {
return false
}
}
async checkblacklist() {
this.checkinit()
const hwid = this.get_hwid()
const post_data = {
type: "checkblacklist",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
hwid: hwid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return true
} else {
return false
}
}
async log(message) {
this.checkinit()
const post_data = {
type: "log",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
message: message,
pcuser: os.userInfo().username
}
await this.__do_request(post_data)
}
async fetchOnline() {
this.checkinit()
const post_data = {
type: "fetchOnline",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
if (Number(response["users"]) === 0) {
return null
} else {
return Number(response["users"])
}
} else {
return false
}
}
async fetchStats() {
this.checkinit()
const post_data = {
type: "fetchStats",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
this.__load_app_data(response["appinfo"])
}
}
async chatGet(channel) {
this.checkinit()
const post_data = {
type: "chatget",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
channel: channel
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return response["messages"]
} else {
return false
}
}
async chatSend(message, channel) {
this.checkinit()
const post_data = {
type: "chatsend",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
channel: channel,
message: message
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
return true
} else {
return false
}
}
async changeUsername(username) {
this.checkinit()
const post_data = {
type: "changeUsername",
newUsername: username,
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
console.log("Username changed successfully")
} else {
return false
}
}
async logout() {
this.checkinit()
const post_data = {
type: "logout",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
console.log("Logged out successfully")
this.sleep(5000)
process.exit(0)
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async enable2fa(code) {
this.checkinit()
const post_data = {
type: "2faenable",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
...(code && { code: code })
}
const response = await this.__do_request(post_data)
if (response["success"] === true) {
if (!code) {
console.log(response)
console.log(
"Your 2fa secret code is: " + response["2fa"]["secret_code"]
)
this.displayQrCode(response["2fa"]["QRCode"])
console.log(
"Please enter the code you received in your authenticator app."
)
const userCode = await new Promise(resolve => {
readline.question("Enter the code: ", input => {
resolve(input)
})
})
await this.enable2fa(userCode) // Recursively call with the entered code
} else {
console.log("2FA enabled successfully")
}
} else {
console.log(response["message"])
this.sleep(5000)
process.exit(0)
}
}
async disable2fa() {
this.checkinit()
const code = await new Promise(resolve => {
readline.question("Enter the code: ", input => {
resolve(input)
})
})
const post_data = {
type: "2fadisable",
name: this.name,
ownerid: this.ownerid,
sessionid: this.sessionid,
code: code
}
const response = await this.__do_request(post_data)
console.log(response["message"])
await this.sleep(5000)
}
async checkinit() {
if (!this.sessionid && !this.initialized) {
console.log("Application not initialized")
this.sleep(5000)
process.exit(0)
}
}
get_hwid() {
const platform = os.platform()
if (platform === "linux") {
try {
const hwid = fs.readFileSync("/etc/machine-id", "utf-8").trim()
return hwid
} catch (error) {
console.error("Error reading /etc/machine-id:", error)
throw new Error("Failed to retrieve HWID on Linux")
}
} else if (platform === "win32") {
try {
const winUser = os.userInfo().username
const sidOutput = execSync(
`wmic useraccount where name='${winUser}' get sid`
)
.toString()
.split("\n")
const sid = sidOutput[1]?.trim()
if (!sid) {
throw new Error("Failed to retrieve SID on Windows: SID is undefined")
}
return sid
} catch (error) {
console.error("Error retrieving SID on Windows:", error)
throw new Error("Failed to retrieve HWID on Windows")
}
} else if (platform === "darwin") {
try {
const output = execSync(
"ioreg -l | grep IOPlatformSerialNumber"
).toString()
const parts = output.split("=")
const serial = parts[1]?.trim().replace(/"/g, "") ?? ""
return serial
} catch (error) {
console.error("Error retrieving serial number on macOS:", error)
throw new Error("Failed to retrieve HWID on macOS")
}
} else {
throw new Error("Unsupported platform for HWID retrieval")
}
}
async displayQrCode(qrCodeUrl) {
try {
const qrCode = await QRCode.toDataURL(qrCodeUrl)
const base64Data = qrCode.split(",")[1]
if (!base64Data) {
throw new Error("Invalid QR code data")
}
const img = Buffer.from(base64Data, "base64")
const outputPath = this.path ?? "qrcode.png"
fs.writeFileSync(outputPath, img)
const platform = os.platform()
let openCommand
if (platform === "win32") {
openCommand = `start ${outputPath}`
} else if (platform === "darwin") {
openCommand = `open ${outputPath}`
} else if (platform === "linux") {
openCommand = `xdg-open ${outputPath}`
} else {
console.error("Unsupported platform for opening the QR code image")
return
}
exec(openCommand, error => {
if (error) {
console.error("Failed to display the QR code image:", error)
}
})
} catch (error) {
console.error("Failed to generate QR code:", error)
}
}
getCheckSum() {
const md5Hash = crypto.createHash("md5")
const file = fs.readFileSync(process.argv.slice(2).join(""), {
encoding: "binary"
})
md5Hash.update(file)
const digest = md5Hash.digest("hex")
return digest
}
// Private functions (cannot be called from outside the class)
async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async __do_request(data) {
try {
const response = await fetch(this.url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
// @ts-ignore - Why does it error? No fucking clue but it works
body: new URLSearchParams(data).toString()
})
if (!response.ok) {
console.error(`HTTP error! Status: ${response.status}`)
this.sleep(5000)
process.exit(0)
}
const responseData = await response.json()
const dontRunExtra = ["log", "file", "2faenable", "2fadisable"]
if (dontRunExtra.includes(data.type)) {
return responseData
}
const signature = response.headers.get("x-signature-ed25519")
const timestamp = response.headers.get("x-signature-timestamp")
if (!signature || !timestamp) {
console.log("Missing signature or timestamp in response headers")
this.sleep(5000)
process.exit(0)
}
const server_time = new Date(Number(timestamp) * 1000).toUTCString()
const current_time = new Date().toUTCString()
const buffer_seconds = 5 // Allowable time difference in seconds
const time_difference =
Math.abs(
new Date(server_time).getTime() - new Date(current_time).getTime()
) / 1000
if (time_difference > buffer_seconds + 20) {
console.log(
`Time difference is too large: ${time_difference} seconds, try syncing your date and time settings.`
)
this.sleep(5000)
process.exit(0)
}
if (
!verifyKey(
Buffer.from(JSON.stringify(responseData), "utf-8"),
signature,
timestamp,
this.public_key
)
) {
console.log(
"Signature checksum failed. Request was tampered with or session ended most likely."
)
await this.sleep(3000)
process.exit(0)
}
this.logEvent(JSON.stringify(responseData) + "\n")
return responseData
} catch (error) {
console.error("Unexpected error:", error)
this.sleep(5000)
process.exit(0)
}
}
logEvent(message) {
console.log(message)
if (!this.loggingEnabled) return
const exeName =
process.argv[1].split("\\").pop() || process.argv[1].split("/").pop()
const logDirectory = `${os.homedir()}/AppData/Roaming/KeyAuth/debug/${exeName}`
try {
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory, { recursive: true })
}
const logFileName = `${new Date()
.toLocaleDateString("en-US", {
month: "short",
day: "2-digit",
year: "numeric"
})
.replace(/ /g, "_")}_logs.txt`
const logFilePath = `${logDirectory}/${logFileName}`
// Redact sensitive fields
message = this.redactField(message, "sessionid")
message = this.redactField(message, "ownerid")
message = this.redactField(message, "app")
message = this.redactField(message, "version")
message = this.redactField(message, "fileid")
message = this.redactField(message, "webhooks")
message = this.redactField(message, "nonce")
const logMessage = `[${new Date().toISOString()}] [${exeName}] ${message}\n`
fs.appendFileSync(logFilePath, logMessage, "utf-8")
} catch (error) {
if (error instanceof Error) {
console.error(`Error logging data: ${error.message}`)
} else {
console.error("Error logging data: Unknown error")
}
}
}
redactField(content, field) {
const regex = new RegExp(`"${field}":\\s*".*?"`, "g")
return content.replace(regex, `"${field}": "[REDACTED]"`)
}
__load_app_data(data) {
this.app_data = {
numUsers: data["numUsers"],
numKeys: data["numKeys"],
app_ver: data["version"],
customer_panel: data["customerPanelLink"],
onlineUsers: data["numOnlineUsers"]
}
}
__load_user_data(data) {
this.user_data = {
username: data["username"],
ip: data["ip"],
hwid: data["hwid"] || "N/A",
expires: data["subscriptions"][0]["expiry"],
createdate: data["createdate"],
lastlogin: data["lastlogin"],
subscription: data["subscriptions"][0]["subscription"],
subscriptions: data["subscriptions"]
}
}
}