Transform a layer into a fuzzy layer by replacing some default values by random objects. :param p: the Packet instance to fuzz :return: the fuzzed packet.
(p, # type: _P
_inplace=0, # type: int
)
| 2638 | |
| 2639 | @conf.commands.register |
| 2640 | def fuzz(p, # type: _P |
| 2641 | _inplace=0, # type: int |
| 2642 | ): |
| 2643 | # type: (...) -> _P |
| 2644 | """ |
| 2645 | Transform a layer into a fuzzy layer by replacing some default values |
| 2646 | by random objects. |
| 2647 | |
| 2648 | :param p: the Packet instance to fuzz |
| 2649 | :return: the fuzzed packet. |
| 2650 | """ |
| 2651 | if not _inplace: |
| 2652 | p = p.copy() |
| 2653 | q = cast(Packet, p) |
| 2654 | while not isinstance(q, NoPayload): |
| 2655 | new_default_fields = {} |
| 2656 | multiple_type_fields = [] # type: List[str] |
| 2657 | for f in q.fields_desc: |
| 2658 | if isinstance(f, PacketListField): |
| 2659 | for r in getattr(q, f.name): |
| 2660 | fuzz(r, _inplace=1) |
| 2661 | elif isinstance(f, MultipleTypeField): |
| 2662 | # the type of the field will depend on others |
| 2663 | multiple_type_fields.append(f.name) |
| 2664 | elif f.default is not None: |
| 2665 | if not isinstance(f, ConditionalField) or f._evalcond(q): |
| 2666 | rnd = f.randval() |
| 2667 | if rnd is not None: |
| 2668 | new_default_fields[f.name] = rnd |
| 2669 | # Process packets with MultipleTypeFields |
| 2670 | if multiple_type_fields: |
| 2671 | # freeze the other random values |
| 2672 | new_default_fields = { |
| 2673 | key: (val._fix() if isinstance(val, VolatileValue) else val) |
| 2674 | for key, val in new_default_fields.items() |
| 2675 | } |
| 2676 | q.default_fields.update(new_default_fields) |
| 2677 | new_default_fields.clear() |
| 2678 | # add the random values of the MultipleTypeFields |
| 2679 | for name in multiple_type_fields: |
| 2680 | fld = cast(MultipleTypeField, q.get_field(name)) |
| 2681 | rnd = fld._find_fld_pkt(q).randval() |
| 2682 | if rnd is not None: |
| 2683 | new_default_fields[name] = rnd |
| 2684 | q.default_fields.update(new_default_fields) |
| 2685 | q = q.payload |
| 2686 | return p |