secdev / scapy

Scapy: the Python-based interactive packet manipulation program & library.
https://scapy.net
GNU General Public License v2.0
10.83k stars 2.04k forks source link

pdfdump crashes with UnicodeEncodeError #4319

Open Abbath opened 8 months ago

Abbath commented 8 months ago

Brief description

When dumping packets with random bytes the following happens:

Traceback (most recent call last):
  File "/home/user/test2.py", line 10, in <module>
    packet.pdfdump("test.pdf")
  File "/home/user/.local/lib/python3.10/site-packages/scapy/base_classes.py", line 474, in pdfdump
    canvas = self.canvas_dump(**kargs)
  File "/home/user/.local/lib/python3.10/site-packages/scapy/packet.py", line 930, in canvas_dump
    vt = pyx.text.text(XSTART + 3.5, (YTXT - y) * YMUL, r"\font\cmssfont=cmss10\cmssfont{%s}" % tex_escape(fval))  # noqa: E501
  File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1455, in wrapped
    return f(self, *args, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1491, in text
    return self.instance.text(*args, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1331, in text
    return self.text_pt(unit.topt(x), unit.topt(y), *args, **kwargs)
  File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1305, in text_pt
    left_pt, right_pt, height_pt, depth_pt = self.do_typeset(expr, self.texmessages_run_default + self.texmessages_run + texmessages)
  File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1225, in do_typeset
    return self._execute(expr, texmessages, STATE_TYPESET, STATE_TYPESET)
  File "/home/user/.local/lib/python3.10/site-packages/pyx/text.py", line 1085, in _execute
    expr.encode(self.texenc)
UnicodeEncodeError: 'ascii' codec can't encode character '\u0700' in position 32: ordinal not in range(128)

It seems that texenc in pyx is set to default ASCII. If I change it to 'utf-8' everything works fine.

Scapy version

2.5.0

Python version

3.10

Operating system

Kubuntu 22.04

Additional environment information

No response

How to reproduce

This will eventually fail

from scapy.layers.inet import Ether, UDP, IP
from scapy.compat import raw
import numpy as np
import time

while True:
  packet = Ether(dst="00:00:00:00:00:00", src="00:00:00:00:00:00", type=0x800)/IP(src="0.0.0.0", dst="0.0.0.0")/UDP(sport=0, dport=0)
  packet = packet/raw(np.random.bytes(512))
  packet.pdfdump("test.pdf")
  time.sleep(1)

Actual result

Exception

Expected result

A PDF

Related resources

No response

guedou commented 8 months ago

Thanks for your report. Can you share a single packet that reproduce the issue?

Abbath commented 8 months ago

example.zip

<Ether  dst=00:00:00:00:00:00 src=00:00:00:00:00:00 type=IPv4 |<IP  version=4 ihl=5 tos=0x0 len=540 id=1 flags= frag=0 ttl=64 proto=udp chksum=0x78d1 src=0.0.0.0 dst=0.0.0.0 |<UDP  sport=0 dport=0 len=520 chksum=0x93d8 |<Raw  load='\\xb0o߂\\x98\\xbc\n\\xd8y\x14Z\\x97\x02G+m_\\xa9\\xfb\\xed&\\x90S\\xf5c:\\x98\\xf2\\xa3c0/R\\x89\\xf8\\x98\\xfd\\xc2ɒ\\\\xd1X\\x89gqQ\\x87\\xe9\x04\\x8dcI\\x80[mf\\xf3m\x1cAS\\x82y\\xc9=#\\x91\\xc7*j\\xf2\\xba|\\xb4\x0b\\\\x82/\\xf4\x1c\\xd0X\\x97\\xccoT\\xfd\\xfd[\x03G\\xb8b\\xe9N\\xc8\\\\x9f\\x9f"oq{\\xed\\xeb\\xfe\x1d?7\\xf9\\xe4N\\xf5\\xc5>\\xe5q\\x9f\\x8c9\\xac\\xb5\\xe4B\\xcal\\xab\x08\\xf4P)\\xe7\x00\\xda`\\x84q\t\\xd0\x16t \\xe6\\xaf\\xc5\x0b\\xe9\\xf6\\xa8}#{/\\xac\\xc3}\x17[6\\x97E\\x8dB\\xc7T#9\x05R?\\x97\\x87\\xc0\\xe5Vg\\x87)\\x8ab\\x92\\xc8]f\x1a\\xed\\xc0\\x8f^\\xe7鏄^\\x86q\x0f\\x96\\x84\\xa14\x13l\\xea\\xfb\x0cd\\xa2\\xcc1L\\xfdT\\x93+\\xec!b@l|\\xb3\\xedw\\xed\\x82Q\\xe7\\xeb}x\\xfcl8\\xcd̹\u05eb\\x98\\xbb\t|\\xe4\\\xc9"\\xfcy-ɤVMw]\\xfc}\x01\x17\\xc7&\x03\\xe0A\x0f\\xfd3\x06o4\\x83\\xd1]\\xa3\\xd9\'`,\\xae\\x9c\\xd4\\xfa\\x97\\x9b\x0f\\xe6\'ܡ\\xb2\\xab\\xf1\\xe3o5p\\xcds(\x119\\xb6p\\xd2_>\\xddL\\xf6M\x18}\\x92\x16moE|\x14Du\\x97-\\xf0\\xfe\\xc7\\xf7\\xe1\\xf9\\xae\\xdc-_\x05Vi\\xab\\xaa>\\xf0\\xb0\x01\\x84\\x89<Bt\\xfb\\xa7\\x8c\x01tX8*\\x92\\x95YM\\xf1\\xfb\\xc7\x13\\xc1\\xe7\\xd1%5\\xa0\\x83\\xa8}!\\xa6\\xea3\\xa8\\xc7\\xd4[\x1c\\xbc\\x98_tw\\xee)\\x92JP\\xfe\\x8c\\xcdS\\xbc\\xbc\\xa3\r\\x88\\x91͙\x19\x06\\xc5\\xfd\\xbb\x1b\x7fη\\xa7/K\\xe8lWe.\x1d"\x11\x01\\xf7\\xd7U\\xfdF\x1c\\xcbkL\\\xa2\x14g\x17\\x8f\\xb4\\x8ecn\\xa1\\xc3\\xd3Ҩ\\x9fJ\\x91\x13C\\xb7\\xb9x\\xf1R\\xae=7S\x7f\\xdd\\xd1eY\\x92H+u\\xbb\\x9ba\\xa9\\xf5\x07\\xf8\x0e#\\xcd\\xccgਁ\\xa8ۣ\\xb6\\xb3\\xc3@\x03#\\xb7\\xd9\ \r\x7f\\x9c\x04\\x9c\\xd0`\\xa4^@V\\xbf\\x96\\xb5%' |>>>>
guedou commented 8 months ago

Sorry, I cannot use this. Please share a pcap or something that we can reproduce.

Abbath commented 8 months ago

Pcap is inside the example.zip

guedou commented 8 months ago

Sorry, I haven't seen the zip file in the GitHub email. What's the fix that you propose?

Abbath commented 8 months ago

I propose to add some additional escaping somewhere in Packet.canvas_dump

                if isinstance(fval, str):
                    #HERE MAYBE?
                    if len(fval) > 18:
                        fval = fval[:18] + "[...]"
                else:
                    fval = ""

Changing PyX to unicode breaks LaTeX. Changing Packet representation in general may break something else.

gpotter2 commented 7 months ago

I can't replicate. Your example.zip works just fine for me.

Abbath commented 4 months ago

Are you doing this on Windows?