asnmap

repository·main·Indexed 22 days ago

https://github.com/projectdiscovery/asnmap

A Go-based CLI and library used to map network information—including ASNs, Organizations, DNS, or IP addresses—to their corresponding CIDR ranges. Designed for reconnaissance and attack surface expansion, it supports JSON and CSV output and integrates with other ProjectDiscovery tools like tlsx, dnsx, naabu, and nuclei. Requires Go 1.21 or later and a ProjectDiscovery Cloud Platform API token for authentication.

Tokens
2.5K
Snippets
13
Records
13
Agent score
78%

What's inside asnmap

  1. Map ASN, IP, Domain, or Organization to CIDR

    main

    asnmap can take various input types to resolve CIDR ranges. It supports single values, multiple comma-separated values, or reading from a file. It also supports STDIN for piping data.

    Supported input types:

    • ASN: e.g., AS14421
    • DNS/Domain: e.g., example.com
    • IP: e.g., 93.184.216.34
    • ORG: e.g., GOOGLE
    # Using specific flags
    asnmap -a AS45596 -silent
    asnmap -i 100.19.12.21 -silent
    asnmap -d hackerone.com -silent
    asnmap -org GOOGLE -silent
    
    # Using STDIN
    echo GOOGLE | ./asnmap -silent
  2. Integrate asnmap into security workflows

    main

    Since asnmap supports STDIN and provides CIDR ranges, its output can be piped directly into other ProjectDiscovery tools to expand attack surfaces.

    Common patterns include:

    • Piping CIDRs to tlsx for TLS inspection.
    • Piping CIDRs to dnsx for DNS resolution.
    • Piping CIDRs to naabu for port scanning.
    • Piping naabu results into httpx or nuclei.
    echo AS54115 | asnmap | naabu -p 443 | httpx | nuclei -id tech-detect
  3. Handle Authentication and API Keys

    main

    The asnmap client requires an API key to interact with the ProjectDiscovery cloud API.

    Authentication Methods:

    1. Environment Variable: Set the PDCP_API_KEY environment variable. This is the recommended method for programmatic use.
    2. Credential Handler: The client automatically attempts to retrieve credentials using the pdcp.PDCPCredHandler during initialization.

    Errors:

    • If the API key is missing or invalid, the client returns ErrUnAuthorized (which corresponds to a 401 Unauthorized HTTP status).
    # Set the API key in your environment
    export PDCP_API_KEY="your_api_key_here"
  4. Generate JSON output with asnmap

    main

    For automation and post-processing, use the -json flag. This returns a structured JSON object containing the timestamp, input, ASN number, ASN name, country, and an array of CIDR ranges.

    echo hackerone.com | ./asnmap -json -silent | jq
  5. Reference: asnmap CLI flags

    main

    The following flags are available for the asnmap command line tool:

    INPUT:
       -a, -asn string[]     target asn to lookup, example: -a AS5650
       -i, -ip string[]      target ip to lookup, example: -i 100.19.12.21, -i 2a10:ad40::
       -d, -domain string[]  target domain to lookup, example: -d google.com, -d facebook.com
       -org string[]         target organization to lookup, example: -org GOOGLE
       -f, -file string[]    targets to lookup from file
    
    CONFIGURATIONS:
       -config string           path to the asnmap configuration file
       -r, -resolvers string[]  list of resolvers to use
    
    UPDATE:
       -up, -update                 update asnmap to latest version
       -duc, -disable-update-check  disable automatic asnmap update check
    
    OUTPUT:
       -o, -output string  file to write output to
       -j, -json           display json format output
       -c, -csv            display csv format output
       -v6                 display ipv6 cidr ranges in cli output
       -v, -verbose        display verbose output
       -silent             display silent output
       -version            show version of the project
  6. Initialize the asnmap Client with NewClient()

    main

    To use asnmap as a library, initialize a new Client using NewClient(). The client automatically determines the base server URL from the SERVER_URL environment variable or defaults to https://asn.projectdiscovery.io/. The client is configured to ignore expired SSL certificates by default.

    Note: You must provide a valid API key via the PDCP_API_KEY environment variable or through the ProjectDiscovery credential handler for requests to succeed.

    package main
    
    import (
    	"fmt"
    	"github.com/projectdiscovery/asnmap"
    )
    
    func main() {
    	client, err := asnmap.NewClient()
    	if err != nil {
    		panic(err)
    	}
    	fmt.Printf("Client initialized: %+v\n", client)
    }
  7. Perform lookups using GetData()

    main

    The GetData method is the primary way to perform ASN, IP, or Organization lookups. It automatically identifies the input type (ASN, ASNID, IP, or Org) and constructs the appropriate API request.

    Input Types Supported:

    • ASN: e.g., AS1234 (the method will strip the as prefix internally).
    • ASNID: e.g., 1234.
    • IP: e.g., 8.8.8.8.
    • Org: e.g., Google.

    Parameters:

    • input string: The value to query.
    • medatadas ...string: Optional metadata parameters (variadic).

    Returns:

    • []*Response: A slice of response objects containing the lookup results.
    • error: An error if the input type is unknown, the request fails, or the API returns an error (e.g., ErrUnAuthorized).
    // Example: Lookup an IP
    results, err := client.GetData("8.8.8.8")
    if err != nil {
    	// handle error
    }
    
    // Example: Lookup an ASN
    results, err := client.GetData("AS15169")
  8. Configure proxies in the asnmap Client

    main

    You can route asnmap client requests through a proxy using the SetProxy method. This method supports both direct proxy strings and proxy lists (which can include file paths).

    Supported Proxy Schemes:

    • http / https
    • socks5

    Usage Patterns:

    1. Single Proxy String: Pass a URL-formatted proxy string.
    2. Proxy List/File: Pass a slice of strings. If a string is a valid file path, the client will read the file and attempt to use the first valid proxy found within it.

    If no valid proxy can be established, SetProxy returns an error.

    // Using a single proxy string
    _, err := client.SetProxy([]string{"http://proxy.example.com:8080"})
    
    // Using a file containing a list of proxies
    _, err := client.SetProxy([]string{"/path/to/proxies.txt"})
  9. Perform lookups with custom response input using GetDataWithCustomInput()

    main

    If you need the Input field in the returned Response objects to match a specific string rather than the automatically identified input, use GetDataWithCustomInput. This is useful when you want to preserve the exact formatting of your original query in the results.

    // The 'inputToUseInResponse' will be assigned to the 'Input' field of every result
    results, err := client.GetDataWithCustomInput("AS15169", "google_asn")