Python Weaponry Development - Weaponry Chapter Whois Information Collection Modularization (45)

Python Weaponry Development - Weaponry Chapter Whois Information Collection Modularization (45)

When we penetrate, we need to conduct comprehensive information collection. In addition to active information collection, we often also conduct passive information collection. Whois information collection is one of them. We can use some websites to collect Whois information, such as Domain name Whois query - Webmaster's Home , for example, as shown in the figure below, we use this website to query some related domain name information about qq.com, as shown in the figure:

Insert image description here

So how do we use Python to develop this function? , the principle of Whois information collection is to use the command line to call the whois command query or to call some websites to query related information. We need to pay attention to two points:

  1. First determine whether the domain name exists
  2. Then check the whois information of this domain name

Here is a simple example script to get the location and whois information of an IP address:

Make sure you have installed the requests library, which can be installed using the following command:

pip install requests
import requests

def get_ip_details(ip):
    url = f"http://ip-api.com/json/{
      
      ip}"
    response = requests.get(url)
    data = response.json()
    
    if data['status'] == 'fail':
        return "无法获取IP信息"
    
    ip_details = {
    
    
        'IP地址': data['query'],
        '所在国家': data['country'],
        '所在城市': data['city'],
        '运营商': data['isp'],
        'ASN': data['as'],
        '是否代理': data['proxy']
    }
    
    return ip_details

def get_whois_info(ip):
    url = f"http://ip-api.com/whois/{
      
      ip}"
    response = requests.get(url)
    whois_info = response.text
    
    return whois_info

if __name__ == "__main__":
    ip = input("请输入IP地址: ")
    ip_details = get_ip_details(ip)
    whois_info = get_whois_info(ip)
    
    print("IP详细信息:")
    for key, value in ip_details.items():
        print(f"{
      
      key}: {
      
      value}")
    
    print("\nWhois信息:")
    print(whois_info)

When running the script, enter the IP address you want to query, and the script will return the IP details and whois information:

Insert image description here

Guess you like

Origin blog.csdn.net/qq_64973687/article/details/135584170