The script takes a list of two letter country code, “ru,cn” as arguments, retrieves the IP ranges assigned to the country from Nirsoft (http://nirsoft.net/countryip/) and then submits the Start and End IPs of the ranges to http://www.analyticsmarket.com/freetools/ipregex and writes the regex in the proper format for pasting into a Mimecast (email filtering) Content Examination Policy.
#requires -version 2 <# .SYNOPSIS Creates files which contain the regexes to match IP ranges by two letter country code. The contents of the files are for use in Mimecast's Content Definitions for Geoblocking .DESCRIPTION Gathers the IP ranges allocated to the country specified by it's two letter country code. Uses http://www.analyticsmarket.com/freetools/ipregex for the regex generation and http://www.nirsoft.net/countryip/$cc.csv .PARAMETER CountryCodes -CountryCodes al,ar,az,bs,by,bt,bo .INPUTS List of two letter country codes separated by commas "af,al" .OUTPUTS Write to a file in the current directory named for the two letter country code .txt "af.txt" .NOTES Version: 1.0 Author: Erick Kinnee Creation Date: 6/2/17 Purpose/Change: Initial script development .EXAMPLE Get-GetCountryCodeIpRegex -CountryCodes al,ar,az,bs,by,bt,bo #> function Get-IPRegex() { Param( [string]$StartIP, [string]$EndIP ) #Find an existing IE window $ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -like "*Internet Explorer*" } #If not, make one if ($ie -eq $null) { $ie = New-Object -COM InternetExplorer.Application } #Don't need to see it $ie.Visible = $false #$true is fun to leave running on one monitor #Make the initial connection to the site $ie.Navigate('http://www.analyticsmarket.com/freetools/ipregex') #Need to wait until IE is done while ($ie.Busy -eq $true -or $ie.ReadyState -ne '4') { Start-Sleep -Seconds 1; } #Gather input fields in the page $inputs = ($ie.Document.body.getElementsByTagName('input')| where {$_.id -eq 'StartingIP' -or $_.id -eq 'EndingIP' -or $_.value -eq 'Generate RegEx'}) while ($ie.Busy -eq $true -or $ie.ReadyState -ne '4') { Start-Sleep -Seconds 1; } #Assign our data to the inputs $inputs[0].value = $StartIP $inputs[1].value = $EndIP #Submit the form $inputs[2].click() while ($ie.Busy -eq $true -or $ie.ReadyState -ne '4') { Start-Sleep -Seconds 1; } #Get the regex from the site response, remove ^ and $ from the regex and add [ and ] $ipregex = $ie.Document.getElementById('URL').textContent-replace "^","[" -replace "$","]" while ($ie.Busy -eq $true -or $ie.ReadyState -ne '4') { Start-Sleep -Seconds 1; } #Force some Garbage Collection [GC]::Collect() [GC]::WaitForPendingFinalizers() #"Return" the regex to the pipeline $ipregex } function Get-GetCountryCodeIpRegex() { Param( [string[]]$CountryCodes ) #Loop the list of two letter country codes foreach ($cc in $CountryCodes) { $cc = $cc.ToLower(); #Url to the csv file named <countrycode>.csv $url = "http://www.nirsoft.net/countryip/$cc.csv"; #Retrieve the csv file $content = (Invoke-WebRequest -Uri $url -UseBasicParsing -SessionVariable $session -ErrorAction SilentlyContinue).Content #Turn the csv into a list removing empty lines $content = $content.Split([environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries) #Loop over list of IP start and ends foreach ($item in $content) { #Split the csv line and keep the first two items, the start and end ips, also remove newlines $iprange = $item.Split(',', [System.StringSplitOptions]::RemoveEmptyEntries)[0..1] -split "`n" $cidrregex = Get-IPRegex -StartIP $iprange[0] -EndIP $iprange[1] if ($cidrregex -ne $null) { #Add e comment that contains the IP range "#" + $cidr[0] + "-" + $cidr[1] | Out-File -FilePath "$CC.txt" -Append #Write the regex in the proper format for mimecast's content examination "1 regex " + $cidrregex | Out-File -FilePath "$CC.txt" -Append } } } }
submitted by /u/ekinnee
[link] [comments]
The post Script to generate regexes from IP ranges appeared first on How to Code .NET.