윈도우 방화벽을 이용해서 한국에서의 접속 IP 만 접근 가능 하도록 파워쉘로 만들었다.
특정 포트를 지정해 줘야 한다. 기존에 allow되어 있던 부분은 사용안함 처리 해야 한다.
소스는 아래와 같다.
#get-help about_signing 참조하라는 에러 발생시,
#Set-ExecutionPolicy RemoteSigned 실행.
#############Config#################
$countryCode="KR" #차단을 하지 않을 나라의 CountryCode
$geoIPcsvPath="c:\GeoIPCountryWhoisPaid.csv"
#$geoIPcsvPath="\GeoIPCountryWhois.csv" #ISE에서 실행시 찾지 못함.
$ruleName="allowCountry"
$allowPort="3389,1433"
#############Config#################
$geoData=Import-Csv $geoIPcsvPath -header sIP, eIP, cc | where-object {$_.cc –eq $countryCode}
$geoDataTotal=$geoData.Count
$remoteIP=""
###### 룰이 있는 지 체크 ########
function ruleExistsChk ($ruleName)
{
$fw=New-object -comObject HNetCfg.FwPolicy2; # http://blogs.technet.com/b/jamesone/archive/2009/02/18/how-to-manage-the-windows-firewall-settings-with-powershell.aspx
$RuleCHK=$fw.rules | where-object {$_.name –eq $ruleName}
if(!$RuleCHK){
#$deny_rule_name + " 룰이 생성되어 있지 않습니다."; exit;
#허용하는 룰 생성.
netsh advfirewall firewall add rule name="$ruleName" localport="$allowPort" protocol=TCP localip=any dir=in action=allow profile="any" interfacetype="any"
}
}
###### 룰이 있는 지 체크 ########
$count=1
foreach ($geoIP in $geoData)
{
#$remoteIP+=@($geoIP.sIP+"-"+$geoIP.eIP+",") #배열로 저장.
$remoteIP+=$geoIP.sIP+"-"+$geoIP.eIP+","
try {
#remoteAddr가 한개의 룰에 약 300개 이상이면 등록이 안됨. 안전하게 200으로 설정.
if(($count%200) -eq 0)
{
$makeRuleName=$ruleName+$countryCode+$count
ruleExistsChk($makeRuleName)
netsh advfirewall firewall set rule name=$makeRuleName new remoteip="$remoteIP"
$remoteIP=""
}elseif($geoDataTotal -eq $count){
$makeRuleName=$ruleName+$countryCode+$count
ruleExistsChk($makeRuleName)
netsh advfirewall firewall set rule name=$makeRuleName new remoteip="$remoteIP"
$remoteIP=""
}
}catch {
Write-Warning "Error occured: $_"
}
$count++
}
댓글 없음:
댓글 쓰기