對於這個任務,你可能想要使用像scapy或PacketFu分組各具特色的框架,因爲這將讓您指定,而不需要一個新的進程來啓動每次發送什麼類型的數據包時間。
您也可以使用像Nping,hping3或nemesis這樣的數據包製作工具,但這些還需要爲每個探針啓動一個新進程(據我所知)。
您可以在Nmap腳本中使用socket:bind
來爲每個connect
選擇一個源端口,但您必須進行大量調整以獲得接近完整Nmap端口掃描性能的任何位置。首先,您必須一次發送多個數據包,但您可能會超負荷目標或網絡,並遇到擁塞問題。儘管如此,使用少量(20個左右)的工作線程來加快速度可能是值得的。
要加速當前的方法,請使用-n
跳過反向名稱解析。您可以通過在後臺啓動一些nmap進程或使用GNU Parallel來並行化它。您可以嘗試使用--max-retries
來減少重試次數,因爲默認最大值爲10,但此處的快速測試顯示,使用-Pn
和單個端口,Nmap只會嘗試2次才能到達端口。如果你想變得很花哨,你可以使用--initial-rtt-timeout
和--max-rtt-timeout
,因爲Nmap在啓動時會故意保守,假設它會發送很多數據包並更好地瞭解網絡狀況。在沒有主機發現的情況下掃描一個主機上的一個端口意味着它沒有機會自我調整。如果針對目標運行ping -c 10
,則可以使用其最後一行輸出來設置這些值。例如,如果我看到:
$ ping -c 10 scanme.nmap.com
PING scanme.nmap.com (74.207.244.221) 56(84) bytes of data.
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=1 ttl=56 time=75.0 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=2 ttl=56 time=75.5 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=3 ttl=56 time=73.9 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=4 ttl=56 time=75.4 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=5 ttl=56 time=74.7 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=6 ttl=56 time=78.9 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=7 ttl=56 time=73.6 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=8 ttl=56 time=79.4 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=9 ttl=56 time=74.8 ms
64 bytes from scanme.nmap.org (74.207.244.221): icmp_req=10 ttl=56 time=75.1 ms
--- scanme.nmap.com ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9012ms
rtt min/avg/max/mdev = 73.646/75.691/79.459/1.857 ms
然後我可以設置--initial-rtt-timeout
到avg + 4 * mdev = 83ms
給自己的4個標準偏差一個不錯的大幅周圍的平均往返時間。 Nmap的默認值是等待1000毫秒(1秒),所以這是一個很大的加速。以防萬一,將--max-rtt-timeout
設置爲初始值的10倍。