CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
palahsu

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: palahsu/DDoS-Ripper
Path: blob/main/DRipper Free/pytransform/__init__.py
Views: 299
1
# These module alos are used by protection code, so that protection
2
# code needn't import anything
3
import os
4
import platform
5
import sys
6
import struct
7
8
# Because ctypes is new from Python 2.5, so pytransform doesn't work
9
# before Python 2.5
10
#
11
from ctypes import cdll, c_char, c_char_p, c_int, c_void_p, \
12
pythonapi, py_object, PYFUNCTYPE, CFUNCTYPE
13
from fnmatch import fnmatch
14
15
#
16
# Support Platforms
17
#
18
plat_path = 'platforms'
19
20
plat_table = (
21
('windows', ('windows', 'cygwin-*')),
22
('darwin', ('darwin', 'ios')),
23
('linux', ('linux*',)),
24
('freebsd', ('freebsd*', 'openbsd*')),
25
('poky', ('poky',)),
26
)
27
28
arch_table = (
29
('x86', ('i?86', )),
30
('x86_64', ('x64', 'x86_64', 'amd64', 'intel')),
31
('arm', ('armv5',)),
32
('armv6', ('armv6l',)),
33
('armv7', ('armv7l',)),
34
('ppc64', ('ppc64le',)),
35
('mips32', ('mips',)),
36
('aarch32', ('aarch32',)),
37
('aarch64', ('aarch64', 'arm64'))
38
)
39
40
#
41
# Hardware type
42
#
43
HT_HARDDISK, HT_IFMAC, HT_IPV4, HT_IPV6, HT_DOMAIN = range(5)
44
45
#
46
# Global
47
#
48
_pytransform = None
49
50
51
class PytransformError(Exception):
52
pass
53
54
55
def dllmethod(func):
56
def wrap(*args, **kwargs):
57
return func(*args, **kwargs)
58
return wrap
59
60
61
@dllmethod
62
def version_info():
63
prototype = PYFUNCTYPE(py_object)
64
dlfunc = prototype(('version_info', _pytransform))
65
return dlfunc()
66
67
68
@dllmethod
69
def init_pytransform():
70
major, minor = sys.version_info[0:2]
71
# Python2.5 no sys.maxsize but sys.maxint
72
# bitness = 64 if sys.maxsize > 2**32 else 32
73
prototype = PYFUNCTYPE(c_int, c_int, c_int, c_void_p)
74
init_module = prototype(('init_module', _pytransform))
75
ret = init_module(major, minor, pythonapi._handle)
76
if (ret & 0xF000) == 0x1000:
77
raise PytransformError('Initialize python wrapper failed (%d)'
78
% (ret & 0xFFF))
79
return ret
80
81
82
@dllmethod
83
def init_runtime():
84
prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int)
85
_init_runtime = prototype(('init_runtime', _pytransform))
86
return _init_runtime(0, 0, 0, 0)
87
88
89
@dllmethod
90
def encrypt_code_object(pubkey, co, flags, suffix=''):
91
_pytransform.set_option(6, suffix.encode())
92
prototype = PYFUNCTYPE(py_object, py_object, py_object, c_int)
93
dlfunc = prototype(('encrypt_code_object', _pytransform))
94
return dlfunc(pubkey, co, flags)
95
96
97
@dllmethod
98
def generate_license_file(filename, priname, rcode, start=-1, count=1):
99
prototype = PYFUNCTYPE(c_int, c_char_p, c_char_p, c_char_p, c_int, c_int)
100
dlfunc = prototype(('generate_project_license_files', _pytransform))
101
return dlfunc(filename.encode(), priname.encode(), rcode.encode(),
102
start, count) if sys.version_info[0] == 3 \
103
else dlfunc(filename, priname, rcode, start, count)
104
105
106
@dllmethod
107
def generate_license_key(prikey, keysize, rcode):
108
prototype = PYFUNCTYPE(py_object, c_char_p, c_int, c_char_p)
109
dlfunc = prototype(('generate_license_key', _pytransform))
110
return dlfunc(prikey, keysize, rcode) if sys.version_info[0] == 2 \
111
else dlfunc(prikey, keysize, rcode.encode())
112
113
114
@dllmethod
115
def get_registration_code():
116
prototype = PYFUNCTYPE(py_object)
117
dlfunc = prototype(('get_registration_code', _pytransform))
118
return dlfunc()
119
120
121
@dllmethod
122
def get_expired_days():
123
prototype = PYFUNCTYPE(py_object)
124
dlfunc = prototype(('get_expired_days', _pytransform))
125
return dlfunc()
126
127
128
@dllmethod
129
def clean_obj(obj, kind):
130
prototype = PYFUNCTYPE(c_int, py_object, c_int)
131
dlfunc = prototype(('clean_obj', _pytransform))
132
return dlfunc(obj, kind)
133
134
135
def clean_str(*args):
136
tdict = {
137
'str': 0,
138
'bytearray': 1,
139
'unicode': 2
140
}
141
for obj in args:
142
k = tdict.get(type(obj).__name__)
143
if k is None:
144
raise RuntimeError('Can not clean object: %s' % obj)
145
clean_obj(obj, k)
146
147
148
def get_hd_info(hdtype, size=256):
149
if hdtype not in range(HT_DOMAIN + 1):
150
raise RuntimeError('Invalid parameter hdtype: %s' % hdtype)
151
t_buf = c_char * size
152
buf = t_buf()
153
if (_pytransform.get_hd_info(hdtype, buf, size) == -1):
154
raise PytransformError('Get hardware information failed')
155
return buf.value.decode()
156
157
158
def show_hd_info():
159
return _pytransform.show_hd_info()
160
161
162
def assert_armored(*names):
163
prototype = PYFUNCTYPE(py_object, py_object)
164
dlfunc = prototype(('assert_armored', _pytransform))
165
166
def wrapper(func):
167
def wrap_execute(*args, **kwargs):
168
dlfunc(names)
169
return func(*args, **kwargs)
170
return wrap_execute
171
return wrapper
172
173
174
def get_license_info():
175
info = {
176
'ISSUER': None,
177
'EXPIRED': None,
178
'HARDDISK': None,
179
'IFMAC': None,
180
'IFIPV4': None,
181
'DOMAIN': None,
182
'DATA': None,
183
'CODE': None,
184
}
185
rcode = get_registration_code().decode()
186
if rcode.startswith('*VERSION:'):
187
index = rcode.find('\n')
188
info['ISSUER'] = rcode[9:index].split('.')[0].replace('-sn-1.txt', '')
189
rcode = rcode[index+1:]
190
191
index = 0
192
if rcode.startswith('*TIME:'):
193
from time import ctime
194
index = rcode.find('\n')
195
info['EXPIRED'] = ctime(float(rcode[6:index]))
196
index += 1
197
198
if rcode[index:].startswith('*FLAGS:'):
199
index += len('*FLAGS:') + 1
200
info['FLAGS'] = ord(rcode[index - 1])
201
202
prev = None
203
start = index
204
for k in ['HARDDISK', 'IFMAC', 'IFIPV4', 'DOMAIN', 'FIXKEY', 'CODE']:
205
index = rcode.find('*%s:' % k)
206
if index > -1:
207
if prev is not None:
208
info[prev] = rcode[start:index]
209
prev = k
210
start = index + len(k) + 2
211
info['CODE'] = rcode[start:]
212
i = info['CODE'].find(';')
213
if i > 0:
214
info['DATA'] = info['CODE'][i+1:]
215
info['CODE'] = info['CODE'][:i]
216
return info
217
218
219
def get_license_code():
220
return get_license_info()['CODE']
221
222
223
def get_user_data():
224
return get_license_info()['DATA']
225
226
227
def _match_features(patterns, s):
228
for pat in patterns:
229
if fnmatch(s, pat):
230
return True
231
232
233
def _gnu_get_libc_version():
234
try:
235
prototype = CFUNCTYPE(c_char_p)
236
ver = prototype(('gnu_get_libc_version', cdll.LoadLibrary('')))()
237
return ver.decode().split('.')
238
except Exception:
239
pass
240
241
242
def format_platform(platid=None):
243
if platid:
244
return os.path.normpath(platid)
245
246
plat = platform.system().lower()
247
mach = platform.machine().lower()
248
249
for alias, platlist in plat_table:
250
if _match_features(platlist, plat):
251
plat = alias
252
break
253
254
if plat == 'linux':
255
cname, cver = platform.libc_ver()
256
if cname == 'musl':
257
plat = 'musl'
258
elif cname == 'libc':
259
plat = 'android'
260
elif cname == 'glibc':
261
v = _gnu_get_libc_version()
262
if v and len(v) >= 2 and (int(v[0]) * 100 + int(v[1])) < 214:
263
plat = 'centos6'
264
265
for alias, archlist in arch_table:
266
if _match_features(archlist, mach):
267
mach = alias
268
break
269
270
if plat == 'windows' and mach == 'x86_64':
271
bitness = struct.calcsize('P'.encode()) * 8
272
if bitness == 32:
273
mach = 'x86'
274
275
return os.path.join(plat, mach)
276
277
278
# Load _pytransform library
279
def _load_library(path=None, is_runtime=0, platid=None, suffix='', advanced=0):
280
path = os.path.dirname(__file__) if path is None \
281
else os.path.normpath(path)
282
283
plat = platform.system().lower()
284
name = '_pytransform' + suffix
285
if plat == 'linux':
286
filename = os.path.abspath(os.path.join(path, name + '.so'))
287
elif plat == 'darwin':
288
filename = os.path.join(path, name + '.dylib')
289
elif plat == 'windows':
290
filename = os.path.join(path, name + '.dll')
291
elif plat == 'freebsd':
292
filename = os.path.join(path, name + '.so')
293
else:
294
raise PytransformError('Platform %s not supported' % plat)
295
296
if platid is not None and os.path.isfile(platid):
297
filename = platid
298
elif platid is not None or not os.path.exists(filename) or not is_runtime:
299
libpath = platid if platid is not None and os.path.isabs(platid) else \
300
os.path.join(path, plat_path, format_platform(platid))
301
filename = os.path.join(libpath, os.path.basename(filename))
302
303
if not os.path.exists(filename):
304
raise PytransformError('Could not find "%s"' % filename)
305
306
try:
307
m = cdll.LoadLibrary(filename)
308
except Exception as e:
309
if sys.flags.debug:
310
print('Load %s failed:\n%s' % (filename, e))
311
raise
312
313
# Removed from v4.6.1
314
# if plat == 'linux':
315
# m.set_option(-1, find_library('c').encode())
316
317
if not os.path.abspath('.') == os.path.abspath(path):
318
m.set_option(1, path.encode() if sys.version_info[0] == 3 else path)
319
320
# Required from Python3.6
321
m.set_option(2, sys.byteorder.encode())
322
323
if sys.flags.debug:
324
m.set_option(3, c_char_p(1))
325
m.set_option(4, c_char_p(not is_runtime))
326
327
# Disable advanced mode by default
328
m.set_option(5, c_char_p(not advanced))
329
330
# Set suffix for private package
331
if suffix:
332
m.set_option(6, suffix.encode())
333
334
return m
335
336
337
def pyarmor_init(path=None, is_runtime=0, platid=None, suffix='', advanced=0):
338
global _pytransform
339
_pytransform = _load_library(path, is_runtime, platid, suffix, advanced)
340
return init_pytransform()
341
342
343
def pyarmor_runtime(path=None, suffix='', advanced=0):
344
pyarmor_init(path, is_runtime=1, suffix=suffix, advanced=advanced)
345
init_runtime()
346
347
# ----------------------------------------------------------
348
# End of pytransform
349
# ----------------------------------------------------------
350
351
#
352
# Not available from v5.6
353
#
354
355
356
def generate_capsule(licfile):
357
prikey, pubkey, prolic = _generate_project_capsule()
358
capkey, newkey = _generate_pytransform_key(licfile, pubkey)
359
return prikey, pubkey, capkey, newkey, prolic
360
361
362
@dllmethod
363
def _generate_project_capsule():
364
prototype = PYFUNCTYPE(py_object)
365
dlfunc = prototype(('generate_project_capsule', _pytransform))
366
return dlfunc()
367
368
369
@dllmethod
370
def _generate_pytransform_key(licfile, pubkey):
371
prototype = PYFUNCTYPE(py_object, c_char_p, py_object)
372
dlfunc = prototype(('generate_pytransform_key', _pytransform))
373
return dlfunc(licfile.encode() if sys.version_info[0] == 3 else licfile,
374
pubkey)
375
376
377
#
378
# Deprecated functions from v5.1
379
#
380
@dllmethod
381
def encrypt_project_files(proname, filelist, mode=0):
382
prototype = PYFUNCTYPE(c_int, c_char_p, py_object, c_int)
383
dlfunc = prototype(('encrypt_project_files', _pytransform))
384
return dlfunc(proname.encode(), filelist, mode)
385
386
387
def generate_project_capsule(licfile):
388
prikey, pubkey, prolic = _generate_project_capsule()
389
capkey = _encode_capsule_key_file(licfile)
390
return prikey, pubkey, capkey, prolic
391
392
393
@dllmethod
394
def _encode_capsule_key_file(licfile):
395
prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p)
396
dlfunc = prototype(('encode_capsule_key_file', _pytransform))
397
return dlfunc(licfile.encode(), None)
398
399
400
@dllmethod
401
def encrypt_files(key, filelist, mode=0):
402
t_key = c_char * 32
403
prototype = PYFUNCTYPE(c_int, t_key, py_object, c_int)
404
dlfunc = prototype(('encrypt_files', _pytransform))
405
return dlfunc(t_key(*key), filelist, mode)
406
407
408
@dllmethod
409
def generate_module_key(pubname, key):
410
t_key = c_char * 32
411
prototype = PYFUNCTYPE(py_object, c_char_p, t_key, c_char_p)
412
dlfunc = prototype(('generate_module_key', _pytransform))
413
return dlfunc(pubname.encode(), t_key(*key), None)
414
415
#
416
# Compatible for PyArmor v3.0
417
#
418
@dllmethod
419
def old_init_runtime(systrace=0, sysprofile=1, threadtrace=0, threadprofile=1):
420
'''Only for old version, before PyArmor 3'''
421
pyarmor_init(is_runtime=1)
422
prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int)
423
_init_runtime = prototype(('init_runtime', _pytransform))
424
return _init_runtime(systrace, sysprofile, threadtrace, threadprofile)
425
426
427
@dllmethod
428
def import_module(modname, filename):
429
'''Only for old version, before PyArmor 3'''
430
prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p)
431
_import_module = prototype(('import_module', _pytransform))
432
return _import_module(modname.encode(), filename.encode())
433
434
435
@dllmethod
436
def exec_file(filename):
437
'''Only for old version, before PyArmor 3'''
438
prototype = PYFUNCTYPE(c_int, c_char_p)
439
_exec_file = prototype(('exec_file', _pytransform))
440
return _exec_file(filename.encode())
441
442