Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding two encoding functions breaks print #193

Open
skleber opened this issue Jan 27, 2024 · 1 comment
Open

Adding two encoding functions breaks print #193

skleber opened this issue Jan 27, 2024 · 1 comment

Comments

@skleber
Copy link
Contributor

skleber commented Jan 27, 2024

Assuming field is a Model.Vocabulary.Field, then calling:

field.addEncodingFunction(TypeEncodingFunction(Integer))
field.addEncodingFunction(TypeEncodingFunction(HexaString))
# now there are two encoding functions
field.str_data()

... results in the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[59], line 1
----> 1 field.str_data()

File ~/.pyenv/versions/3.9.18/lib/python3.9/site-packages/netzob/Common/Utils/Decorators.py:190, in typeCheck.<locals>._typeCheck_.<locals>.wrapped_f(*args, **kwargs)
    184         if argument is not None and not isinstance(argument,
    185                                                    final_types[i]):
    186             raise TypeError(
    187                 "Invalid type for arguments, expecting: {0} and received {1}".
    188                 format(', '.join([t.__name__ for t in final_types
    189                                   ]), argument.__class__.__name__))
--> 190 return func(*args, **kwargs)

File ~/.pyenv/versions/3.9.18/lib/python3.9/site-packages/netzob/Model/Vocabulary/AbstractField.py:895, in AbstractField.str_data(self, deepness)
    857 @typeCheck(int)
    858 def str_data(self, deepness=0):
    859     """Returns a string which shows the associated messages of the current
    860     symbol/field, after applying the symbol/field definition.
    861 
   (...)
    893 
    894     """
--> 895     result = self.getCells(encoded=True)
    896     return str(result)

...

File ~/.pyenv/versions/3.9.18/lib/python3.9/site-packages/netzob/Model/Vocabulary/Types/BitArray.py:466, in BitArray.decode(data, unitSize, endianness, sign)
    464 if data is None:
    465     raise TypeError("data cannot be None")
--> 466 return data.tobytes()

AttributeError: 'bytes' object has no attribute 'tobytes'

It may be a rare situation that you want to have two encoding functions, however, a twisted protocol could use a hexastring to actually represent an integer. If this should not be supported by Netzob, why can there be multiple encoding functions for one field? And if not, could you remove the option to add more than one encoding function to prevent this error? Or prevent the chaining of incompatible encoding functions?

@skleber
Copy link
Contributor Author

skleber commented Jan 27, 2024

I just found some note of mine about possible (future) use cases of multiple chained encoding functions.


One use case of multiple encoding functions executed one after another, is some kind of transmission encoding, like base64:

  1. raw bit data gets interpreted as ASCII bytewise
  2. every three chars of the ASCII representation get encoded in 4 ASCII characters as base64

Another example would be some XOR-coding repeatedly applying to the field data an XOR with a constant value.
Thereafter, the result can be interpreted as any kind of data (Integer, ASCII, IP, Hex, ...)
Similar codings might exist.


Another use case might be to make some data more readable. E. g. a Integer could be formated with thousands-markers every three digits (like 1'000'000) with an encoding function called consecutively to the interpretation of the raw data by the Integer-encoding function. Likewise leading or trailing whitespaces or padding zeros could be hidden from the output by a encoding function further formatting either string or number data representations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant