Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nmap os scan hangs #2104

Closed
ameidatou opened this issue Aug 15, 2020 · 10 comments · Fixed by res0nat0r/nmap#1
Closed

Nmap os scan hangs #2104

ameidatou opened this issue Aug 15, 2020 · 10 comments · Fixed by res0nat0r/nmap#1
Labels

Comments

@ameidatou
Copy link

ameidatou commented Aug 15, 2020

Bug
When triggering a nmap OS scan for a specific host, nmap hangs with CPU usage 100%. This doesn't happen systematically but quite often

To Reproduce
sudo nmap -O -p 1027,1028,5000,443,8081,80,8080,1026,1025 --open -Pn -ddd -vv 85.2.144.13
if it doesn't happen the first time, please repeat for 3 to 5 times and you should encounter the issue.

  • Log:
    The log hangs at this point
Host 85.2.144.13. ProbesToSend 8:       ProbesActive 6
SENT (3.2078s) TCP [192.168.1.6:38140 > 85.2.144.13:443 SFPU seq=2929557940 ack=217905394 off=10 res=0 win=256 csum=0xCED1 urp=0 <wscale 10,nop,mss 265,timestamp 4294967295 0,sackOK>] IP [ver=4 ihl=5 tos=0x00 iplen=60 id=14722 foff=0 ttl=47 proto=6 csum=0xab7c]
Send probe (type: OFP_T1_7, subid: 2) to 85.2.144.13
pcap wait time is 24807.
Rejecting TCP packet because of bad TCP header
Host 85.2.144.13. ProbesToSend 7:       ProbesActive 7
pcap wait time is 23720.
Host 85.2.144.13. ProbesToSend 8:       ProbesActive 6
SENT (3.2331s) TCP [192.168.1.6:38141 > 85.2.144.13:443 A seq=2929557940 ack=217905394 off=10 res=0 win=1024 csum=0xCBEB urp=0 <wscale 10,nop,mss 265,timestamp 4294967295 0,sackOK>] IP [ver=4 ihl=5 tos=0x00 iplen=60 id=49426 flg=D foff=0 ttl=45 proto=6 csum=0xe5eb]
Send probe (type: OFP_T1_7, subid: 3) to 85.2.144.13
pcap wait time is 24787.
  • gdb bt:
(gdb) bt
#0  0x000055b6c370f232 in readip_pcap(pcap*, unsigned int*, long, timeval*, link_header*, bool) ()
#1  0x000055b6c370f33d in readipv4_pcap(pcap*, unsigned int*, long, timeval*, link_header*, bool) ()
#2  0x000055b6c36d1bd7 in OSScan::os_scan_ipv4(std::vector<Target*, std::allocator<Target*> >&) ()
#3  0x000055b6c36d2a26 in OSScan::os_scan(std::vector<Target*, std::allocator<Target*> >&) ()
#4  0x000055b6c36c47c0 in nmap_main(int, char**) ()
#5  0x000055b6c3695cc6 in main ()
  • Top:
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                                                                        
19718 root      20   0   67876  22928   1316 R 100.0  0.1  45:47.65 nmap                                                                                                                                                                                                                                                                             
  • ps:
$ ps -ef |grep nmap
root     19717 20920  0 10:24 pts/11   00:00:00 sudo nmap -O -p 1027,1028,5000,443,8081,80,8080,1026,1025 --open -Pn -ddd -vv 85.2.144.13
root     19718 19717 99 10:24 pts/11   00:46:35 nmap -O -p 1027,1028,5000,443,8081,80,8080,1026,1025 --open -Pn -ddd -vv 85.2.144.13

Expected behavior
Nmap os scan finishes in less than 30s.

Version info (please complete the following information):

  • OS: Linux 5.4.0-42-generic #46~18.04.1-Ubuntu x86_64 x86_64 x86_64 GNU/Linux
  • Output of nmap --version:
Platform: x86_64-unknown-linux-gnu
Compiled with: nmap-liblua-5.3.5 openssl-1.0.2n nmap-libssh2-1.8.2 libz-1.2.11 libpcre-8.39 nmap-libpcap-1.9.0 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select
@ameidatou ameidatou added the Nmap label Aug 15, 2020
@ameidatou
Copy link
Author

ameidatou commented Aug 15, 2020

after some debugging, I found there is an infinite loop in file tcpip.cc in function validateTCPhdr() in line:

nmap/tcpip.cc

Line 1374 in 0acdeb5

while (optlen > 0) {

@ameidatou
Copy link
Author

ameidatou commented Aug 15, 2020

with extra logging, notice that tcpc and optlen are not changing, and the loop goes infinite:

Host 85.2.144.13. ProbesToSend 11:      ProbesActive 3
pcap wait time is 2000.
Issue 2104 - call readipv4_pcap().
Issue 2104 - call readip_pcap().
Issue 2104 - call validatepkt().
Issue 2104 - call validateTCPhdr().
Issue 2104 - in validateTCPhdr: tcpc 2, len 32, optlen 12.
Issue 2104 - start while: tcpc 2, len 32, optlen 12.
Issue 2104: OPTLEN_IS expected/optlen: 4/12.
Issue 2104 - end while: tcpc 4, len 32, optlen 8.
Issue 2104 - start while: tcpc 4, len 32, optlen 8.
Issue 2104: OPTLEN_IS expected/optlen: 2/8.
Issue 2104 - end while: tcpc 1, len 32, optlen 6.
Issue 2104 - start while: tcpc 1, len 32, optlen 6.
Issue 2104 - end while: tcpc 1, len 32, optlen 5.
Issue 2104 - start while: tcpc 1, len 32, optlen 5.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.
Issue 2104 - start while: tcpc 98, len 32, optlen 4.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.
Issue 2104 - start while: tcpc 98, len 32, optlen 4.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.
Issue 2104 - start while: tcpc 98, len 32, optlen 4.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.
Issue 2104 - start while: tcpc 98, len 32, optlen 4.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.
Issue 2104 - start while: tcpc 98, len 32, optlen 4.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.
Issue 2104 - start while: tcpc 98, len 32, optlen 4.
Issue 2104 - end while: tcpc 98, len 32, optlen 4.

@ameidatou
Copy link
Author

@dmiller-nmap
Copy link

The target has a very weird behavior during OS detection; the TCP End-of-Options-List option is corrupted in many cases, essentially being replaced by 2 random bytes. I will look at our code and see if I can eliminate the parsing problem.

@dmiller-nmap
Copy link

@ameidatou What version of Nmap are you actually using? These values don't match the existing code. It would also be helpful to have a packet capture of a scan where the actual bug triggers.

@ameidatou
Copy link
Author

ameidatou commented Aug 18, 2020

@dmiller-nmap below is my nmap version:

Nmap version 7.80SVN ( https://nmap.org )
Platform: x86_64-unknown-linux-gnu
Compiled with: nmap-liblua-5.3.5 openssl-1.0.2n nmap-libssh2-1.9.0 libz-1.2.11 libpcre-8.39 nmap-libpcap-1.9.1 nmap-libdnet-1.12 ipv6
Compiled without:
Available nsock engines: epoll poll select

Not sure what you mean by:

These values, don't match the existing code?

and attached are:

@dmiller-nmap
Copy link

ok, I think I see the issue. If the bogus option has a length of 0, then *tcpc == 0, and this line moves it back one place and the loop gets stuck:

tcpc += (*tcpc - 1);

I'll put in a check for this.

@dmiller-nmap
Copy link

Fix incoming. Interestingly, this bug was introduced in my fix to #1204, which is a transposition of this issue number :)

@ameidatou
Copy link
Author

ameidatou commented Aug 18, 2020

Thanks @dmiller-nmap for your quick feedback and quick fix!
I tested it on that specific host and it works fine!

res0nat0r added a commit to res0nat0r/nmap that referenced this issue Oct 7, 2020
* Implement Ncat proxy creds via environment variable. Fixes nmap#2060, closes nmap#2073

* Fix --resume from IPv6 scans

* Use correct default buffer position. Closes nmap#2084

* Clarify upper boundary for variable-length numerical fields

* Make maximize_fdlimit return rlim_t on appropriate platforms. Closes nmap#2085. Fixes nmap#2079

* Credential object is creds.Account, not brute.Account. See nmap#2086

* Clarify location of the Error object

* Use correct default buffer position. Closes nmap#2086

* Minor optimization of url.parse_query()

* Output of matched fingerprints in http-default-accounts. Fixes nmap#2077

* Document that --open implies --defeat-rst-ratelimit since 7.40

* SNMP scripts are enabled on non-standard ports. See nmap#1473

* Increases SQL Server version resolution

* Eliminate reflection false positives in http-shellshock. Closes nmap#2089

* Unify AFP pathname serialization

* Correct AFP name extraction from responses. Closes nmap#2091
FPGetFileDirParms and FPEnumerateExt2 could crash due to unpacking from
out-of-bounds positions. This latent issue got exposed by converting from
bin.unpack to more stringent string.unpack

* Clarified parsing of the volume list in AFP FPGetSrvrParms

* Add cross references between the 2 whois scripts

* Streamline Boolean expressions

* Centralize AFP timestamp conversion to string

* Fix a word-wrapping issue

* Prevent SSH2 KEX confusion. Fixes nmap#2105

* Add ssh2.fetch_host_key() support for group 16

* Handle case of corrupted TCP options with length 0. Fixes nmap#2104

* Add iDRAC9 fingerprint to http-default-accounts. Closes nmap#2096

* fix license url: http -> https

* Implementation of TLS SNI override in Ncat
Closes nmap#2087, closes nmap#1928, fixes nmap#1927, fixes nmap#1974

* Fix off-by-one issue in last change. Fixes nmap#2107

* Be more strict with TCP options parsing, avoid reading off the end of TCP options. See nmap#2107

* Remove nmap-update

This feature was never publicly released, and has not been distributed
in our binary builds for a couple versions now. It needed to be removed
in order to reduce the number of places Nmap looks for data files. See nmap#2051

* If fetchfile didn't find the XSL, use a relative path on all platforms.

* Do not search NMAPDATADIR on Windows as it is not defined. See nmap#2051

* Remove an unused variable

* Require trailing '/' to match a directory name with --script. See nmap#2051

* Stop using Shellshock in header name. Fixes nmap#1983

* Fix line wrapping

* Speed improvement for script afp-ls. Closes nmap#2098

* New option --discovery-ignore-rst. Closes nmap#1616

* Nbase is needed for __attribute__ on Windows

* include string_pool in Windows build

* Use larger buffer size for socket errors (WSAETIMEDOUT was longer).

* Allow multiple UDP payloads per port. Closes nmap#1859 (payloads to be committed later)

* New UDP payloads. Closes nmap#1860

* Use ASCII chars for some payload data where it makes sense

* Pass error along instead of printing (link error)

* OpenSSL 1.1.X renamed libs: libeay32->libcrypto ssleay32->libssl

* More OpenSSL DLL name changes

* One last libeay32->libcrypto name change

* Fix loopback detection on Windows with new Npcap

* Add some popular favicon hashes

* Update nmap-mac-prefixes

* Update nmap-services from IANA

* Handle too-short response in s7-info. See nmap#2117

* Remove a todo item that is done (--resolve-all)

* Update dated 'class' network terms to CIDR. Closes nmap#2054

* Call superclass's init method from derived class

* Use signed value for tcp header offset and option lengths to detect underflow

* Correctly check for unsigned subtraction underflow.

* Add some missing changelog entries

* Tell LGTM to use the correct version of Python (2)

* Process new Linux and OpenBSD fingerprints

* Only get SSL options if we use them, currently for NO_SSLv2

* Process a few service fingerprint submissions

* Add a requested feature

* Try to make sure enough data is present before parsing. See nmap#2117

* Replace hyphens in the client SSH banner
Hyphen is not allowed in the software version string (RFC 4253, section 4.2)

* Update the SSH protocol flow. Closes nmap#1460
Allows the server to start the key exchange before the protocol version
exchange (banner exchange) is completed

* Silence static analysis warning

LGTM points out that since comparison with sizeof(buf) coerces n to
unsigned, all negative values become very large values, which are
necessarily larger than sizeof(buf), so the test is redundant. We still
want the test in our code to be explicit that we are checking for it, so
reordering the comparisons should silence the warning. A good optimizing
compiler should be able to combine the two conditions anyway.

See github/codeql#4249

* Be explicit about truncating division (timeout is in whole milliseconds)

* Improve docs on -Pn and host discovery

"Host discovery" is the preferred term over "ping scan" because of
confusion with ICMP Echo Request, a.k.a. "ping" as used by the "ping"
utility. Warn when users use -Pn because it has negative impact on scan
times since ultrascan timing parameters fall back to slow initial
defaults.

* Fix a config issue with LGTM (libverbs not linked in libpcap)

* Update IPv6 classifier based on new submissions through 2020-09-14

* Fix a meaningless error message when parsing IPv6 extension headers.

* Allow %F date format to mean YYYY-mm-dd like GNU date

* Remove duplicate test conditionals already tested in enclosing block

* Properly handle pcap reads in iocp engine. Fixes nmap#2126

Still has an odd code smell, but this fixes my test case with Nping.

* Add missing prototype

* Make IOCP the default Nsock engine on Windows. See nmap#2126

* Update macosx build to OpenSSL 1.1.1h, use jhbuild for all build steps

* Default rule base for script mysql-audit. See nmap#2125

* Avoid masked use of date before 1/1/1970 UTC. Fixes nmap#2136, closes nmap#2137

* Fix a CHANGELOG typo

* Reintegrate Nmap 7.90 release branch

* Bump version and regen docs for 7.90SVN post-release

* Only warn about protocol specs in port list with -p. Fixes nmap#2135

* Handle a weird IOCP error for UDP sockets. Fixes nmap#2140

Co-authored-by: nnposter <nnposter@e0a8ed71-7df4-0310-8962-fdc924857419>
Co-authored-by: dmiller <dmiller@e0a8ed71-7df4-0310-8962-fdc924857419>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@ameidatou @dmiller-nmap and others