Как пользоваться Nmap для сканирования сети
Nmap — это очень популярный сканер сети с открытым исходным кодом, который может использоваться как в Windows, так и в Linux. Программа Nmap или Network Mapper была разработана Гордоном Луоном и на данный момент используется специалистами по безопасности и системными администраторами по всему миру.
Эта программа помогает системным администраторам очень быстро понять какие компьютеры подключены к сети, узнать их имена, а также посмотреть какое программное обеспечение на них установлено, какая операционная система и какие типы фильтров применяются. Функциональность программы может быть расширена за счет собственного скриптового языка, который позволяет администраторам автоматизировать много действий.
Например, с помощью скриптов можно автоматически обнаруживать новые уязвимости безопасности в вашей сети. Namp может использоваться с хорошими и плохими намерениями, будьте аккуратны, чтобы не использовать nmap против закона. В этой инструкции мы рассмотрим как пользоваться namp для сканирования портов в операционной системе Linux. Но сначала нужно попытаться понять как работает эта утилита.
Как работает Nmap?
В компьютерных сетях все подключенные устройства имеют свой ip адрес. Каждый компьютер поддерживает протокол ping, с помощью которого можно определить подключен ли он к сети. Мы просто отправляем ping запрос компьютеру, и если он отзывается, то считаем, что он подключен. Nmap использует немного иной подход. Компьютеры также определенным образом реагируют на те или иные сетевые пакеты, утилита просто отправляет нужные пакеты и смотрит какие хосты прислали ответ.
Но об этом вы, наверное, уже знаете. Более интересно то как Nmap узнает какие сервисы запущены на машине. Суть работы всех сетевых программ основана на портах. Чтобы получить сообщение из сети, программа должна открыть порт на вашем компьютере и ждать входящих соединений. А для отправки сообщения по сети нужно подключиться к уже другой программой (адресатом) порту. Затем программе необходимо будет открыть порт, на котором она будет ждать ответа.
Утилита nmap в процессе сканирования сети перебирает доступный диапазон портов и пытается подключиться к каждому из них. Если подключение удалось, в большинстве случаев, передав несколько пакетов программа может даже узнать версию программного обеспечения, которые ожидает подключений к этому порту. Теперь, после того, как мы рассмотрели основы, рассмотрим как пользоваться nmap для сканирования портов и сети.
Синтаксис Nmap
Команда запуска Nmap очень проста для этого достаточно передать ей в параметрах целевой IP адрес или сеть, а также указать опции при необходимости:
$ nmap опции адрес
Теперь давайте рассмотрим основные опции, которые понадобятся нам в этой статье.
- -sL — просто создать список работающих хостов, но не сканировать порты nmap;
- -sP — только проверять доступен ли хост с помощью ping;
- -PN — считать все хосты доступными, даже если они не отвечают на ping;
- -sS/sT/sA/sW/sM — TCP сканирование;
- -sU — UDP сканирование nmap;
- -sN/sF/sX — TCP NULL и FIN сканирование;
- -sC — запускать скрипт по умолчанию;
- -sI — ленивое Indle сканирование;
- -p — указать диапазон портов для проверки;
- -sV — детальное исследование портов для определения версий служб;
- -O — определять операционную систему;
- -T5 — скорость сканирования, чем больше, тем быстрее;
- -D — маскировать сканирование с помощью фиктивных IP;
- -S — изменить свой IP адрес на указанный;
- -e — использовать определенный интерфейс;
- —spoof-mac — установить свой MAC адрес;
- -A — определение операционной системы с помощью скриптов.
Теперь, когда мы рассмотрели все основные опции, давайте поговорим о том, как выполняется сканирование портов nmap.
Как пользоваться Nmap для сканирования портов в Linux
Дальше рассмотрим примеры nmap. Сначала давайте рассмотрим как найти все подключенные к сети устройства, для этого достаточно использовать опцию -sL и указать маску нашей сети. в моем случае это 192.168.1.1/24. Маску вашей локальной сети вы можете найти, выполнив команду:
Из вывода для используемого интерфейса возьмите число после слеша, а до слэша укажите ip вашего роутера. Команда на сканирование сети nmap будет выглядеть вот так:
nmap -sL 192.168.1.1/24
Иногда это сканирование может не дать никаких результатов, потому что некоторые операционные системы имеют защиту от сканирования портов. Но это можно обойти, просто использовав для сканирования ping всех ip адресов сети, для этого есть опция -sn:
nmap -sn 192.168.1.1/24
Как видите, теперь программа обнаружила активные устройства в сети. Дальше мы можем сканировать порты nmap для нужного узла запустив утилиту без опций:
sudo nmap 192.168.1.1
Теперь мы можем видеть, что у нас открыто несколько портов, все они используются каким-либо сервисом на целевой машине. Каждый из них может быть потенциально уязвимым, поэтому иметь много открытых портов на машине небезопасно. Но это еще далеко не все, что вы можете сделать, дальше вы узнаете как пользоваться nmap.
Чтобы узнать более подробную информацию о машине и запущенных на ней сервисах вы можете использовать опцию -sV. Утилита подключится к каждому порту и определит всю доступную информацию:
sudo nmap -sV 192.168.1.1
На нашей машине запущен ftp, а поэтому мы можем попытаться рассмотреть эту службу подробнее с помощью стандартных скриптов nmap. Скрипты позволяют проверить порт более детально, найти возможные уязвимости. Для этого используйте опцию -sC и -p чтобы задать порт:
sudo nmap -sC 192.168.56.102 -p 21
Мы выполняли скрипт по умолчанию, но есть еще и другие скрипты, например, найти все скрипты для ftp вы можете командой:
sudo find /usr/share/nmap/scripts/ -name ‘*.nse’ | grep ftp
Затем попытаемся использовать один из них, для этого достаточно указать его с помощью опции —script. Но сначала вы можете посмотреть информацию о скрипте:
sudo nmap —script-help ftp-brute.nse
Этот скрипт будет пытаться определить логин и пароль от FTP на удаленном узле. Затем выполните скрипт:
sudo nmap —script ftp-brute.nse 192.168.1.1 -p 21
В результате скрипт подобрал логин и пароль, admin/admin. Вот поэтому не нужно использовать параметры входа по умолчанию.
Также можно запустить утилиту с опцией -A, она активирует более агрессивный режим работы утилиты, с помощью которого вы получите большую часть информации одной командой:
sudo nmap -A 192.168.1.1
Обратите внимание, что здесь есть почти вся информация, которую мы уже видели раньше. Ее можно использовать чтобы увеличить защиту этой машины.
Выводы
В этой статье мы рассмотрели как выполняется сканирование портов nmap, а также несколько простых примеров использования этой утилиты. Эти команды nmap могут быть полезными многим системным администраторам, чтобы улучшить безопасность их систем. Но это далеко не все возможности утилиты. Продолжайте экспериментировать с утилитой чтобы узнать больше только не в чужих сетях!
Nmap with gui windows
While Nmap was once a Unix-only tool, a Windows version was released in 2000 and has since become the second most popular Nmap platform (behind Linux). Because of this popularity and the fact that many Windows users do not have a compiler, binary executables are distributed for each major Nmap release. We support Nmap on Windows 7 and newer, as well as Windows Server 2008 and newer. We also maintain a guide for users who must run Nmap on earlier Windows releases. While it has improved dramatically, the Windows port is not quite as efficient as on Unix. Here are the known limitations:
Nmap only supports ethernet interfaces (including most 802.11 wireless cards and many VPN clients) for raw packet scans. Unless you use the -sT -Pn options, RAS connections (such as PPP dialups) and certain VPN clients are not supported. This support was dropped when Microsoft removed raw TCP/IP socket support in Windows XP SP2. Now Nmap must send lower-level ethernet frames instead.
When using Nmap with WinPcap instead of Npcap, you cannot generally scan your own machine from itself (using a loopback IP such as 127.0.0.1 or any of its registered IP addresses). This is a Windows limitation that we have worked around in Npcap, which is included in the Windows self-installer. Users stuck with WinPcap can use a TCP connect scan without pinging ( -sT -Pn ) as that uses the high level socket API rather than sending raw packets.
Scan speeds on Windows are generally comparable to those on Unix, though the latter often has a slight performance edge. One exception to this is connect scan ( -sT ), which is often much slower on Windows because of deficiencies in the Windows networking API. This is a shame, since that is the one TCP scan that works over all networking types (not just ethernet, like the raw packet scans). Connect scan performance can be improved substantially by applying the Registry changes in the nmap_performance.reg file included with Nmap. By default these changes are applied for you by the Nmap executable installer. This registry file is in the nmap- directory of the Windows binary zip file, and nmap- /mswin32 in the source tarball (where is the version number of the specific release). These changes increase the number of ephemeral ports reserved for user applications (such as Nmap) and reduce the time delay before a closed connection can be reused. Most people simply check the box to apply these changes in the executable Nmap installer, but you can also apply them by double-clicking on nmap_performance.reg , or by running the command regedt32 nmap_performance.reg . To make the changes by hand, add these three Registry DWORD values to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters :
Set a large value such as 65534 (0x0000fffe). See MS KB 196271.
Set the minimum value (0x0000001e). See MS KB 149532.
Set to 1 so TCPTimedWaitDelay is checked.
I would like to thank Ryan Permeh of eEye, Andy Lutomirski , and Jens Vogt for their hard work on the Nmap Windows port. For many years, Nmap was a Unix-only tool, and it would likely still be that way if not for their efforts.
Windows users have three choices for installing Nmap, all of which are available from the download page at https://nmap.org/download.html.
Windows Self-installer
Every Nmap release includes a Windows self-installer named nmap- -setup.exe (where is the version number of the specific release). Most Nmap users choose this option since it is so easy. Another advantage of the self-installer is that it provides the option to install the Zenmap GUI and other tools. Simply run the installer file and let it walk you through panels for choosing an install path and installing Npcap. The installer was created with the open-source Nullsoft Scriptable Install System. After it completes, read the section called “Executing Nmap on Windows” for instructions on executing Nmap on the command-line or through Zenmap.
Command-line Zip Binaries
Most users prefer installing Nmap with the self-installer discussed previously.
Every stable Nmap release comes with Windows command-line binaries and associated files in a Zip archive. No graphical interface is included, so you need to run nmap.exe from a DOS/command window. Or you can download and install a superior command shell such as those included with the free Cygwin system available from http://www.cygwin.com. Here are the step-by-step instructions for installing and executing the Nmap .zip binaries.
Installing the Nmap zip binaries
Uncompress the zip file into the directory you want Nmap to reside in. An example would be C:\Program Files . A directory called nmap- should be created, which includes the Nmap executable and data files. Microsoft Windows XP and later includes zip extraction—just right-click on the file in Explorer . If you do not have a Zip decompression program, there is one (called unzip) in Cygwin described above, or you can download the open-source and free 7-Zip utility. Commercial alternatives are WinZip and PKZIP.
For improved performance, apply the Nmap Registry changes discussed previously.
Nmap requires the free Npcap packet capture library. We include a recent Npcap installer which is available in the zip file as npcap- .exe , where is the Npcap version rather than the Nmap version. Alternatively, you can obtain and install the latest version from https://npcap.org.
Due to the way Nmap is compiled, it requires the Microsoft Visual C++ 2013 Redistributable Package of runtime components. Many systems already have this installed from other packages, but you should run vcredist_x86.exe from the zip file just in case you need it. Pass the /q option to run these installers in quiet (non interactive) mode.
Instructions for executing your compiled Nmap are given in the section called “Executing Nmap on Windows”.
Compile from Source Code
Most Windows users prefer to use the Nmap binary self-installer, but compilation from source code is an option, particularly if you plan to help with Nmap development. Compilation requires Microsoft Visual C++ 2013, which is part of their commercial Visual Studio suite. Any of the Visual Studio 2013 editions should work, including the free Visual C++ 2013 Express.
Some of Nmap’s dependencies on Windows are inconvenient to build. For this reason, precompiled binaries of the dependencies are stored in Subversion, in the directory /nmap-mswin32-aux . When building from source, whether from a source code release or from Subversion, check out /nmap-mswin32-aux as described below.
Compiling Nmap on Windows from Source
Download the Windows dependencies from Subversion with the command svn checkout https://svn.nmap.org/nmap-mswin32-aux . The build files are configured to look for dependencies in this checked-out directory. If you want to build the dependencies yourself instead, you will have to reconfigure the Visual Studio project files to point to the alternate directory.
Decide whether to obtain the Nmap source code by downloading the latest release from nmap.org, or using a Subversion client to retrieve even newer (but less tested) code from our repository. These instructions are for the web download approach, but using Subversion instead is straightforward (see the section called “Obtaining Nmap from the Subversion (SVN) Repository”).
Download the latest Nmap source distribution from https://nmap.org/download.html. It has the name nmap- .tar.bz2 or nmap- .tgz . Those are the same tar file compressed using bzip2 or gzip, respectively. The bzip2-compressed version is smaller.
Uncompress the source code file you just downloaded. The source code directory and the nmap-mswin32-aux must be in the same parent directory. Recent releases of the free Cygwin distribution can handle both the .tar.bz2 and .tgz formats. Use the command tar xvjf nmap-version.tar.bz2 or tar xvzf nmap-version.tgz , respectively. Alternatively, the common WinZip application can decompress these files.
Open Visual Studio and the Nmap solution file ( nmap- /mswin32/nmap.sln ).
Right click on Solution ‘nmap’ in the Solution Explorer sidebar and choose “ Configuration Manager ” . Ensure that the active solution configuration is Release and then close the Configuration Manager.
Build Nmap by pressing F7 or choosing “ Build Solution ” from the GUI. Nmap should begin compiling, and end with the line “ — Done — ” saying that all projects built successfully and there were zero failures.
The executable and data files can be found in nmap- /mswin32/Release/ . You can copy them to a preferred directory as long as they are all kept together.
Ensure that you have Npcap installed. You can obtain it by installing our binary self-installer or executing npcap- .exe from our zip package. Alternatively, you can obtain the official installer at https://npcap.org.
Instructions for executing your compiled Nmap are given in the next section.
If you wish to build an Nmap executable Windows installer or Zenmap executable, see docs/win32-installer-zenmap-buildguide.txt in the Nmap SVN repository.
Many people have asked whether Nmap can be compiled with the gcc / g++ included with Cygwin or other compilers. Some users have reported success with this, but we don’t maintain instructions for building Nmap under Cygwin.
Executing Nmap on Windows
Nmap releases now include the Zenmap graphical user interface for Nmap. If you used the Nmap installer and left the Zenmap field checked, there should be a new Zenmap entry on your desktop and Start Menu. Click this to get started. Zenmap is fully documented in Chapter 12, Zenmap GUI Users’ Guide. While many users love Zenmap, others prefer the traditional command-line approach to executing Nmap. Here are detailed instructions for users who are unfamiliar with command-line interfaces:
Make sure the user you are logged in as has administrative privileges on the computer (user should be a member of the administrators group).
Open a command/DOS Window. Though it can be found in the program menu tree, the simplest approach is to choose “ Start ” -> “ Run ” and type cmd . Opening a Cygwin window (if you installed it) by clicking on the Cygwin icon on the desktop works too, although the necessary commands differ slightly from those shown here.
Change to the directory you installed Nmap into. You can skip this step if Nmap is already in your command path (the Zenmap isntaller adds it there by default). Otherwise, type the following commands.
On Windows releases prior to Windows 7, specify \Program Files\Nmap instead. The directory will also be different if you chose to install Nmap in a non-default location.
Execute nmap.exe . Figure 2.1 is a screen shot showing a simple example.
Figure 2.1. Executing Nmap from a Windows command shell
If you execute Nmap frequently, you can add the Nmap directory ( c:\Program Files (x86)\Nmap by default on Windows 7) to your command execution path. The exact place to set this varies by Windows platform. On my Windows XP box, which installs Nmap in c:\Program Files\Nmap , I do the following:
From the desktop, right click on My Computer and then click “ properties ” .
In the System Properties window, click the “ Advanced ” tab.
Click the “ Environment Variables ” button.
Choose Path from the System variables section, then hit edit.