[Pkg-netmeasure-discuss] Bug#887804: python3-scapy: scapy.pton_ntop.inet_pton(socket.AF_INET6, '2000::') fails

Daniel Kahn Gillmor dkg at fifthhorseman.net
Sat Jan 20 04:51:35 UTC 2018


On Fri 2018-01-19 22:26:26 -0500, Daniel Kahn Gillmor wrote:
> In python2:
>
>>>> import scapy.pton_ntop, socket
>>>> scapy.pton_ntop.inet_pton(socket.AF_INET6, '2000::')
> ' \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>>>
>
> But in python3:
>
>>>> import scapy.pton_ntop, socket
>>>> scapy.pton_ntop.inet_pton(socket.AF_INET6, '2000::1')
> hello
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python3/dist-packages/scapy/pton_ntop.py", line 24, in inet_pton
>     while b"::" in addr:
> TypeError: 'in <string>' requires string as left operand, not bytes
>>>> 
>
>
> the "hello" is particularly weird.  it comes from
> /usr/lib/python3/dist-packages/scapy/pton_ntop.py:
>
> def inet_pton(af, addr):
>     """Convert an IP address from text representation into binary form"""
>     print('hello')
>
even worse, this gets triggered whenever creating a series of packets
from a series of addresses in IPv6 on python3.

compare the following simple script in python 2.7 vs. python3:

1 dkg at host:~$ cat test.py
#!/usr/bin/env python3

from scapy.all import IP, IPv6, TCP, sr, conf

examples = {
    'v4single': IP(dst='192.0.2.1',ttl=(1,5))/TCP(dport=80,flags='S'),
    'v4dual': IP(dst=['192.0.2.1','192.0.2.2'],ttl=(1,5))/TCP(dport=80,flags='S'),
    'v6single': IPv6(dst='2001:db8::1',hlim=(1,5))/TCP(dport=80,flags='S'),
    'v6dual': IPv6(dst=['2001:db8::1','2001:db8::2'],hlim=(1,5))/TCP(dport=80,flags='S')
}
for ex in ['v4single', 'v4dual', 'v6single', 'v6dual']:
    print(ex)
    print(examples[ex].summary())

0 dkg at host:~$ ipython test.py
v4single
IP / TCP 192.0.2.254:ftp_data > 192.0.2.1:http S
v4dual
IP / TCP 192.0.2.254:ftp_data > ['192.0.2.1', '192.0.2.2']:http S
v6single
IPv6 / TCP 2001:db8::fffe:ftp_data > 2001:db8::1:http S
v6dual
IPv6 / TCP 2001:db8::fffe:ftp_data > [Net6('2001:db8::1'), Net6('2001:db8::2')]:http S
0 dkg at host:~$ ipython3 test.py
v4single
IP / TCP 192.0.2.254:ftp_data > 192.0.2.1:http S
v4dual
IP / TCP 192.0.2.254:ftp_data > ['192.0.2.1', '192.0.2.2']:http S
v6single
IPv6 / TCP 2001:db8::fffe:ftp_data > 2001:db8::1:http S
v6dual
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/dkg/test.py in <module>()
     11 for ex in ['v4single', 'v4dual', 'v6single', 'v6dual']:
     12     print(ex)
---> 13     print(examples[ex].summary())
     14 

/usr/lib/python3/dist-packages/scapy/packet.py in summary(self, intern)
   1014     def summary(self, intern=0):
   1015         """Prints a one line summary of a packet."""
-> 1016         found,s,needed = self._do_summary()
   1017         return s
   1018 

/usr/lib/python3/dist-packages/scapy/packet.py in _do_summary(self)
    990 
    991     def _do_summary(self):
--> 992         found,s,needed = self.payload._do_summary()
    993         if s:
    994             s = " / "+s

/usr/lib/python3/dist-packages/scapy/packet.py in _do_summary(self)
    995         ret = ""
    996         if not found or self.__class__ in needed:
--> 997             ret = self.mysummary()
    998             if type(ret) is tuple:
    999                 ret,n = ret

/usr/lib/python3/dist-packages/scapy/layers/inet.py in mysummary(self)
    581             return self.underlayer.sprintf("TCP %IP.src%:%TCP.sport% > %IP.dst%:%TCP.dport% %TCP.flags%")
    582         elif conf.ipv6_enabled and isinstance(self.underlayer, scapy.layers.inet6.IPv6):
--> 583             return self.underlayer.sprintf("TCP %IPv6.src%:%TCP.sport% > %IPv6.dst%:%TCP.dport% %TCP.flags%")
    584         else:
    585             return self.sprintf("TCP %TCP.sport% > %TCP.dport% %TCP.flags%")

/usr/lib/python3/dist-packages/scapy/packet.py in sprintf(self, fmt, relax)
    973                         val = getattr(self,fld)
    974                         if fld in self.fieldtype:
--> 975                             val = self.fieldtype[fld].i2repr(self,val)
    976                 else:
    977                     val = self.payload.sprintf("%%%s%%" % sfclsfld, relax)

/usr/lib/python3/dist-packages/scapy/layers/inet6.py in i2repr(self, pkt, x)
    214             return self.i2h(pkt,x)
    215         elif not isinstance(x, Net6) and not type(x) is list:
--> 216             if in6_isaddrTeredo(x):   # print Teredo info
    217                 server, flag, maddr, mport = teredoAddrExtractInfo(x)
    218                 return "%s [Teredo srv: %s cli: %s:%s]" % (self.i2h(pkt, x), server, maddr,mport)

/usr/lib/python3/dist-packages/scapy/utils6.py in in6_isaddrTeredo(x)
    535     format.
    536     """
--> 537     our = inet_pton(socket.AF_INET6, x)[0:4]
    538     teredoPrefix = inet_pton(socket.AF_INET6, conf.teredoPrefix)[0:4]
    539     return teredoPrefix == our

TypeError: inet_pton() argument 2 must be str, not map
1 dkg at host:~$ 




         --dkg



More information about the Pkg-netmeasure-discuss mailing list