레이블이 해외 IP 체크인 게시물을 표시합니다. 모든 게시물 표시
레이블이 해외 IP 체크인 게시물을 표시합니다. 모든 게시물 표시

2014년 10월 28일 화요일

현재 접속자 중 해외에서 접속한 사람이 있는 지 확인 하는 파워쉘 스크립트.

이 스크립트를 사용하려면 kisa에서 whois openapi key 발급 받아야 됨.

###################### Config ###################### #kisa 에서 발급받은 api key#http://whois.kisa.or.kr/kor/whois/openAPI_KeyCre.jsp$kisaKey = "00000000000000000000"###################### Config ###################### ######  kisa open api를 이용해 한국 아이피인지 체크 ########function whoisKR ($IpAddress){    if(-not $IpAddress) {echo "input ip address"; return}        $countryCode = ""    # Do whois lookup with ARIN on the IP address, do crude error check.    $webclient = new-object System.Net.WebClient    $webclient.Encoding = [System.Text.Encoding]::UTF8    [xml][/xml] $ipxml = $webclient.DownloadString("http://whois.kisa.or.kr/openapi/ipascc.jsp?key=$kisaKey&query=$IpAddress&answer=xml")     if (-not $?) { echo "error" ; return }        $countryCode = $ipxml.whois.countryCode        if($countryCode -ne "KR")    {        return $FALSE #NOT KR    }else{        return $TRUE #KR    }}######  kisa open api를 이용해 한국 아이피인지 체크 #########whoisKR#whoisKR "222.122.20.1"#whoisKR "54.217.151.196"###### MAIN######  netstat 를 통해 외부 IP가 한국 꺼인지 아닌 지 체크.  ##############  한국이 아니면 해당 IP를 deny 룰에 적용.  ############## 참조 : http://www.lazywinadmin.com/2014/08/powershell-parse-this-netstatexe.html$ForeignAddressIP = @()# Get the output of netstat$data = netstat -nat# Keep only the line with the data (we remove the first lines)$data = $data[5..$data.count]# Each line need to be splitted and get rid of unnecessary spacesforeach ($line in $data){	# Get rid of the first whitespaces, at the beginning of the line	$line = $line -replace '^\s+', ''	# Split each property on whitespaces block	$line = $line -split '\s+'	$temp = ($line[2] -split ":")[0]	#예외처리	switch ($temp)	{	"0.0.0.0" {}	"127.0.0.1" {}	"*"{}	"["{}	default {$ForeignAddressIP += $temp}	}}$ForeignAddressIP = $ForeignAddressIP | Sort-Object -uniqueforeach( $checkIP in $ForeignAddressIP){   #한국이면 true, 아니면 false   if(whoisKR $checkIP)   {      '한국 IP : ' + $checkIP   }else{       '!!!!경고, 한국 IP 아님!!!! : ' + $checkIP   }   '-----------------------'}cmd /c pause | out-null