Skip to content

Commit

Permalink
No-debug-dissector in UTscapy - stabilize (#3563)
Browse files Browse the repository at this point in the history
* No-debug-dissector in UTScapy - stabilize nmap

* Disable tox --paralell on windows
  • Loading branch information
gpotter2 authored May 2, 2022
1 parent 1fc4543 commit 53c764d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions scapy/tools/UTscapy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #


Expand Down Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions test/linux.uts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
16 changes: 12 additions & 4 deletions test/nmap.uts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,15 +65,19 @@ 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)

= 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

Expand Down
54 changes: 20 additions & 34 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -1649,35 +1641,29 @@ 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)

= 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)

= 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)
Expand Down
6 changes: 2 additions & 4 deletions test/scapy/layers/dns_edns0.uts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 2 additions & 4 deletions test/scapy/layers/radius.uts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 53c764d

Please sign in to comment.