Advertisement
anmiles

Whether IP is blocked by RKN

Apr 16th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Check whether ip address is blocked
  4. .DESCRIPTION
  5.     Check whether ip address is within one of specifised subnet cidrs
  6. .PARAMETER addresses
  7.     List of ips or dns names or http addresses to check
  8. .PARAMETER strict
  9.     Whether to throw an exception when [resolved] ip address is in blocked cidr
  10. .EXAMPLE
  11.     blocked site.com
  12.     # check whether site.com is blocked
  13. .EXAMPLE
  14.     blocked http://site2.com/qwerty?asd=wer
  15.     # check whether site2.com is blocked
  16. .EXAMPLE
  17.     blocked 1.2.3.4
  18.     # check whether 1.2.3.4 is blocked
  19. .EXAMPLE
  20.     blocked 5.6.7.8 -strict
  21.     # check whether 5.6.7.8 is within one of blocked networks and throw an exception if it is
  22. .EXAMPLE
  23.     blocked 11.12.13.14 15.16.17.18 google.com
  24.     # check whether either of [11.12.13.14, 15.16.17.18, google.com] is within one of blocked networks.
  25. #>
  26.  
  27. Param (
  28.     [Parameter(ValueFromRemainingArguments = $true)][string[]]$addresses,
  29.     [switch]$strict
  30. )
  31.  
  32. $blocked_networks = @(
  33.     "103.246.200.0/22",
  34.     "109.239.140.0/24",
  35.     "128.199.0.0/16",
  36.     "13.125.0.0/16",
  37.     "13.230.0.0/15",
  38.     "13.56.0.0/14",
  39.     "149.154.160.0/20",
  40.     "149.154.164.0/22",
  41.     "149.154.168.0/22",
  42.     "149.154.172.0/22",
  43.     "159.122.128.0/18",
  44.     "159.203.0.0/16",
  45.     "159.65.0.0/16",
  46.     "159.89.0.0/16",
  47.     "165.227.0.0/16",
  48.     "167.99.0.0/16",
  49.     "174.138.0.0/17",
  50.     "176.67.169.0/24",
  51.     "178.239.88.0/21",
  52.     "178.63.0.0/16",
  53.     "18.130.0.0/16",
  54.     "18.144.0.0/16",
  55.     "18.204.0.0/14",
  56.     "18.218.0.0/16",
  57.     "18.236.0.0/15",
  58.     "185.166.212.0/23",
  59.     "185.229.227.0/24",
  60.     "188.166.0.0/17",
  61.     "195.154.0.0/17",
  62.     "203.104.128.0/20",
  63.     "203.104.144.0/21",
  64.     "203.104.152.0/22",
  65.     "206.189.0.0/16",
  66.     "34.240.0.0/13",
  67.     "34.248.0.0/13",
  68.     "35.176.0.0/15",
  69.     "35.178.0.0/15",
  70.     "35.180.0.0/16",
  71.     "45.76.82.0/23",
  72.     "46.101.128.0/17",
  73.     "51.136.0.0/15",
  74.     "52.32.0.0/16",
  75.     "52.56.0.0/16",
  76.     "52.57.0.0/16",
  77.     "54.212.0.0/15",
  78.     "54.228.0.0/15",
  79.     "64.137.0.0/17",
  80.     "68.171.224.0/19",
  81.     "74.82.64.0/19",
  82.     "91.108.12.0/22",
  83.     "91.108.16.0/22",
  84.     "91.108.20.0/22",
  85.     "91.108.24.0/23",
  86.     "91.108.33.0/24",
  87.     "91.108.36.0/23",
  88.     "91.108.38.0/23",
  89.     "91.108.4.0/22",
  90.     "91.108.56.0/22",
  91.     "91.108.8.0/22",
  92.     "94.177.224.0/21",
  93.     "98.158.176.0/20"
  94. )
  95.  
  96. Function CheckAddress($address) {
  97.     $ips = @()
  98.    
  99.     if ($address -match "^([\d\.]+)$") {
  100.         $ips += $address
  101.     } else {
  102.         $address = $address -replace "^(\w+://)?([^/]+).*?$", '$2'
  103.        
  104.         [System.Net.Dns]::GetHostAddresses($address) | % {
  105.             $ips += $_.IPAddressToString
  106.         }
  107.     }
  108.    
  109.     $ips | % {
  110.         if ($blocked = IsIpBlocked -ip $_) {
  111.             $message = "IP $_ is within blocked network $blocked"
  112.            
  113.             if ($strict) {
  114.                 throw $message
  115.             } else {
  116.                 Write-Host $message -ForegroundColor Red
  117.             }
  118.         } else {
  119.             Write-Host "IP $_ is not within blocked networks" -ForegroundColor Green
  120.         }
  121.     }
  122. }
  123.  
  124. Function IsIpBlocked($ip) {
  125.     $blocked = $null
  126.    
  127.     if (!$ip) { return $blocked }
  128.  
  129.     $blocked_networks | % {
  130.         if (IsIpWithinCidr -ip $ip -cidr $_) {
  131.             $blocked = $_
  132.         }
  133.     }
  134.  
  135.     return $blocked
  136. }
  137.  
  138. Function IsIpWithinCidr($ip, $cidr) {
  139.     if (!($ip -is [System.Net.IPAddress])) {
  140.         $ip = [System.Net.IPAddress]::Parse($ip)
  141.     }
  142.    
  143.     $parts = $cidr.Split('/');
  144.     $subnet_ip = [System.Net.IPAddress]::Parse($parts[0])
  145.     $subnet_bits = [int]$parts[1]
  146.     $ip_int = [BitConverter]::ToInt32($ip.GetAddressBytes(), 0)
  147.     $subnet_ip_int = [BitConverter]::ToInt32($subnet_ip.GetAddressBytes(), 0)
  148.     $subnet_mask_int = [System.Net.IPAddress]::HostToNetworkOrder(-1 -shl (32 - $subnet_bits))
  149.     return (($ip_int -band $subnet_mask_int) -eq ($subnet_ip_int -band $subnet_mask_int))
  150. }
  151.  
  152. if ($addresses.Count -gt 0) {      
  153.     $addresses | % { CheckAddress -address $_ }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement