0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
40
app/args.py
Normal file
40
app/args.py
Normal file
@ -0,0 +1,40 @@
|
||||
#!/usr/local/env python3
|
||||
|
||||
"""MODULE: Provides CLI arguments to the application."""
|
||||
|
||||
import argparse
|
||||
|
||||
from app.query_normalisation import get_public_ip
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace: # pragma: no cover
|
||||
"""Get arguments from user via the command line.
|
||||
|
||||
Args:
|
||||
None
|
||||
|
||||
Returns:
|
||||
argparse.Namespace: parsed arguments
|
||||
"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Query information about an IP address or domain name."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-q",
|
||||
"--query",
|
||||
help="IP/domain name to query (default: current public IP)",
|
||||
default=get_public_ip(),
|
||||
)
|
||||
parser.add_argument(
|
||||
"-p",
|
||||
"--prefixes",
|
||||
help="show advertised prefixes",
|
||||
action="store_true",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--noheader",
|
||||
help="do not print header",
|
||||
action="store_true",
|
||||
)
|
||||
return parser.parse_args()
|
60
app/ip_info.py
Normal file
60
app/ip_info.py
Normal file
@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""MODULE: Provides functions to call various APIs to retrieve IP/prefix information."""
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import ipaddress
|
||||
import requests
|
||||
|
||||
|
||||
def get_ip_information(ipv4_address: ipaddress.IPv4Address) -> Optional[dict]:
|
||||
"""Retrieves information about a given IPv4 address from IP-API.com.
|
||||
|
||||
Args:
|
||||
ipv4_address (ipaddress.IPv4Address): IPv4 address to query
|
||||
|
||||
Returns:
|
||||
Optional[dict]: API response
|
||||
"""
|
||||
api_endpoint: str = f"http://ip-api.com/json/{ipv4_address}"
|
||||
try:
|
||||
resp: requests.Response = requests.get(api_endpoint, timeout=10)
|
||||
resp.raise_for_status()
|
||||
ret: dict | None = resp.json() if resp.json().get("status") == "success" else None
|
||||
except (requests.exceptions.JSONDecodeError, requests.exceptions.HTTPError):
|
||||
ret = None
|
||||
return ret
|
||||
|
||||
|
||||
def get_autonomous_system_number(as_info: str) -> str:
|
||||
"""Parses AS number from provided AS information.
|
||||
|
||||
Args:
|
||||
as_info (str): AS information
|
||||
|
||||
Returns:
|
||||
str: AS number
|
||||
"""
|
||||
as_number: str = as_info.split(" ")[0]
|
||||
return as_number
|
||||
|
||||
|
||||
def get_prefix_information(autonomous_system: str) -> Optional[list]:
|
||||
"""Retrieves prefix information about a given autonomous system.
|
||||
|
||||
Args:
|
||||
autonomous_system (str): autonomous system to query, e.g. AS123
|
||||
|
||||
Returns:
|
||||
Optional[list]: API response
|
||||
"""
|
||||
api_endpoint: str = f"https://api.hackertarget.com/aslookup/?q={str(autonomous_system)}"
|
||||
try:
|
||||
resp: requests.Response = requests.get(api_endpoint, timeout=10)
|
||||
resp.raise_for_status()
|
||||
except requests.exceptions.HTTPError:
|
||||
return None
|
||||
prefixes: list[str] = resp.text.split("\n")
|
||||
prefixes.pop(0)
|
||||
return prefixes
|
76
app/main.py
Normal file
76
app/main.py
Normal file
@ -0,0 +1,76 @@
|
||||
#!/usr/local/bin/python3
|
||||
|
||||
"""MODULE: Main application module."""
|
||||
|
||||
import sys
|
||||
|
||||
from app.args import parse_args
|
||||
from app.ip_info import (
|
||||
get_autonomous_system_number,
|
||||
get_ip_information,
|
||||
get_prefix_information,
|
||||
)
|
||||
from app.print_table import generate_prefix_string, print_table
|
||||
from app.query_normalisation import is_ip_address, resolve_domain_name
|
||||
|
||||
HEADER = """-----------------------------------------------
|
||||
| IP Address Information Lookup Tool (iPilot) |
|
||||
| By Luke Tainton (@luketainton) |
|
||||
-----------------------------------------------\n"""
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Main function.
|
||||
|
||||
Args:
|
||||
None
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
args = parse_args()
|
||||
if not args.noheader:
|
||||
print(HEADER)
|
||||
|
||||
# Set IP to passed IP address, or resolve passed domain name to IPv4
|
||||
ip_address = resolve_domain_name(args.query) if not is_ip_address(args.query) else args.query
|
||||
|
||||
# If not given an IPv4, and can't resolve to IPv4, then throw error and exit
|
||||
if not ip_address:
|
||||
print("ERROR: could not resolve query to IPv4 address.")
|
||||
sys.exit(1)
|
||||
|
||||
# Get information from API
|
||||
ip_info: dict | None = get_ip_information(ip_address)
|
||||
if not ip_info:
|
||||
print("ERROR: could not retrieve IP information from API.")
|
||||
sys.exit(1)
|
||||
as_number: str = get_autonomous_system_number(ip_info["as"])
|
||||
|
||||
# Assemble list for table generation
|
||||
country: str = ip_info["country"]
|
||||
region: str = ip_info["regionName"]
|
||||
city: str = ip_info["city"]
|
||||
table_data: list = [
|
||||
["IP Address", ip_info["query"]],
|
||||
["Organization", ip_info["org"]],
|
||||
["Location", f"{country}/{region}/{city}"],
|
||||
["Timezone", ip_info["timezone"]],
|
||||
["Internet Service Provider", ip_info["isp"]],
|
||||
["Autonomous System", as_number],
|
||||
]
|
||||
|
||||
# If wanted, get prefix information
|
||||
if args.prefixes:
|
||||
prefix_info = get_prefix_information(as_number)
|
||||
if not prefix_info:
|
||||
print("ERROR: could not retrieve prefix information from API.")
|
||||
sys.exit(1)
|
||||
else:
|
||||
table_data.append(["Prefixes", generate_prefix_string(prefix_info)])
|
||||
|
||||
print_table(table_data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
22
app/print_table.py
Normal file
22
app/print_table.py
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""MODULE: Provides functions for preparing, then printing, retrieved data."""
|
||||
|
||||
from tabulate import tabulate
|
||||
|
||||
|
||||
def generate_prefix_string(prefixes: list) -> str | None:
|
||||
"""Generate a string that spilts prefixes into rows of 4."""
|
||||
num_per_row = 4
|
||||
try:
|
||||
ret: str = ""
|
||||
for i in range(0, len(prefixes), num_per_row):
|
||||
ret += ", ".join(prefixes[i : i + num_per_row]) + "\n"
|
||||
return ret
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
|
||||
def print_table(table_data) -> None: # pragma: no cover
|
||||
"""Print table generated by tabulate."""
|
||||
print(tabulate(table_data))
|
34
app/query_normalisation.py
Normal file
34
app/query_normalisation.py
Normal file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""MODULE: Provides functions that ensure an IP address is
|
||||
available to query the APIs for."""
|
||||
|
||||
import ipaddress
|
||||
import socket
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def is_ip_address(query: str) -> bool:
|
||||
"""Verifies if a given query is a valid IPv4 address."""
|
||||
try:
|
||||
ipaddress.ip_address(query)
|
||||
return True
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
|
||||
def resolve_domain_name(domain_name: str) -> ipaddress.IPv4Address | None:
|
||||
"""Resolve a domain name via DNS or return None."""
|
||||
try:
|
||||
result: str = socket.gethostbyname(domain_name)
|
||||
ip_address: ipaddress.IPv4Address = ipaddress.IPv4Address(result)
|
||||
return ip_address
|
||||
except (socket.gaierror, ipaddress.AddressValueError):
|
||||
return None
|
||||
|
||||
|
||||
def get_public_ip() -> ipaddress.IPv4Address:
|
||||
"""Get the user's current public IPv4 address."""
|
||||
ip_address: str = requests.get("https://api.ipify.org", timeout=10).text
|
||||
return ipaddress.IPv4Address(ip_address)
|
Reference in New Issue
Block a user