ValentinBELYN / icmplib

Easily forge ICMP packets and make your own ping and traceroute.
GNU Lesser General Public License v3.0
267 stars 45 forks source link

Bug: struct.error: unpack requires a buffer of 4 bytes #12

Closed weeman1337 closed 3 years ago

weeman1337 commented 3 years ago

I am running a python script that pings a host in a specific interval.
From time to time (~5 times a day) I get this error:

File "/home/asd/venv/asd/lib/python3.7/site-packages/icmplib/ping.py", line 123, in ping
      reply = socket.receive()
   File "/home/asd/venv/asd/lib/python3.7/site-packages/icmplib/sockets.py", line 313, in receive
      reply_time=reply_time)
   File "/home/asd/venv/asd/lib/python3.7/site-packages/icmplib/sockets.py", line 210, in _read_reply
      self._config.ICMP_PAYLOAD_OFFSET
struct.error: unpack requires a buffer of 4 bytes

Here is the line calling icmplib:

icmp_ping(SOME_IP, 3)
ValentinBELYN commented 3 years ago

Hi 👋,

I took a little while to find the cause of the problem. I added several conditions to ensure that the responses received are well structured. Can you update icmplib to the latest version (1.1.3) and tell me if you still have this problem?

pip3 install --upgrade icmplib
weeman1337 commented 3 years ago

Thanks! I will update the lib and report back here.

What is the result if the struct check fails?

ValentinBELYN commented 3 years ago

If the test fails, the received packet is ignored so as not to impact the operation of the library.

weeman1337 commented 3 years ago

Hi, @ValentinBELYN I have updated the lib to version 1.1.3.

It still produces the error. The lines in the stack trace changed a bit:

Trace: Traceback (most recent call last):
  File "/home/asd/asd/src/asd.py", line 94, in run_check
    result = plugin.run(spec)
  File "/home/asd/asd/src/plugins/check/ping.py", line 17, in run
    result = icmp_ping(check_vars['ips'][check_vars['ipfield']], check_vars['packages'])
  File "/home/asd/venv/asd/lib/python3.7/site-packages/icmplib/ping.py", line 123, in ping
    reply = socket.receive()
  File "/home/asd/venv/asd/lib/python3.7/site-packages/icmplib/sockets.py", line 320, in receive
    reply_time=reply_time)
  File "/home/asd/venv/asd/lib/python3.7/site-packages/icmplib/sockets.py", line 217, in _read_reply
    self._config.ICMP_PAYLOAD_OFFSET
struct.error: unpack requires a buffer of 4 bytes
ValentinBELYN commented 3 years ago

Thanks for taking the time to test 😃

The patch in version 1.1.3 partially fixed the problem and I noticed it too late... 😢 The next version available at the end of the week (1.2) should completely fix the problem. If you can't wait, you can test this version by typing the following commands:

# Uninstall icmplib
pip3 uninstall icmplib

# Download and extract this repository
wget -qO- https://github.com/ValentinBELYN/icmplib/archive/master.tar.gz | tar -xzf -

# Install the version under development (which contains the fix):
python3 setup.py install
ValentinBELYN commented 3 years ago

icmplib 1.2 is available! This version fully fixes your problem and brings new features concerning the quality of service.

pip3 install --upgrade icmplib

I await your feedback on it 😃

weeman1337 commented 3 years ago

Thanks for the info. I will update it and give your feedback here in some days.

weeman1337 commented 3 years ago

Hey @ValentinBELYN I did not get the error any more after the update. Thanks!

ValentinBELYN commented 3 years ago

Great! So I close this issue. Thanks for reporting this issue and testing. 👍