RELEASE: Version 1.0 (#3)

This commit is contained in:
2022-06-25 22:27:01 +01:00
committed by GitHub
parent 374d77a2d1
commit e374dc195b
15 changed files with 354 additions and 3 deletions

0
app/__init__.py Normal file
View File

5
app/_version.py Normal file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env python3
"""MODULE: Specifies app version."""
VERSION = "1.0"

33
app/args.py Normal file
View File

@@ -0,0 +1,33 @@
#!/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:
"""Get arguments from user via the command line."""
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()

28
app/ip_info.py Normal file
View File

@@ -0,0 +1,28 @@
#!/usr/bin/env python3
"""MODULE: Provides functions to call various APIs to retrieve IP/prefix information."""
import ipaddress
import requests
def get_ip_information(ipv4_address: ipaddress.IPv4Address) -> dict:
"""Retrieves information about a given IPv4 address from IP-API.com."""
api_endpoint = f"http://ip-api.com/json/{ipv4_address}"
resp = requests.get(api_endpoint).json()
return resp
def get_autonomous_system_number(as_info: str) -> str:
"""Parses AS number from provided AS information."""
as_number = as_info.split(" ")[0]
return as_number
def get_prefix_information(autonomous_system: int) -> list:
"""Retrieves prefix information about a given autonomous system."""
api_endpoint = f"https://api.hackertarget.com/aslookup/?q={str(autonomous_system)}"
resp = requests.get(api_endpoint).text
prefixes = resp.split("\n")
prefixes.pop(0)
return prefixes

View File

@@ -1,8 +1,65 @@
#!/usr/local/bin/python3
"""MODULE: Main application module."""
import sys
from app.args import parse_args
from app.print_table import print_table, generate_prefix_string
from app.query_normalisation import is_ip_address, resolve_domain_name
from app.ip_info import ( # pragma: no cover
get_ip_information,
get_autonomous_system_number,
get_prefix_information,
)
HEADER = """-----------------------------------------------
| IP Address Information Lookup Tool (iPilot) |
| By Luke Tainton (@luketainton) |
-----------------------------------------------\n"""
def main():
# Commands here
"""Main function."""
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 = get_ip_information(ip_address)
as_number = get_autonomous_system_number(ip_info.get("as"))
# Assemble list for table generation
table_data = [
["IP Address", ip_info.get("query")],
["Organization", ip_info.get("org")],
[
"Location",
f"{ip_info.get('country')}/{ip_info.get('regionName')}/{ip_info.get('city')}",
],
["Timezone", ip_info.get("timezone")],
["Internet Service Provider", ip_info.get("isp")],
["Autonomous System", as_number],
]
# If wanted, get prefix information
if args.prefixes:
prefix_info = get_prefix_information(as_number)
table_data.append(["Prefixes", generate_prefix_string(prefix_info)])
print_table(table_data)
if __name__ == "__main__":
main()

22
app/print_table.py Normal file
View 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:
"""Generate a string that spilts prefixes into rows of 4."""
num_per_row = 4
try:
ret = ""
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:
"""Print table generated by tabulate."""
print(tabulate(table_data))

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env python3
"""MODULE: Provides functions that ensure an IP address is available to query the APIs for."""
import socket
import ipaddress
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:
"""Resolve a domain name via DNS or return None."""
try:
ip_address = socket.gethostbyname(domain_name)
except socket.gaierror:
ip_address = None
return ip_address
def get_public_ip() -> ipaddress.IPv4Address:
"""Get the user's current public IPv4 address."""
ip_address = requests.get("https://api.ipify.org").text
return ip_address