GO KALI FREE
IntermediateOSINT

Nmap Advanced Techniques: Expert Network Scanning

Master advanced Nmap scanning techniques including NSE scripting, firewall evasion, performance tuning, and custom scan types for professional penetration testing.

#Nmap#Network Scanning#NSE#Penetration Testing#Network Security

Beyond Basic Nmap

If you have mastered basic Nmap scans like -sV and -sC, it is time to explore advanced techniques. Below you will find the Nmap Scripting Engine (NSE), advanced scan types, performance optimization, firewall evasion, and output processing that professional penetration testers rely on daily.

Prerequisites

Before studying advanced Nmap, you should understand:

  • **Nmap Beginner Tutorial** — Basic scanning flags and usage
  • **Networking Basics** — TCP flags, port states, protocols
  • **Linux Commands Explained** — Shell scripting, output redirection
  • **Firewall Fundamentals** — Packet filtering rules
  • Advanced Scan Types

    TCP Null, FIN, and Xmas Scans

    These scan types exploit TCP RFC behavior to bypass firewalls:

    # Null scan — no flags set
    nmap -sN target.com
    
    # FIN scan — only FIN flag
    nmap -sF target.com
    
    # Xmas scan — FIN, PSH, URG flags
    nmap -sX target.com
    

    Open ports on RFC-compliant systems send no response. Closed ports send RST. These do not work against Windows, which always sends RST.

    Idle Scan (-sI)

    A stealth scan using a zombie host to mask your IP:

    # Find a suitable zombie host
    nmap -p 80 --script ipidseq zombie-host.com
    
    # Perform idle scan through the zombie
    nmap -sI zombie-host.com target.com
    

    FTP Bounce Scan (-b)

    nmap -b ftpuser:ftppass@ftp-server.com target.com
    

    Nmap Scripting Engine (NSE)

    Script Categories

    # List all categories
    nmap --script-help all | grep "Categories:"
    
    # auth — Authentication credential testing
    # broadcast — Network broadcast discovery
    # brute — Credential brute forcing
    # default — Default script set (-sC)
    # discovery — Service and host discovery
    # exploit — Exploit modules
    # safe — Non-disruptive scripts
    # vuln — Vulnerability detection
    

    Using NSE Scripts

    # Run specific scripts
    nmap --script http-enum,http-headers target.com
    
    # Run all scripts in a category
    nmap --script "vuln" target.com
    
    # Run safe discovery scripts
    nmap --script "safe and discovery" target.com
    
    # Run scripts with arguments
    nmap --script http-brute --script-args "http-brute.path=/admin,userdb=users.txt,passdb=pass.txt" target.com
    

    Custom NSE Script Development

    description = [[Checks if the web server version is vulnerable]]
    
    author = "KaliGo User"
    license = "Same as Nmap"
    categories = {"safe", "discovery"}
    
    local http = require "http"
    
    portrule = function(host, port)
      return port.protocol == "tcp" and port.number == 80
    end
    
    action = function(host, port)
      local response = http.get(host, port, "/")
      if not response or not response.headers then
        return nil
      end
    
      local server = response.headers["server"] or "Unknown"
      local vulnerable = {
        ["Apache/2.4.49"] = "CVE-2021-41773"
      }
    
      for version, cve in pairs(vulnerable) do
        if server:find(version) then
          return string.format("VULNERABLE: %s", cve)
        end
      end
      return string.format("Server: %s (not vulnerable)", server)
    end
    

    Firewall Evasion Techniques

    Packet Fragmentation

    # Fragment into 8-byte pieces
    nmap -f target.com
    
    # Custom MTU
    nmap --mtu 16 target.com
    

    Decoy Scans

    # Random decoys
    nmap -D RND:10 target.com
    
    # Specific decoy IPs
    nmap -D 192.168.1.10,10.0.0.1,me target.com
    

    Source Port Manipulation

    # Use source port that may be allowed by firewall
    nmap --source-port 53 target.com
    nmap --source-port 20 target.com
    

    Custom Timing

    # Slow, stealthy scan
    nmap -T1 --max-retries 1 --randomize-hosts --data-length 100 target.com
    

    Performance Optimization

    Timing Templates

    # -T0 Paranoid — Very slow, IDS evasion
    # -T1 Sneaky — Slow, IDS evasion
    # -T2 Polite — Slower, less bandwidth
    # -T3 Normal — Default
    # -T4 Aggressive — Fast, assumes fast network
    # -T5 Insane — Very fast, may miss ports
    
    nmap -T4 target.com
    

    Fine-Tuning

    nmap --min-hostgroup 64 --max-hostgroup 256 target.com
    nmap --min-parallelism 10 --max-parallelism 20 target.com
    nmap --min-rtt-timeout 100ms --max-rtt-timeout 1000ms target.com
    nmap --host-timeout 30m target.com
    

    Real-World Scenarios

    Internal Network Penetration Test

    # Phase 1: Host discovery
    nmap -sn 192.168.1.0/24 -oA live_hosts
    
    # Phase 2: Quick port scan
    nmap -sS -T4 -p- --min-rate=10000 -iL live_hosts.gnmap -oA all_ports
    
    # Phase 3: Service detection
    nmap -sV -sC -T4 -iL live_hosts.gnmap -p $(paste -sd, open_ports.txt) -oA services
    
    # Phase 4: Vulnerability scanning
    nmap --script vuln -iL live_hosts.gnmap -p $(paste -sd, open_ports.txt) -oA vulns
    

    Common Mistakes

    Using too aggressive timing: -T5 can cause packet loss and false negatives.

    Not accounting for rate limiting: Some services rate-limit. Use --scan-delay.

    Ignoring firewall responses: ICMP unreachable or TCP RST from firewalls provide useful intelligence.

    Not verifying results: False positives and negatives occur. Verify with other tools.

    Best Practices

  • **Start broad, then deep** — Begin with wide discovery, then focus
  • **Use version detection (-sV) with intensity** --version-intensity 9
  • **Save output in all formats** — Use -oA
  • **Use safe scripts first** — Intrusive scripts may crash services
  • **Combine with other tools** — Validate findings with netcat, curl
  • Related Tools

  • **Masscan** — Faster scanning for large networks
  • **Netcat** — Manual service probing
  • **Zenmap** — Graphical Nmap interface
  • **Ndiff** — Compare scan results over time
  • Related Articles

  • Nmap Beginner Tutorial
  • Networking Basics
  • Ethical Hacking Fundamentals
  • Subdomain Enumeration
  • Summary

    Advanced Nmap techniques include specialized scan types (Null, FIN, Xmas, Idle), NSE scripting for vulnerability detection, firewall evasion through fragmentation and decoys, and performance tuning. Proper output processing maximizes the value of scans.

    Knowledge Check

  • How does an Idle scan (-sI) differ from a regular SYN scan?
  • What is the purpose of the -f flag and how does it evade firewalls?
  • Name three NSE script categories and their uses.
  • Why does -T5 sometimes produce less accurate results?
  • How can decoy scans (-D) help during reconnaissance?
  • Frequently Asked Questions

    What is the Nmap Scripting Engine (NSE)?

    NSE is Nmap's built-in scripting engine that extends scanning with Lua scripts for vulnerability detection, brute forcing, and service discovery. Scripts are organized into categories like vuln, brute, and discovery. See the [Nmap Beginner Tutorial](/learn/nmap-beginner-tutorial) for basic scanning.

    How does an idle scan (-sI) work?

    An idle scan uses a zombie host to mask your IP address. Nmap measures the zombie's IP ID sequence number before and after probing the target. Changes in the zombie's ID indicate the target's port state, making the scan appear to come from the zombie.

    What is the difference between -sN, -sF, and -sX scans?

    Null scan (-sN) sends no TCP flags, FIN scan (-sF) sends only the FIN flag, and Xmas scan (-sX) sends FIN, PSH, and URG flags. These exploit RFC behavior where open ports don't respond and closed ports send RST. They don't work against Windows.

    How does packet fragmentation evade firewalls?

    The -f flag fragments packets into 8-byte pieces, splitting TCP headers across multiple fragments. Some firewalls and IDS systems fail to reassemble fragmented packets properly, allowing the scan to bypass filtering rules.

    What do decoy scans (-D) accomplish?

    Decoy scans mix your traffic with fake source IPs, making it harder for defenders to identify the real attacker. Use RND for random decoys or specify IPs. Ensure the 'me' keyword includes your real IP so you receive responses.

    Why does -T5 sometimes produce less accurate results?

    -T5 (Insane) sends packets as fast as possible, which can overwhelm targets and cause packet loss, leading to false negatives. Use -T4 for fast scanning with reliability, and -T1 or -T2 when stealth is required.

    How do you write a custom NSE script?

    Custom NSE scripts are written in Lua with a portrule function (determines when to run) and an action function (performs the check). Use the http library for web requests. Scripts go in ~/.nmap/scripts/ and run with --script.

    What is the FTP bounce scan (-b)?

    FTP bounce scan abuses the PORT command in FTP to scan targets through an FTP server. The FTP server connects to the target on your behalf, masking your IP. This technique rarely works on modern FTP servers due to security restrictions.

    How do timing templates affect scan performance?

    Timing templates (-T0 through -T5) control scan speed. -T0 is paranoid (IDS evasion), -T3 is default, and -T5 is insane. Higher speeds sacrifice accuracy and stealth for speed. For most engagements, -T3 or -T4 is appropriate.

    What output formats does Nmap support?

    Nmap supports -oN (normal text), -oX (XML for Metasploit import), -oG (grepable), and -oA (all formats). XML output is most useful for automated processing. Always use -oA to save results in every format for later analysis.