From 162ad765d2d07e898389e9e7cff3cdcbddb2b9f2 Mon Sep 17 00:00:00 2001 From: kbandla Date: Wed, 31 May 2017 22:07:10 -0400 Subject: [PATCH 1/3] On windows, check if the file is opened in binary mode --- dpkt/pcap.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dpkt/pcap.py b/dpkt/pcap.py index d2854afc..1fb4c9c3 100644 --- a/dpkt/pcap.py +++ b/dpkt/pcap.py @@ -197,6 +197,8 @@ class Writer(object): """ def __init__(self, fileobj, snaplen=1500, linktype=DLT_EN10MB, nano=False): + if fileobj.mode != 'wb': + raise ValueError('PCAP file not in binary mode (rb)') self.__f = fileobj self._precision = 9 if nano else 6 magic = TCPDUMP_MAGIC_NANO if nano else TCPDUMP_MAGIC @@ -240,6 +242,9 @@ class Reader(object): def __init__(self, fileobj): self.name = getattr(fileobj, 'name', '<%s>' % fileobj.__class__.__name__) + if sys.platform.startswith('win'): + if fileobj.mode != 'rb': + raise ValueError('PCAP file (%s) not opened in binary mode (rb)'%self.name) self.__f = fileobj buf = self.__f.read(FileHdr.__hdr_len__) self.__fh = FileHdr(buf) From fad3d74548ff73e73a367c623e25fa09bde499b5 Mon Sep 17 00:00:00 2001 From: kbandla Date: Wed, 31 May 2017 22:34:18 -0400 Subject: [PATCH 2/3] Fix typo for pcap.Writer --- dpkt/pcap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpkt/pcap.py b/dpkt/pcap.py index 1fb4c9c3..2df27940 100644 --- a/dpkt/pcap.py +++ b/dpkt/pcap.py @@ -198,7 +198,7 @@ class Writer(object): def __init__(self, fileobj, snaplen=1500, linktype=DLT_EN10MB, nano=False): if fileobj.mode != 'wb': - raise ValueError('PCAP file not in binary mode (rb)') + raise ValueError('PCAP file NOT in binary mode (wb)') self.__f = fileobj self._precision = 9 if nano else 6 magic = TCPDUMP_MAGIC_NANO if nano else TCPDUMP_MAGIC From bdc50271d9696c86f42fa5cad19d40eb876a2813 Mon Sep 17 00:00:00 2001 From: Kiran Bandla Date: Tue, 22 Mar 2022 01:04:03 -0400 Subject: [PATCH 3/3] Expand checks to all OS While testing the PR some more, I noticed that python3 on Linux did not open in binary mode when we do no pass 'b'. Therefore it seemed appropriate to expand this check to all OS, and not just windows. --- dpkt/pcap.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dpkt/pcap.py b/dpkt/pcap.py index 2df27940..c709c157 100644 --- a/dpkt/pcap.py +++ b/dpkt/pcap.py @@ -197,7 +197,7 @@ class Writer(object): """ def __init__(self, fileobj, snaplen=1500, linktype=DLT_EN10MB, nano=False): - if fileobj.mode != 'wb': + if 'b' not in fileobj.mode: raise ValueError('PCAP file NOT in binary mode (wb)') self.__f = fileobj self._precision = 9 if nano else 6 @@ -242,9 +242,8 @@ class Reader(object): def __init__(self, fileobj): self.name = getattr(fileobj, 'name', '<%s>' % fileobj.__class__.__name__) - if sys.platform.startswith('win'): - if fileobj.mode != 'rb': - raise ValueError('PCAP file (%s) not opened in binary mode (rb)'%self.name) + if 'b' not in fileobj.mode: + raise ValueError('PCAP file (%s) not opened in binary mode (rb)' % self.name) self.__f = fileobj buf = self.__f.read(FileHdr.__hdr_len__) self.__fh = FileHdr(buf)