Skip to content

Commit 53b3f4a

Browse files
committed
Remove dead code, use pwnlib.logging for warnings
Also brings back the use of secrets.randbelow() if Python 3 is being used
1 parent 2dc7b37 commit 53b3f4a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

pwnlib/data/kctf/pow.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
# This file had been modified:
1818
# * Unnecessary code has been disabled
19+
# * It uses pwnlib logging instead of sys.stderr.write()
1920
# * 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
2122
# * The 'can_bypass' mechanism has been removed to eliminate the dependence on ecdsa
2223
# * str(b, 'utf-8') has been replaced with six.ensure_str(b, 'utf-8')
2324
# * bytes(s, 'utf-8') has been replaced with six.ensure_binary(s, 'utf-8')
@@ -27,21 +28,29 @@
2728

2829
import base64
2930
# import os
30-
import random
31+
# import secrets
3132
# import socket
32-
import sys
33+
# import sys
3334
# import hashlib
3435
import six
36+
from pwnlib.log import getLogger
3537
from pwnlib.util import packing
3638

39+
log = getLogger(__name__)
40+
3741
try:
3842
import gmpy2
3943
HAVE_GMP = True
4044
except ImportError:
4145
HAVE_GMP = False
4246
# sys.stderr.write("[NOTICE] Running 10x slower, gotta go fast? pip3 install gmpy2\n")
4347

44-
GMP_NOTICE_ISSUED = False
48+
try:
49+
import secrets
50+
HAVE_SECRETS = True
51+
except ImportError:
52+
import random
53+
HAVE_SECRETS = False
4554

4655
VERSION = 's'
4756
MODULUS = 2**1279-1
@@ -73,23 +82,17 @@ def gmpy_sloth_square(y, diff, p):
7382
return int(y)
7483

7584
def sloth_root(x, diff, p):
76-
global GMP_NOTICE_ISSUED
7785
if HAVE_GMP:
7886
return gmpy_sloth_root(x, diff, p)
7987
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")
8389
return python_sloth_root(x, diff, p)
8490

8591
def sloth_square(x, diff, p):
86-
global GMP_NOTICE_ISSUED
8792
if HAVE_GMP:
8893
return gmpy_sloth_square(x, diff, p)
8994
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")
9396
return python_sloth_square(x, diff, p)
9497

9598
def encode_number(num):
@@ -111,8 +114,11 @@ def encode_challenge(arr):
111114
return '.'.join([VERSION] + list(map(encode_number, arr)))
112115

113116
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)
116122
return encode_challenge([diff, x])
117123

118124
def solve_challenge(chal):

0 commit comments

Comments
 (0)