nmap安裝和使用


nmap安裝和使用

安裝

官網地址 https://nmap.org/download.html

許多流行的Linux發行版(Redhat、Mandrake、Suse等)都使用RPM軟件包管理系統,方便快速安裝二進制軟件包。我們已經寫了一個詳細的[安裝我們的RPM包指南](https://nmap.org/book/inst linux.html\inst安裝-rpm),盡管這些簡單的命令通常能起到以下作用:

Many popular Linux distributions (Redhat, Mandrake, Suse, etc) use the RPM package management system for quick and easy binary package installation. We have written a detailed guide to installing our RPM packages, though these simple commands usually do the trick:

rpm -vhU https://nmap.org/dist/nmap-7.91-1.x86_64.rpm

使用

Map help 可以看到如下列表 但是大部分情況下只用到其中的幾個

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 consecutively - 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
  --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

這里我自己整理了一下常用的命令和解釋方便更容易理解

// 掃描類型
-sT    TCP connect() 掃描,這是最基本的 TCP 掃描方式。這種掃描很容易被檢測到,在目標主機的日志中會記錄大批的連接請求以及錯誤信息。    
-sS    TCP 同步掃描 (TCP SYN),因為不必全部打開一個 TCP 連接,所以這項技術通常稱為半開掃描 (half-open)。這項技術最大的好處是,很少有系統能夠把這記入系統日志。不過,你需要 root 權限來定制 SYN 數據包。    
-sF,-sX,-sN    秘密 FIN 數據包掃描、聖誕樹 (Xmas Tree)、空 (Null) 掃描模式。這些掃描方式的理論依據是:關閉的端口需要對你的探測包回應 RST 包,而打開的端口必需忽略有問題的包(參考 RFC 793 第 64 頁)。    
-sP    ping 掃描,用 ping 方式檢查網絡上哪些主機正在運行。當主機阻塞 ICMP echo 請求包是 ping 掃描是無效的。nmap 在任何情況下都會進行 ping 掃描,只有目標主機處於運行狀態,才會進行后續的掃描。    
-sU    UDP 的數據包進行掃描,如果你想知道在某台主機上提供哪些 UDP(用戶數據報協議,RFC768) 服務,可以使用此選項。    
-sA    ACK 掃描,這項高級的掃描方法通常可以用來穿過防火牆。    
-sW    滑動窗口掃描,非常類似於 ACK 的掃描。    
-sR    RPC 掃描,和其它不同的端口掃描方法結合使用。    
-b    FTP 反彈攻擊 (bounce attack),連接到防火牆后面的一台 FTP 服務器做代理,接着進行端口掃描。
// 掃描參數
-P0    在掃描之前,不 ping 主機。    
-PT    掃描之前,使用 TCP ping 確定哪些主機正在運行。    
-PS    對於 root 用戶,這個選項讓 nmap 使用 SYN 包而不是 ACK 包來對目標主機進行掃描。    
-PI    設置這個選項,讓 nmap 使用真正的 ping(ICMP echo 請求)來掃描目標主機是否正在運行。    
-PB    這是默認的 ping 掃描選項。它使用 ACK(-PT) 和 ICMP(-PI) 兩種掃描類型並行掃描。如果防火牆能夠過濾其中一種包,使用這種方法,你就能夠穿過防火牆。    
-O    這個選項激活對 TCP/IP 指紋特征 (fingerprinting) 的掃描,獲得遠程主機的標志,也就是操作系統類型。    
-I    打開 nmap 的反向標志掃描功能。    
-f    使用碎片 IP 數據包發送 SYN、FIN、XMAS、NULL。包增加包過濾、入侵檢測系統的難度,使其無法知道你的企圖。    
-v    冗余模式。強烈推薦使用這個選項,它會給出掃描過程中的詳細信息。    
-S <IP>    在一些情況下,nmap 可能無法確定你的源地址 (nmap 會告訴你)。在這種情況使用這個選項給出你的 IP 地址。    
-g port    設置掃描的源端口。一些天真的防火牆和包過濾器的規則集允許源端口為 DNS(53) 或者 FTP-DATA(20) 的包通過和實現連接。顯然,如果攻擊者把源端口修改為 20 或者 53,就可以摧毀防火牆的防護。    
-oN    把掃描結果重定向到一個可讀的文件 logfilename 中。    
-oS    掃描結果輸出到標准輸出。    
--host_timeout    設置掃描一台主機的時間,以毫秒為單位。默認的情況下,沒有超時限制。    
--max_rtt_timeout    設置對每次探測的等待時間,以毫秒為單位。如果超過這個時間限制就重傳或者超時。默認值是大約 9000 毫秒。    
--min_rtt_timeout    設置 nmap 對每次探測至少等待你指定的時間,以毫秒為單位。    
-M count    置進行 TCP connect() 掃描時,最多使用多少個套接字進行並行的掃描。 

運行實例

nmap www.baidu.com    #查看主機當前開放的端口
nmap -p 1024-65535 www.baidu.com    #查看主機端口(1024-65535)中開放的端口
nmap -PS www.baidu.com        #探測目標主機開放的端口
nmap -PS22,80,3306  www.baidu.com    #探測該地址所列表出的端口是否開放
nmap -O www.baidu.com    #探測目標主機操作系統類型
nmap -sP www.baidu.com   #探測該地址是否在線
sudo nmap -sS -sV -Pn -v www.baidu.com	# 跳過主機發現探測開放端口

nmap掃描原理介紹

掃描方式

  • 全連接掃描:三次握手 防火牆能有效攔截,故很少使用 (產生大量日志,很少使用)

  • 半鏈接掃描:三次握手前兩次,SYN->SYN/ACK->RST 端口開放 SYN->RST/ACK端口關閉 (不記日志,隱蔽性好)

  • 秘密掃描:發送FIN,返回RST (端口關閉,回復RST包;端口開放,不回復)

  • 半鏈接掃描又叫做間接掃描。FIN掃描、Xmas掃描、Null掃描對Windows無效

整合到定時任務里面進行ip監測

 /**
     * 直接在本地執行 shell
     * 
     * @param commands 執行的腳本
     * @return 執行結果信息
     */
    public static String runLocalShell(String commands) {
        Runtime runtime = Runtime.getRuntime();

        StringBuilder stringBuffer;
        BufferedReader reader;
        Process process;
            stringBuffer = new StringBuilder();
            try {
                process = runtime.exec(commands);
                InputStream inputStream = process.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                String buf;
                while ((buf = reader.readLine()) != null) {
                    //舍棄PID 進程信息
                    if (buf.contains("PID")) {
                        break;
                    }
                    stringBuffer.append(buf.trim()).append(LINE_SEPARATOR);
                }

            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        return stringBuffer.toString();
    }
    
    
   /**
   * 調用方法
   **/
   
    public static String ContrastValue = "Host seems down";
    
    public static String command = "nmap -sP 1.204.69.51";

    /**
     * Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
     * Nmap done: 1 IP address (0 hosts up) scanned in 3.01 seconds
     * @param args
     */
    public static void main(String[] args) {
       String resultCommand = ShellRunUtils.runLocalShell(command);
        if (resultCommand.contains(ContrastValue)){
            System.out.println("主機不在線 Host seems down");
        }else{
            System.out.println("1 host up");
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM