Skip to content

Instantly share code, notes, and snippets.

@SevenLayerJedi
Last active January 21, 2019 20:00
Show Gist options
  • Save SevenLayerJedi/3daa5b3626b6f5a61e414432783e9863 to your computer and use it in GitHub Desktop.
Save SevenLayerJedi/3daa5b3626b6f5a61e414432783e9863 to your computer and use it in GitHub Desktop.
##############################################
# MODULES #
##############################################
# Download the Free GeoIP DB
# https://lite.ip2location.com/file-download
# https://lite.ip2location.com/download?id=9
# https://gallery.technet.microsoft.com/scriptcenter/List-the-IP-addresses-in-a-60c5bb6b
import-module .\Get-IPrange.ps1
##############################################
# FUNCTIONS #
##############################################
Function Get-GeoIPAllCities([string]$InputFile){
$fileContents = Import-Csv -Delimiter ',' -Path $InputFile
$arrayAllCity = @()
foreach ($l in $fileContents){
$arrayAllCity += $l.City
}
$arrayAllCity = $arrayAllCity | sort -unique
RETURN $arrayAllCity
}
Function Get-GeoIPCityInfo([string]$InputFile, [string]$City, [string]$OutputFile){
# Import as a CSV the Colorado.txt CSV file
$fileContents = Import-Csv -Delimiter ',' -Path $InputFile
# Create a new blank file that will be named colorado-%city_name%.txt
'"IP","COUNTRY_ABV","COUNTRY","STATE","CITY","LATITUDE","LONGITUDE","ZIPCODE","TIMEZONE"' | out-file -filepath $OutputFile
# Iterate through the CSV line by line
foreach ($l in $fileContents){
# Filter out the results by city
if ($($l.city).ToString() -eq "$City"){
Write-host " [+] $($l.IP_START),$($l.IP_STOP),$($l.COUNTRY),$($l.STATE),$($l.CITY),$($l.ZIPCODE),$($l.TIMEZONE)" -foregroundcolor CYAN
# Use the IP Range script to get the list of IP's
$ipRange = Get-IPrange -start $(([System.Net.IPAddress]$($l.IP_START)).IPAddressToString) -end $(([System.Net.IPAddress]$($l.IP_STOP)).IPAddressToString)
# Iterate through each IP and append the results to the file we created earlier
foreach ($ip in $ipRange){
if ($ip.split('.')[-1] -notlike 0 -AND $ip.split('.')[-1] -notlike 255){
"$($ip),$($l.COUNTRY),$($l.STATE),$($l.CITY),$($l.LATITUDE),$($l.LONGITUDE),$($l.ZIPCODE),$($l.TIMEZONE)" | out-file -filepath $OutputFile -append
}
}
}
}
}
##############################################
# MAIN #
##############################################
# Create colorado.txt CSV file with headers
'"IP_START","IP_STOP","COUNTRY_ABV","COUNTRY","STATE","CITY","LATITUDE","LONGITUDE","ZIPCODE","TIMEZONE"' | out-file -filepath "colorado.txt"
# The CSV only contains IP's from Colorado
# Get the contents of the GeoIP DB and then filter on index 4 (city) for colorado
gc .\IP2LOCATION-LITE-DB11.CSV | ?{$($_.split(',')[4]) -like "*colorado*"} | out-file -filepath "colorado.txt" -append
# Get all the city names
Write-host " [+] Pulling City Names" -foregroundcolor green
$allCityNames = Get-GeoIPAllCities -InputFile .\colorado.txt
# Go through each city and export the ips into individual files
foreach ($city in $allCityNames){
$formatedCityName = $city.trim().replace(" ","_")
Write-host " [+] Exporting: $($City)" -foregroundcolor green
Get-GeoIPCityInfo -InputFile .\colorado.txt -City "$($city)" -OutputFile ".\colorado-$($formatedCityName).txt"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment