1+ import os
2+
3+ import json # json
4+
5+ import binascii # hex encoding
6+
7+ import requests # https requests
8+
9+ from uuid import uuid4 # gen random guid
10+
11+ from Crypto .Cipher import AES
12+ from Crypto .Hash import SHA256
13+ from Crypto .Util .Padding import pad , unpad
14+ # aes + padding, sha256
15+
16+ import webbrowser , platform , subprocess , datetime
17+
18+ from requests_toolbelt .adapters .fingerprint import FingerprintAdapter
19+
20+ class api :
21+ name = ownerid = secret = ""
22+
23+ def __init__ (self , name , ownerid , secret ):
24+ self .name = name
25+
26+ self .ownerid = ownerid
27+
28+ self .secret = secret
29+
30+ session_id = session_iv = ""
31+
32+ def init (self ):
33+ self .session_iv = str (uuid4 ())[:8 ]
34+
35+ init_iv = SHA256 .new (self .session_iv .encode ()).hexdigest ()
36+
37+ post_data = {
38+ "type" : binascii .hexlify (("init" ).encode ()),
39+ "name" : binascii .hexlify (self .name .encode ()),
40+ "ownerid" : binascii .hexlify (self .ownerid .encode ()),
41+ "init_iv" : init_iv
42+ }
43+
44+ response = self .__do_request (post_data )
45+
46+ response = encryption .decrypt (response , self .secret , init_iv )
47+
48+ if response == "KeyAuth_Disabled" :
49+ print ("The program key you tried to use doesn't exist" )
50+ sys .exit ()
51+ if response == "KeyAuth_Initialized" :
52+ print ("Initialized" )
53+ else :
54+ print ("The program key you tried to use doesn't exist" )
55+ sys .exit ()
56+
57+
58+ def login (self , key , hwid = None ):
59+ if hwid is None : hwid = others .get_hwid ()
60+
61+ self .session_iv = str (uuid4 ())[:8 ]
62+
63+ init_iv = SHA256 .new (self .session_iv .encode ()).hexdigest ()
64+
65+ post_data = {
66+ "type" : binascii .hexlify (("login" ).encode ()),
67+ "key" : encryption .encrypt (key , self .secret , init_iv ),
68+ "hwid" : encryption .encrypt (hwid , self .secret , init_iv ),
69+ "name" : binascii .hexlify (self .name .encode ()),
70+ "ownerid" : binascii .hexlify (self .ownerid .encode ()),
71+ "init_iv" : init_iv
72+ }
73+
74+ response = self .__do_request (post_data )
75+
76+ response = encryption .decrypt (response , self .secret , init_iv )
77+
78+ if response == "KeyAuth_Valid" :
79+ print ("Logged in" )
80+ if response == "KeyAuth_Invalid" :
81+ print ("Key not found" )
82+ if os .path .exists ("C:\\ ProgramData\\ keysave.txt" ):
83+ os .remove ("C:\\ ProgramData\\ keysave.txt" )
84+ sys .exit ()
85+ if response == "KeyAuth_InvalidHWID" :
86+ print ("This computer doesn't match the computer the key is locked to. If you reset your computer, contact the application owner" )
87+ if os .path .exists ("C:\\ ProgramData\\ keysave.txt" ):
88+ os .remove ("C:\\ ProgramData\\ keysave.txt" )
89+ sys .exit ()
90+ if response == "KeyAuth_Expired" :
91+ print ("This key is expired" )
92+ if os .path .exists ("C:\\ ProgramData\\ keysave.txt" ):
93+ os .remove ("C:\\ ProgramData\\ keysave.txt" )
94+ sys .exit ()
95+ else :
96+ print ("Application Failed To Connect. Try again or contact application owner" )
97+ if os .path .exists ("C:\\ ProgramData\\ keysave.txt" ):
98+ os .remove ("C:\\ ProgramData\\ keysave.txt" )
99+ sys .exit ()
100+
101+
102+ def __do_request (self , post_data ):
103+ headers = {"User-Agent" : "KeyAuth" }
104+
105+ rq = requests .Session ()
106+
107+ rq_out = rq .post (
108+ "https://keyauth.com/api/" , data = post_data , headers = headers , verify = False
109+ )
110+
111+ return rq_out .text
112+
113+
114+ class others :
115+ @staticmethod
116+ def get_hwid ():
117+ if platform .system () != "Windows" :
118+ return "None"
119+
120+ cmd = subprocess .Popen ("wmic useraccount where name='%username%' get sid" , stdout = subprocess .PIPE , shell = True )
121+
122+ (suppost_sid , error ) = cmd .communicate ()
123+
124+ suppost_sid = suppost_sid .split (b'\n ' )[1 ].strip ()
125+
126+ return suppost_sid .decode ()
127+
128+
129+ class encryption :
130+ @staticmethod
131+ def encrypt_string (plain_text , key , iv ):
132+ plain_text = pad (plain_text , 16 )
133+
134+ aes_instance = AES .new (key , AES .MODE_CBC , iv )
135+
136+ raw_out = aes_instance .encrypt (plain_text )
137+
138+ return binascii .hexlify (raw_out )
139+
140+ @staticmethod
141+ def decrypt_string (cipher_text , key , iv ):
142+ cipher_text = binascii .unhexlify (cipher_text )
143+
144+ aes_instance = AES .new (key , AES .MODE_CBC , iv )
145+
146+ cipher_text = aes_instance .decrypt (cipher_text )
147+
148+ return unpad (cipher_text , 16 )
149+
150+ @staticmethod
151+ def encrypt (message , enc_key , iv ):
152+ try :
153+ _key = SHA256 .new (enc_key .encode ()).hexdigest ()[:32 ]
154+
155+ _iv = SHA256 .new (iv .encode ()).hexdigest ()[:16 ]
156+
157+ return encryption .encrypt_string (message .encode (), _key .encode (), _iv .encode ()).decode ()
158+ except :
159+ print ("Invalid App Secret" )
160+ sys .exit ()
161+
162+ @staticmethod
163+ def decrypt (message , enc_key , iv ):
164+ try :
165+ _key = SHA256 .new (enc_key .encode ()).hexdigest ()[:32 ]
166+
167+ _iv = SHA256 .new (iv .encode ()).hexdigest ()[:16 ]
168+
169+ return encryption .decrypt_string (message .encode (), _key .encode (), _iv .encode ()).decode ()
170+ except :
171+ print ("Invalid App Secret" )
172+ sys .exit ()
0 commit comments