• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1% Regression tests for Scapy Nmap module
2
3~ nmap
4
5############
6############
7+ Basic Nmap OS fingerprints tests
8
9= Module loading
10load_module('nmap')
11
12= Test functions
13
14d = nmap_udppacket_sig(IP()/UDP(), IP(raw(IP()/ICMP(type=3, code=2)/IPerror()/UDPerror())))
15assert len(d) == 9
16
17d = nmap_tcppacket_sig(IP()/TCP())
18assert len(d) == 5
19
20= Fetch database
21~ netaccess
22
23try:
24    from urllib.request import urlopen
25except ImportError:
26    from urllib2 import urlopen
27
28filename = 'nmap-os-fingerprints' + str(RandString(6))
29
30def _test():
31    with open(filename, 'wb') as fd:
32        fd.write(urlopen('https://raw.githubusercontent.com/nmap/nmap/9efe1892/nmap-os-fingerprints').read())
33
34retry_test(_test)
35
36conf.nmap_base = filename
37
38= Database loading
39~ netaccess
40
41print(conf.nmap_kdb.base, conf.nmap_kdb.filename, len(conf.nmap_kdb.get_base()))
42assert len(conf.nmap_kdb.get_base()) > 100
43
44= fingerprint test: www.secdev.org
45~ netaccess needs_root
46with no_debug_dissector():
47    score, fprint = nmap_fp('www.secdev.org')
48
49print(score, fprint)
50assert score > 0.5
51assert fprint
52
53= fingerprint test: gateway
54~ netaccess needs_root
55with no_debug_dissector():
56    score, fprint = nmap_fp(conf.route.route('0.0.0.0')[2])
57
58print(score, fprint)
59assert score > 0.5
60assert fprint
61
62= fingerprint test: to text
63~  netaccess needs_root
64
65import re as re_
66
67with no_debug_dissector():
68    a = nmap_sig("www.secdev.org", 80, 81)
69
70a
71for x in nmap_sig2txt(a).split("\n"):
72    assert re_.match(r"\w{2,4}\(.*\)", x)
73
74= nmap_udppacket_sig test: www.google.com
75~ netaccess needs_root
76
77with no_debug_dissector():
78    a = nmap_sig("www.google.com", ucport=80)
79
80assert len(a) > 3
81assert len(a["PU"]) > 0
82
83+ Nmap errors triggering
84
85= Nmap base not available
86
87conf.nmap_kdb.filename = "invalid"
88conf.nmap_kdb.reload()
89assert conf.nmap_kdb.filename == None
90
91= Clear temp files
92try:
93    os.remove(filename)
94except:
95    pass
96
97