Python批量化获取ip地址归属地

2023-05-02 11:22:02 浏览数 (1)

前言

今天碰到个需求,拿nuclei扫出的洞需要判断一下是哪些国家的目标,这总不能手工一个个去看吧,好几千个目标一个个看那简直蠢到不行,既然这样就拿python写一个脚本来自动化判断咯。

实现

ip2region: https://github.com/lionsoul2014/ip2region

主要实现方法是通过开源的ip2region库,这个库可以准确的识别IP地址的归属地

代码中的from ip2Region import Ip2Region可能会爆红,忽略即可。

代码语言:javascript复制
# -*- coding:utf-8 -*-
import re
import socket
import time
from urllib.parse import urlparse

import xlwt
from ip2Region import Ip2Region

def testSearch(ip_s):
    dbFile = "./ip2region.db"
    searcher = Ip2Region(dbFile)
    try:
        print("开始检测:", (ip_s))
        sTime = time.time() * 1000
        data = searcher.binarySearch(ip_s)
        # elif algorithm == "memory":
        #     data = searcher.memorySearch(line)
        # else:
        #     data = searcher.btreeSearch(line)
        eTime = time.time() * 1000
        ip_info = ("%s|%s|%s" % (ip_s, data["city_id"], data["region"].decode('utf-8')))
        print("检测完成:"   ip_info)
        return ip_info

    except Exception as e:
        print("[Error]: %s" % e)
    searcher.close()

def all_in():
    row_id = 1
    book = xlwt.Workbook()
    sheet = book.add_sheet('sheet')
    title = ['源ip', '国家', '省市', '运营商']
    print("正在提取ip")
    patternIp()
    print("正在提取域名")
    patterDomain()
    for col in range(len(title)):
        sheet.write(0, col, title[col])
    with open('ip.txt', 'r') as file:
        for line in file.readlines():
            ip = line.strip()
            try:
                data = testSearch(ip)
                ct = data.split('|')[2].strip()
                pv = data.split('|')[4].strip()
                city = data.split('|')[5].strip()
                yys = data.split('|')[6].strip()
                if ct == "0":
                    print("地址库中未找到对应的IP归属地,请更新地址库或者确定ip准确性!")
                    sheet.write(row_id, 0, ip)
                    sheet.write(row_id, 1, "/")
                    sheet.write(row_id, 2, "/")
                    sheet.write(row_id, 3, "/")
                    row_id  = 1
                else:
                    if pv == "0":
                        print("省市查询为空!")
                        sheet.write(row_id, 0, ip)
                        sheet.write(row_id, 1, ct)
                        sheet.write(row_id, 2, "/")
                        sheet.write(row_id, 3, "/")
                        row_id  = 1
                    else:
                        if city == "0":
                            sheet.write(row_id, 0, ip)
                            sheet.write(row_id, 1, ct)
                            sheet.write(row_id, 2, pv   "-"   "///")
                            sheet.write(row_id, 3, "/")
                            row_id  = 1
                        else:
                            if yys == "0":
                                sheet.write(row_id, 0, ip)
                                sheet.write(row_id, 1, ct)
                                sheet.write(row_id, 2, pv   "-"   city)
                                sheet.write(row_id, 3, "/")
                                row_id  = 1
                            else:
                                sheet.write(row_id, 0, ip)
                                sheet.write(row_id, 1, ct)
                                sheet.write(row_id, 2, pv   "-"   city)
                                sheet.write(row_id, 3, yys)
                                row_id  = 1
            except Exception as e:
                print("[Error]: %s" % e)
                sheet.write(row_id, 0, ip)
                sheet.write(row_id, 1, "检测异常,请手动检测!")
                row_id  = 1
    book.save('score.xls')

def patternIp():
    reg = r"b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)b"
    with open('wso2-upload.txt', 'r') as f:
        ipList = re.findall(reg, f.read())

    with open('ip.txt', "w") as i:
        for ip in ipList:
            i.write(ip   "n")

def domain2Ip(domain):
    global address
    try:
        time.sleep(0.1)
        address = socket.getaddrinfo(domain, 'http')
    except Exception as e:
        print(e)
    return address[0][4][0]

def patterDomain():
    reg = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.& ]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F])) '
    with open('wso2-upload.txt', 'r') as f:
        urlList = re.findall(reg, f.read())

    with open("ip.txt", "w") as i:
        for u in urlList:
            i.write(domain2Ip(get_domain(u))   "n")

def get_domain(url):
    o = urlparse(url)
    domain = o.hostname
    return domain

if __name__ == "__main__":
    all_in()
    # patternIp()
    # print(domain2Ip("www.baidu.com"))
    # print(get_domain("https://www.baidu.com"))
    # patterDomain()

项目目录结构:

效果

浏览量: 265

0 人点赞