From 53c764d71147497f8088d30a441f5dd7a4e9be57 Mon Sep 17 00:00:00 2001 From: gpotter2 Date: Mon, 2 May 2022 10:51:13 +0200 Subject: [PATCH] No-debug-dissector in UTscapy - stabilize (#3563) * No-debug-dissector in UTScapy - stabilize nmap * Disable tox --paralell on windows --- .appveyor.yml | 2 +- scapy/tools/UTscapy.py | 12 ++++++++ test/linux.uts | 7 ++--- test/nmap.uts | 16 +++++++--- test/regression.uts | 54 ++++++++++++--------------------- test/scapy/layers/dns_edns0.uts | 6 ++-- test/scapy/layers/radius.uts | 6 ++-- test/tls.uts | 36 +++++++--------------- 8 files changed, 63 insertions(+), 76 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index fa2c97e6f..e92812742 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -66,7 +66,7 @@ test_script: - set TOX_PARALLEL_NO_SPINNER=1 # Main unit tests - - "%PYTHON%\\python -m tox --parallel -- %UT_FLAGS%" + - "%PYTHON%\\python -m tox -- %UT_FLAGS%" after_test: # Run codecov diff --git a/scapy/tools/UTscapy.py b/scapy/tools/UTscapy.py index 0b5da233d..f02fcbfa4 100644 --- a/scapy/tools/UTscapy.py +++ b/scapy/tools/UTscapy.py @@ -93,6 +93,17 @@ def scapy_path(fname): os.path.dirname(__file__), '../../', fname )) + +class no_debug_dissector: + """Context object used to disable conf.debug_dissector""" + def __enter__(self): + self.old_dbg = conf.debug_dissector + conf.debug_dissector = False + + def __exit__(self, exc_type, exc_value, traceback): + conf.debug_dissector = self.old_dbg + + # Import tool # @@ -570,6 +581,7 @@ def import_UTscapy_tools(ses): ses["Bunch"] = Bunch ses["retry_test"] = retry_test ses["scapy_path"] = scapy_path + ses["no_debug_dissector"] = no_debug_dissector if WINDOWS: from scapy.arch.windows import _route_add_loopback _route_add_loopback() diff --git a/test/linux.uts b/test/linux.uts index 1a9733289..bd1bc2245 100644 --- a/test/linux.uts +++ b/test/linux.uts @@ -12,11 +12,10 @@ ~ netaccess IP TCP linux needs_root old_l3socket = conf.L3socket -old_debug_dissector = conf.debug_dissector -conf.debug_dissector = False conf.L3socket = L3RawSocket -x = sr1(IP(dst="www.google.com")/TCP(sport=RandShort(), dport=80, flags="S"),timeout=3) -conf.debug_dissector = old_debug_dissector +with no_debug_dissector(): + x = sr1(IP(dst="www.google.com")/TCP(sport=RandShort(), dport=80, flags="S"),timeout=3) + conf.L3socket = old_l3socket x assert x[IP].ottl() in [32, 64, 128, 255] diff --git a/test/nmap.uts b/test/nmap.uts index 8003b5f24..d71c86b13 100644 --- a/test/nmap.uts +++ b/test/nmap.uts @@ -44,14 +44,18 @@ assert len(conf.nmap_kdb.get_base()) > 100 = fingerprint test: www.secdev.org ~ netaccess needs_root -score, fprint = nmap_fp('www.secdev.org') +with no_debug_dissector(): + score, fprint = nmap_fp('www.secdev.org') + print(score, fprint) assert score > 0.5 assert fprint = fingerprint test: gateway ~ netaccess needs_root -score, fprint = nmap_fp(conf.route.route('0.0.0.0')[2]) +with no_debug_dissector(): + score, fprint = nmap_fp(conf.route.route('0.0.0.0')[2]) + print(score, fprint) assert score > 0.5 assert fprint @@ -61,7 +65,9 @@ assert fprint import re as re_ -a = nmap_sig("www.secdev.org", 80, 81) +with no_debug_dissector(): + a = nmap_sig("www.secdev.org", 80, 81) + a for x in nmap_sig2txt(a).split("\n"): assert re_.match(r"\w{2,4}\(.*\)", x) @@ -69,7 +75,9 @@ for x in nmap_sig2txt(a).split("\n"): = nmap_udppacket_sig test: www.google.com ~ netaccess needs_root -a = nmap_sig("www.google.com", ucport=80) +with no_debug_dissector(): + a = nmap_sig("www.google.com", ucport=80) + assert len(a) > 3 assert len(a["PU"]) > 0 diff --git a/test/regression.uts b/test/regression.uts index cb89d79a2..94649cc6c 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -1365,10 +1365,8 @@ assert ssid == "ROUTE-821E295" = Sending and receiving an ICMP ~ netaccess needs_root IP ICMP icmp_firewall def _test(): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - x = sr1(IP(dst="www.google.com")/ICMP(),timeout=3) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + x = sr1(IP(dst="www.google.com")/ICMP(),timeout=3) x assert x[IP].ottl() in [32, 64, 128, 255] assert 0 <= x[IP].hops() <= 126 @@ -1451,14 +1449,12 @@ retry_test(_test) from functools import partial # flooding methods do not support timeout. Packing the test for security def _test_flood(ip, flood_function, add_ether=False): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - p = IP(dst=ip)/TCP(sport=RandShort(), dport=80, flags="S") - if add_ether: - p = Ether()/p - p.show2() - x = flood_function(p, timeout=0.5, maxretries=10) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + p = IP(dst=ip)/TCP(sport=RandShort(), dport=80, flags="S") + if add_ether: + p = Ether()/p + p.show2() + x = flood_function(p, timeout=0.5, maxretries=10) if type(x) == tuple: x = x[0][0][1] x @@ -1495,10 +1491,8 @@ finally: = Sending and receiving an ICMPv6EchoRequest ~ netaccess ipv6 def _test(): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - x = sr1(IPv6(dst="www.google.com")/ICMPv6EchoRequest(),timeout=3) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + x = sr1(IPv6(dst="www.google.com")/ICMPv6EchoRequest(),timeout=3) x assert x[IPv6].ottl() in [32, 64, 128, 255] assert 0 <= x[IPv6].hops() <= 126 @@ -1630,10 +1624,8 @@ assert a.sent_time is None = Port scan ~ netaccess needs_root IP TCP def _test(): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - ans,unans=sr(IP(dst="www.google.com/30")/TCP(dport=[80,443]), timeout=2) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + ans,unans=sr(IP(dst="www.google.com/30")/TCP(dport=[80,443]), timeout=2) # Backward compatibility: Python 2 only if six.PY2: @@ -1649,13 +1641,11 @@ retry_test(_test) def _test(): old_debug_match = conf.debug_match conf.debug_match = True - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - ans, unans = sr(IP(dst="www.google.fr") / TCP(sport=RandShort(), dport=80, flags="S"), timeout=2) - assert ans[0].query == ans[0][0] - assert ans[0].answer == ans[0][1] + with no_debug_dissector(): + ans, unans = sr(IP(dst="www.google.fr") / TCP(sport=RandShort(), dport=80, flags="S"), timeout=2) + assert ans[0].query == ans[0][0] + assert ans[0].answer == ans[0][1] conf.debug_match = old_debug_match - conf.debug_dissector = old_debug_dissector assert ans and not unans retry_test(_test) @@ -1663,10 +1653,8 @@ retry_test(_test) = Send & receive with retry ~ netaccess needs_root IP ICMP def _test(): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - ans, unans = sr(IP(dst=["8.8.8.8", "1.2.3.4"]) / TCP(sport=RandShort(), dport=53, flags="S"), timeout=2, retry=1) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + ans, unans = sr(IP(dst=["8.8.8.8", "1.2.3.4"]) / TCP(sport=RandShort(), dport=53, flags="S"), timeout=2, retry=1) assert len(ans) == 1 and len(unans) == 1 retry_test(_test) @@ -1674,10 +1662,8 @@ retry_test(_test) = Send & receive with multi ~ netaccess needs_root IP ICMP def _test(): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - ans, unans = sr(IP(dst=["8.8.8.8", "1.2.3.4"]) / TCP(sport=RandShort(), dport=53, flags="S"), timeout=2, multi=1) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + ans, unans = sr(IP(dst=["8.8.8.8", "1.2.3.4"]) / TCP(sport=RandShort(), dport=53, flags="S"), timeout=2, multi=1) assert len(ans) >= 1 and len(unans) == 1 retry_test(_test) diff --git a/test/scapy/layers/dns_edns0.uts b/test/scapy/layers/dns_edns0.uts index ba225bf1d..d35871b9c 100644 --- a/test/scapy/layers/dns_edns0.uts +++ b/test/scapy/layers/dns_edns0.uts @@ -64,10 +64,8 @@ raw(tlv) == b'\x00\x02\x00\x00' ~ netaccess needs_root def _test(): - old_debug_dissector = conf.debug_dissector - conf.debug_dissector = False - r = sr1(IP(dst="l.root-servers.net")/UDP()/DNS(qd=[DNSQR(qtype="SOA", qname=".")], ar=[DNSRROPT(z=0, rdata=[EDNS0TLV(optcode="NSID")])]), timeout=1) - conf.debug_dissector = old_debug_dissector + with no_debug_dissector(): + r = sr1(IP(dst="l.root-servers.net")/UDP()/DNS(qd=[DNSQR(qtype="SOA", qname=".")], ar=[DNSRROPT(z=0, rdata=[EDNS0TLV(optcode="NSID")])]), timeout=1) len(r.ar) and DNSRROPT in r.ar and len(r.ar[DNSRROPT].rdata) and len([x for x in r.ar[DNSRROPT].rdata if x.optcode == 3]) retry_test(_test) diff --git a/test/scapy/layers/radius.uts b/test/scapy/layers/radius.uts index 325b8513e..5c8d25421 100644 --- a/test/scapy/layers/radius.uts +++ b/test/scapy/layers/radius.uts @@ -221,12 +221,10 @@ assert pkt.attributes[2].type == 24 assert pkt.attributes[2].len == 18 conf.contribs.setdefault("radius", {})["auto-defrag"] = False -_od = conf.debug_dissector -conf.debug_dissector = False -pkt = Radius(s) +with no_debug_dissector(): + pkt = Radius(s) -conf.debug_dissector = _od assert len(pkt.attributes) == 4 assert pkt.attributes[0].type == 79 assert pkt.attributes[1].type == 79 diff --git a/test/tls.uts b/test/tls.uts index a676d5320..3161b307b 100644 --- a/test/tls.uts +++ b/test/tls.uts @@ -1104,11 +1104,9 @@ from scapy.layers.tls.record import _TLSMsgListField # Unknown type assert isinstance(_TLSMsgListField.m2i(_TLSMsgListField("", []), TLS(type=0), b'\x00\x03\x03\x00\x03abc'), Raw) -old_debug_dissector = conf.debug_dissector -conf.debug_dissector = False -# not even bytes to make it crash -assert isinstance(_TLSMsgListField.m2i(_TLSMsgListField("", []), TLS(type=20), 1), Raw) -conf.debug_dissector = old_debug_dissector +with no_debug_dissector(): + # not even bytes to make it crash + assert isinstance(_TLSMsgListField.m2i(_TLSMsgListField("", []), TLS(type=20), 1), Raw) = Test x25519 dissection in ServerKeyExchange @@ -1350,13 +1348,9 @@ test_tls_without_cryptography() = Truncated TCP segment -dd = conf.debug_dissector -conf.debug_dissector = False - -pkt = Ether(hex_bytes('00155dfb587a00155dfb58430800450005dc54d3400070065564400410d40a00000d01bb044e8b86744e16063ac45010faf06ba9000016030317c30200005503035cb336a067d53a5d2cedbdfec666ac740afbd0637ddd13eddeab768c3c63abee20981a0000d245f1c905b329323ad67127cd4b907a49f775c331d0794149aca7cdc02800000d0005000000170000ff010001000b000ec6000ec300090530820901308206e9a00302010202132000036e72aded906765595fae000000036e72300d06092a864886f70d01010b050030818b310b30090603550406130255533113')) -assert TLSServerHello in pkt - -conf.debug_dissector = dd +with no_debug_dissector(): + pkt = Ether(hex_bytes('00155dfb587a00155dfb58430800450005dc54d3400070065564400410d40a00000d01bb044e8b86744e16063ac45010faf06ba9000016030317c30200005503035cb336a067d53a5d2cedbdfec666ac740afbd0637ddd13eddeab768c3c63abee20981a0000d245f1c905b329323ad67127cd4b907a49f775c331d0794149aca7cdc02800000d0005000000170000ff010001000b000ec6000ec300090530820901308206e9a00302010202132000036e72aded906765595fae000000036e72300d06092a864886f70d01010b050030818b310b30090603550406130255533113')) + assert TLSServerHello in pkt ############################################################################### ########################### TLS Misc tests #################################### @@ -1482,13 +1476,9 @@ assert raw(p) == a = Issue 2763 -dd = conf.debug_dissector -conf.debug_dissector = False - -p = Ether(b'RU\x10\x00\x02\x02RT\x00\x124V\x08\x00E\x00\x05\xc8\r\xd8\x00\x00@\x06\x96\x9d\x9c&\xce\x12\xc0\xa8\xa5\xd9\x01\xbb\xc0\x1f\x00w$\x02\x03\xbe\xc5#P\x10#(\x0b\x9e\x00\x00\x16\x03\x03\x0e4\x02\x00\x00M\x03\x03^\xfa\xb5~\x88\xdf\xdc#}\'\xa0\xff\xa2\xe2\xb5\xec\x0e\x93\xa8\xe0\xde\x01[\x13[F\x151 x\xc6\xcc `)\x00\x00\x8aZ\x90l\xda\x0b\xe1\xec[i\x13\xa7\x8e\xb9a\x98"\x8a7L\x9d\x90\xe0\x01\x06c$9\xc0\'\x00\x00\x05\xff\x01\x00\x01\x00\x0b\x00\x0c\x8e\x00\x0c\x8b\x00\x06n0\x82\x06j0\x82\x05R\xa0\x03\x02\x01\x02\x02\x10EY\xe8\x1c\x1e\x9a\xe0?X\xaa\xc3\xbc\xcd`jh0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x0b\x05\x000\x81\x8f1\x0b0\t\x06\x03U\x04\x06\x13\x02GB1\x1b0\x19\x06\x03U\x04\x08\x13\x12Greater Manchester1\x100\x0e\x06\x03U\x04\x07\x13\x07Salford1\x180\x16\x06\x03U\x04\n\x13\x0fSectigo Limited1705\x06\x03U\x04\x03\x13.Sectigo RSA Domain Validation Secure Server CA0\x1e\x17\r190309000000Z\x17\r210308235959Z0W1!0\x1f\x06\x03U\x04\x0b\x13\x18Domain Control Validated1\x1d0\x1b\x06\x03U\x04\x0b\x13\x14PositiveSSL Wildcard1\x130\x11\x06\x03U\x04\x03\x0c\n*.mql5.net0\x82\x01"0\r\x06\t*\x86H\x86\xf7\r\x01\x01\x01\x05\x00\x03\x82\x01\x0f\x000\x82\x01\n\x02\x82\x01\x01\x00\xcb\xbcn=\xbaGd\xe1XB\x07\xc9\xb1\xc8/\x86\xaa4Z\xbdNk\xfb\xffR\x8f\xe4\x1c^\x91m8\xb9^\x97\xa5\xd3N\xfb\x80\x92\x8ap\xda\x15\x9f\xee\xe7\xb3\xc8?\xb0>~\xaa\x07\x91\xb1\x99q\xe2\xe5\xc8\x9b\x1d5\xa0\x96,\x98\xdaW\x93\x95\x8e%\xe8\xd4L\xeb\xcbSg\x15"\xba\xb7\xc7\x1f\xe9\xd6\x1a\xe6E\x1d\xc8\x1e%\xd36\xe0/r\xd1\xce1C\xce\x91&\xa1\x08*R\xbf\x8cu\xb0\xda\x0e\x1e2\xd66\x1df&3\x9b\x03\x0b\xcam:\xf7\x12\xd9ud(\xae\xdc\xbci\x85\xbd\xcf\xeb{\x15:\xbd\x0e\x11\x1bi\xd8\xff]y~E\x15\x95\xee\xe9\xea\xc6Cr~\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