paulc / dnslib

A Python library to encode/decode DNS wire-format packets
https://github.com/paulc/dnslib
BSD 2-Clause "Simplified" License
295 stars 84 forks source link

Error in malformed requests #9

Closed brunobg closed 4 years ago

brunobg commented 4 years ago

I get a bunch of these errors after running the server for a while:

Traceback (most recent call last):                                                                                        
File "/usr/lib/python3.6/socketserver.py", line 654, in process_request_thread
self.finish_request(request, client_address)     
File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
self.RequestHandlerClass(request, client_address, self)                                                                             
File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
self.handle()
File "/usr/local/lib/python3.6/dist-packages/dnslib/server.py", line 130, in handle                                                     length = struct.unpack("!H",bytes(data[:2]))[0]                                                                                   struct.error: unpack requires a buffer of 2 bytes                                                                                     

Apparently they are harmless, but I wonder if dnslib is handling something in the DNS protocol wrongly. If not it'd be nice to have a try/catch to avoid logging these. Any ideas of what is causing them?

I also get a few like these:

Error unpacking DNSQuestion [offset=344]: Not enough bytes [offset=344,remaining=59,requested=79]

paulc commented 4 years ago

It looks like the request is truncated so that the server can't read the length field (first two bytes) - I'v put a check for this into server.py in 0.9.13