Nmap 개요

  • 네트워크 스캐닝 도구이다.
  • 내경우에는 주로 포트스캔을 위해서 사용한다.

설치

  • yum 으로 설치가 가능하나 옛날 버전이 설치되는 경우가 있다.
  • 내 환경의 경우, 2022/12/13 기준으로 6.40 이 설치된다. (6.40은 무려 2013년 7월29일 버전이다!)
  • 2022년 12월기준으로는 7.93이 최신버전이다.
  • 따라서, yum 버전이 설치되어 있다면 제거하고 최신버전을 설치하도록 한다.
yum remove nmap
rpm -vhU https://nmap.org/dist/nmap-7.93-1.x86_64.rpm

옵션

옵션이 엄청나게 많다.

$ nmap -h
Nmap 7.93 ( https://nmap.org )
Usage: nmap [Scan Type(s)] [Options] {target specification}
TARGET SPECIFICATION:
  Can pass hostnames, IP addresses, networks, etc.
  Ex: scanme.nmap.org, microsoft.com/24, 192.168.0.1; 10.0.0-255.1-254
  -iL <inputfilename>: Input from list of hosts/networks
  -iR <num hosts>: Choose random targets
  --exclude <host1[,host2][,host3],...>: Exclude hosts/networks
  --excludefile <exclude_file>: Exclude list from file
HOST DISCOVERY:
  -sL: List Scan - simply list targets to scan
  -sn: Ping Scan - disable port scan
  -Pn: Treat all hosts as online -- skip host discovery
  -PS/PA/PU/PY[portlist]: TCP SYN/ACK, UDP or SCTP discovery to given ports
  -PE/PP/PM: ICMP echo, timestamp, and netmask request discovery probes
  -PO[protocol list]: IP Protocol Ping
  -n/-R: Never do DNS resolution/Always resolve [default: sometimes]
  --dns-servers <serv1[,serv2],...>: Specify custom DNS servers
  --system-dns: Use OS's DNS resolver
  --traceroute: Trace hop path to each host
SCAN TECHNIQUES:
  -sS/sT/sA/sW/sM: TCP SYN/Connect()/ACK/Window/Maimon scans
  -sU: UDP Scan
  -sN/sF/sX: TCP Null, FIN, and Xmas scans
  --scanflags <flags>: Customize TCP scan flags
  -sI <zombie host[:probeport]>: Idle scan
  -sY/sZ: SCTP INIT/COOKIE-ECHO scans
  -sO: IP protocol scan
  -b <FTP relay host>: FTP bounce scan
PORT SPECIFICATION AND SCAN ORDER:
  -p <port ranges>: Only scan specified ports
    Ex: -p22; -p1-65535; -p U:53,111,137,T:21-25,80,139,8080,S:9
  --exclude-ports <port ranges>: Exclude the specified ports from scanning
  -F: Fast mode - Scan fewer ports than the default scan
  -r: Scan ports sequentially - don't randomize
  --top-ports <number>: Scan <number> most common ports
  --port-ratio <ratio>: Scan ports more common than <ratio>
SERVICE/VERSION DETECTION:
  -sV: Probe open ports to determine service/version info
  --version-intensity <level>: Set from 0 (light) to 9 (try all probes)
  --version-light: Limit to most likely probes (intensity 2)
  --version-all: Try every single probe (intensity 9)
  --version-trace: Show detailed version scan activity (for debugging)
SCRIPT SCAN:
  -sC: equivalent to --script=default
  --script=<Lua scripts>: <Lua scripts> is a comma separated list of
           directories, script-files or script-categories
  --script-args=<n1=v1,[n2=v2,...]>: provide arguments to scripts
  --script-args-file=filename: provide NSE script args in a file
  --script-trace: Show all data sent and received
  --script-updatedb: Update the script database.
  --script-help=<Lua scripts>: Show help about scripts.
           <Lua scripts> is a comma-separated list of script-files or
           script-categories.
OS DETECTION:
  -O: Enable OS detection
  --osscan-limit: Limit OS detection to promising targets
  --osscan-guess: Guess OS more aggressively
TIMING AND PERFORMANCE:
  Options which take <time> are in seconds, or append 'ms' (milliseconds),
  's' (seconds), 'm' (minutes), or 'h' (hours) to the value (e.g. 30m).
  -T<0-5>: Set timing template (higher is faster)
  --min-hostgroup/max-hostgroup <size>: Parallel host scan group sizes
  --min-parallelism/max-parallelism <numprobes>: Probe parallelization
  --min-rtt-timeout/max-rtt-timeout/initial-rtt-timeout <time>: Specifies
      probe round trip time.
  --max-retries <tries>: Caps number of port scan probe retransmissions.
  --host-timeout <time>: Give up on target after this long
  --scan-delay/--max-scan-delay <time>: Adjust delay between probes
  --min-rate <number>: Send packets no slower than <number> per second
  --max-rate <number>: Send packets no faster than <number> per second
FIREWALL/IDS EVASION AND SPOOFING:
  -f; --mtu <val>: fragment packets (optionally w/given MTU)
  -D <decoy1,decoy2[,ME],...>: Cloak a scan with decoys
  -S <IP_Address>: Spoof source address
  -e <iface>: Use specified interface
  -g/--source-port <portnum>: Use given port number
  --proxies <url1,[url2],...>: Relay connections through HTTP/SOCKS4 proxies
  --data <hex string>: Append a custom payload to sent packets
  --data-string <string>: Append a custom ASCII string to sent packets
  --data-length <num>: Append random data to sent packets
  --ip-options <options>: Send packets with specified ip options
  --ttl <val>: Set IP time-to-live field
  --spoof-mac <mac address/prefix/vendor name>: Spoof your MAC address
  --badsum: Send packets with a bogus TCP/UDP/SCTP checksum
OUTPUT:
  -oN/-oX/-oS/-oG <file>: Output scan in normal, XML, s|<rIpt kIddi3,
     and Grepable format, respectively, to the given filename.
  -oA <basename>: Output in the three major formats at once
  -v: Increase verbosity level (use -vv or more for greater effect)
  -d: Increase debugging level (use -dd or more for greater effect)
  --reason: Display the reason a port is in a particular state
  --open: Only show open (or possibly open) ports
  --packet-trace: Show all packets sent and received
  --iflist: Print host interfaces and routes (for debugging)
  --append-output: Append to rather than clobber specified output files
  --resume <filename>: Resume an aborted scan
  --noninteractive: Disable runtime interactions via keyboard
  --stylesheet <path/URL>: XSL stylesheet to transform XML output to HTML
  --webxml: Reference stylesheet from Nmap.Org for more portable XML
  --no-stylesheet: Prevent associating of XSL stylesheet w/XML output
MISC:
  -6: Enable IPv6 scanning
  -A: Enable OS detection, version detection, script scanning, and traceroute
  --datadir <dirname>: Specify custom Nmap data file location
  --send-eth/--send-ip: Send using raw ethernet frames or IP packets
  --privileged: Assume that the user is fully privileged
  --unprivileged: Assume the user lacks raw socket privileges
  -V: Print version number
  -h: Print this help summary page.
EXAMPLES:
  nmap -v -A scanme.nmap.org
  nmap -v -sn 192.168.0.0/16 10.0.0.0/8
  nmap -v -iR 10000 -Pn -p 80
SEE THE MAN PAGE (https://nmap.org/book/man.html) FOR MORE OPTIONS AND EXAMPLES

자주사용하는 옵션

  • v: verbose 모드. 상세한 설명을 출력해준다.
  • sV: Probe open ports to determine service/version info (서버에서 운영중인 서비스나 버전등을 확인한다.)
  • O: OS 디텍션 기능을 켠다. (이 기능을 켜려면 root권한으로 nmap을 실행해야 한다.)
  • A: OS 디텍션, 버전확인, 스크립트 스캔 기능을 켠다. 이 스캔이 가장 정확하고 상세한 정보를 보여준다. (이 기능을 켜려면 root권한으로 nmap을 실행해야 한다.)
  • D: 디코이 스캔. 스캔하는 IP주소가 세계 여러 곳에서 온 것 처럼 보이게 한다.

-sV 옵션 실행 예

└─$ nmap -sV 192.168.56.102 
Starting Nmap 7.93 ( https://nmap.org ) at 2023-12-06 23:55 EST
Nmap scan report for 192.168.56.102
Host is up (0.0053s latency).
Not shown: 997 closed tcp ports (conn-refused)
PORT    STATE SERVICE      VERSION
135/tcp open  msrpc        Microsoft Windows RPC
139/tcp open  netbios-ssn  Microsoft Windows netbios-ssn
445/tcp open  microsoft-ds Microsoft Windows XP microsoft-ds
Service Info: OSs: Windows, Windows XP; CPE: cpe:/o:microsoft:windows, cpe:/o:microsoft:windows_xp

Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 6.91 seconds

-O 옵션 실행 예

└─$ sudo nmap -O 192.168.56.102
[sudo] password for kali: 
Starting Nmap 7.93 ( https://nmap.org ) at 2023-12-06 23:55 EST
Nmap scan report for 192.168.56.102
Host is up (0.0011s latency).
Not shown: 997 closed tcp ports (reset)
PORT    STATE SERVICE
135/tcp open  msrpc
139/tcp open  netbios-ssn
445/tcp open  microsoft-ds
MAC Address: 08:00:27:5E:2C:05 (Oracle VirtualBox virtual NIC)
Device type: general purpose
Running: Microsoft Windows XP|2003
OS CPE: cpe:/o:microsoft:windows_xp::sp2:professional cpe:/o:microsoft:windows_server_2003
OS details: Microsoft Windows XP Professional SP2 or Windows Server 2003
Network Distance: 1 hop

OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 2.70 seconds


-A 옵션 실행 예

└─$ sudo nmap -A 192.168.56.102
Starting Nmap 7.93 ( https://nmap.org ) at 2023-12-06 23:56 EST
Nmap scan report for 192.168.56.102
Host is up (0.0018s latency).
Not shown: 997 closed tcp ports (reset)
PORT    STATE SERVICE      VERSION
135/tcp open  msrpc        Microsoft Windows RPC
139/tcp open  netbios-ssn  Microsoft Windows netbios-ssn
445/tcp open  microsoft-ds Windows XP microsoft-ds
MAC Address: 08:00:27:5E:2C:05 (Oracle VirtualBox virtual NIC)
Device type: general purpose
Running: Microsoft Windows XP|2003
OS CPE: cpe:/o:microsoft:windows_xp::sp2:professional cpe:/o:microsoft:windows_server_2003
OS details: Microsoft Windows XP Professional SP2 or Windows Server 2003
Network Distance: 1 hop
Service Info: OSs: Windows, Windows XP; CPE: cpe:/o:microsoft:windows, cpe:/o:microsoft:windows_xp

Host script results:
|_smb2-time: Protocol negotiation failed (SMB2)
|_clock-skew: mean: -4h30m00s, deviation: 6h21m50s, median: -9h00m00s

...생략

NSE Script

  • Nmap에는 기본 기능에서 제공하지 않는 기능을 확장할 수 있다.
  • Nmap Scripting Engine (NSE) Script (이하, NSE스크립트)가 그것이다.
  • NSE에는 카테고리라는 개념이 있다.
  • 수행하는 작업의 종류에 따라 auth, broadcast, default. discovery, dos, exploit, external, fuzzer, intrusive, malware, safe, version, vuln 등이 있다.
  • 예를들면 특정 취약점에 대한 스캔 기능등은 vuln 카테고리로 배포된다.
  • lua 라는 언어로 개발할 수 있다고 한다.
  • 따라서 lua 실행환경이 미리 설치되어 있을 필요가 있다.
  • nmap 최신버전을 설치하면 lua 실행환경도 함께 설치된다.
  • .nse 라는 파일 확장자를 가진다.

NSE스크립트 위치 확인

  • nmap이 인식하는 NSE 스크립트 디렉토리는 /usr/share/nmap/scripts/ 이다.
  • 이 디렉토리에 nse 스크립트를 복사해둘 필요가 있다.
  • locate 커맨드를 사용할 수도 있다.
  • (locate가 설치되어 있지 않다면 다음 커맨드를 참고한다. )
    sudo yum install mlocate
    sudo updatedb
    
locate *.nse 

NSE스크립트 설치하기

  1. wget 등으로 nse 스크립트를 다운로드 받는다 .
  2. 다운로드 받은 스크립트를 /usr/share/nmap/scripts/ 에 복사한다 .
  3. nmap –script-updatedb 커맨드로 nmap의 scriptdb를 업데이트 해준다. (이렇게 해야 nmap이 모듈로서 인식할 수 있는 것 같다.)

NSE스크립트 사용법

  • -script 옵션을 사용해서 사용할 수 있다.