16
16
17
17
# This file had been modified:
18
18
# * Unnecessary code has been disabled
19
+ # * It uses pwnlib logging instead of sys.stderr.write()
19
20
# * The notice about installing gmpy2 has been moved into functions to make for a quieter import
20
- # * The use of secrets.randbelow () has been replaced with random.randrange () for Python2 compatibility
21
+ # * For Python 2 compatibility, random.randrange () is used if secrets.randbelow () is not available
21
22
# * The 'can_bypass' mechanism has been removed to eliminate the dependence on ecdsa
22
23
# * str(b, 'utf-8') has been replaced with six.ensure_str(b, 'utf-8')
23
24
# * bytes(s, 'utf-8') has been replaced with six.ensure_binary(s, 'utf-8')
27
28
28
29
import base64
29
30
# import os
30
- import random
31
+ # import secrets
31
32
# import socket
32
- import sys
33
+ # import sys
33
34
# import hashlib
34
35
import six
36
+ from pwnlib .log import getLogger
35
37
from pwnlib .util import packing
36
38
39
+ log = getLogger (__name__ )
40
+
37
41
try :
38
42
import gmpy2
39
43
HAVE_GMP = True
40
44
except ImportError :
41
45
HAVE_GMP = False
42
46
# sys.stderr.write("[NOTICE] Running 10x slower, gotta go fast? pip3 install gmpy2\n")
43
47
44
- GMP_NOTICE_ISSUED = False
48
+ try :
49
+ import secrets
50
+ HAVE_SECRETS = True
51
+ except ImportError :
52
+ import random
53
+ HAVE_SECRETS = False
45
54
46
55
VERSION = 's'
47
56
MODULUS = 2 ** 1279 - 1
@@ -73,23 +82,17 @@ def gmpy_sloth_square(y, diff, p):
73
82
return int (y )
74
83
75
84
def sloth_root (x , diff , p ):
76
- global GMP_NOTICE_ISSUED
77
85
if HAVE_GMP :
78
86
return gmpy_sloth_root (x , diff , p )
79
87
else :
80
- if not GMP_NOTICE_ISSUED :
81
- sys .stderr .write ("[NOTICE] kctf-pow running 10x slower, gotta go fast? pip3 install gmpy2\n " )
82
- GMP_NOTICE_ISSUED = True
88
+ log .warning_once ("kctf-pow is running 10x slower, gotta go fast? pip3 install gmpy2" )
83
89
return python_sloth_root (x , diff , p )
84
90
85
91
def sloth_square (x , diff , p ):
86
- global GMP_NOTICE_ISSUED
87
92
if HAVE_GMP :
88
93
return gmpy_sloth_square (x , diff , p )
89
94
else :
90
- if not GMP_NOTICE_ISSUED :
91
- sys .stderr .write ("[NOTICE] kctf-pow running 10x slower, gotta go fast? pip3 install gmpy2\n " )
92
- GMP_NOTICE_ISSUED = True
95
+ log .warning_once ("kctf-pow is running 10x slower, gotta go fast? pip3 install gmpy2" )
93
96
return python_sloth_square (x , diff , p )
94
97
95
98
def encode_number (num ):
@@ -111,8 +114,11 @@ def encode_challenge(arr):
111
114
return '.' .join ([VERSION ] + list (map (encode_number , arr )))
112
115
113
116
def get_challenge (diff ):
114
- sys .stderr .write ("[WARNING] kctf-pow using random.randrange() which is not cryptographically secure\n " )
115
- x = random .randrange (CHALSIZE )
117
+ if HAVE_SECRETS :
118
+ x = secrets .randbelow (CHALSIZE )
119
+ else :
120
+ log .warning_once ("kctf-pow is using random.randrange() which is not cryptographically secure" )
121
+ x = random .randrange (CHALSIZE )
116
122
return encode_challenge ([diff , x ])
117
123
118
124
def solve_challenge (chal ):
0 commit comments