信息安全很重要,你的地理位置可能暴露了!!!
使用python和GeoLite2獲取目標的地理位置
1 #! /usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 ''' 5 Created on 2019年12月8日 6 7 @author: Admin 8 ''' 9 10 from copy import copy 11 import optparse 12 import re 13 14 import geoip2.database 15 16 17 reader = geoip2.database.Reader('GeoLite2-City.mmdb') 18 19 # 查詢IP地址對應的物理地址 20 def ip_get_location(ip_address): 21 # 載入指定IP相關數據 22 response = reader.city(ip_address) 23 24 #讀取國家代碼 25 Country_IsoCode = response.country.iso_code 26 #讀取國家名稱 27 Country_Name = response.country.name 28 #讀取國家名稱(中文顯示) 29 Country_NameCN = response.country.names['zh-CN'] 30 #讀取州(國外)/省(國內)名稱 31 Country_SpecificName = response.subdivisions.most_specific.name 32 #讀取州(國外)/省(國內)代碼 33 Country_SpecificIsoCode = response.subdivisions.most_specific.iso_code 34 #讀取城市名稱 35 City_Name = response.city.name 36 #讀取郵政編碼 37 City_PostalCode = response.postal.code 38 #獲取緯度 39 Location_Latitude = response.location.latitude 40 #獲取經度 41 Location_Longitude = response.location.longitude 42 43 if(Country_IsoCode == None): 44 Country_IsoCode = "None" 45 if(Country_Name == None): 46 Country_Name = "None" 47 if(Country_NameCN == None): 48 Country_NameCN = "None" 49 if(Country_SpecificName == None): 50 Country_SpecificName = "None" 51 if(Country_SpecificIsoCode == None): 52 Country_SpecificIsoCode = "None" 53 if(City_Name == None): 54 City_Name = "None" 55 if(City_PostalCode == None): 56 City_PostalCode = "None" 57 if(Location_Latitude == None): 58 Location_Latitude = "None" 59 if(Location_Longitude == None): 60 Location_Longitude = "None" 61 62 print('================Start===================') 63 print('[*] Target: ' + ip_address + ' GeoLite2-Located ') 64 print(u' [+] 國家編碼: ' + Country_IsoCode) 65 print(u' [+] 國家名稱: ' + Country_Name) 66 print(u' [+] 國家中文名稱: ' + Country_NameCN) 67 print(u' [+] 省份或州名稱: ' + Country_SpecificName) 68 print(u' [+] 省份或州編碼: ' + Country_SpecificIsoCode) 69 print(u' [+] 城市名稱 : ' + City_Name) 70 print(u' [+] 城市郵編 : ' + City_PostalCode) 71 print(u' [+] 緯度: ' + str(Location_Latitude)) 72 print(u' [+] 經度 : ' + str(Location_Longitude)) 73 print('===============End======================') 74 75 # 檢驗和處理ip地址 76 def seperate_ip(ip_address): 77 ip_match = r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)\.)(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)$" 78 ip_match_list = r"^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)\.)(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2}(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9])-(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[1-9]|0?[1-9]0)$" 79 80 if re.match(ip_match, ip_address): 81 try: 82 ip_get_location(ip_address) 83 except Exception as e: 84 print e 85 elif re.match(ip_match_list, ip_address): 86 ip_start = ip_address.split('-')[0].split('.')[3] 87 ip_end = ip_address.split('-')[1] 88 # 如果ip地址范圍一樣,則直接執行 89 if(ip_start == ip_end): 90 try: 91 seperate_ip(ip_address.split('-')[0]) 92 except Exception as e: 93 print e 94 elif ip_start > ip_end: 95 print 'the value of ip, that you input, has been wrong! try again!' 96 exit(0) 97 else: 98 ip_num_list = ip_address.split('-')[0].split('.') 99 ip_num_list.pop() 100 for ip_last in range(int(ip_start), int(ip_end)+1): 101 ip_add = '.'.join(ip_num_list)+'.'+str(ip_last) 102 try: 103 ip_get_location(ip_add) 104 except Exception as e: 105 print e 106 else: 107 print 'Wrong type of ip address!' 108 print '100.8.11.58 100.8.11.58-100 alike!' 109 110 # 主方法數體 111 def main(): 112 parser = optparse.OptionParser('Usage%prog -i <single ip address> -I <multi ip address> \n \n{ Egg: python getIpLocation.py -i(I) 100.8.11.58(100.8.11.58-100) }') 113 parser.add_option('-i', dest='SIp',type='string', help='specify pcap filename') 114 parser.add_option('-I', dest='MIp',type='string', help='specify pcap filename') 115 (options, args) = parser.parse_args() 116 if options.SIp == None and options.MIp == None: 117 print (parser.usage) 118 exit(0) 119 ip_address = options.SIp 120 ip_address_multi = options.MIp 121 if ip_address==None: 122 seperate_ip(ip_address_multi) 123 else: 124 seperate_ip(ip_address) 125 126 if __name__ == '__main__': 127 main()
結果: