From 58f45167b9755a8d134fc43704cee0be4e0f3ae9 Mon Sep 17 00:00:00 2001
From: Pierre LALET <pierre.lalet@cea.fr>
Date: Fri, 29 Sep 2017 23:58:22 +0200
Subject: [PATCH] Python 3: fix DNS

---
 scapy/layers/dns.py | 21 +++++++++------------
 test/dnssecRR.uts   | 28 ++++++++++++++--------------
 test/edns0.uts      | 16 ++++++++--------
 3 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/scapy/layers/dns.py b/scapy/layers/dns.py
index 57807122..7630fb41 100644
--- a/scapy/layers/dns.py
+++ b/scapy/layers/dns.py
@@ -397,9 +397,9 @@ def bitmap2RRlist(bitmap):
             warning("bitmap too short (%i)" % len(bitmap))
             return
 
-        window_block = ord(bitmap[0]) # window number
-        offset = 256*window_block # offset of the Resource Record
-        bitmap_len = ord(bitmap[1]) # length of the bitmap in bytes
+        window_block = orb(bitmap[0]) # window number
+        offset = 256 * window_block # offset of the Resource Record
+        bitmap_len = orb(bitmap[1]) # length of the bitmap in bytes
 
         if bitmap_len <= 0 or bitmap_len > 32:
             warning("bitmap length is no valid (%i)" % bitmap_len)
@@ -411,7 +411,7 @@ def bitmap2RRlist(bitmap):
         for b in range(len(tmp_bitmap)):
             v = 128
             for i in range(8):
-                if ord(tmp_bitmap[b]) & v:
+                if orb(tmp_bitmap[b]) & v:
                     # each of the RR is encoded as a bit
                     RRlist += [ offset + b*8 + i ]
                 v = v >> 1
@@ -431,10 +431,8 @@ def RRlist2bitmap(lst):
 
     import math
 
-    bitmap = ""
-    lst = sorted(set(lst))
-
-    lst = [abs(x) for x in lst if x <= 65535]
+    bitmap = b""
+    lst = [abs(x) for x in sorted(set(lst)) if x <= 65535]
 
     # number of window blocks
     max_window_blocks = int(math.ceil(lst[-1] / 256.))
@@ -446,7 +444,7 @@ def RRlist2bitmap(lst):
         # First, filter out RR not encoded in the current window block
         # i.e. keep everything between 256*wb <= 256*(wb+1)
         rrlist = sorted(x for x in lst if 256 * wb <= x < 256 * (wb + 1))
-        if rrlist == []:
+        if not rrlist:
             continue
 
         # Compute the number of bytes used to store the bitmap
@@ -454,12 +452,11 @@ def RRlist2bitmap(lst):
             bytes_count = 1
         else:
             max = rrlist[-1] - 256*wb
-            bytes_count = int(math.ceil(max / 8)) + 1  # use at least 1 byte
+            bytes_count = int(math.ceil(max // 8)) + 1  # use at least 1 byte
         if bytes_count > 32: # Don't encode more than 256 bits / values
             bytes_count = 32
 
-        bitmap += struct.pack("B", wb)
-        bitmap += struct.pack("B", bytes_count)
+        bitmap += struct.pack("BB", wb, bytes_count)
 
         # Generate the bitmap
 	# The idea is to remove out of range Resource Records with these steps
diff --git a/test/dnssecRR.uts b/test/dnssecRR.uts
index becb1905..4069fbee 100644
--- a/test/dnssecRR.uts
+++ b/test/dnssecRR.uts
@@ -46,41 +46,41 @@ RRlist2bitmap(bitmap2RRlist(b)) == b
 
 = DNSRRNSEC(), basic instanciation
 t = DNSRRNSEC()
-str(t) == b'\x00\x00/\x00\x01\x00\x00\x00\x00\x00\x01\x00'
+raw(t) == b'\x00\x00/\x00\x01\x00\x00\x00\x00\x00\x01\x00'
 
 = DNSRRRNSEC(), check parameters
 t = DNSRRNSEC(rrname="scapy.secdev.org.", rclass=42, ttl=28, nextname="www.secdev.org.", typebitmaps=RRlist2bitmap([1,2,3,4,1234]))
-str(t) == b'\x05scapy\x06secdev\x03org\x00\x00/\x00*\x00\x00\x00\x1c\x000\x03www\x06secdev\x03org\x00\x00\x01x\x04\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 '
+raw(t) == b'\x05scapy\x06secdev\x03org\x00\x00/\x00*\x00\x00\x00\x1c\x000\x03www\x06secdev\x03org\x00\x00\x01x\x04\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 '
 
 + Test NSEC3 RR
 
 = DNSRRNSEC3(), basic instanciation
 t = DNSRRNSEC3()
-str(t) == b'\x00\x002\x00\x01\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00'
+raw(t) == b'\x00\x002\x00\x01\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00'
 
 = DNSRRRNSEC3(), check parameters
 t = DNSRRNSEC3(rrname="scapy.secdev.org.", rclass=42, ttl=28, hashalg=7, iterations=80, saltlength=28, salt=b"\x28\x07", hashlength=31, nexthashedownername="XXX.scapy.secdev.org", typebitmaps=RRlist2bitmap([1,2,3,4,1234]))
-str(t) == b'\x05scapy\x06secdev\x03org\x00\x002\x00*\x00\x00\x00\x1c\x00<\x07\x00\x00P\x1c(\x07\x1fXXX.scapy.secdev.org\x00\x01x\x04\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 '
+raw(t) == b'\x05scapy\x06secdev\x03org\x00\x002\x00*\x00\x00\x00\x1c\x00<\x07\x00\x00P\x1c(\x07\x1fXXX.scapy.secdev.org\x00\x01x\x04\x1b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 '
 
 + Test NSEC3PARAM RR
 
 = DNSRRNSEC3PARAM(), basic instanciation
 t = DNSRRNSEC3PARAM()
-str(t) == b'\x00\x003\x00\x01\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00'
+raw(t) == b'\x00\x003\x00\x01\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00'
 
 = DNSRRRNSEC3PARAM(), check parameters
 t = DNSRRNSEC3(rrname="scapy.secdev.org.", rclass=42, ttl=28, hashalg=7, flags=80, iterations=80, saltlength=28, salt=b"\x28\x07")
-str(t) == b'\x05scapy\x06secdev\x03org\x00\x002\x00*\x00\x00\x00\x1c\x00\x08\x07P\x00P\x1c(\x07\x00'
+raw(t) == b'\x05scapy\x06secdev\x03org\x00\x002\x00*\x00\x00\x00\x1c\x00\x08\x07P\x00P\x1c(\x07\x00'
 
 + Test RRSIG RR
 
 = DNSRRRSIG(), basic instanciation
 t = DNSRRRSIG()
-str(t) == b'\x00\x00.\x00\x01\x00\x00\x00\x00\x00\x13\x00\x01\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
+raw(t) == b'\x00\x00.\x00\x01\x00\x00\x00\x00\x00\x13\x00\x01\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
 
 = DNSRRRSIG(), check parameters
 t = DNSRRRSIG(rrname="test.example.com.", type=46, rclass=12, ttl=64, originalttl=2807, keytag=42, signersname="test.rsig", signature="test RSIG")
-str(t) == b'\x04test\x07example\x03com\x00\x00.\x00\x0c\x00\x00\x00@\x00&\x00\x01\x05\x00\x00\x00\n\xf7\x00\x00\x00\x00\x00\x00\x00\x00\x00*\x04test\x04rsig\x00test RSIG'
+raw(t) == b'\x04test\x07example\x03com\x00\x00.\x00\x0c\x00\x00\x00@\x00&\x00\x01\x05\x00\x00\x00\n\xf7\x00\x00\x00\x00\x00\x00\x00\x00\x00*\x04test\x04rsig\x00test RSIG'
 
 = DNSRRRSIG(), dissection
 rrsig = b'\x03isc\x03org\x00\x00.\x00\x01\x00\x00\x96O\x00\x9b\x00\x02\x05\x02\x00\x00\xa8\xc0K-3\xd9K\x05\xa6\xd9\xed6\x03isc\x03org\x00\xac\xb2_I\x9e\xdcU\xca/3\x1c\xdf{\xba\xd5\x80\xb0 \xa4~\x98\x95\xab~\x84\xb2\x1f9\x17#\x7f\xfeP\xb9\xfb\x8d\x13\x19\xd7\x7f\x9e/\x1c\xd7rv<\xc6\xd3\xf1\xae8\rh\xba\x1e\xaa\xe6\xf1\x1e\x1d\xdaS\xd4\\\xfd\xa3`P\xa1\xe0\xa2\x860\xd4?\xb4}j\x81O\x03\xdc&v\x13\xd4(k\xa07\x8f-\x08e\x06\xff\xb8h\x8f\x16j\xe4\xd92\xd2\x99\xc2\xb4'
@@ -91,11 +91,11 @@ t.rrname == 'isc.org.' and t.labels == 2 and t.keytag == 60726 and t.signature[-
 
 = DNSRRDNSKEY(), basic instanciation
 t = DNSRRDNSKEY()
-str(t) == b'\x00\x000\x00\x01\x00\x00\x00\x00\x00\x04\x01\x00\x03\x05' and t.sprintf("%flags%") == 'Z'
+raw(t) == b'\x00\x000\x00\x01\x00\x00\x00\x00\x00\x04\x01\x00\x03\x05' and t.sprintf("%flags%") == 'Z'
 
 = DNSRRDNSKEY(), check parameters
 t = DNSRRDNSKEY(rrname="www.secdev.org.", type=42, rclass=12, ttl=1234, rdlen=567, flags=2807, protocol=195, algorithm=66, publickey="strong public key")
-str(t) == b'\x03www\x06secdev\x03org\x00\x00*\x00\x0c\x00\x00\x04\xd2\x027\n\xf7\xc3Bstrong public key'
+raw(t) == b'\x03www\x06secdev\x03org\x00\x00*\x00\x0c\x00\x00\x04\xd2\x027\n\xf7\xc3Bstrong public key'
 
 = DNSRRDNSKEY(), dissection
 t = DNSRRDNSKEY(b'\x03dlv\x03isc\x03org\x00\x000\x00\x01\x00\x00\x1bq\x01\t\x01\x01\x03\x05\x04@\x00\x00\x03\xc72\xef\xf9\xa2|\xeb\x10N\xf3\xd5\xe8&\x86\x0f\xd6<\xed>\x8e\xea\x19\xadm\xde\xb9a\'\xe0\xccC\x08M~\x94\xbc\xb6n\xb8P\xbf\x9a\xcd\xdfdJ\xb4\xcc\xd7\xe8\xc8\xfb\xd27sx\xd0\xf8^I\xd6\xe7\xc7g$\xd3\xc2\xc6\x7f>\x8c\x01\xa5\xd8VK+\xcb~\xd6\xea\xb8[\xe9\xe7\x03z\x8e\xdb\xe0\xcb\xfaN\x81\x0f\x89\x9e\xc0\xc2\xdb!\x81p{C\xc6\xeft\xde\xf5\xf6v\x90\x96\xf9\xe9\xd8`1\xd7\xb9\xcae\xf8\x04\x8f\xe8C\xe7\x00+\x9d?\xc6\xf2o\xd3Ak\x7f\xc90\xea\xe7\x0cO\x01e\x80\xf7\xbe\x8eq\xb1<\xf1&\x1c\x0b^\xfdDdc\xad\x99~B\xe8\x04\x00\x03,t="\xb4\xb6\xb6\xbc\x80{\xb9\x9b\x05\x95\\;\x02\x1eS\xf4p\xfedq\xfe\xfc00$\xe05\xba\x0c@\xabTv\xf3W\x0e\xb6\t\r!\xd9\xc2\xcd\xf1\x89\x15\xc5\xd5\x17\xfej_T\x99\x97\xd2j\xff\xf85b\xca\x8c|\xe9O\x9fd\xfdT\xadL3taK\x96\xac\x13a')
@@ -106,7 +106,7 @@ t.rrname == "dlv.isc.org." and t.rdlen == 265 and t.sprintf("%flags%") == 'SZ' a
 = DNSRRDS() and DNSRRDLV(), basic instancaition
 ds = DNSRRDS()
 dlv = DNSRRDLV(type=43)
-str(ds) == str(dlv)
+raw(ds) == raw(dlv)
 
 = DNSRRDS(), check parameters
 t = DNSRRDS(b'\x03isc\x03org\x00\x00+\x00\x01\x00\x01Q(\x00\x182\\\x05\x01\x98!\x13\xd0\x8bLj\x1d\x9fj\xee\x1e"7\xae\xf6\x9f?\x97Y')
@@ -119,17 +119,17 @@ t = DNSRR(type="TXT", rdata="test")
 
 = DNSRRR(), check parameters
 t = DNSRR(b'\x04test\x00\x00\x10\x00\x01\x00\x00\x00\x00\x018\xffScapy is an interactive packet manipulation program that enables you to sniff, mangle, send network packets ; test equipments ; probe and discover networks ; quickly develop new protocols. It can easily handle most classical tasks like scanning, tracerout7ing, probing, unit tests, attacks or network discovery.')
-t.type == 16 and t.rdlen == 312 and t.rdata[:5] == "Scapy" and t.rdata[-10:] == "discovery."
+t.type == 16 and t.rdlen == 312 and t.rdata[:5] == b"Scapy" and t.rdata[-10:] == b"discovery."
 
 + Test DNSRRTSIG RR
 
 = DNSRRTSIG basic instanciation
 t = DNSRRTSIG()
-str(t) == b"\x00\x00\xfa\x00\x01\x00\x00\x00\x00\x00\x1b\thmac-sha1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00"
+raw(t) == b"\x00\x00\xfa\x00\x01\x00\x00\x00\x00\x00\x1b\thmac-sha1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00"
 
 = DNSRRTSIG(), check parameters
 t = DNSRRTSIG(rrname="SAMPLE-ALG.EXAMPLE.", time_signed=853804800, fudge=300)
-str(t) == b"\nSAMPLE-ALG\x07EXAMPLE\x00\x00\xfa\x00\x01\x00\x00\x00\x00\x00\x1b\thmac-sha1\x00\x00\x002\xe4\x07\x00\x01,\x00\x14\x00\x00\x00\x00\x00\x00"
+raw(t) == b"\nSAMPLE-ALG\x07EXAMPLE\x00\x00\xfa\x00\x01\x00\x00\x00\x00\x00\x1b\thmac-sha1\x00\x00\x002\xe4\x07\x00\x01,\x00\x14\x00\x00\x00\x00\x00\x00"
 
 = TimeField methods
 
diff --git a/test/edns0.uts b/test/edns0.uts
index 927ccd6e..1490418a 100644
--- a/test/edns0.uts
+++ b/test/edns0.uts
@@ -7,15 +7,15 @@
 
 = EDNS0TLV(), basic instanciation
 tlv = EDNS0TLV()
-str(tlv) == b'\x00\x00\x00\x00'
+raw(tlv) == b'\x00\x00\x00\x00'
 
 = EDNS0TLV(), check parameters
 tlv = EDNS0TLV(optcode=42, optlen=12, optdata="edns0tlv")
-str(tlv) == b'\x00*\x00\x0cedns0tlv'
+raw(tlv) == b'\x00*\x00\x0cedns0tlv'
 
 = EDNS0TLV(), check computed optlen
 tlv = EDNS0TLV(optdata="edns0tlv")
-str(tlv) == b'\x00\x00\x00\x08edns0tlv'
+raw(tlv) == b'\x00\x00\x00\x08edns0tlv'
 
 = EDNS0TLV(), dissection
 tlv = EDNS0TLV(b'\x00*\x00\x08edns0tlv')
@@ -25,15 +25,15 @@ tlv.optcode == 42 and tlv.optlen == 8 and tlv.optdata == "edns0tlv"
 
 = DNSRROPT(), basic instanciation
 opt = DNSRROPT()
-str(opt) == b'\x00\x00)\x10\x00\x00\x00\x80\x00\x00\x00'
+raw(opt) == b'\x00\x00)\x10\x00\x00\x00\x80\x00\x00\x00'
 
 = DNSRROPT(), check parameters
 opt = DNSRROPT(rrname="rropt", type=42, rclass=123, extrcode=1, version=2, z=3, rdlen=4, rdata=[EDNS0TLV()])
-str(opt) == b'\x05rropt\x00\x00*\x00{\x01\x02\x00\x03\x00\x04\x00\x00\x00\x00'
+raw(opt) == b'\x05rropt\x00\x00*\x00{\x01\x02\x00\x03\x00\x04\x00\x00\x00\x00'
 
 = DNSRROPT() & EDN0TLV(), check parameters
 opt = DNSRROPT(rrname="rropt", type=42, rclass=123, extrcode=1, version=2, z=3, rdlen=4, rdata=[EDNS0TLV(optcode=42, optlen=12, optdata="edns0tlv")])
-str(opt) == b'\x05rropt\x00\x00*\x00{\x01\x02\x00\x03\x00\x04\x00*\x00\x0cedns0tlv'
+raw(opt) == b'\x05rropt\x00\x00*\x00{\x01\x02\x00\x03\x00\x04\x00*\x00\x0cedns0tlv'
 
 = DNSRROP(), dissection
 opt = DNSRROPT(b'\x05rropt\x00\x00*\x00{\x01\x02\x00\x03\x00\x0c\x00*\x00\x0cedns0tlv')
@@ -43,7 +43,7 @@ opt.rrname == "rropt." and opt.rdlen == 12 and opt.rdata[0].optcode == 42 and op
 
 = EDNS-PING - basic instanciation
 tlv = EDNS0TLV(optcode=5, optdata=b"\x00\x11\x22\x33")
-str(tlv) == b'\x00\x05\x00\x04\x00\x11"3'
+raw(tlv) == b'\x00\x05\x00\x04\x00\x11"3'
 
 #= EDNS-PING - Live test
 #~ netaccess
@@ -58,7 +58,7 @@ str(tlv) == b'\x00\x05\x00\x04\x00\x11"3'
 
 = NSID- basic instanciation
 tlv = EDNS0TLV(optcode=2, optdata="")
-str(tlv) == b'\x00\x02\x00\x00'
+raw(tlv) == b'\x00\x02\x00\x00'
 
 = NSID - Live test
 ~ netaccess
-- 
GitLab