Update hostname-discovery.py
Added the addition of the discovered host in the /etc/hosts file if the entry is not present
This commit is contained in:
parent
fd87c99abc
commit
70d932d772
|
|
@ -2,6 +2,8 @@ from autorecon.plugins import ServiceScan
|
||||||
import requests
|
import requests
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
import urllib3
|
import urllib3
|
||||||
|
import os
|
||||||
|
import ipaddress
|
||||||
|
|
||||||
urllib3.disable_warnings()
|
urllib3.disable_warnings()
|
||||||
|
|
||||||
|
|
@ -30,6 +32,32 @@ class RedirectHostnameDiscovery(ServiceScan):
|
||||||
if redirect_host:
|
if redirect_host:
|
||||||
service.info(f"[+] Redirect detected: {url} → {location}")
|
service.info(f"[+] Redirect detected: {url} → {location}")
|
||||||
service.info(f"[+] Hostname found in redirect: {redirect_host}")
|
service.info(f"[+] Hostname found in redirect: {redirect_host}")
|
||||||
|
|
||||||
|
# Add to /etc/hosts if not already present (with tab) and only for valid IP-hostname pair
|
||||||
|
hosts_path = '/etc/hosts'
|
||||||
|
ip = service.target.address
|
||||||
|
|
||||||
|
# Check if ip is a valid IP address and not equal to the hostname
|
||||||
|
try:
|
||||||
|
ipaddress.ip_address(ip)
|
||||||
|
if redirect_host and redirect_host != ip:
|
||||||
|
entry = f"{ip}\t{redirect_host}"
|
||||||
|
try:
|
||||||
|
# Read existing entries
|
||||||
|
with open(hosts_path, 'r') as f:
|
||||||
|
hosts_data = f.read()
|
||||||
|
# Check if entry already exists (tab or space, for safety)
|
||||||
|
if entry not in hosts_data and f"{ip} {redirect_host}" not in hosts_data:
|
||||||
|
with open(hosts_path, 'a') as f:
|
||||||
|
f.write(f"\n{entry}\n")
|
||||||
|
service.info(f"[+] Added {redirect_host} to /etc/hosts for {ip}")
|
||||||
|
else:
|
||||||
|
service.info(f"[+] {redirect_host} already present in /etc/hosts for {ip}")
|
||||||
|
else:
|
||||||
|
service.info(f"[+] Skipped adding /etc/hosts entry for invalid or duplicate IP-hostname pair ({ip}, {redirect_host})")
|
||||||
|
except ValueError:
|
||||||
|
service.info(f"[+] Skipped adding /etc/hosts entry because {ip} is not a valid IP address.")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
service.info(f"[+] Redirect detected, but no hostname could be parsed: {location}")
|
service.info(f"[+] Redirect detected, but no hostname could be parsed: {location}")
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue