Skip to content

Commit 05afef0

Browse files
authored
Correctly truncate and deduplicate strings (#492)
1 parent 236d73d commit 05afef0

File tree

8 files changed

+352
-271
lines changed

8 files changed

+352
-271
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ Fixes
2929
- Fixed a crash when using the network monitor
3030
- Session can now be "quiet" by passing an empty list of loggers
3131
- Process Monitor: fixed Thread.isAlive for Python 3.9 compability
32+
- Correctly truncate values of the string primitive when max_len or size is set.
33+
- The string primitive will no longer generate duplicates when max_len or size is set.
34+
- Greatly improved string to bytes conversion speed.
3235

3336
v0.2.1
3437
------

boofuzz/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def s_random(value="", min_length=0, max_length=1, num_mutations=25, fuzzable=Tr
649649
Generate a random chunk of data while maintaining a copy of the original. A random length range can be specified.
650650
For a static length, set min/max length to be the same.
651651
652-
:type value: Raw
652+
:type value: str or bytes
653653
:param value: (Optional, def="") Original value
654654
:type min_length: int
655655
:param min_length: (Optional, def=0) Minimum length of random block
@@ -705,25 +705,30 @@ def s_mirror(primitive_name=None, name=None):
705705
blocks.CURRENT.push(Mirror(name=name, primitive_name=primitive_name, request=blocks.CURRENT))
706706

707707

708-
def s_string(value="", size=-1, padding=b"\x00", encoding="ascii", fuzzable=True, max_len=-1, name=None):
708+
def s_string(value="", size=None, padding=b"\x00", encoding="ascii", fuzzable=True, max_len=None, name=None):
709709
"""
710710
Push a string onto the current block stack.
711711
712712
:type value: str
713713
:param value: (Optional, def="")Default string value
714714
:type size: int
715-
:param size: (Optional, def=-1) Static size of this field, leave -1 for dynamic.
715+
:param size: (Optional, def=None) Static size of this field, leave None for dynamic.
716716
:type padding: Character
717717
:param padding: (Optional, def="\\x00") Value to use as padding to fill static field size.
718718
:type encoding: str
719-
:param encoding: (Optonal, def="ascii") String encoding, ex: utf_16_le for Microsoft Unicode.
719+
:param encoding: (Optional, def="ascii") String encoding, ex: utf_16_le for Microsoft Unicode.
720720
:type fuzzable: bool
721721
:param fuzzable: (Optional, def=True) Enable/disable fuzzing of this primitive
722722
:type max_len: int
723-
:param max_len: (Optional, def=-1) Maximum string length
723+
:param max_len: (Optional, def=None) Maximum string length
724724
:type name: str
725725
:param name: (Optional, def=None) Specifying a name gives you direct access to a primitive
726726
"""
727+
# support old interface where default was -1 instead of None
728+
if size == -1:
729+
size = None
730+
if max_len == -1:
731+
max_len = None
727732

728733
blocks.CURRENT.push(
729734
String(

boofuzz/fuzzable.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class Fuzzable(object):
2525
:type name: str, optional
2626
:param name: Name, for referencing later. Names should always be provided, but if not, a default name will be given,
2727
defaults to None
28-
:type default_value: Any
28+
:type default_value: Any, optional
2929
:param default_value: Value used when the element is not being fuzzed - should typically represent a valid value.
3030
Can be a static value, or a ReferenceValueTestCaseSession, defaults to None
31-
:type fuzzable: bool
31+
:type fuzzable: bool, optional
3232
:param fuzzable: Enable fuzzing of this primitive, defaults to True
33-
:type fuzz_values: list
33+
:type fuzz_values: list, optional
3434
:param fuzz_values: List of custom fuzz values to add to the normal mutations, defaults to None
3535
"""
3636

@@ -62,9 +62,6 @@ def name(self):
6262
6363
:rtype: str
6464
"""
65-
# if self._name is None:
66-
# Fuzzable.name_counter += 1
67-
# self._name = "{0}{1}".format(type(self).__name__, Fuzzable.name_counter)
6865
return self._name
6966

7067
@property

boofuzz/helpers.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,5 @@ def get_boofuzz_version(boofuzz_class):
439439
return "v-.-.-"
440440

441441

442-
def str_to_bytes(value):
443-
result = value
444-
# if python2, str is already bytes compatible
445-
if six.PY3:
446-
if isinstance(value, six.text_type):
447-
temp = [bytes([ord(i)]) for i in value]
448-
result = b"".join(temp)
449-
return result
442+
def str_to_bytes(value, encoding="utf-8", errors="replace"):
443+
return six.ensure_binary(value, encoding=encoding, errors=errors)

boofuzz/primitives/random_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RandomData(Fuzzable):
1818
:type name: str, optional
1919
:param default_value: Value used when the element is not being fuzzed - should typically represent a valid value,
2020
defaults to None
21-
:type default_value: Any, optional
21+
:type default_value: str or bytes, optional
2222
:param min_length: Minimum length of random block, defaults to 0
2323
:type min_length: int, optional
2424
:param max_length: Maximum length of random block, defaults to 1

0 commit comments

Comments
 (0)