Repository: secdev/scapy

Issue:
TypeError when opening malformed pcap
#### Brief description

Scapy crashes when opening a malformed pcap file.

#### Environment

-   Scapy version:  scapy-2.4.4rc2.dev6 from a `pip3 install --upgrade git+git://github.com/secdev/scap`
-   Python version: 3.5
-   Operating System: Debian

#### How to reproduce

```python
$ cat test.py
import sys
import scapy
import scapy.utils

scapy.utils.rdpcap(sys.argv[1])
$ python3 test.py sample.pcap
Traceback (most recent call last):
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1139, in read_packet
    p = self.LLcls(s)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    scapy.utils.rdpcap(sys.argv[1])
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 951, in rdpcap
    return fdesc.read_all(count=count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1154, in read_all
    res = RawPcapReader.read_all(self, count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1094, in read_all
    p = self.read_packet()
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1147, in read_packet
    p = conf.raw_layer(s)
TypeError: 'NoneType' object is not callable
$
```

#### Actual result

```python
Traceback (most recent call last):
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1139, in read_packet
    p = self.LLcls(s)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    scapy.utils.rdpcap(sys.argv[1])
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 951, in rdpcap
    return fdesc.read_all(count=count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1154, in read_all
    res = RawPcapReader.read_all(self, count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1094, in read_all
    p = self.read_packet()
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1147, in read_packet
    p = conf.raw_layer(s)
TypeError: 'NoneType' object is not callable
```

#### Expected result

Not a crash.

#### Related resources

[sample.pcap.zip](https://github.com/secdev/scapy/files/5006905/sample.pcap.zip)


TypeError when opening malformed pcap
#### Brief description

Scapy crashes when opening a malformed pcap file.

#### Environment

-   Scapy version:  scapy-2.4.4rc2.dev6 from a `pip3 install --upgrade git+git://github.com/secdev/scap`
-   Python version: 3.5
-   Operating System: Debian

#### How to reproduce

```python
$ cat test.py
import sys
import scapy
import scapy.utils

scapy.utils.rdpcap(sys.argv[1])
$ python3 test.py sample.pcap
Traceback (most recent call last):
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1139, in read_packet
    p = self.LLcls(s)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    scapy.utils.rdpcap(sys.argv[1])
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 951, in rdpcap
    return fdesc.read_all(count=count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1154, in read_all
    res = RawPcapReader.read_all(self, count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1094, in read_all
    p = self.read_packet()
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1147, in read_packet
    p = conf.raw_layer(s)
TypeError: 'NoneType' object is not callable
$
```

#### Actual result

```python
Traceback (most recent call last):
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1139, in read_packet
    p = self.LLcls(s)
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    scapy.utils.rdpcap(sys.argv[1])
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 951, in rdpcap
    return fdesc.read_all(count=count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1154, in read_all
    res = RawPcapReader.read_all(self, count)
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1094, in read_all
    p = self.read_packet()
  File "/home/user/ven/lib/python3.8/site-packages/scapy/utils.py", line 1147, in read_packet
    p = conf.raw_layer(s)
TypeError: 'NoneType' object is not callable
```

#### Expected result

Not a crash.

#### Related resources

[sample.pcap.zip](https://github.com/secdev/scapy/files/5006905/sample.pcap.zip)




Discussion:

Resolving patch:
diff --git a/scapy/utils.py b/scapy/utils.py
--- a/scapy/utils.py
+++ b/scapy/utils.py
@@ -1127,7 +1127,8 @@ def __init__(self, filename, fdesc, magic):
             self.LLcls = conf.l2types[self.linktype]
         except KeyError:
             warning("PcapReader: unknown LL type [%i]/[%#x]. Using Raw packets" % (self.linktype, self.linktype))  # noqa: E501
-            self.LLcls = conf.raw_layer
+            from scapy.packet import Raw
+            self.LLcls = conf.raw_layer or Raw
 
     def read_packet(self, size=MTU):
         rp = super(PcapReader, self).read_packet(size=size)
@@ -1144,7 +1145,8 @@ def read_packet(self, size=MTU):
                 from scapy.sendrecv import debug
                 debug.crashed_on = (self.LLcls, s)
                 raise
-            p = conf.raw_layer(s)
+            from scapy.packet import Raw
+            p = (conf.raw_layer or Raw)(s)
         power = Decimal(10) ** Decimal(-9 if self.nano else -6)
         p.time = EDecimal(pkt_info.sec + power * pkt_info.usec)
         p.wirelen = pkt_info.wirelen
@@ -1325,7 +1327,8 @@ def read_packet(self, size=MTU):
         except Exception:
             if conf.debug_dissector:
                 raise
-            p = conf.raw_layer(s)
+            from scapy.packet import Raw
+            p = (conf.raw_layer or Raw)(s)
         if tshigh is not None:
             p.time = EDecimal((tshigh << 32) + tslow) / tsresol
         p.wirelen = wirelen


Test patch: