scapy icon indicating copy to clipboard operation
scapy copied to clipboard

Add json()

Open gpotter2 opened this issue 1 year ago • 1 comments

  • add a json() function
  • add more command() tests

The rationale is: I've had someone ask me if there was a format that could be used for dirty grepping. This must NOT be used as a pickle alternative (I'm feeling like I have to remind this because some projects / people were asking for that..) ... as the binary form is just fine for that.

gpotter2 avatar Feb 16 '24 16:02 gpotter2

Codecov Report

Merging #4283 (abfc483) into master (0708e67) will increase coverage by 32.30%. Report is 1 commits behind head on master. The diff coverage is 95.45%.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #4283       +/-   ##
===========================================
+ Coverage   49.67%   81.98%   +32.30%     
===========================================
  Files         342      349        +7     
  Lines       76269    81879     +5610     
===========================================
+ Hits        37889    67131    +29242     
+ Misses      38380    14748    -23632     
Files Coverage Δ
scapy/asn1/asn1.py 86.28% <100.00%> (+19.06%) :arrow_up:
scapy/packet.py 84.11% <96.96%> (ø)
scapy/volatile.py 85.24% <75.00%> (+48.50%) :arrow_up:

... and 262 files with indirect coverage changes

codecov[bot] avatar Feb 16 '24 17:02 codecov[bot]

@guedou Feel free to merge if ok :)

gpotter2 avatar Feb 21 '24 23:02 gpotter2

@gpotter2 from a user perspective, I think that it will make more sense that command(json=True) and json() returns the same thing, i.e. a string. I find it odd that command(json=True) returns a dict. What do you think?

guedou avatar Feb 22 '24 08:02 guedou

The command() you're talking about it not the Packet's one but the VolatileValue / ASN.1 ones. I'd say no one calls those. The Packet's command has no json parameter.

gpotter2 avatar Feb 22 '24 12:02 gpotter2

Sorry for the mistake!

guedou avatar Feb 22 '24 12:02 guedou

Nice!

Looks like some packets can't be serialized:

 === Uncaught Python exception: ===                                                                                                                           
TypeError: Object of type UUID is not JSON serializable  
...
    p.json()
  File "/home/vagrant/scapy/scapy/packet.py", line 1778, in json
    pc = self.payload.json()
         ^^^^^^^^^^^^^^^^^^^
  File "/home/vagrant/scapy/scapy/packet.py", line 1778, in json
    pc = self.payload.json()
         ^^^^^^^^^^^^^^^^^^^
  File "/home/vagrant/scapy/scapy/packet.py", line 1778, in json
    pc = self.payload.json()
         ^^^^^^^^^^^^^^^^^^^
  File "/home/vagrant/scapy/scapy/packet.py", line 1777, in json
    dump = json.dumps({k: v for (k, v) in self._command(json=True)})
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type UUID is not JSON serializable

==144946== ERROR: libFuzzer: fuzz target exited

As far as I can tell it can be reproduced with something like

DceRpc4(object='00480009-0800-0000-0000-0600c0000000').json()

evverx avatar Feb 22 '24 13:02 evverx

That was expected, we'll fix them as they come. This isn't really a critical feature as the output is rather fuzzy.

gpotter2 avatar Feb 22 '24 18:02 gpotter2