-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzz.py
291 lines (227 loc) · 8.9 KB
/
fuzz.py
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
"""Fuzz the signers to search for vulnerabilities.
This module is intended to be run using `pythonfuzz`. See
https://gitlab.com/gitlab-org/security-products/analyzers/fuzzers/pythonfuzz
It has been optimized for CPython 3.11 due to extensive use of try/except blocks.
Note:
Install `pythonfuzz` via:
pip install \
--extra-index-url https://gitlab.com/api/v4/projects/19904939/packages/pypi/simple \
pythonfuzz
Or:
poetry source add --priority explicit \
pythonfuzz \
https://gitlab.com/api/v4/projects/19904939/packages/pypi/simple
poetry add --group dev --source pythonfuzz pythonfuzz
Usage:
python fuzz.py <signer> [fuzzer args...]
where signer is one of:
- blake2signer
- blake2timestampsigner
- blake2serializersigner
and fuzzer args are pythonfuzz options.
Example:
python fuzz.py blake2signer .fuzzed_blake2signer --runs 10000
"""
import importlib.util
import signal
import sys
from contextvars import ContextVar
from functools import partial
from functools import wraps
from io import BytesIO
from types import FrameType
from typing import Any
from typing import Callable
from typing import Dict
from typing import Optional
from typing import Type
from typing import TypeVar
from typing import cast
from blake2signer import Blake2SerializerSigner
from blake2signer import Blake2Signer
from blake2signer import Blake2TimestampSigner
from blake2signer.bases import Blake2SignerBase
from blake2signer.errors import ConversionError
from blake2signer.errors import InvalidOptionError
from blake2signer.errors import MissingDependencyError
from blake2signer.errors import UnsignedDataError
from blake2signer.hashers import HasherChoice
from blake2signer.serializers import NullSerializer
SignedT = TypeVar('SignedT')
SignerT = TypeVar('SignerT', bound=Blake2SignerBase)
# Global signers collection to save on instantiation time
signers_ctx: ContextVar[Dict[str, Blake2SignerBase]] = ContextVar(
'signers_ctx',
default={},
)
def kbinterrupt_handler(signum: int, _: Optional[FrameType]) -> None:
"""Handle keyboard interrupt (CTRL+C)."""
print()
print('Process interrupted!')
sys.exit(128 + signum)
def get_signer(
klass: Type[SignerT],
*,
hasher: HasherChoice,
secret: bytes,
**signer_kwargs: Any,
) -> SignerT:
"""Get a signer of the given class, for the given hasher.
If the secret is improper for the class, then a generic one will be used, caching the
class instantiation in a static variable for the duration of the script.
Args:
klass: Signer class.
Keyword Args:
hasher: A hasher from `HasherChoice`.
secret: A secret.
**signer_kwargs: Extra signer arguments.
Returns:
An instance of a signer.
"""
try:
return klass(secret, hasher=hasher, **signer_kwargs)
except InvalidOptionError:
# The secret is invalid, let's use a global signer cache to save on instantiation time
signers = signers_ctx.get()
signer_name = f'{klass.__name__}_{hasher.value}'
try:
signer = cast(SignerT, signers[signer_name])
except KeyError:
signer = klass(b's' * klass.MIN_SECRET_SIZE, hasher=hasher, **signer_kwargs)
signers[signer_name] = signer
signers_ctx.set(signers)
return signer
def check_signing(
data: bytes,
*,
sign: Callable[[bytes], SignedT],
unsign: Callable[[SignedT], bytes],
) -> None:
"""Check signing data using given sign/unsign functions."""
try:
signed = sign(data)
except UnsignedDataError:
return
unsigned = unsign(signed)
if unsigned != data:
raise ValueError(
f'data mismatch: original:0x{data.hex()} != unsigned:0x{unsigned.hex()}', # noqa: E231
)
def import_pythonfuzz() -> Any:
"""Import PythonFuzz if possible, otherwise return a stub that will fail when used."""
if importlib.util.find_spec('pythonfuzz') is None:
class PythonFuzz: # pylint: disable=R0903
"""Stub for PythonFuzz."""
def __init__(self, _: Callable[[bytes], None]) -> None:
"""Stub for PythonFuzz."""
raise MissingDependencyError(
'pythonfuzz can not be used if it is not installed: '
+ 'python3 -m pip install --extra-index-url '
+ 'https://gitlab.com/api/v4/projects/19904939/packages/pypi/simple '
+ 'pythonfuzz',
)
def __call__(self, *_: Any, **__: Any) -> None:
"""Make this class callable."""
else:
from pythonfuzz.main import PythonFuzz # type: ignore # pylint: disable=C0415
return PythonFuzz
def fuzz(func: Callable[[bytes], None]) -> Callable[[], None]:
"""Fuzz given function with pythonfuzzer.
This decorator wraps PythonFuzz so that it can be safely used even if it is not installed.
Returns:
The decorated function.
"""
@wraps(func)
def inner() -> None:
"""Run pythonfuzzer."""
pythonfuzz = import_pythonfuzz()
pythonfuzz(func)()
return inner
@fuzz
def fuzz_blake2signer(buf: bytes) -> None: # pragma: nocover
"""Fuzz Blake2Signer to search for vulnerabilities.
Raises:
ValueError: Unsigned data doesn't match original data.
"""
hasher: HasherChoice # PyCharm wrongly thinks that `hasher` is a string...
for hasher in HasherChoice:
signer: Blake2Signer = get_signer(Blake2Signer, hasher=hasher, secret=buf)
# Check regular signing
check_signing(buf, sign=signer.sign, unsign=signer.unsign)
# Check signing in parts
check_signing(buf, sign=signer.sign_parts, unsign=signer.unsign_parts)
@fuzz
def fuzz_blake2timestampsigner(buf: bytes) -> None: # pragma: nocover
"""Fuzz Blake2TimestampSigner to search for vulnerabilities.
Raises:
ValueError: Unsigned data doesn't match original data.
"""
hasher: HasherChoice # PyCharm wrongly thinks that `hasher` is a string...
for hasher in HasherChoice:
signer: Blake2TimestampSigner = get_signer(
Blake2TimestampSigner,
hasher=hasher,
secret=buf,
)
# Check regular signing
check_signing(buf, sign=signer.sign, unsign=partial(signer.unsign, max_age=None))
# Check signing in parts
check_signing(
buf,
sign=signer.sign_parts,
unsign=partial(signer.unsign_parts, max_age=None),
)
@fuzz
def fuzz_blake2serializersigner(buf: bytes) -> None: # noqa: C901 # pragma: nocover
"""Fuzz Blake2SerializerSigner to search for vulnerabilities.
Raises:
ValueError: Unsigned data doesn't match original data.
"""
hasher: HasherChoice # PyCharm wrongly thinks that `hasher` is a string...
for hasher in HasherChoice:
signer: Blake2SerializerSigner = get_signer(
Blake2SerializerSigner,
hasher=hasher,
secret=buf,
serializer=NullSerializer,
)
# Check regular signing
check_signing(buf, sign=signer.dumps, unsign=signer.loads)
# Check signing in parts
check_signing(buf, sign=signer.dumps_parts, unsign=signer.loads_parts)
# Check signing using a file
def dump(data: bytes) -> BytesIO:
"""Serialize and sign data to a file."""
file = BytesIO()
try: # try/except blocks are fine for CPython >= 3.11
signer.dump(data, file) # noqa: B023 # pylint: disable=W0640 # usage on purpose
except ConversionError as exc: # This should be impossible, so let's check it
raise ValueError(
f'impossible ConversionError achieved with 0x{data.hex()}',
) from exc
return file
def load(file: BytesIO) -> bytes:
"""Recover original data from a signed serialized file from `dump`."""
file.seek(0)
return signer.load(file) # noqa: B023 # pylint: disable=W0640 # usage on purpose
check_signing(buf, sign=dump, unsign=load)
def main() -> None:
"""Run fuzzing according to user arguments."""
if len(sys.argv) < 2:
print(f'Usage: {sys.argv[0]} <signer> [fuzzer args...]')
print('Where signer is one of the signers provided by this package')
sys.exit(1)
wanted_signer = sys.argv.pop(1) # Remove argument, so the fuzzer doesn't get it
func_name = f'fuzz_{wanted_signer.lower()}'
if func_name not in globals():
print(
'Signer does not exist or can not be fuzzed: fuzzer not implemented for',
wanted_signer,
)
sys.exit(1)
fuzzer = globals()[func_name]
print('Fuzzing for', wanted_signer, '...')
fuzzer()
signal.signal(signal.SIGINT, kbinterrupt_handler)
if __name__ == '__main__': # pragma: nocover
main()