Skip to content

Commit

Permalink
1.6 release
Browse files Browse the repository at this point in the history
1.6 release
  • Loading branch information
maldevel committed Dec 20, 2015
1 parent a32c732 commit 0619eda
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 173 deletions.
41 changes: 22 additions & 19 deletions README.md
Expand Up @@ -12,16 +12,15 @@ Python 3.x

**Features**
---
* Retrieve Geolocation of IP or Domain.
* Run program with no arguments to get your IP Geolocation.
* Retrieve Geolocation of multiple IPs or Domains loaded from file. Each target in new line.
* Retrieve IP or Domain Geolocation.
* Retrieve your own IP Geolocation.
* Retrieve Geolocation for IPs or Domains loaded from file. Each target in new line.
* Define your own custom User Agent string.
* Select random User-Agent strings from file. Each User Agent string in new line.
* Proxy support.
* Pick a random User-Agent string from file. Each User Agent string in new line.
* Open IP geolocation in Google Maps using the default browser.
* Export results to csv, xml and txt format.


**Geolocation Information**
---
* ASN
Expand All @@ -42,37 +41,41 @@ Python 3.x
---
```
$ ./ip2geolocation.py -h
usage: ip2geolocation.py [-h] [-t host] [-T file] [-u user-agent] [-U file]
[-r] [-g] [-x url] [--csv file] [--xml file]
[-e file]
usage: ip2geolocation.py [-h] [-m] [-t TARGET] [-T file] [-u User-Agent] [-r]
[-U file] [-g] [--no-print] [-v] [-x PROXY] [-e file]
[-ec file] [-ex file]
IPGeoLocation 1.5
IPGeoLocation 1.6
Retrieve IP Geolocation information from http://ip-api.com
optional arguments:
-h, --help show this help message and exit
-t host, --target host
-m, --my-ip Get Geolocation info for my IP address.
-t TARGET, --target TARGET
IP Address or Domain to be analyzed.
-T file, --tlist file
A list of IPs/Domains targets, each target in new line.
-u user-agent, --useragent user-agent
Set the User-Agent request header (default: IP2GeoLocation 1.5).
-u User-Agent, --user-agent User-Agent
Set the User-Agent request header (default: IP2GeoLocation 1.6).
-r Pick User-Agent strings randomly from a file.
-U file, --ulist file
A list of User-Agent strings, each string in new line.
-r Pick User-Agent strings randomly from a file.
-g Open IP location in Google maps with default browser.
-x url, --proxy url Setup proxy server (example: http://127.0.0.1:8080).
--csv file Export results in CSV format.
--xml file Export results in XML format.
--no-print Do not print results to terminal.
-v, --verbose Enable verbose printing.
-x PROXY, --proxy PROXY
Setup proxy server (example: http://127.0.0.1:8080)
-e file, --txt file Export results.
-ec file, --csv file Export results in CSV format.
-ex file, --xml file Export results in XML format.
```


**Examples**
---
**Retrieve your IP Geolocation**
* ./ip2geolocation.py
* ./ip2geolocation.py -m

**Retrieve IP Geolocation**
* ./ip2geolocation.py -t x.x.x.x
Expand Down Expand Up @@ -101,8 +104,8 @@ optional arguments:
**Export results to TXT file**
* ./ip2geolocation.py -t x.x.x.x -e /path/to/results.txt

**Retrieve IP Geolocation of multiple targets**
**Retrieve IP Geolocation for many targets**
* ./ip2geolocation.py -T /path/to/targets/targets.txt

**Retrieve IP Geolocation of multiple targets and export to xml**
**Retrieve IP Geolocation for many targets and export results to xml**
* ./ip2geolocation.py -T /path/to/targets/targets.txt --xml /path/to/results.xml
268 changes: 195 additions & 73 deletions geolocation/IpGeoLocationLib.py
Expand Up @@ -29,103 +29,177 @@
import os.path
import random
from time import sleep
from utilities import MyExceptions


class IpGeoLocationLib:
"""Retrieve IP Geolocation information from http://ip-api.com"""

def __init__(self):
self.URL = 'http://ip-api.com/json/{}'
self.Proxy = request.ProxyHandler({})


def GetInfo(self, target, userAgent, targetList=None, randomUserAgent=False, uAgentList=None, proxy=False):
self.RandomUA = False
self.UserAgentFile = None
self.UserAgents = None
self.TargetsFile = None
self.Targets = None
self.Verbose = False
self.NoPrint = False

def GetInfo(self, target, userAgent, targetsFile=None, rUserAgent=False, userAgentFile=None, proxy=False, noprint=False, verbose=False):
"""Retrieve information"""

self.UserAgent = userAgent

if randomUserAgent and uAgentList is not None:
userAgent = self.__pickRandomUserAgent(uAgentList)
if(userAgent):
self.UserAgent = userAgent
else:
print('Unable to pick a random User Agent string from file.')
return False
self.UserAgent = userAgent
self.RandomUA = rUserAgent
self.Verbose = verbose
self.NoPrint = noprint

try:

if userAgentFile and os.path.isfile(userAgentFile) and os.access(userAgentFile, os.R_OK):
self.UserAgentFile = userAgentFile
if self.Verbose:
self.__print('Loading User-Agent strings from file..')
self.__loadUserAgents()

if targetsFile and os.path.isfile(targetsFile) and os.access(targetsFile, os.R_OK):
self.TargetsFile = targetsFile

if proxy:
self.Proxy = request.ProxyHandler({'http':proxy.scheme + '://' + proxy.netloc})
opener = request.build_opener(self.Proxy)
request.install_opener(opener)


if targetList is not None:
return self.__retrieveGeolocations(targetList)
else:
return self.__retrieveGeolocation(target)#my ip
if proxy:
self.Proxy = request.ProxyHandler({'http':proxy.scheme + '://' + proxy.netloc})
opener = request.build_opener(self.Proxy)
request.install_opener(opener)
if self.Verbose:
self.__print('Proxy ({}) has been configured.'.format(proxy.scheme + '://' + proxy.netloc))


if self.TargetsFile:
results = self.__retrieveGeolocations()

return results

else:
if self.Verbose:
self.__print('Retrieving target Geolocation..')

result = self.__retrieveGeolocation(target)

return result


except MyExceptions.UserAgentFileEmptyError:
self.__printError("User-Agent strings file is empty!")
except MyExceptions.InvalidTargetError:
self.__printError('Please provide a valid Domain or IP address.')
except MyExceptions.TargetsFileEmptyError:
self.__printError('Targets file is empty!.')
except MyExceptions.UserAgentFileNotSpecifiedError:
self.__printError('User-Agent strings file has not been provided!.')
except MyExceptions.TargetsFileNotSpecifiedError:
self.__printError('Targets file has not been provided!.')
except:
self.__printError("An unexpected error occurred")

return False


def __retrieveGeolocations (self, targetsFile):
"""Retrieve IP Geolocation for each target in the file list"""
try:
IpGeoLocObjs = []

if os.path.isfile(targetsFile) and os.access(targetsFile, os.R_OK):
targets = [line.strip() for line in open(targetsFile, 'r') if line.strip()]

for target in targets:
IpGeoLocObjs.append(self.__retrieveGeolocation(target))
if len(targets)>=150:
sleep(.500)#1/2 sec - ip-api will automatically ban any IP address doing over 150 requests per minute
def __retrieveGeolocations (self):
"""Retrieve IP Geolocation for each target in the list"""
IpGeoLocObjs = []

if self.Verbose:
self.__print('Loading targets from file..')

self.__loadTargets()

return IpGeoLocObjs
except:
return False
if self.Verbose:
self.__print('Retrieving targets Geolocations..')

for target in self.Targets:
IpGeoLocObjs.append(self.__retrieveGeolocation(target))
if len(self.Targets)>=150:
sleep(.500) #1/2 sec - ip-api will automatically ban any IP address doing over 150 requests per minute

return IpGeoLocObjs


def __retrieveGeolocation(self, target):
"""Retrieve IP Geolocation for single target"""
try:

if target is None:
query = 'My IP'
target=''
elif self.__isValidIPAddress(target):
query = target
else:
ip = self.__hostnameToIP(target)#domain?
if not ip:
print('Please provide a valid Domain or IP address.')
return False
query = target
target = ip

if not target:
query = 'My IP'
target=''

req = request.Request(self.URL.format(target), data=None, headers={
'User-Agent':self.UserAgent
})
elif self.__isValidIPAddress(target):
query = target

response = request.urlopen(req)
else:
ip = self.__hostnameToIP(target)#domain?
if not ip:
raise MyExceptions.InvalidTargetError()

if response.code == 200:
encoding = response.headers.get_content_charset()
return IpGeoLocation(query, json.loads(response.read().decode(encoding)))
else:
#print('Unable to contact service.')
return False
except:
return False
query = target
target = ip

if self.RandomUA and self.UserAgentFile:
self.__pickRandomUserAgent()

if self.Verbose:
self.__print('User-Agent string used: {}.'.format(self.UserAgent))

req = request.Request(self.URL.format(target), data=None, headers={
'User-Agent':self.UserAgent
})

response = request.urlopen(req)

if response.code == 200:
encoding = response.headers.get_content_charset()
if self.Verbose:
self.__print('Geolocation information has been retrieved.')

def __pickRandomUserAgent(self, userAgentFileList):
"""Pick randomly a user agent string from a provided file"""
try:
if os.path.isfile(userAgentFileList) and os.access(userAgentFileList, os.R_OK):
lines = [line.strip() for line in open(userAgentFileList, 'r') if line.strip()]
return random.choice(lines)
return False
except:
return False
ipGeoLocObj = IpGeoLocation(query, json.loads(response.read().decode(encoding)))

if not self.NoPrint:
self.__printIPGeoLocation(ipGeoLocObj)

return ipGeoLocObj

return False


def __loadUserAgents(self):
"""Load user-agent strings from file"""
if not self.UserAgentFile:
raise MyExceptions.UserAgentFileNotSpecifiedError()

self.UserAgents = [line.strip() for line in open(self.UserAgentFile, 'r') if line.strip()]
if self.Verbose:
self.__print('User-Agent strings loaded.')

if len(self.UserAgents) == 0:
raise MyExceptions.UserAgentFileEmptyError()


def __loadTargets(self):
"""Load targets from file"""
if not self.TargetsFile:
raise MyExceptions.TargetsFileNotSpecifiedError()

self.Targets = [line.strip() for line in open(self.TargetsFile, 'r') if line.strip()]
if self.Verbose:
self.__print('Targets loaded.')

if len(self.Targets) == 0:
raise MyExceptions.TargetsFileEmptyError()


def __pickRandomUserAgent(self):
"""Pick randomly a user-agent string from list"""
if not self.UserAgents or len(self.UserAgents) == 0:
raise MyExceptions.UserAgentFileEmptyError()

self.UserAgent = random.choice(self.UserAgents)


def __hostnameToIP(self, hostname):
Expand All @@ -143,4 +217,52 @@ def __isValidIPAddress(self, ip):
return True
except:
return False



def __print(self, message, newLine=False):
if newLine:
print('{}\n'.format(message))
else:
print('{}'.format(message))


def __printError(self, message):
print('Error: {}\n'.format(message))


def __printIPGeoLocation(self, ipGeoLocation):
print("""
Target: {}
IP: {}
ASN: {}
City: {}
Country: {}
Country Code: {}
ISP: {}
Latitude: {}
Longtitude: {}
Organization: {}
Region Code: {}
Region Name: {}
Timezone: {}
Zip Code: {}
Google Maps: {}
""".format(ipGeoLocation.Query,
ipGeoLocation.IP,
ipGeoLocation.ASN,
ipGeoLocation.City,
ipGeoLocation.Country,
ipGeoLocation.CountryCode,
ipGeoLocation.ISP,
ipGeoLocation.Latitude,
ipGeoLocation.Longtitude,
ipGeoLocation.Organization,
ipGeoLocation.Region,
ipGeoLocation.RegionName,
ipGeoLocation.Timezone,
ipGeoLocation.Zip,
ipGeoLocation.GoogleMapsLink
)#.encode('cp737', errors='replace').decode('cp737')
)

0 comments on commit 0619eda

Please sign in to comment.